Module: Favor::Api::Client

Defined in:
lib/favor/api/client.rb,
lib/favor/api/client/version.rb

Defined Under Namespace

Classes: Response

Constant Summary collapse

API_ENDPOINT =
'http://widget.favor.life/api/v1'
VERSION =
"0.1.0"
@@options =
{
    method: 'get'
}

Class Method Summary collapse

Class Method Details

.configure {|@@options| ... } ⇒ Object

Yields:



29
30
31
32
# File 'lib/favor/api/client.rb', line 29

def self.configure(&proc)
  fail ArgumentError, "Block is required." unless block_given?
  yield @@options
end

.image_detail(article_id, position, opts = {}) ⇒ Object

Show detail of the photo.



42
43
44
45
46
47
# File 'lib/favor/api/client.rb', line 42

def self.image_detail(article_id, position, opts = {})
  opts[:path] = '/photo.json'
  opts[:a] = article_id
  opts[:n] = position
  self.send_request(opts)
end

.image_search(article_id, opts = {}) ⇒ Object

Search image by article_id



35
36
37
38
39
# File 'lib/favor/api/client.rb', line 35

def self.image_search(article_id, opts = {})
  opts[:path] = '/photo_list.json'
  opts[:a] = article_id
  self.send_request(opts)
end

.optionsObject

Default search options



20
21
22
# File 'lib/favor/api/client.rb', line 20

def self.options
  @@options
end

.options=(opts) ⇒ Object

Set default search options



25
26
27
# File 'lib/favor/api/client.rb', line 25

def self.options=(opts)
  @@options = opts
end

.send_request(opts) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/favor/api/client.rb', line 49

def self.send_request(opts)
  opts = self.options.merge(opts) if self.options
  http_response = call_api(opts)
  res = Response.new(http_response)
  unless http_response.kind_of? Net::HTTPSuccess
    err_msg = "HTTP Response: #{http_response.code} #{http_response.message}"
    err_msg += " - #{res.error}" if res.error
    fail Favor::Api::RequestError, err_msg
  end
  res
end