Class: ChartMogul::APIResource
- Extended by:
- Forwardable
- Defined in:
- lib/chartmogul/api_resource.rb
Direct Known Subclasses
Customer, CustomerInvoices, Customers, DataSource, DataSources, Enrichment::Customers, Enrichment::DeprecatedCustomer, Invoice, Invoices, Metrics::ARPAs, Metrics::ARRs, Metrics::ASPs, Metrics::Activities, Metrics::AllKeyMetrics, Metrics::CustomerChurnRates, Metrics::CustomerCounts, Metrics::LTVs, Metrics::MRRChurnRates, Metrics::MRRs, Metrics::Subscriptions, Ping, Plan, PlanGroup, PlanGroup::PlanGroups, PlanGroups::Plans, Plans, Subscription, Subscriptions, Transactions::Payment, Transactions::Refund
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
-
.resource_name ⇒ Object
readonly
Returns the value of attribute resource_name.
-
.resource_path ⇒ Object
readonly
Returns the value of attribute resource_path.
-
.resource_root_key ⇒ Object
readonly
Returns the value of attribute resource_root_key.
-
.skip_case_conversion ⇒ Object
readonly
Returns the value of attribute skip_case_conversion.
Class Method Summary collapse
- .connection ⇒ Object
- .handle_other_error(exception) ⇒ Object
- .handle_request_error(exception) ⇒ Object
- .handling_errors ⇒ Object
- .set_resource_name(name) ⇒ Object
- .set_resource_path(path) ⇒ Object
- .set_resource_root_key(root_key) ⇒ Object
-
.set_skip_case_conversion(value) ⇒ Object
When true, response hash keys won't be converted to snake case.
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_name ⇒ Object (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_path ⇒ Object (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_key ⇒ Object (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 |
.skip_case_conversion ⇒ Object (readonly)
Returns the value of attribute skip_case_conversion.
20 21 22 |
# File 'lib/chartmogul/api_resource.rb', line 20 def skip_case_conversion @skip_case_conversion end |
Class Method Details
.connection ⇒ Object
39 40 41 |
# File 'lib/chartmogul/api_resource.rb', line 39 def self.connection Thread.current[THREAD_CONNECTION_KEY] ||= build_connection end |
.handle_other_error(exception) ⇒ Object
79 80 81 |
# File 'lib/chartmogul/api_resource.rb', line 79 def self.handle_other_error(exception) raise ChartMogul::ChartMogulError, exception. end |
.handle_request_error(exception) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/chartmogul/api_resource.rb', line 51 def self.handle_request_error(exception) response = exception.response[:body] http_status = exception.response[:status] case http_status when 400 = "JSON schema validation hasn't passed." raise ChartMogul::SchemaInvalidError.new(, http_status: 400, response: response) when 401 = 'No valid API key provided' raise ChartMogul::UnauthorizedError.new(, http_status: 401, response: response) when 403 = 'The requested action is forbidden.' raise ChartMogul::ForbiddenError.new(, http_status: 403, response: response) when 404 = "The requested #{resource_name} could not be found." raise ChartMogul::NotFoundError.new(, http_status: 404, response: response) when 422 = "The #{resource_name} could not be created or updated." raise ChartMogul::ResourceInvalidError.new(, http_status: 422, response: response) when 500..504 = 'ChartMogul API server response error' raise ChartMogul::ServerError.new(, http_status: http_status, response: response) else = "#{resource_name} request error has occurred." raise ChartMogul::ChartMogulError.new(, http_status: http_status, response: response) end end |
.handling_errors ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/chartmogul/api_resource.rb', line 43 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 |
.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 |
.set_skip_case_conversion(value) ⇒ Object
When true, response hash keys won't be converted to snake case
35 36 37 |
# File 'lib/chartmogul/api_resource.rb', line 35 def self.set_skip_case_conversion(value) @skip_case_conversion = value end |