Sha256: 74ef0ba589f3f16317e41844b8fba5cf15d0e35b7ee9e6179120849787cc36fa

Contents?: true

Size: 1.37 KB

Versions: 1

Compression:

Stored size: 1.37 KB

Contents

require 'active_support'
require 'net/http'
require 'cgi'

require 'duedil/response'

module Duedil
  module Client
    extend ActiveSupport::Concern

    included do
      attr_accessor :api_key, :version, :base_url, :sandbox, :locale
    end

    def initialize(options = {})
      @api_key  = options.delete(:api_key)
      @version  = options.delete(:version)
      @base_url = options.delete(:base_url)
      @sandbox  = options.delete(:sandbox).present?
      @locale   = options.delete(:locale) || 'uk'
      @http     = options.delete(:http) || Net::HTTP
    end

    private

    def base_path
      @base_path ||= begin
        path = "/#{version}"
        path += "/sandbox" if sandbox
        path += "/#{locale}"
        path
      end
    end

    def path(endpoint)
      "#{base_path}/#{endpoint}"
    end

    def request(path, params = {})
      request_uri = request_uri(path, params.merge(:api_key => api_key))
      response = @http.get_response(base_url, request_uri)
      Response.new(response)
    end

    def request_uri(path, hash = {})
      query_params = hash.map do |key, value|
        if value.is_a? Hash
          "#{escape(key)}=#{escape(value.to_json)}"
        else
          "#{escape(key)}=#{escape(value)}"
        end
      end

      path + '?' + query_params.flatten.join('&')
    end

    def escape(value)
      CGI.escape value.to_s
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
duedil-client-0.0.1 lib/duedil/client.rb