Class: DaFace::Api::Adapters::Base

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

Direct Known Subclasses

EmHttpRequestAdapter, ExconAdapter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBase

Returns a new instance of Base.



7
8
9
10
11
12
13
14
15
# File 'lib/da_face/api/adapters/base.rb', line 7

def initialize
  @host = DaFace.configuration.api_host
  @path_prefix = DaFace.configuration.api_path_prefix
  @user = DaFace.configuration.user
  @api_key = DaFace.configuration.api_key

  raise DaFace::AdapterError.new('Missing user for authentication') unless @user
  raise DaFace::AdapterError.new('Missing api_key for authentication') unless @api_key
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



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

def api_key
  @api_key
end

#hostObject (readonly)

Returns the value of attribute host.



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

def host
  @host
end

#path_prefixObject (readonly)

Returns the value of attribute path_prefix.



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

def path_prefix
  @path_prefix
end

#userObject (readonly)

Returns the value of attribute user.



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

def user
  @user
end

Instance Method Details

#api_auth_headerObject

Constructs the auth header for Datasift



43
44
45
# File 'lib/da_face/api/adapters/base.rb', line 43

def api_auth_header
  "#{self.user}:#{self.api_key}"
end

#api_pathObject

Constructs the base api path for Datasift



38
39
40
# File 'lib/da_face/api/adapters/base.rb', line 38

def api_path
  "#{self.host}#{self.path_prefix}"
end

#default_headersObject

Constructs default headers for operations



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

def default_headers
  {
    'Authorization' => api_auth_header,
    'Accept' => 'application/json'
  }
end

#encode_form(payload) ⇒ Object



33
34
35
# File 'lib/da_face/api/adapters/base.rb', line 33

def encode_form payload
  URI.encode_www_form(payload)
end

#get_headersObject



25
26
27
# File 'lib/da_face/api/adapters/base.rb', line 25

def get_headers
  default_headers
end

#post_headersObject



29
30
31
# File 'lib/da_face/api/adapters/base.rb', line 29

def post_headers
  default_headers.merge({'Content-Type' => 'application/x-www-form-urlencoded'})
end

#set_rate_limit_status(rate_limit, remaining) ⇒ Object



55
56
57
58
59
60
# File 'lib/da_face/api/adapters/base.rb', line 55

def set_rate_limit_status rate_limit, remaining
  DaFace.set_rate_limit_status do |status|
    status.limit = rate_limit
    status.remaining = remaining
  end
end

#url_params(params) ⇒ Object

Transforms a level 1 hash to valid url params

This will return something like “?something=thing” to be appended to url path



51
52
53
# File 'lib/da_face/api/adapters/base.rb', line 51

def url_params params
  '?' + URI.encode(params.collect{ |key, value| "#{key}=#{value}"}.join('&')) unless params.empty?        
end