Sha256: c4eee1d4cb15c738de74338ca7c414629d1e5bc7be711621f59bf2beb00465f0

Contents?: true

Size: 1.55 KB

Versions: 8

Compression:

Stored size: 1.55 KB

Contents

require 'faraday'

module Gupshup
  module HTTP
    class Client
      attr_accessor :adapter
      attr_reader :last_response, :last_request
      def initialize
        @adapter = Faraday.default_adapter
      end

      def _request(request)
        @connection = Faraday.new(url: "#{request.host}:#{request.port.to_s}", ssl: { verify: true }) do |f|
          f.request :url_encoded
          f.adapter @adapter
          f.headers = request.headers
        end
        @last_request = request
        @last_response = nil
        response = send(request)
        if response.status == 504
          object = { message: 'Request timeout', code: 504 }.to_json
        elsif response.body && !response.body.empty?
          object = response.body
        elsif response.status == 400
          object = { message: 'Bad request', code: 400 }.to_json
        end

        gupshup_response = Gupshup::Response.new(response.status, object, headers: response.headers)
        @last_response = gupshup_response

        gupshup_response
      end

      def send(request)
        @connection.send(request.method.downcase.to_sym,
                         request.url,
                         request.method == 'GET' ? request.params : request.data)
      rescue Faraday::Error => e
        raise Gupshup::REST::GupshupError, e
      end

      def request(host, port, method, url, data = {}, headers = {}, params = {}, auth = nil, timeout = nil)
        request = Gupshup::Request.new(host, port, method, url, params, data, headers)
        _request(request)

      end
    end
  end
end


Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
gupshup-rb-0.2.1 lib/gupshup-rb/http/http_client.rb
gupshup-rb-0.2.0 lib/gupshup-rb/http/http_client.rb
gupshup-rb-0.1.9 lib/gupshup-rb/http/http_client.rb
gupshup-rb-0.1.8.4 lib/gupshup-rb/http/http_client.rb
gupshup-rb-0.1.8.3 lib/gupshup-rb/http/http_client.rb
gupshup-rb-0.1.8.2 lib/gupshup-rb/http/http_client.rb
gupshup-rb-0.1.8.1 lib/gupshup-rb/http/http_client.rb
gupshup-rb-0.1.8 lib/gupshup-rb/http/http_client.rb