Class: Slash::NetHttpConnection

Inherits:
Connection show all
Defined in:
lib/slash/nethttp.rb

Overview

Class to handle connections to remote web services. This class is used by ActiveResource::Base to interface with REST services.

Constant Summary collapse

@@request_types =
{
  :get => Net::HTTP::Get,
  :post => Net::HTTP::Post,
  :put => Net::HTTP::Put,
  :delete => Net::HTTP::Delete
}

Instance Attribute Summary collapse

Attributes inherited from Connection

#timeout

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Connection

create_default, #delete, #get, #head, #post, #put

Instance Attribute Details

#proxyObject

Returns the value of attribute proxy.



25
26
27
# File 'lib/slash/nethttp.rb', line 25

def proxy
  @proxy
end

#ssl_optionsObject

Returns the value of attribute ssl_options.



25
26
27
# File 'lib/slash/nethttp.rb', line 25

def ssl_options
  @ssl_options
end

Class Method Details

.request_typesObject



21
22
23
# File 'lib/slash/nethttp.rb', line 21

def self.request_types
  @@request_types
end

Instance Method Details

#request(method, uri, options = {}) ⇒ Object

Raises:

  • (ArgumentError)


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/slash/nethttp.rb', line 45

def request(method, uri, options = {})
  raise ArgumentError, 'this connection does not support async mode' if options[:async]

  options = options.dup
  prepare_request(uri, options)

  rqtype = @@request_types[method] || raise(ArgumentError, "Unsupported method #{method}")
  params = options[:params]
  if !params.blank?
    if [:post, :put].include?(method)
      form_data = params
    else
      uri = uri.dup
      uri.query_values = (uri.query_values(:notation => :flat) || {}).to_mash.update(params)
    end
  end
  rq = rqtype.new(uri.query.blank? ? uri.path : "#{uri.path}?#{uri.query}", options[:headers])
  rq.form_data = form_data if form_data
  rq.body = options[:body] if options[:body]

  resp = http_request(uri, rq)
  if block_given?
    yield resp
  else
    check_and_raise(resp)
  end
end

#timeout=(timeout) ⇒ Object

Set the number of seconds after which HTTP requests to the remote service should time out.



34
35
36
37
# File 'lib/slash/nethttp.rb', line 34

def timeout=(timeout)
  @timeout = timeout
  configure_http(@http) if @http
end