Sha256: bc7639b3fc3a9e9fb6cad04e556d65b3030b116093ed6cc674ea2fb1f68ac05c

Contents?: true

Size: 1.77 KB

Versions: 2

Compression:

Stored size: 1.77 KB

Contents

module GetResponse
  class Request

    class << self
      DEFAULT_API_ENDPOINT = 'https://api3.getresponse360.com'
      DEFAULT_API_VERSION  = 'v3'

      [:post, :get].each do |method|
        define_method method do |path, params|
          connection.send("#{method}") do |req|
            req.url build_api_path(path)
            req.params = params if method == :get
            req.body = (params || {}).to_json if method == :post
            req.headers = headers
          end
        end
      end

      # TODO
      def delete
        puts 'delete = todo'
      end

      private

      def connection
        @connection ||= Faraday.new(url: api_endpoint) do |faraday|
          faraday.request :url_encoded
          faraday.adapter Faraday.default_adapter
        end
      end

      def api_endpoint
        GetResponse.config.api_endpoint || DEFAULT_API_ENDPOINT
      end

      def build_api_path(path)
        "#{api_version}/#{path}"
      end

      def api_version
        GetResponse.config.api_version || DEFAULT_API_VERSION
      end

      def headers
        user_agent_header
          .merge(json_header)
          .merge(auth_header)
          .merge(domain_header)
      end

      def user_agent_header
        { 'User-Agent' => 'GetResponse Ruby SDK' }
      end

      def json_header
        { 'Content-Type' => 'application/json' }
      end

      def auth_header
        return {} unless GetResponse.config.api_key
        { 'X-Auth-Token' => "api-key #{GetResponse.config.api_key}" }
      end

      def domain_header
        return {} unless GetResponse.config.domain_header
        { 'X-Domain' => GetResponse.config.domain_header }
      end

      def logging(msg)
        puts "[GETRESPONSE-LOG] #{msg}" if GetResponse.config.verbose
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
get_response-0.1.0 lib/get_response/request.rb
getresponse_ruby-0.1.0 lib/get_response/request.rb