2013-02-05 14:16:51 -05:00
|
|
|
require_dependency 'rest_client'
|
2013-02-19 15:16:50 -05:00
|
|
|
require_dependency 'version'
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-02-14 12:57:26 -05:00
|
|
|
module DiscourseHub
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
class NicknameUnavailable < RuntimeError; end
|
|
|
|
|
|
|
|
def self.nickname_available?(nickname)
|
2013-04-15 14:52:07 -04:00
|
|
|
json = get('/users/nickname_available', {nickname: nickname})
|
|
|
|
[json['available'], json['suggestion']]
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.nickname_match?(nickname, email)
|
2013-04-15 14:52:07 -04:00
|
|
|
json = get('/users/nickname_match', {nickname: nickname, email: email})
|
|
|
|
[json['match'], json['available'] || false, json['suggestion']]
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.register_nickname(nickname, email)
|
|
|
|
json = post('/users', {nickname: nickname, email: email})
|
|
|
|
if json.has_key?('success')
|
|
|
|
true
|
|
|
|
else
|
2013-02-12 10:13:09 -05:00
|
|
|
raise NicknameUnavailable # TODO: report ALL the errors
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-04-15 14:52:07 -04:00
|
|
|
def self.unregister_nickname(nickname)
|
|
|
|
json = delete('/memberships/' + nickname)
|
|
|
|
json.has_key?('success')
|
|
|
|
end
|
|
|
|
|
2013-02-12 10:13:09 -05: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-19 15:16:50 -05:00
|
|
|
def self.discourse_version_check
|
2013-03-01 16:01:36 -05:00
|
|
|
get('/version_check', {
|
|
|
|
installed_version: Discourse::VERSION::STRING,
|
|
|
|
forum_title: SiteSetting.title,
|
2013-04-24 12:55:01 -04:00
|
|
|
forum_domain: Discourse.current_hostname,
|
2013-04-24 14:13:20 -04:00
|
|
|
contact_email: SiteSetting.contact_email,
|
|
|
|
topic_count: Topic.count
|
2013-03-01 16:01:36 -05:00
|
|
|
})
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def self.get(rel_url, params={})
|
2013-04-15 14:52:07 -04:00
|
|
|
singular_action :get, rel_url, params
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.post(rel_url, params={})
|
2013-04-15 14:52:07 -04:00
|
|
|
collection_action :post, rel_url, params
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-02-12 10:13:09 -05:00
|
|
|
def self.put(rel_url, params={})
|
2013-04-15 14:52:07 -04:00
|
|
|
collection_action :put, rel_url, params
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.delete(rel_url, params={})
|
|
|
|
singular_action :delete, rel_url, params
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.singular_action(action, rel_url, params={})
|
|
|
|
JSON.parse RestClient.send(action, "#{hub_base_url}#{rel_url}", {params: {access_token: access_token}.merge(params), accept: accepts } )
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.collection_action(action, rel_url, params={})
|
|
|
|
JSON.parse RestClient.send(action, "#{hub_base_url}#{rel_url}", {access_token: access_token}.merge(params), content_type: :json, accept: accepts )
|
2013-02-12 10:13:09 -05:00
|
|
|
end
|
|
|
|
|
2013-02-14 12:57:26 -05:00
|
|
|
def self.hub_base_url
|
2013-02-05 14:16:51 -05:00
|
|
|
if Rails.env == 'production'
|
|
|
|
'http://api.discourse.org/api'
|
|
|
|
else
|
2013-02-14 12:57:26 -05:00
|
|
|
'http://local.hub:3000/api'
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.access_token
|
2013-03-05 13:53:23 -05:00
|
|
|
SiteSetting.discourse_org_access_key
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.accepts
|
|
|
|
[:json, 'application/vnd.discoursehub.v1']
|
|
|
|
end
|
|
|
|
end
|