Sha256: f9b06913b798328f957a84993bb01c4cc8c8e0784a64a671c6ef06232b4ae21f

Contents?: true

Size: 1.2 KB

Versions: 14

Compression:

Stored size: 1.2 KB

Contents

require 'net/http'
require 'net/https'

module Spreedly
  class Connection

    attr_accessor :endpoint

    def initialize(endpoint)
      @endpoint = URI.parse(endpoint)
    end

    def request(method, body, headers = {})
      case method
      when :get
        http.get(endpoint.request_uri, headers)
      when :post
        http.post(endpoint.request_uri, body, headers)
      when :put
        http.put(endpoint.request_uri, body, headers)
      when :delete
        http.delete(endpoint.request_uri, headers)
      when :options
        http.request(OptionsWithResponseBody.new(endpoint.request_uri, headers))
      else
        raise ArgumentError, "Unsupported request method #{method.to_s.upcase}"
      end
    end

    private
    def http
      http = Net::HTTP.new(endpoint.host, endpoint.port)
      configure_ssl(http)
      http.open_timeout = 64
      http.read_timeout = 64
      http
    end

    def configure_ssl(http)
      return unless endpoint.scheme == "https"

      http.use_ssl = true
      http.verify_mode = OpenSSL::SSL::VERIFY_PEER
    end

  end

  class OptionsWithResponseBody < Net::HTTPRequest
    METHOD = 'OPTIONS'
    REQUEST_HAS_BODY = false
    RESPONSE_HAS_BODY = true
  end

end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
spreedly-2.0.26 lib/spreedly/connection.rb
spreedly-2.0.25 lib/spreedly/connection.rb
spreedly-2.0.24 lib/spreedly/connection.rb
spreedly-2.0.23 lib/spreedly/connection.rb
spreedly-2.0.22 lib/spreedly/connection.rb
spreedly-2.0.21 lib/spreedly/connection.rb
spreedly-2.0.20 lib/spreedly/connection.rb
spreedly-2.0.19 lib/spreedly/connection.rb
spreedly-2.0.18 lib/spreedly/connection.rb
spreedly-2.0.17 lib/spreedly/connection.rb
spreedly-2.0.16 lib/spreedly/connection.rb
spreedly-2.0.15 lib/spreedly/connection.rb
spreedly-2.0.14 lib/spreedly/connection.rb
spreedly-2.0.13 lib/spreedly/connection.rb