2013-02-06 03:16:51 +08:00
|
|
|
require_dependency 'rest_client'
|
2013-02-20 04:16:50 +08:00
|
|
|
require_dependency 'version'
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-02-15 01:57:26 +08:00
|
|
|
module DiscourseHub
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
class NicknameUnavailable < RuntimeError; end
|
|
|
|
|
|
|
|
def self.nickname_available?(nickname)
|
|
|
|
response = get('/users/nickname_available', {nickname: nickname})
|
|
|
|
[response['available'], response['suggestion']]
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.nickname_match?(nickname, email)
|
|
|
|
response = get('/users/nickname_match', {nickname: nickname, email: email})
|
|
|
|
[response['match'], response['available'] || false, response['suggestion']]
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.register_nickname(nickname, email)
|
|
|
|
json = post('/users', {nickname: nickname, email: email})
|
|
|
|
if json.has_key?('success')
|
|
|
|
true
|
|
|
|
else
|
2013-02-12 23:13:09 +08:00
|
|
|
raise NicknameUnavailable # TODO: report ALL the errors
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-12 23:13:09 +08:00
|
|
|
def self.change_nickname(current_nickname, new_nickname)
|
|
|
|
json = put("/users/#{current_nickname}/nickname", {nickname: new_nickname})
|
|
|
|
if json.has_key?('success')
|
|
|
|
true
|
|
|
|
else
|
|
|
|
raise NicknameUnavailable # TODO: report ALL the errors
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2013-02-20 04:16:50 +08:00
|
|
|
def self.discourse_version_check
|
2013-03-02 05:01:36 +08:00
|
|
|
get('/version_check', {
|
|
|
|
installed_version: Discourse::VERSION::STRING,
|
|
|
|
forum_title: SiteSetting.title,
|
|
|
|
forum_domain: Discourse.current_hostname
|
|
|
|
})
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def self.get(rel_url, params={})
|
2013-02-15 01:57:26 +08:00
|
|
|
response = RestClient.get( "#{hub_base_url}#{rel_url}", {params: {access_token: access_token}.merge(params), accept: accepts } )
|
2013-02-06 03:16:51 +08:00
|
|
|
JSON.parse(response)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.post(rel_url, params={})
|
2013-02-15 01:57:26 +08:00
|
|
|
response = RestClient.post( "#{hub_base_url}#{rel_url}", {access_token: access_token}.merge(params), content_type: :json, accept: accepts )
|
2013-02-06 03:16:51 +08:00
|
|
|
JSON.parse(response)
|
|
|
|
end
|
|
|
|
|
2013-02-12 23:13:09 +08:00
|
|
|
def self.put(rel_url, params={})
|
2013-02-15 01:57:26 +08:00
|
|
|
response = RestClient.put( "#{hub_base_url}#{rel_url}", {access_token: access_token}.merge(params), content_type: :json, accept: accepts )
|
2013-02-12 23:13:09 +08:00
|
|
|
JSON.parse(response)
|
|
|
|
end
|
|
|
|
|
2013-02-15 01:57:26 +08:00
|
|
|
def self.hub_base_url
|
2013-02-06 03:16:51 +08:00
|
|
|
if Rails.env == 'production'
|
|
|
|
'http://api.discourse.org/api'
|
|
|
|
else
|
2013-02-15 01:57:26 +08:00
|
|
|
'http://local.hub:3000/api'
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.access_token
|
2013-03-06 02:53:23 +08:00
|
|
|
SiteSetting.discourse_org_access_key
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.accepts
|
|
|
|
[:json, 'application/vnd.discoursehub.v1']
|
|
|
|
end
|
|
|
|
end
|