Class: ChartMogul::APIResource

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/chartmogul/api_resource.rb

Constant Summary collapse

RETRY_STATUSES =
[429, *500..599].freeze
RETRY_EXCEPTIONS =
[
  'Faraday::ConnectionFailed',
  'Faraday::RetriableResponse'
].freeze
BACKOFF_FACTOR =
2
INTERVAL_RANDOMNESS =
0.5
INTERVAL =
1
MAX_INTERVAL =
60
THREAD_CONNECTION_KEY =
'chartmogul_ruby.api_resource.connection'

Class Attribute Summary collapse

Class Method Summary collapse

Methods inherited from Object

#allowed_for_write?, #assign_all_attributes, #assign_writeable_attributes, attributes, define_private_writer, define_reader, define_writer, #initialize, #instance_attributes, new_from_json, readonly_attr, #serialize_for_write, #serialized_value_for_attr, writeable_attr, writeable_attributes

Constructor Details

This class inherits a constructor from ChartMogul::Object

Class Attribute Details

.resource_nameObject (readonly)

Returns the value of attribute resource_name.



20
21
22
# File 'lib/chartmogul/api_resource.rb', line 20

def resource_name
  @resource_name
end

.resource_pathObject (readonly)

Returns the value of attribute resource_path.



20
21
22
# File 'lib/chartmogul/api_resource.rb', line 20

def resource_path
  @resource_path
end

.resource_root_keyObject (readonly)

Returns the value of attribute resource_root_key.



20
21
22
# File 'lib/chartmogul/api_resource.rb', line 20

def resource_root_key
  @resource_root_key
end

Class Method Details

.connectionObject



43
44
45
# File 'lib/chartmogul/api_resource.rb', line 43

def self.connection
  Thread.current[THREAD_CONNECTION_KEY] ||= build_connection
end

.handle_other_error(exception) ⇒ Object



83
84
85
# File 'lib/chartmogul/api_resource.rb', line 83

def self.handle_other_error(exception)
  raise ChartMogul::ChartMogulError, exception.message
end

.handle_request_error(exception) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/chartmogul/api_resource.rb', line 55

def self.handle_request_error(exception)
  response = exception.response[:body]
  http_status = exception.response[:status]
  case http_status
  when 400
    message = "JSON schema validation hasn't passed."
    raise ChartMogul::SchemaInvalidError.new(message, http_status: 400, response: response)
  when 401
    message = 'No valid API key provided'
    raise ChartMogul::UnauthorizedError.new(message, http_status: 401, response: response)
  when 403
    message = 'The requested action is forbidden.'
    raise ChartMogul::ForbiddenError.new(message, http_status: 403, response: response)
  when 404
    message = "The requested #{resource_name} could not be found."
    raise ChartMogul::NotFoundError.new(message, http_status: 404, response: response)
  when 422
    message = "The #{resource_name} could not be created or updated."
    raise ChartMogul::ResourceInvalidError.new(message, http_status: 422, response: response)
  when 500..504
    message = 'ChartMogul API server response error'
    raise ChartMogul::ServerError.new(message, http_status: http_status, response: response)
  else
    message = "#{resource_name} request error has occurred."
    raise ChartMogul::ChartMogulError.new(message, http_status: http_status, response: response)
  end
end

.handling_errorsObject



47
48
49
50
51
52
53
# File 'lib/chartmogul/api_resource.rb', line 47

def self.handling_errors
  yield
rescue Faraday::ClientError, Faraday::ServerError => e
  e.response ? handle_request_error(e) : handle_other_error(e)
rescue StandardError => e
  handle_other_error(e)
end

.immutable_keysObject



34
35
36
# File 'lib/chartmogul/api_resource.rb', line 34

def self.immutable_keys
  @immutable_keys ||= []
end

.set_immutable_keys(array) ⇒ Object

When set with keys, nested hash keys of these immutable keys won’t be converted to snake case



39
40
41
# File 'lib/chartmogul/api_resource.rb', line 39

def self.set_immutable_keys(array)
  @immutable_keys = array
end

.set_resource_name(name) ⇒ Object



26
27
28
# File 'lib/chartmogul/api_resource.rb', line 26

def self.set_resource_name(name)
  @resource_name = name
end

.set_resource_path(path) ⇒ Object



22
23
24
# File 'lib/chartmogul/api_resource.rb', line 22

def self.set_resource_path(path)
  @resource_path = ChartMogul::ResourcePath.new(path)
end

.set_resource_root_key(root_key) ⇒ Object



30
31
32
# File 'lib/chartmogul/api_resource.rb', line 30

def self.set_resource_root_key(root_key)
  @resource_root_key = root_key
end