Class: ActiveResource::PersistentConnection

Inherits:
Object
  • Object
show all
Defined in:
lib/active_resource/persistent_http_mock.rb,
lib/active_resource/persistent_connection.rb

Overview

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

Direct Known Subclasses

RestApi::UserAwareConnection

Defined Under Namespace

Classes: ServerRefusedConnection

Constant Summary collapse

HTTP_FORMAT_HEADER_NAMES =
{  :get => 'Accept',
  :put => 'Content-Type',
  :post => 'Content-Type',
  :patch => 'Content-Type',
  :delete => 'Accept',
  :head => 'Accept'
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site, format = ActiveResource::Formats::XmlFormat) ⇒ PersistentConnection

The site parameter is required and will set the site attribute to the URI for the remote resource service.

Raises:

  • (ArgumentError)


49
50
51
52
53
54
55
# File 'lib/active_resource/persistent_connection.rb', line 49

def initialize(site, format = ActiveResource::Formats::XmlFormat)
  raise ArgumentError, 'Missing site URI' unless site
  @user = @password = nil
  @uri_parser = URI.const_defined?(:Parser) ? URI::Parser.new : URI
  self.site = site
  self.format = format
end

Instance Attribute Details

#auth_typeObject

Returns the value of attribute auth_type.



33
34
35
# File 'lib/active_resource/persistent_connection.rb', line 33

def auth_type
  @auth_type
end

#connection_nameObject

Override to change the Persistent slot



43
44
45
# File 'lib/active_resource/persistent_connection.rb', line 43

def connection_name
  @connection_name
end

#formatObject

Returns the value of attribute format.



34
35
36
# File 'lib/active_resource/persistent_connection.rb', line 34

def format
  @format
end

#passwordObject

Returns the value of attribute password.



33
34
35
# File 'lib/active_resource/persistent_connection.rb', line 33

def password
  @password
end

#proxyObject

Returns the value of attribute proxy.



33
34
35
# File 'lib/active_resource/persistent_connection.rb', line 33

def proxy
  @proxy
end

#siteObject

Returns the value of attribute site.



33
34
35
# File 'lib/active_resource/persistent_connection.rb', line 33

def site
  @site
end

#ssl_optionsObject

Returns the value of attribute ssl_options.



33
34
35
# File 'lib/active_resource/persistent_connection.rb', line 33

def ssl_options
  @ssl_options
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



33
34
35
# File 'lib/active_resource/persistent_connection.rb', line 33

def timeout
  @timeout
end

#userObject

Returns the value of attribute user.



33
34
35
# File 'lib/active_resource/persistent_connection.rb', line 33

def user
  @user
end

Class Method Details

.requestsObject



37
38
39
# File 'lib/active_resource/persistent_connection.rb', line 37

def requests
  @@requests ||= []
end

Instance Method Details

#debug_output=(debug_output) ⇒ Object

Sets the debug output stream for HTTP requests



85
86
87
# File 'lib/active_resource/persistent_connection.rb', line 85

def debug_output=(debug_output)
  @debug_output = debug_output
end

#delete(path, headers = {}) ⇒ Object

Executes a DELETE request (see HTTP protocol documentation if unfamiliar). Used to delete resources.



114
115
116
# File 'lib/active_resource/persistent_connection.rb', line 114

def delete(path, headers = {})
  with_auth { request(:delete, path, build_request_headers(headers, :delete, self.site.merge(path))) }
end

#get(path, headers = {}) ⇒ Object

Executes a GET request. Used to get (find) resources. Note; removed format.decode wrapper and .get method call



108
109
110
# File 'lib/active_resource/persistent_connection.rb', line 108

def get(path, headers = {})
  with_auth { request(:get, path, build_request_headers(headers, :get, self.site.merge(path))) }
end

#head(path, headers = {}) ⇒ Object

Executes a HEAD request. Used to obtain meta-information about resources, such as whether they exist and their size (via response headers).



138
139
140
# File 'lib/active_resource/persistent_connection.rb', line 138

def head(path, headers = {})
  with_auth { request(:head, path, build_request_headers(headers, :head, self.site.merge(path))) }
end

#patch(path, body = '', headers = {}) ⇒ Object

Executes a PATCH request. Used to create new resources.



132
133
134
# File 'lib/active_resource/persistent_connection.rb', line 132

def patch(path, body = '', headers = {})
  with_auth { request(:patch, path, body.to_s, build_request_headers(headers, :patch, self.site.merge(path))) }
end

#post(path, body = '', headers = {}) ⇒ Object

Executes a POST request. Used to create new resources.



126
127
128
# File 'lib/active_resource/persistent_connection.rb', line 126

def post(path, body = '', headers = {})
  with_auth { request(:post, path, body.to_s, build_request_headers(headers, :post, self.site.merge(path))) }
end

#put(path, body = '', headers = {}) ⇒ Object

Executes a PUT request (see HTTP protocol documentation if unfamiliar). Used to update resources.



120
121
122
# File 'lib/active_resource/persistent_connection.rb', line 120

def put(path, body = '', headers = {})
  with_auth { request(:put, path, body.to_s, build_request_headers(headers, :put, self.site.merge(path))) }
end