module Favor::Api::Client

Constants

API_ENDPOINT
VERSION

Public Class Methods

configure() { |options| ... } click to toggle source
# 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 = {}) click to toggle source

Show detail of the photo.

# 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
options() click to toggle source

Default search options

# File lib/favor/api/client.rb, line 20
def self.options
  @@options
end
options=(opts) click to toggle source

Set default search options

# File lib/favor/api/client.rb, line 25
def self.options=(opts)
  @@options = opts
end
send_request(opts) click to toggle source
# 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

Private Class Methods

call_api(opts) click to toggle source
# File lib/favor/api/client.rb, line 97
def self.call_api(opts)
  http_method = opts.delete(:method)
  if http_method == 'post'
    request_url = prepare_url({path: opts.delete(:path)})
    Net::HTTP.post_form(URI::parse(request_url), opts)
  else
    request_url = prepare_url(opts)
    Net::HTTP.get_response(URI::parse(request_url))
  end
end
prepare_url(opts) click to toggle source
# File lib/favor/api/client.rb, line 108
def self.prepare_url(opts)
  path = opts.delete(:path)
  query_string = opts.empty? ? '' : '?' + URI.encode_www_form(opts)
  API_ENDPOINT + path + query_string
end