class PayPal::SDK::HttpAdapters::EmHttp

Attributes

connection_options[R]

Public Class Methods

new(uri, proxy) click to toggle source
# File lib/paypal-sdk/http_adapters/em_http.rb, line 9
def initialize(uri, proxy)
  @uri = uri
  @connection_options = {
    tls: {}
  }
  @connection_options[:proxy] = {
    host: proxy.host,
    port: proxy.port,
    authorization: [proxy.user, proxy.password]
  } if proxy
end

Public Instance Methods

ca_file=(value) click to toggle source
# File lib/paypal-sdk/http_adapters/em_http.rb, line 33
def ca_file=(value)
  @connection_options[:tls][:cert_chain_file] = value
end
cert=(value) click to toggle source
# File lib/paypal-sdk/http_adapters/em_http.rb, line 45
def cert=(value)
  raise NotImplementedError, "cert=#{value}" if value
end
key=(value) click to toggle source
# File lib/paypal-sdk/http_adapters/em_http.rb, line 49
def key=(value)
  raise NotImplementedError, "key=#{value}" if value
end
open_timeout=(value) click to toggle source
# File lib/paypal-sdk/http_adapters/em_http.rb, line 21
def open_timeout=(value)
  @connection_options[:connect_timeout] = value
end
read_timeout=(value) click to toggle source
# File lib/paypal-sdk/http_adapters/em_http.rb, line 25
def read_timeout=(value)
  @connection_options[:inactivity_timeout] = value
end
request(method, path, header, body) click to toggle source
# File lib/paypal-sdk/http_adapters/em_http.rb, line 67
def request(method, path, header, body)
  within_session do
    uri = URI.parse(path)

    client = run_request(@connection, method,
      path: uri.path,
      query: uri.query,
      head: header,
      body: body
    )

    if client.error
      raise client.error
    else
      build_response(client)
    end
  end
end
ssl_version=(value) click to toggle source
# File lib/paypal-sdk/http_adapters/em_http.rb, line 41
def ssl_version=(value)
  @connection_options[:tls][:ssl_version] = value
end
start() { |self| ... } click to toggle source
# File lib/paypal-sdk/http_adapters/em_http.rb, line 86
def start
  raise 'session is already started' if @started

  begin
    @started = true
    @connection = EM::HttpRequest.new(@uri, @connection_options)

    within_reactor do
      yield(self)
    end
  ensure
    @connection = nil
    @started = nil
  end
end
started?() click to toggle source
# File lib/paypal-sdk/http_adapters/em_http.rb, line 102
def started?
  @started
end
use_ssl=(value) click to toggle source
# File lib/paypal-sdk/http_adapters/em_http.rb, line 29
def use_ssl=(value)
  true
end
verify_mode=(value) click to toggle source
# File lib/paypal-sdk/http_adapters/em_http.rb, line 37
def verify_mode=(value)
  @connection_options[:tls][:verify_peer] = (value > 0)
end

Protected Instance Methods

build_response(client) click to toggle source
# File lib/paypal-sdk/http_adapters/em_http.rb, line 146
def build_response(client)
  klass = Net::HTTPResponse.send(:response_class,
    client.response_header.status.to_s
  )

  response = klass.new(
    client.response_header.http_version,
    client.response_header.status.to_s,
    client.response_header.http_reason
  )

  client.response_header.raw.each do |k, v|
    response.add_field(k, v)
  end

  response.body = client.response

  response.instance_variable_set :@read, true

  response
end
run_request(connection, method, options) click to toggle source
# File lib/paypal-sdk/http_adapters/em_http.rb, line 135
def run_request(connection, method, options)
  connection.setup_request(method, options).tap do |client|
    unless client.error
      fiber = Fiber.current
      client.callback { fiber.resume(client) }
      client.errback { fiber.resume(client) }
      Fiber.yield
    end
  end
end
within_reactor() { || ... } click to toggle source
# File lib/paypal-sdk/http_adapters/em_http.rb, line 118
def within_reactor
  result = nil

  if EM.reactor_running?
    result = yield
  else
    EM.run do
      Fiber.new do
        result = yield
        EM.stop
      end.resume
    end
  end

  result
end
within_session() { || ... } click to toggle source
# File lib/paypal-sdk/http_adapters/em_http.rb, line 108
def within_session
  if started?
    yield
  else
    start do
      yield
    end
  end
end