module Hair::Api::Client

Constants

API_ENDPOINT
VERSION

Public Class Methods

configure() { |options| ... } click to toggle source
# File lib/hair/api/client.rb, line 29
def self.configure(&proc)
  fail ArgumentError, "Block is required." unless block_given?
  yield @@options
end
options() click to toggle source

Default search options

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

Set default search options

# File lib/hair/api/client.rb, line 25
def self.options=(opts)
  @@options = opts
end
record(key, title, url, published_at, opts = {}) click to toggle source

Record status of use key is 10 characters, published_at is Time object.

# File lib/hair/api/client.rb, line 43
def self.record(key, title, url, published_at, opts = {})
  opts[:method] = 'post'
  opts[:path] = '/record'
  opts[:image_key] = key
  opts[:entry_title] = title
  opts[:entry_url] = url
  opts[:approve_date] = published_at.strftime('%Y-%m-%d')
  self.send_request(opts)
end
send_request(opts) click to toggle source
# File lib/hair/api/client.rb, line 53
def self.send_request(opts)
  opts = self.options.merge(opts) if self.options
  http_response = call_api(opts)
  res = Response.new(http_response.body)
  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 Hair::Api::RequestError, err_msg
  end
  res
end

Private Class Methods

call_api(opts) click to toggle source
# File lib/hair/api/client.rb, line 91
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/hair/api/client.rb, line 102
def self.prepare_url(opts)
  path = opts.delete(:path)
  query_string = opts.empty? ? '' : '?' + URI.encode_www_form(opts)
  API_ENDPOINT + path + query_string
end