class Pricefinder::Client
Constants
- API_HOST
- USER_AGENT
Attributes
configuration[R]
Public Class Methods
new(options = nil)
click to toggle source
# File lib/pricefinder/client.rb, line 15 def initialize(options = nil) @config = nil @retry_count = 0 unless options.nil? @configuration = Configuration.new(options) check_valid_config end # Create Connection connection # Get Access Token get_access_token end
Public Instance Methods
access_token()
click to toggle source
# File lib/pricefinder/client.rb, line 50 def access_token @configuration.access_token end
build_errors(response)
click to toggle source
# File lib/pricefinder/client.rb, line 69 def build_errors(response) errors = [] errors << "Unauthorized Token: #{access_token}" if response.status == 401 errors << "Could not handle response" if response.status == 500 return errors end
check_valid_config()
click to toggle source
# File lib/pricefinder/client.rb, line 31 def check_valid_config if configuration.nil? || !configuration.valid? @configuration = nil raise Error::MissingClientRequiredConfig end end
connection()
click to toggle source
# File lib/pricefinder/client.rb, line 38 def connection return @connection if instance_variable_defined?(:@connection) check_valid_config @connection = Faraday.new(API_HOST) do |faraday| faraday.request :url_encoded faraday.headers[:user_agent] = USER_AGENT faraday.response :json faraday.adapter Faraday.default_adapter end end
get(path, params = {}, options = {})
click to toggle source
# File lib/pricefinder/client.rb, line 54 def get(path, params = {}, options = {}) response = @connection.get do |request| request.headers['Authorization'] = "Bearer #{access_token}" request.url path request.params = params end handle_response(response) end
handle_response(response)
click to toggle source
# File lib/pricefinder/client.rb, line 64 def handle_response(response) return response.body if response.body return { errors: build_errors(response) } end
Private Instance Methods
authenticate_client_credentials(client_id, client_secret)
click to toggle source
# File lib/pricefinder/client.rb, line 109 def authenticate_client_credentials(client_id, client_secret) response = @connection.post('oauth/token', { :grant_type => 'client_credentials', :client_id => client_id, :client_secret => client_secret }) if response.status == 200 && token = response.body["tokenKey"] @configuration.access_token = token else raise Error::InvalidCredentials end end
get_access_token(force_auth = false)
click to toggle source
# File lib/pricefinder/client.rb, line 89 def get_access_token(force_auth = false) config_params = @configuration.config_params # If we were passed an access token use it if !force_auth && token = config_params[:access_token] @configuration.access_token = token return end # We need to generate a new token but we are missing the required params if force_auth && (config_params[:client_id].nil? || config_params[:client_id].nil?) raise Error::MissingClientRequiredConfig end # Otherwise get a new token using credentials if (client_id = config_params[:client_id]) && (client_secret = config_params[:client_secret]) authenticate_client_credentials(client_id, client_secret) end end