Class: DaFace::Api::Adapters::Base
- Inherits:
-
Object
- Object
- DaFace::Api::Adapters::Base
- Defined in:
- lib/da_face/api/adapters/base.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#path_prefix ⇒ Object
readonly
Returns the value of attribute path_prefix.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Instance Method Summary collapse
-
#api_auth_header ⇒ Object
Constructs the auth header for Datasift.
-
#api_path ⇒ Object
Constructs the base api path for Datasift.
-
#default_headers ⇒ Object
Constructs default headers for operations.
- #encode_form(payload) ⇒ Object
- #get_headers ⇒ Object
-
#initialize ⇒ Base
constructor
A new instance of Base.
- #post_headers ⇒ Object
- #set_rate_limit_status(rate_limit, remaining) ⇒ Object
-
#url_params(params) ⇒ Object
Transforms a level 1 hash to valid url params.
Constructor Details
#initialize ⇒ Base
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_key ⇒ Object (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 |
#host ⇒ Object (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_prefix ⇒ Object (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 |
#user ⇒ Object (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_header ⇒ Object
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_path ⇒ Object
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_headers ⇒ Object
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_headers ⇒ Object
25 26 27 |
# File 'lib/da_face/api/adapters/base.rb', line 25 def get_headers default_headers end |
#post_headers ⇒ Object
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 |