Sha256: d06c9a50e9381b77c0e004d17e92e92b53460dcb27e85e1a015ed230028eec4c

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 KB

Contents

require 'net/http'
module SimpleHttp
  class Client
    attr_accessor :uri, :headers, :http_method, :query

    def initialize(opts)
      raise 'URL must be present' unless opts[:url]
      raise 'http_method must be present' unless opts[:http_method]

      @uri = URI(opts[:url])
      @http_method = opts[:http_method]
      @headers = opts[:headers] || {}
      @query = opts[:query]
    end

    def call
      enable_ssl
      set_headers
      http.request(request)
    end

    private

    def set_headers
      request["Accept"] = headers[:accept] if headers[:accept]
      request["Authorization"] = headers[:Authorization] if headers[:Authorization]
      request["Content-Type"] = headers[:content_type] if headers[:content_type]
      request["Cookie"] = headers[:cookie] if headers[:cookie]
    end

    def request
      @request ||= (http_method == :post ? Net::HTTP::Post.new(uri) : Net::HTTP::Get.new(uri))
    end

    def enable_ssl
      return unless uri.scheme == 'https'

      http.use_ssl = true
      http.ssl_version = :TLSv1_2
    end

    def http
      @http ||= Net::HTTP.new(uri.host, uri.port)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
simple_http_service-0.1.0 lib/simple_http/client.rb