class ChartMogul::APIResource
Constants
- BACKOFF_FACTOR
- INTERVAL
- INTERVAL_RANDOMNESS
- MAX_INTERVAL
- RETRY_EXCEPTIONS
- RETRY_STATUSES
- THREAD_CONNECTION_KEY
Attributes
resource_name[R]
resource_path[R]
resource_root_key[R]
Public Class Methods
connection()
click to toggle source
# File lib/chartmogul/api_resource.rb, line 43 def self.connection Thread.current[THREAD_CONNECTION_KEY] ||= build_connection end
handle_other_error(exception)
click to toggle source
# File lib/chartmogul/api_resource.rb, line 83 def self.handle_other_error(exception) raise ChartMogul::ChartMogulError, exception.message end
handle_request_error(exception)
click to toggle source
# 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_errors() { || ... }
click to toggle source
# 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_keys()
click to toggle source
# File lib/chartmogul/api_resource.rb, line 34 def self.immutable_keys @immutable_keys ||= [] end
set_immutable_keys(array)
click to toggle source
When set with keys, nested hash keys of these immutable keys won't be converted to snake case
# File lib/chartmogul/api_resource.rb, line 39 def self.set_immutable_keys(array) @immutable_keys = array end
set_resource_name(name)
click to toggle source
# File lib/chartmogul/api_resource.rb, line 26 def self.set_resource_name(name) @resource_name = name end
set_resource_path(path)
click to toggle source
# 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)
click to toggle source
# File lib/chartmogul/api_resource.rb, line 30 def self.set_resource_root_key(root_key) @resource_root_key = root_key end
Private Class Methods
build_connection()
click to toggle source
# File lib/chartmogul/api_resource.rb, line 91 def self.build_connection Faraday.new(url: ChartMogul.api_base) do |faraday| faraday.use Faraday::Request::BasicAuthentication, ChartMogul.account_token, ChartMogul.secret_key faraday.use Faraday::Response::RaiseError faraday.request :retry, max: ChartMogul.max_retries, retry_statuses: RETRY_STATUSES, max_interval: MAX_INTERVAL, backoff_factor: BACKOFF_FACTOR, interval_randomness: INTERVAL_RANDOMNESS, interval: INTERVAL, exceptions: RETRY_EXCEPTIONS faraday.adapter Faraday::Adapter::NetHttp end end