Sha256: 471eaee7db9365ecd6359422f40161a7ce27732246ef27e37f6a35e1cedbd443

Contents?: true

Size: 1.7 KB

Versions: 2

Compression:

Stored size: 1.7 KB

Contents

require 'hashie'
require 'typhoeus'
require 'multi_json'

module Chartmogul
  module V1
    class Request
      attr_reader :url, :userpwd, :params, :response

      # Public: Constructor.
      #
      # url     - The String url.
      # body    - The String body.
      # headers - The Hash headers.
      # method  - The Symbol request method (default: :get).
      # userpwd - The String with credentials for Basic Authentication.
      # params  - The Hash query params.
      def initialize(url, body: nil, headers: nil, method: :get, userpwd: nil, **params)
        @url      = url
        @userpwd  = userpwd
        @params   = params
        @response = Typhoeus::Request.new(url,
          body:           body,
          connecttimeout: 5,
          headers:        headers,
          method:         method,
          params:         params,
          timeout:        10,
          userpwd:        userpwd,
        ).run
      end

      # Public: Get next page.
      #
      # Returns the instance of Chartmogul::V1::Request.
      def next_page
        Request.new url, params.merge(page: params[:page] + 1, userpwd: userpwd)
      end

      # Returns true at successful request , false otherwise.
      def ok?
        [200, 201].include? code
      end

      # Public: Get response code.
      #
      # Returns Integer code of response.
      def code
        response.code
      end

      # Public: Get return code.
      #
      # Returns Integer return code.
      def return_code
        response.return_code
      end

      # Public: Get body.
      #
      # Returns the instance of Hashie::Mash.
      def body
        @body ||= Hashie::Mash.new(MultiJson.load response.body || '')
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
chartmogul_client-0.0.4 lib/chartmogul/v1/request.rb
chartmogul_client-0.0.3 lib/chartmogul/v1/request.rb