Sha256: 0012d5a48dc1c7bea148c126041d54ab017471d10afdda8c13e2ece466698147

Contents?: true

Size: 1.22 KB

Versions: 8

Compression:

Stored size: 1.22 KB

Contents

# frozen_string_literal: true

module ActiveCampaign
  class API # :nodoc:
    attr_reader :connection

    def initialize
      setup
    end

    def setup
      @connection = Faraday.new(::ActiveCampaign.config.api_url) do |f|
        f.request :json
        f.response :json, content_type: "application/json"

        f.use ::ActiveCampaign::ResponseParser
        f.use Faraday::Request::UrlEncoded

        f.adapter Faraday.default_adapter
      end

      @connection.headers["Api-Token"] = ::ActiveCampaign.config.api_key
      @connection.headers["User-Agent"] = "Ruby ActiveCampaign (ractive_campaign)"

      self
    end

    # @private
    def request(opts = {})
      method  = opts.delete(:_method)
      path    = opts.delete(:_path)
      headers = opts.delete(:_headers)

      opts.delete_if { |key, _| key.to_s =~ /^_/ } # Remove all internal parameters

      response = @connection.send method do |request|
        request.headers.merge!(headers) if headers

        if method == :get
          request.url path, opts
        else
          request.url path
          request.body = opts
        end
      end

      {
        parsed_data: response.env[:parsed_data],
        _response: response
      }
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
ractive_campaign-0.1.6 lib/active_campaign/api.rb
ractive_campaign-0.1.5 lib/active_campaign/api.rb
ractive_campaign-0.1.4 lib/active_campaign/api.rb
ractive_campaign-0.1.3 lib/active_campaign/api.rb
ractive_campaign-0.1.2 lib/active_campaign/api.rb
ractive_campaign-0.1.1 lib/active_campaign/api.rb
ractive_campaign-0.1.0 lib/active_campaign/api.rb
ractive_campaign-0.0.1 lib/active_campaign/api.rb