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-0.23.0.beta8 lib/cobrato/request.rb
cobrato-client-0.23.0.beta7 lib/cobrato/request.rb
cobrato-client-0.23.0.beta6 lib/cobrato/request.rb
cobrato-client-0.23.0.beta5 lib/cobrato/request.rb
cobrato-client-0.23.0.beta4 lib/cobrato/request.rb
cobrato-client-0.23.0.beta3 lib/cobrato/request.rb
cobrato-client-0.23.0.beta2 lib/cobrato/request.rb
cobrato-client-0.23.0.beta1 lib/cobrato/request.rb
cobrato-client-0.22.0 lib/cobrato/request.rb
cobrato-client-0.21.0 lib/cobrato/request.rb
cobrato-client-0.20.0 lib/cobrato/request.rb