Class: Onfleet::APICategory

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/onfleet/api_category.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(category_name, api_key, timeout, throws_exceptions, api_endpoint, default_params) ⇒ APICategory

Returns a new instance of APICategory.



12
13
14
15
16
17
18
19
20
21
22
# 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

Instance Attribute Details

#api_endpointObject

Returns the value of attribute api_endpoint.



10
11
12
# File 'lib/onfleet/api_category.rb', line 10

def api_endpoint
  @api_endpoint
end

#api_keyObject

Returns the value of attribute api_key.



10
11
12
# File 'lib/onfleet/api_category.rb', line 10

def api_key
  @api_key
end

#category_nameObject

Returns the value of attribute category_name.



10
11
12
# File 'lib/onfleet/api_category.rb', line 10

def category_name
  @category_name
end

#default_paramsObject

Returns the value of attribute default_params.



10
11
12
# File 'lib/onfleet/api_category.rb', line 10

def default_params
  @default_params
end

#throws_exceptionsObject

Returns the value of attribute throws_exceptions.



10
11
12
# File 'lib/onfleet/api_category.rb', line 10

def throws_exceptions
  @throws_exceptions
end

#timeoutObject

Returns the value of attribute timeout.



10
11
12
# File 'lib/onfleet/api_category.rb', line 10

def timeout
  @timeout
end

Instance Method Details

#allObject



72
73
74
# File 'lib/onfleet/api_category.rb', line 72

def all
  call(:get)
end

#call(method, params = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# 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 = {}) ⇒ Object



76
77
78
# File 'lib/onfleet/api_category.rb', line 76

def create(args={})
  call(:post, args)
end

#delete(id) ⇒ Object



80
81
82
83
# File 'lib/onfleet/api_category.rb', line 80

def delete(id)
  self.category_name = "#{category_name}/#{id}"
  call(:delete)
end

#find(id) ⇒ Object



67
68
69
70
# File 'lib/onfleet/api_category.rb', line 67

def find(id)
  self.category_name = "#{category_name}/#{id}"
  call(:get)
end

#get_api_endpointObject



113
114
115
# File 'lib/onfleet/api_category.rb', line 113

def get_api_endpoint
  "https://onfleet.com/api/v2/"
end

#raise_error_with_message(parsed_response = {}) ⇒ Object



58
59
60
61
62
63
64
65
# 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) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# 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_urlObject



54
55
56
# File 'lib/onfleet/api_category.rb', line 54

def set_api_url
  @api_url = URI.encode(@api_endpoint + @category_name)
end

#set_instance_defaultsObject



95
96
97
98
99
# 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) ⇒ Boolean

Returns:

  • (Boolean)


105
106
107
# 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?Boolean

Returns:

  • (Boolean)


109
110
111
# 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 = {}) ⇒ Object



85
86
87
88
89
90
91
92
93
# 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