Sha256: 206604ca0387679baed01ab120ec6cc5a4b7202bc1c4b6699b0758137b5d44eb

Contents?: true

Size: 1.08 KB

Versions: 31

Compression:

Stored size: 1.08 KB

Contents

module Cobrato
  class Request
    attr_reader :args

    def initialize(args)
      @args = args
    end

    def run
      request.run
      request.response
    end

    private
      def request
        @request ||= Typhoeus::Request.new(args[:url], options)
      end

      def options
        {
          method:           args[:method],
          params:           args[:params],
          body:             body,
          headers:          headers,
          userpwd:          token,
          accept_encoding:  "gzip",
          params_encoding:  :rack # this is important for filtering with array parameter
        }.reject {|k,v| v.nil?}
      end

      def headers
        headers = args.fetch(:headers) { {} }

        {
          "Accept"         => "application/json",
          "Content-Type"   => "application/json",
          "User-Agent"     => args[:user_agent],
        }.merge(headers)
      end

      def body
        body = args[:body]
        body = MultiJson.dump(body) if body.is_a?(Hash)
        body
      end

      def token
        "#{args[:token]}:x"
      end

  end
end

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
cobrato-client-1.1.7 lib/cobrato/request.rb
cobrato-client-1.1.6 lib/cobrato/request.rb
cobrato-client-1.1.5 lib/cobrato/request.rb
cobrato-client-1.1.4 lib/cobrato/request.rb
cobrato-client-1.1.3 lib/cobrato/request.rb
cobrato-client-1.1.2 lib/cobrato/request.rb
cobrato-client-1.1.1 lib/cobrato/request.rb
cobrato-client-1.1.0 lib/cobrato/request.rb
cobrato-client-1.0.1 lib/cobrato/request.rb
cobrato-client-1.0.0 lib/cobrato/request.rb
cobrato-client-0.27.0 lib/cobrato/request.rb
cobrato-client-0.26.0 lib/cobrato/request.rb
cobrato-client-0.25.0 lib/cobrato/request.rb
cobrato-client-0.24.1 lib/cobrato/request.rb
cobrato-client-0.24.0 lib/cobrato/request.rb
cobrato-client-0.23.0 lib/cobrato/request.rb
cobrato-client-0.23.0.beta12 lib/cobrato/request.rb
cobrato-client-0.23.0.beta11 lib/cobrato/request.rb
cobrato-client-0.23.0.beta10 lib/cobrato/request.rb
cobrato-client-0.23.0.beta9 lib/cobrato/request.rb