class Onfleet::APICategory
Attributes
api_endpoint[RW]
api_key[RW]
category_name[RW]
default_params[RW]
throws_exceptions[RW]
timeout[RW]
Public Class Methods
new(category_name, api_key, timeout, throws_exceptions, api_endpoint, default_params)
click to toggle source
# File lib/onfleet/api_category.rb, line 12 def initialize(category_name, api_key, timeout, throws_exceptions, api_endpoint, default_params) @category_name = category_name @api_key = api_key @api_endpoint = api_endpoint || get_api_endpoint @default_params = default_params @throws_exceptions = throws_exceptions @timeout = timeout @try_count = 0 set_instance_defaults end
Public Instance Methods
all()
click to toggle source
# File lib/onfleet/api_category.rb, line 72 def all call(:get) end
api_key=(value)
click to toggle source
# File lib/onfleet/api_category.rb, line 101 def api_key=(value) @api_key = value.strip if value end
call(method, params = {})
click to toggle source
# File lib/onfleet/api_category.rb, line 24 def call(method, params = {}) ensure_api_key set_api_url params = @default_params.merge(params) if (response = send_api_request(method, params)) parsed_response = response.parsed_response raise_error_with_message(parsed_response) if should_raise_for_response?(response.code) parsed_response end end
create(args={})
click to toggle source
# File lib/onfleet/api_category.rb, line 76 def create(args={}) call(:post, args) end
delete(id)
click to toggle source
# File lib/onfleet/api_category.rb, line 80 def delete(id) self.category_name = "#{category_name}/#{id}" call(:delete) end
find(id)
click to toggle source
# File lib/onfleet/api_category.rb, line 67 def find(id) self.category_name = "#{category_name}/#{id}" call(:get) end
get_api_endpoint()
click to toggle source
# File lib/onfleet/api_category.rb, line 113 def get_api_endpoint "https://onfleet.com/api/v2/" end
raise_error_with_message(parsed_response={})
click to toggle source
# File lib/onfleet/api_category.rb, line 58 def raise_error_with_message(parsed_response={}) parsed_response['message'] ||= {} error = OnfleetError.new(parsed_response['message']['message']) error.code = parsed_response['message']['error'] error.name = parsed_response['code'] Onfleet::API.logger.error "ERROR:#{error.name} #{error.message} ERROR CODE: #{error.code}" raise error end
send_api_request(method, params)
click to toggle source
# File lib/onfleet/api_category.rb, line 35 def send_api_request(method, params) begin Onfleet::API.logger.info "Try Count: #{ @try_count } Start Onfleet API Request #{ @api_url }" response = HTTParty.send(method, @api_url, :body => MultiJson.dump(params), basic_auth: { username: @api_key }, :timeout => @timeout) Onfleet::API.logger.info "Try Count: #{ @try_count } End Onfleet API Request Response Code: #{response.code}" raise OnfleetError, 'Retrying TooManyRequestsError Response Code: 429' if (response.code == 429 && should_retry_if_fails?) return response rescue StandardError => e Onfleet::API.logger.error "ERROR:#{e} #{e.message}" if should_retry_if_fails? @try_count += 1 call(method, params) else raise e end end return nil end
set_api_url()
click to toggle source
# File lib/onfleet/api_category.rb, line 54 def set_api_url @api_url = URI.encode(@api_endpoint + @category_name) end
set_instance_defaults()
click to toggle source
# File lib/onfleet/api_category.rb, line 95 def set_instance_defaults @timeout = (API.timeout || 30) if @timeout.nil? @throws_exceptions = API.throws_exceptions if @throws_exceptions.nil? @throws_exceptions = true if @throws_exceptions.nil? end
should_raise_for_response?(response_code)
click to toggle source
# File lib/onfleet/api_category.rb, line 105 def should_raise_for_response?(response_code) @throws_exceptions && (400..500).include?(response_code) end
should_retry_if_fails?()
click to toggle source
# File lib/onfleet/api_category.rb, line 109 def should_retry_if_fails? Onfleet::API.retry_if_fails && (@try_count < (Onfleet::API.max_try_count || 5)) end
update(args={})
click to toggle source
# File lib/onfleet/api_category.rb, line 85 def update(args={}) if id = args.delete(:id) self.category_name = "#{category_name}/#{id}" call(:put, args) else Onfleet::API.logger.error "ERROR: Please give id of #{@category_name} for update" raise OnfleetError, "Please give id of #{@category_name} for update" end end
Private Instance Methods
ensure_api_key()
click to toggle source
# File lib/onfleet/api_category.rb, line 121 def ensure_api_key unless @api_key Onfleet::API.logger.error "ERROR: You must set an api_key prior to making a call" raise Onfleet::OnfleetError, "You must set an api_key prior to making a call" end end