Class: ActiveCampaignCrm::Connection
- Inherits:
-
Object
- Object
- ActiveCampaignCrm::Connection
- Defined in:
- lib/active_campaign_crm/connection.rb
Overview
Handles all connections with active campaign
Instance Method Summary collapse
- #delete(url) ⇒ Object
- #get(url, params = {}) ⇒ Object
- #handle_response(response) ⇒ Object
-
#initialize ⇒ Connection
constructor
A new instance of Connection.
- #post(url, body) ⇒ Object
- #put(url, body) ⇒ Object
Constructor Details
#initialize ⇒ Connection
Returns a new instance of Connection.
7 8 9 10 11 |
# File 'lib/active_campaign_crm/connection.rb', line 7 def initialize @connection = Faraday.new( url: "#{ActiveCampaignCrm.configuration.account_url}/api/3" ) end |
Instance Method Details
#delete(url) ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/active_campaign_crm/connection.rb', line 40 def delete(url) response = @connection.delete do |req| req.url url req.headers['Api-Token'] = ActiveCampaignCrm.configuration.api_key end handle_response(response) end |
#get(url, params = {}) ⇒ Object
13 14 15 16 17 18 19 20 |
# File 'lib/active_campaign_crm/connection.rb', line 13 def get(url, params = {}) response = @connection.get do |req| req.url url req.headers['Api-Token'] = ActiveCampaignCrm.configuration.api_key req.params = params end handle_response(response) end |
#handle_response(response) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/active_campaign_crm/connection.rb', line 48 def handle_response(response) return JSON.parse response.body if response.success? errors = JSON.parse(response.body)['errors'] unless response.body.empty? = errors.map { |error| error['title'] } unless errors.nil? = .join('-') unless .nil? case response.status when 400 raise ActiveCampaignCrm::BadRequest, when 404 raise ActiveCampaignCrm::NotFound, when 429 raise ActiveCampaignCrm::TooManyRequests, when 500 raise ActiveCampaignCrm::InternalServerError, when 502 raise ActiveCampaignCrm::BadGateway, when 503 raise ActiveCampaignCrm::ServiceUnavailable, when 504 raise ActiveCampaignCrm::GatewayTimeout, else raise StandardError, end end |
#post(url, body) ⇒ Object
22 23 24 25 26 27 28 29 |
# File 'lib/active_campaign_crm/connection.rb', line 22 def post(url, body) response = @connection.post do |req| req.url url req.headers['Api-Token'] = ActiveCampaignCrm.configuration.api_key req.body = body end handle_response(response) end |
#put(url, body) ⇒ Object
31 32 33 34 35 36 37 38 |
# File 'lib/active_campaign_crm/connection.rb', line 31 def put(url, body) response = @connection.put do |req| req.url url req.headers['Api-Token'] = ActiveCampaignCrm.configuration.api_key req.body = body end handle_response(response) end |