Sha256: 93e3af33c20084a351131583d060f26e2eda1ffde797c9e1b1ef7038a5d3e29b

Contents?: true

Size: 1.18 KB

Versions: 2

Compression:

Stored size: 1.18 KB

Contents

# frozen_string_literal: true

module ActiveCampaign
  module ApiHttp # :nodoc:
    extend ActiveSupport::Concern

    HTTP_METHODS = %i[get post put delete].freeze

    module ClassMethods # :nodoc:
      HTTP_METHODS.each do |method|
        class_eval <<-RUBY, __FILE__, __LINE__ + 1
          def #{method}(path, params={})
            send(:'#{method}_raw', path, params) do |parsed_data, response|
              return {} unless [200, 201].include?(parsed_data[:status_code])
              return {} unless parsed_data[:data].present?

              data = parsed_data[:data].first.last

              if data.is_a?(Array)
                new_records data
              else
                new_record data
              end
            end
          end

          def #{method}_raw(path, params={}, &block)
            request(params.merge(:_method => #{method.to_sym.inspect}, :_path => path), &block)
          end
        RUBY
      end

      def request(params = {})
        api = ActiveCampaign::API.new
        request = api.request params

        if block_given?
          yield request[:parsed_data], request[:_response]
        else
          request
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ractive_campaign-0.1.0 lib/active_campaign/api_http.rb
ractive_campaign-0.0.1 lib/active_campaign/api_http.rb