Class: Roar::Transport::NetHTTP::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/roar/transport/net_http/request.rb

Overview

TODO: implement me.

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Request

Returns a new instance of Request.


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/roar/transport/net_http/request.rb', line 8

def initialize(options)
  @uri     = parse_uri(options[:uri]) # TODO: add :uri.
  @as      = options[:as]
  @body    = options[:body]
  @options = options

  @http = Net::HTTP.new(uri.host, uri.port)
  unless options[:pem_file].nil?
    pem = File.read(options[:pem_file])
    @http.use_ssl = true
    @http.cert = OpenSSL::X509::Certificate.new(pem)
    @http.key = OpenSSL::PKey::RSA.new(pem)
    @http.verify_mode = options[:ssl_verify_mode].nil? ? OpenSSL::SSL::VERIFY_PEER : options[:ssl_verify_mode]
  end
end

Instance Method Details

#call(what) {|req| ... } ⇒ Object

Yields:

  • (req)

24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/roar/transport/net_http/request.rb', line 24

def call(what)
  @req = what.new(uri.request_uri)

  # if options[:ssl]
  #   uri.port = Net::HTTP.https_default_port()
  # end
  https!
  basic_auth!

  req.content_type = as
  req["accept"]    = as # TODO: test me. # DISCUSS: if Accept is not set, rails treats this request as as "text/html".
  req.body         = body if body

  yield req if block_given?

  http.request(req).tap do |res|
    handle_error!(res)
  end
end

#getObject


44
45
46
# File 'lib/roar/transport/net_http/request.rb', line 44

def get
  call(Net::HTTP::Get)
end