Class: DaFace::Api::Adapters::ExconAdapter

Inherits:
Base
  • Object
show all
Defined in:
lib/da_face/api/adapters/excon_adapter.rb

Instance Attribute Summary

Attributes inherited from Base

#api_key, #host, #path_prefix, #user

Instance Method Summary collapse

Methods inherited from Base

#api_auth_header, #api_path, #default_headers, #encode_form, #get_headers, #initialize, #post_headers, #set_rate_limit_status, #url_params

Constructor Details

This class inherits a constructor from DaFace::Api::Adapters::Base

Instance Method Details

#connectionObject



5
6
7
# File 'lib/da_face/api/adapters/excon_adapter.rb', line 5

def connection
  Excon.new self.api_path
end

#extract_rate_limit_info(headers) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/da_face/api/adapters/excon_adapter.rb', line 38

def extract_rate_limit_info headers
  if headers.select{|header| header[0] == 'x'}.any?
    {
      :limit => headers['x-ratelimit-limit'],
      :remaining => headers['x-ratelimit-remaining']
    }
  elsif headers.select{|header| header[0] == 'X'}.any?
    {
      :limit => headers['X-RateLimit-Limit'],
      :remaining => headers['X-RateLimit-Remaining']
    }
  else
    {
      :limit => nil,
      :remaining => nil
    }
  end
end

#get(path, params = {}) ⇒ Object



9
10
11
12
# File 'lib/da_face/api/adapters/excon_adapter.rb', line 9

def get path, params={}
  response = connection.get :path => "#{path}#{url_params(params)}", :headers => get_headers
  handle_response(response)
end

#handle_response(response) ⇒ Object



32
33
34
35
36
# File 'lib/da_face/api/adapters/excon_adapter.rb', line 32

def handle_response response
  rate_limit_info = extract_rate_limit_info(response.headers)
  set_rate_limit_status(rate_limit_info[:limit], rate_limit_info[:remaining])
  return parse_body(response.body)
end

#parse_body(body) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/da_face/api/adapters/excon_adapter.rb', line 24

def parse_body body
  if body.empty?
    {}
  else
    JSON.parse(body)
  end
end

#post(path, payload) ⇒ Object



14
15
16
17
# File 'lib/da_face/api/adapters/excon_adapter.rb', line 14

def post path, payload
  response = connection.post :path => path, :body => encode_form(payload), :headers => post_headers
  handle_response(response)
end

#put(path, payload) ⇒ Object



19
20
21
22
# File 'lib/da_face/api/adapters/excon_adapter.rb', line 19

def put path, payload
  response = connection.put :path => "#{path}", :body => encode_form(payload), :headers => post_headers
  handle_response(response)
end