Class: ActiveResource::Connection

Inherits:
Object
  • Object
show all
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',
  :head => 'Accept'
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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.

Raises:

  • (ArgumentError)


96
97
98
99
100
101
# File 'lib/active_resource/connection.rb', line 96

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

#formatObject

Returns the value of attribute format.



86
87
88
# File 'lib/active_resource/connection.rb', line 86

def format
  @format
end

#passwordObject

Returns the value of attribute password.



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

def password
  @password
end

#proxyObject

Returns the value of attribute proxy.



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

def proxy
  @proxy
end

#siteObject

Returns the value of attribute site.



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

def site
  @site
end

#ssl_optionsObject

Returns the value of attribute ssl_options.



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

def ssl_options
  @ssl_options
end

#timeoutObject

Returns the value of attribute timeout.



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

def timeout
  @timeout
end

#userObject

Returns the value of attribute user.



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

def user
  @user
end

Class Method Details

.requestsObject



89
90
91
# File 'lib/active_resource/connection.rb', line 89

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.



143
144
145
# File 'lib/active_resource/connection.rb', line 143

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.



137
138
139
# File 'lib/active_resource/connection.rb', line 137

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).



161
162
163
# File 'lib/active_resource/connection.rb', line 161

def head(path, headers = {})
  request(:head, path, build_request_headers(headers, :head))
end

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

Execute a POST request. Used to create new resources.



155
156
157
# File 'lib/active_resource/connection.rb', line 155

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.



149
150
151
# File 'lib/active_resource/connection.rb', line 149

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