Module: DiscourseHub

Defined in:
lib/discourse_hub.rb

Constant Summary collapse

STATS_FETCHED_AT_KEY =
"stats_fetched_at"

Class Method Summary collapse

Class Method Details

.acceptsObject



113
114
115
# File 'lib/discourse_hub.rb', line 113

def self.accepts
  %w[application/json application/vnd.discoursehub.v1]
end

.collection_action(action, rel_url, params = {}) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/discourse_hub.rb', line 64

def self.collection_action(action, rel_url, params = {})
  connect_opts = connect_opts(params)

  response =
    Excon.public_send(
      action,
      "#{hub_base_url}#{rel_url}",
      {
        body: JSON[params],
        headers: {
          "Referer" => referer,
          "Accept" => accepts.join(", "),
          "Content-Type" => "application/json",
        },
        omit_default_port: true,
      }.merge(connect_opts),
    )

  if (status = response.status) != 200
    Rails.logger.warn(response_status_log_message(rel_url, status))
  end

  begin
    JSON.parse(response.body)
  rescue JSON::ParserError
    Rails.logger.error(response_body_log_message(response.body))
  end
end

.connect_opts(params = {}) ⇒ Object



101
102
103
# File 'lib/discourse_hub.rb', line 101

def self.connect_opts(params = {})
  params.delete(:connect_opts)&.except(:body, :headers, :query) || {}
end

.delete(rel_url, params = {}) ⇒ Object



41
42
43
# File 'lib/discourse_hub.rb', line 41

def self.delete(rel_url, params = {})
  singular_action :delete, rel_url, params
end

.discourse_version_checkObject



13
14
15
# File 'lib/discourse_hub.rb', line 13

def self.discourse_version_check
  get("/version_check", version_check_payload)
end

.get(rel_url, params = {}) ⇒ Object



29
30
31
# File 'lib/discourse_hub.rb', line 29

def self.get(rel_url, params = {})
  singular_action :get, rel_url, params
end

.get_payloadObject



21
22
23
24
25
26
27
# File 'lib/discourse_hub.rb', line 21

def self.get_payload
  if SiteSetting.share_anonymized_statistics && stats_fetched_at < 7.days.ago
    About.fetch_cached_stats.symbolize_keys
  else
    {}
  end
end

.hub_base_urlObject



105
106
107
108
109
110
111
# File 'lib/discourse_hub.rb', line 105

def self.hub_base_url
  if Rails.env.production?
    ENV["HUB_BASE_URL"] || "https://api.discourse.org/api"
  else
    ENV["HUB_BASE_URL"] || "http://local.hub:3000/api"
  end
end

.post(rel_url, params = {}) ⇒ Object



33
34
35
# File 'lib/discourse_hub.rb', line 33

def self.post(rel_url, params = {})
  collection_action :post, rel_url, params
end

.put(rel_url, params = {}) ⇒ Object



37
38
39
# File 'lib/discourse_hub.rb', line 37

def self.put(rel_url, params = {})
  collection_action :put, rel_url, params
end

.refererObject



117
118
119
# File 'lib/discourse_hub.rb', line 117

def self.referer
  Discourse.base_url
end

.response_body_log_message(body) ⇒ Object



97
98
99
# File 'lib/discourse_hub.rb', line 97

def self.response_body_log_message(body)
  "Discourse Hub returned a bad response body: #{body}"
end

.response_status_log_message(rel_url, status) ⇒ Object



93
94
95
# File 'lib/discourse_hub.rb', line 93

def self.response_status_log_message(rel_url, status)
  "Discourse Hub (#{hub_base_url}#{rel_url}) returned a bad status #{status}."
end

.singular_action(action, rel_url, params = {}) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/discourse_hub.rb', line 45

def self.singular_action(action, rel_url, params = {})
  connect_opts = connect_opts(params)

  JSON.parse(
    Excon.public_send(
      action,
      "#{hub_base_url}#{rel_url}",
      {
        headers: {
          "Referer" => referer,
          "Accept" => accepts.join(", "),
        },
        query: params,
        omit_default_port: true,
      }.merge(connect_opts),
    ).body,
  )
end

.stats_fetched_atObject



121
122
123
124
# File 'lib/discourse_hub.rb', line 121

def self.stats_fetched_at
  t = Discourse.redis.get(STATS_FETCHED_AT_KEY)
  t ? Time.zone.at(t.to_i) : 1.year.ago
end

.stats_fetched_at=(time_with_zone) ⇒ Object



17
18
19
# File 'lib/discourse_hub.rb', line 17

def self.stats_fetched_at=(time_with_zone)
  Discourse.redis.set STATS_FETCHED_AT_KEY, time_with_zone.to_i
end

.version_check_payloadObject



6
7
8
9
10
11
# File 'lib/discourse_hub.rb', line 6

def self.version_check_payload
  default_payload = { installed_version: Discourse::VERSION::STRING }.merge!(
    Discourse.git_branch == "unknown" && !Rails.env.test? ? {} : { branch: Discourse.git_branch },
  )
  default_payload.merge!(get_payload)
end