Module: Xmlstats::Endpoint::ClassMethods

Defined in:
lib/xmlstats/endpoint.rb

Instance Method Summary collapse

Instance Method Details

#fetch_json(path = nil, params = {}) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/xmlstats/endpoint.rb', line 48

def fetch_json(path = nil, params = {})
  path ||= @path
  raise "path not defined" unless path

  json = http_get(path, params)
  JSON.load(json)
end

#http_get(path = nil, params = {}) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/xmlstats/endpoint.rb', line 25

def http_get(path = nil, params = {})
  path ||= @path
  raise "path not defined" unless path

  encoded_params =
    params.reject do |k, v|
      v == nil
    end.map do |k, v|
      "#{URI.escape k.to_s}=#{URI.escape v.to_s}"
    end.join("&")

  uri = URI.parse("https://erikberg.com/#{path}.json?#{encoded_params}")

  json = Xmlstats.cacher && Xmlstats.cacher.get(uri.to_s)
  return json if json

  json = Xmlstats.http_getter.get(uri)
  if json
    Xmlstats.cacher.set(uri.to_s, json)
    json
  end
end

#path=(path) ⇒ Object



21
22
23
# File 'lib/xmlstats/endpoint.rb', line 21

def path= path
  @path = path
end