mhenrixon/active_campaign

View on GitHub
lib/active_campaign/client.rb

Summary

Maintainability
A
1 hr
Test Coverage
# frozen_string_literal: true
 
module ActiveCampaign
#
# Provides http request functionality
#
# @author Mikael Henriksson <mikael@mhenrixon.com>
#
class Client
include API
include TransformHash
 
endpoint :account_contacts
endpoint :accounts
endpoint :account_custom_field_meta
endpoint :account_custom_field_data
endpoint :addresses
endpoint :contacts
endpoint :contact_tags
endpoint :deals
endpoint :deal_custom_field_meta
endpoint :deal_custom_field_data
endpoint :deal_stages
endpoint :fields
endpoint :field_values
endpoint :groups
endpoint :lists
endpoint :pipelines
endpoint :tags
endpoint :users
 
attr_reader :config
 
def initialize(conf = {})
@config = Configuration.new.to_h.merge(conf)
end
 
Complex method ActiveCampaign::Client#connection (20.9)
def connection # rubocop:disable Metrics/AbcSize
@connection ||= ::Faraday.new(url: config[:api_url]) do |faraday|
ActiveCampaign::Faraday::Middleware.add_request_middleware(faraday, config)
ActiveCampaign::Faraday::Middleware.add_response_middleware(faraday, config[:response_middleware])
 
faraday.adapter config[:adapter]
 
if (options = faraday.options)
options.timeout = config[:api_timeout]
options.open_timeout = options.timeout
end
 
faraday
end
end
 
def post(*args)
safe_http_call do
connection.post(*args)
end
end
 
def patch(*args)
safe_http_call do
connection.patch(*args)
end
end
 
def put(*args)
safe_http_call do
connection.put(*args)
end
end
 
def delete(*args)
safe_http_call do
connection.delete(*args)
end
end
 
def get(*args)
safe_http_call do
connection.get(*args)
end
end
 
private
 
Method `safe_http_call` has 32 lines of code (exceeds 25 allowed). Consider refactoring.
Complex method ActiveCampaign::Client#safe_http_call (25.9)
def safe_http_call # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
response = yield
response.body
rescue ::Oj::ParseError => e
raise ParsingError, e
rescue ::Faraday::BadRequestError => e
raise BadRequestError, e
rescue ::Faraday::ConnectionFailed => e
raise Unreachable, e
rescue ::Faraday::UnauthorizedError => e
raise UnauthorizedError, e
rescue ::Faraday::ForbiddenError => e
raise ForbiddenError, e
rescue ::Faraday::ResourceNotFound => e
raise ResourceNotFound, e
rescue ::Faraday::ProxyAuthError => e
raise ProxyAuthError, e
rescue ::Faraday::ConflictError => e
raise ConflictError, e
rescue ::Faraday::UnauthorizedError => e
raise UnauthorizedError, e
rescue ::Faraday::UnprocessableEntityError => e
raise UnprocessableEntityError, e
rescue ::Faraday::ServerError => e
raise ServerError, e
rescue ::Faraday::TimeoutError => e
raise TimeoutError, e
rescue ::Faraday::NilStatusError => e
raise NilStatusError, e
rescue ::Faraday::ConnectionFailed => e
raise ConnectionFailed, e
rescue ::Faraday::SSLError => e
raise SSLError, e
end
end
end