Module: Hair::Api::Client

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

Defined Under Namespace

Classes: Response

Constant Summary collapse

API_ENDPOINT =
'https://api.hair.cm'
VERSION =
"0.2.0"
@@options =
{
    method: 'get'
}

Class Method Summary collapse

Class Method Details

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

Yields:



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

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

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

Search image by query



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

def self.image_search(query, opts = {})
  opts[:path] = '/image/search'
  opts[:query] = query
  self.send_request(opts)
end

.optionsObject

Default search options



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

def self.options
  @@options
end

.options=(opts) ⇒ Object

Set default search options



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

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

.record(key, title, url, published_at, opts = {}) ⇒ Object

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



43
44
45
46
47
48
49
50
51
# 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) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
# 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