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
|
|
|
|
2014-05-12 07:06:28 +08:00
|
|
|
def self.version_check_payload
|
|
|
|
{
|
2014-08-12 03:41:56 +08:00
|
|
|
installed_version: Discourse::VERSION::STRING
|
2014-09-10 05:04:10 +08:00
|
|
|
}.merge!( Discourse.git_branch == "unknown" ? {} : {branch: Discourse.git_branch})
|
2014-05-12 07:06:28 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.discourse_version_check
|
|
|
|
get('/version_check', version_check_payload)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def self.get(rel_url, params={})
|
2013-04-16 02:52:07 +08:00
|
|
|
singular_action :get, rel_url, params
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.post(rel_url, params={})
|
2013-04-16 02:52:07 +08:00
|
|
|
collection_action :post, rel_url, params
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-02-12 23:13:09 +08:00
|
|
|
def self.put(rel_url, params={})
|
2013-04-16 02:52:07 +08: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={})
|
2014-08-15 05:29:58 +08:00
|
|
|
JSON.parse RestClient.send(action, "#{hub_base_url}#{rel_url}", {params: params, accept: accepts, referer: referer } )
|
2013-04-16 02:52:07 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.collection_action(action, rel_url, params={})
|
2014-08-15 05:29:58 +08:00
|
|
|
JSON.parse RestClient.send(action, "#{hub_base_url}#{rel_url}", params, content_type: :json, accept: accepts, referer: referer )
|
2013-02-12 23:13:09 +08:00
|
|
|
end
|
|
|
|
|
2013-02-15 01:57:26 +08:00
|
|
|
def self.hub_base_url
|
2014-08-18 14:42:48 +08:00
|
|
|
if Rails.env.production?
|
2016-04-01 05:33:25 +08:00
|
|
|
'https://api.discourse.org/api'
|
2013-02-06 03:16:51 +08:00
|
|
|
else
|
2013-11-20 03:15:05 +08:00
|
|
|
ENV['HUB_BASE_URL'] || 'http://local.hub:3000/api'
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.accepts
|
|
|
|
[:json, 'application/vnd.discoursehub.v1']
|
|
|
|
end
|
2013-08-28 15:18:31 +08:00
|
|
|
|
2014-08-15 05:29:58 +08:00
|
|
|
def self.referer
|
|
|
|
Discourse.base_url
|
|
|
|
end
|
|
|
|
|
2013-08-26 06:41:17 +08:00
|
|
|
end
|