Class: Adtraction::Api::Client
- Inherits:
-
Object
- Object
- Adtraction::Api::Client
- Includes:
- Affiliate::Channels, Affiliate::Programs
- Defined in:
- lib/adtraction/api/client.rb
Instance Attribute Summary collapse
-
#configuration ⇒ Object
Returns the value of attribute configuration.
Instance Method Summary collapse
- #get(path, params: {}, headers: {}, options: {}) ⇒ Object
- #head(path, headers: {}, options: {}) ⇒ Object
-
#initialize(configuration: ::Adtraction::Api.configuration) ⇒ Client
constructor
A new instance of Client.
- #log(message) ⇒ Object
- #patch(path, params: {}, data: {}, headers: {}, options: {}) ⇒ Object
- #post(path, params: {}, data: {}, headers: {}, options: {}) ⇒ Object
- #request(path, method: :get, params: {}, data: {}, headers: {}, options: {}) ⇒ Object
- #to_uri(path) ⇒ Object
Methods included from Affiliate::Programs
Methods included from Affiliate::Channels
Constructor Details
#initialize(configuration: ::Adtraction::Api.configuration) ⇒ Client
Returns a new instance of Client.
6 7 8 9 |
# File 'lib/adtraction/api/client.rb', line 6 def initialize(configuration: ::Adtraction::Api.configuration) self.configuration = configuration raise InvalidApiKeyError, "You need to supply a valid API key!" if self.configuration.api_key.to_s.empty? end |
Instance Attribute Details
#configuration ⇒ Object
Returns the value of attribute configuration.
4 5 6 |
# File 'lib/adtraction/api/client.rb', line 4 def configuration @configuration end |
Instance Method Details
#get(path, params: {}, headers: {}, options: {}) ⇒ Object
19 20 21 |
# File 'lib/adtraction/api/client.rb', line 19 def get(path, params: {}, headers: {}, options: {}) request path, method: :get, params: params, headers: headers, options: end |
#head(path, headers: {}, options: {}) ⇒ Object
31 32 33 |
# File 'lib/adtraction/api/client.rb', line 31 def head(path, headers: {}, options: {}) request path, method: :head, headers: headers, options: end |
#log(message) ⇒ Object
70 71 72 |
# File 'lib/adtraction/api/client.rb', line 70 def log() puts "[Adtraction::Api::Client] - #{Time.now}: #{}" if !.to_s.empty? && self.verbose end |
#patch(path, params: {}, data: {}, headers: {}, options: {}) ⇒ Object
27 28 29 |
# File 'lib/adtraction/api/client.rb', line 27 def patch(path, params: {}, data: {}, headers: {}, options: {}) request path, method: :patch, params: params, data: data, headers: {}, options: end |
#post(path, params: {}, data: {}, headers: {}, options: {}) ⇒ Object
23 24 25 |
# File 'lib/adtraction/api/client.rb', line 23 def post(path, params: {}, data: {}, headers: {}, options: {}) request path, method: :post, params: params, data: data, headers: {}, options: end |
#request(path, method: :get, params: {}, data: {}, headers: {}, options: {}) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/adtraction/api/client.rb', line 35 def request(path, method: :get, params: {}, data: {}, headers: {}, options: {}) headers = { "X-Token" => self.configuration.api_key, "User-Agent" => self.configuration.faraday.fetch(:user_agent, "Adtraction Ruby Client #{::Adtraction::Api::VERSION}"), "Content-Type" => "application/json" }.merge(headers) connection = Faraday.new(url: to_uri(path)) do |builder| builder.response :logger if self.configuration.verbose_faraday? builder.headers = headers builder.response :json yield builder if block_given? builder.adapter self.configuration.faraday.fetch(:adapter, :net_http) end response = case method when :get connection.get do |request| request.params = params if params && !params.empty? end when :post, :patch connection.send(method) do |request| request.body = data.to_json if data && !data.empty? request.params = params if params && !params.empty? end when :head connection.send(method) end return response end |
#to_uri(path) ⇒ Object
14 15 16 17 |
# File 'lib/adtraction/api/client.rb', line 14 def to_uri(path) path = path.gsub(/^\//i, "") path =~ /^http(s)?:\/\// ? path : "https://#{self.configuration.host}/v#{self.configuration.api_version}/#{path}" end |