Sha256: 6b14b5198c251d6b1e4ceada25e275352cfe5e3c3db5d09ec1448277c9662a63

Contents?: true

Size: 1.98 KB

Versions: 8

Compression:

Stored size: 1.98 KB

Contents

module GnipApi
  module Adapters
    class HTTPartyAdapter < GnipApi::Adapters::BaseAdapter
      def post request
        data = HTTParty.post request.uri, :basic_auth => auth, :body => request.payload, :timeout => default_timeout
        httparty_debugger(data)
        return response(request, data)
      end
      
      def delete request
        data = HTTParty.post request.uri, :basic_auth => auth, :body => request.payload, :timeout => default_timeout
        httparty_debugger(data)
        return response(request, data)
      end
      
      def get request
        data = HTTParty.get request.uri, :basic_auth => auth, :timeout => default_timeout
        httparty_debugger(data)
        return response(request, data)
      end

      def stream_get request
        begin
          HTTParty.get request.uri, :headers => request.headers, :stream_body => true, :basic_auth => auth do |data|
            yield(data)
          end
        rescue Zlib::BufError => error
          GnipApi.config.logger.error "STREAM ERROR -> #{error.class} -- #{error.message}\n" + error.backtrace.join("\n")
          raise error
        end
      end

      def auth
        {
          :username => username,
          :password => password
        }
      end
      
      def response request, data
        create_response request, data.code, data.body, data.headers
      end

      private
      def debug_data data
        {
          :request_headers => data.request.instance_variable_get(:@raw_request).to_hash,
          :request_body => data.request.instance_variable_get(:@raw_request).body,
          :request_method => data.request.http_method.to_s,
          :request_url => data.request.path.to_s,
          :response_headers => data.headers,
          :response_status => data.code,
          :response_body => data.body
        }
      end

      def httparty_debugger data
        return nil unless GnipApi.config.debug
        debug_request(debug_data(data))
        return nil
      end
      
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
gnip_api-1.1.3 lib/gnip_api/adapters/httparty_adapter.rb
gnip_api-1.1.2 lib/gnip_api/adapters/httparty_adapter.rb
gnip_api-1.1.1 lib/gnip_api/adapters/httparty_adapter.rb
gnip_api-1.1.0 lib/gnip_api/adapters/httparty_adapter.rb
gnip_api-1.0.6 lib/gnip_api/adapters/httparty_adapter.rb
gnip_api-1.0.5 lib/gnip_api/adapters/httparty_adapter.rb
gnip_api-1.0.4 lib/gnip_api/adapters/httparty_adapter.rb
gnip_api-1.0.3 lib/gnip_api/adapters/httparty_adapter.rb