discourse/lib/discourse_hub.rb

69 lines
1.8 KiB
Ruby
Raw Normal View History

2013-02-06 03:16:51 +08:00
require_dependency 'rest_client'
require_dependency 'version'
2013-02-06 03:16:51 +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
{
installed_version: Discourse::VERSION::STRING,
forum_title: SiteSetting.title,
forum_description: SiteSetting.site_description,
forum_url: Discourse.base_url,
contact_email: SiteSetting.contact_email,
topic_count: Topic.listable_topics.count,
2014-05-12 07:06:28 +08:00
post_count: Post.count,
2013-07-31 05:52:19 +08:00
user_count: User.count,
2014-05-12 07:06:28 +08: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,
login_required: SiteSetting.login_required,
locale: SiteSetting.default_locale
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={})
singular_action :get, rel_url, params
2013-02-06 03:16:51 +08:00
end
def self.post(rel_url, params={})
collection_action :post, rel_url, params
2013-02-06 03:16:51 +08:00
end
def self.put(rel_url, params={})
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-17 00:25:24 +08:00
JSON.parse RestClient.send(action, "#{hub_base_url}#{rel_url}", {params: params, accept: accepts } )
end
def self.collection_action(action, rel_url, params={})
2014-07-17 00:25:24 +08:00
JSON.parse RestClient.send(action, "#{hub_base_url}#{rel_url}", params, content_type: :json, accept: accepts )
end
def self.hub_base_url
2013-02-06 03:16:51 +08:00
if Rails.env == 'production'
'http://api.discourse.org/api'
else
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
end