Sha256: 262052451eb70a128b2254982a0e292544b05f2f3cae465d84e9d936b76c2e41

Contents?: true

Size: 1.59 KB

Versions: 2

Compression:

Stored size: 1.59 KB

Contents

require 'forwardable'

module ThreeScale
  class Client
    class HTTPClient
      extend Forwardable
      def_delegators :@http, :get, :post, :use_ssl?, :active?

      def initialize(options)

        @secure = !!options[:secure]
        @host = options.fetch(:host)
        @persistent = options[:persistent]

        backend_class = @persistent ? Persistent : NetHttp

        @http = backend_class.new(@host)
        @http.ssl! if @secure
      end

      class Persistent
        def initialize(host)
          require 'net/http/persistent'
          @http = ::Net::HTTP::Persistent.new
          @host = host
          @protocol = 'http'
        end

        def ssl!
          @protocol = 'https'
        end

        def get(path)
          uri = full_uri(path)
          get = Net::HTTP::Get.new(uri.request_uri)
          @http.request(uri, get)
        end


        def post(path, payload)
          uri = full_uri(path)
          post = Net::HTTP::Post.new uri.path
          post.set_form_data(payload)

          @http.request(uri, post)
        end

        def full_uri(path)
          URI.join "#{@protocol}://#{@host}", path
        end

      end

      class NetHttp
        extend Forwardable
        def_delegators :@http, :get, :post, :use_ssl?, :active?

        def initialize(host)
          @host = host
          @http = Net::HTTP.new(@host, 80)
        end

        def ssl!
          @http = Net::HTTP.new(@host, 443)
          @http.use_ssl = true
        end

        def post(path, payload)
          @http.post(path, URI.encode_www_form(payload))
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
3scale_client-2.4.0 lib/3scale/client/http_client.rb
3scale_client-2.4.0.pre.1 lib/3scale/client/http_client.rb