Class: ActiveResource::Connection
- Inherits:
-
Object
- Object
- ActiveResource::Connection
- Defined in:
- lib/active_resource/connection.rb,
lib/active_resource/http_mock.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
- HTTP_FORMAT_HEADER_NAMES =
{ :get => 'Accept', :put => 'Content-Type', :post => 'Content-Type', :delete => 'Accept' }
Instance Attribute Summary collapse
-
#format ⇒ Object
Returns the value of attribute format.
-
#password ⇒ Object
Returns the value of attribute password.
-
#site ⇒ Object
Returns the value of attribute site.
-
#timeout ⇒ Object
Returns the value of attribute timeout.
-
#user ⇒ Object
Returns the value of attribute user.
Class Method Summary collapse
Instance Method Summary collapse
-
#delete(path, headers = {}) ⇒ Object
Execute a DELETE request (see HTTP protocol documentation if unfamiliar).
-
#get(path, headers = {}) ⇒ Object
Execute a GET request.
-
#head(path, headers = {}) ⇒ Object
Execute a HEAD request.
-
#initialize(site, format = ) ⇒ Connection
constructor
The
site
parameter is required and will set thesite
attribute to the URI for the remote resource service. -
#post(path, body = '', headers = {}) ⇒ Object
Execute a POST request.
-
#put(path, body = '', headers = {}) ⇒ Object
Execute a PUT request (see HTTP protocol documentation if unfamiliar).
Constructor Details
#initialize(site, format = ) ⇒ Connection
The site
parameter is required and will set the site
attribute to the URI for the remote resource service.
84 85 86 87 88 89 |
# File 'lib/active_resource/connection.rb', line 84 def initialize(site, format = ActiveResource::Formats[:xml]) raise ArgumentError, 'Missing site URI' unless site @user = @password = nil self.site = site self.format = format end |
Instance Attribute Details
#format ⇒ Object
Returns the value of attribute format.
74 75 76 |
# File 'lib/active_resource/connection.rb', line 74 def format @format end |
#password ⇒ Object
Returns the value of attribute password.
73 74 75 |
# File 'lib/active_resource/connection.rb', line 73 def password @password end |
#site ⇒ Object
Returns the value of attribute site.
73 74 75 |
# File 'lib/active_resource/connection.rb', line 73 def site @site end |
#timeout ⇒ Object
Returns the value of attribute timeout.
73 74 75 |
# File 'lib/active_resource/connection.rb', line 73 def timeout @timeout end |
#user ⇒ Object
Returns the value of attribute user.
73 74 75 |
# File 'lib/active_resource/connection.rb', line 73 def user @user end |
Class Method Details
.requests ⇒ Object
77 78 79 |
# File 'lib/active_resource/connection.rb', line 77 def requests @@requests ||= [] end |
Instance Method Details
#delete(path, headers = {}) ⇒ Object
Execute a DELETE request (see HTTP protocol documentation if unfamiliar). Used to delete resources.
121 122 123 |
# File 'lib/active_resource/connection.rb', line 121 def delete(path, headers = {}) request(:delete, path, build_request_headers(headers, :delete)) end |
#get(path, headers = {}) ⇒ Object
Execute a GET request. Used to get (find) resources.
115 116 117 |
# File 'lib/active_resource/connection.rb', line 115 def get(path, headers = {}) format.decode(request(:get, path, build_request_headers(headers, :get)).body) end |
#head(path, headers = {}) ⇒ Object
Execute a HEAD request. Used to obtain meta-information about resources, such as whether they exist and their size (via response headers).
139 140 141 |
# File 'lib/active_resource/connection.rb', line 139 def head(path, headers = {}) request(:head, path, build_request_headers(headers)) end |
#post(path, body = '', headers = {}) ⇒ Object
Execute a POST request. Used to create new resources.
133 134 135 |
# File 'lib/active_resource/connection.rb', line 133 def post(path, body = '', headers = {}) request(:post, path, body.to_s, build_request_headers(headers, :post)) end |
#put(path, body = '', headers = {}) ⇒ Object
Execute a PUT request (see HTTP protocol documentation if unfamiliar). Used to update resources.
127 128 129 |
# File 'lib/active_resource/connection.rb', line 127 def put(path, body = '', headers = {}) request(:put, path, body.to_s, build_request_headers(headers, :put)) end |