2013-02-05 13:16:51 -06:00
|
|
|
require_dependency 'rest_client'
|
2013-02-19 14:16:50 -06:00
|
|
|
require_dependency 'version'
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2013-02-14 11:57:26 -06:00
|
|
|
module DiscourseHub
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2014-05-11 18:06:28 -05:00
|
|
|
def self.version_check_payload
|
|
|
|
{
|
2013-03-01 15:01:36 -06:00
|
|
|
installed_version: Discourse::VERSION::STRING,
|
|
|
|
forum_title: SiteSetting.title,
|
2013-09-11 15:32:49 -05:00
|
|
|
forum_description: SiteSetting.site_description,
|
2013-05-27 15:30:06 -05:00
|
|
|
forum_url: Discourse.base_url,
|
2013-04-24 13:13:20 -05:00
|
|
|
contact_email: SiteSetting.contact_email,
|
2013-05-27 15:30:06 -05:00
|
|
|
topic_count: Topic.listable_topics.count,
|
2014-05-11 18:06:28 -05:00
|
|
|
post_count: Post.count,
|
2013-07-30 16:52:19 -05:00
|
|
|
user_count: User.count,
|
2014-05-11 18:06:28 -05:00
|
|
|
topics_7_days: Topic.listable_topics.where('created_at > ?', 7.days.ago).count,
|
|
|
|
posts_7_days: Post.where('created_at > ?', 7.days.ago).count,
|
|
|
|
users_7_days: User.where('created_at > ?', 7.days.ago).count,
|
2013-11-22 14:30:02 -06:00
|
|
|
login_required: SiteSetting.login_required,
|
|
|
|
locale: SiteSetting.default_locale
|
2014-05-11 18:06:28 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def self.discourse_version_check
|
|
|
|
get('/version_check', version_check_payload)
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def self.get(rel_url, params={})
|
2013-04-15 13:52:07 -05:00
|
|
|
singular_action :get, rel_url, params
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.post(rel_url, params={})
|
2013-04-15 13:52:07 -05:00
|
|
|
collection_action :post, rel_url, params
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
2013-02-12 09:13:09 -06:00
|
|
|
def self.put(rel_url, params={})
|
2013-04-15 13:52:07 -05: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-07-16 11:25:24 -05:00
|
|
|
JSON.parse RestClient.send(action, "#{hub_base_url}#{rel_url}", {params: params, accept: accepts } )
|
2013-04-15 13:52:07 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.collection_action(action, rel_url, params={})
|
2014-07-16 11:25:24 -05:00
|
|
|
JSON.parse RestClient.send(action, "#{hub_base_url}#{rel_url}", params, content_type: :json, accept: accepts )
|
2013-02-12 09:13:09 -06:00
|
|
|
end
|
|
|
|
|
2013-02-14 11:57:26 -06:00
|
|
|
def self.hub_base_url
|
2013-02-05 13:16:51 -06:00
|
|
|
if Rails.env == 'production'
|
|
|
|
'http://api.discourse.org/api'
|
|
|
|
else
|
2013-11-19 13:15:05 -06:00
|
|
|
ENV['HUB_BASE_URL'] || 'http://local.hub:3000/api'
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.accepts
|
|
|
|
[:json, 'application/vnd.discoursehub.v1']
|
|
|
|
end
|
2013-08-28 02:18:31 -05:00
|
|
|
|
2013-08-25 17:41:17 -05:00
|
|
|
end
|