Class: ActiveResource::Connection
- Inherits:
-
Object
- Object
- ActiveResource::Connection
- Defined in:
- lib/active_resource/connection.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", patch: "Content-Type", delete: "Accept", head: "Accept" }
Instance Attribute Summary collapse
-
#auth_type ⇒ Object
Returns the value of attribute auth_type.
-
#bearer_token ⇒ Object
Returns the value of attribute bearer_token.
-
#format ⇒ Object
Returns the value of attribute format.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#open_timeout ⇒ Object
Returns the value of attribute open_timeout.
-
#password ⇒ Object
Returns the value of attribute password.
-
#proxy ⇒ Object
Returns the value of attribute proxy.
-
#read_timeout ⇒ Object
Returns the value of attribute read_timeout.
-
#site ⇒ Object
Returns the value of attribute site.
-
#ssl_options ⇒ Object
Returns the value of attribute ssl_options.
-
#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
Executes a DELETE request (see HTTP protocol documentation if unfamiliar).
-
#get(path, headers = {}) ⇒ Object
Executes a GET request.
-
#head(path, headers = {}) ⇒ Object
Executes a HEAD request.
-
#initialize(site, format = ActiveResource::Formats::JsonFormat, logger: nil) ⇒ Connection
constructor
The
site
parameter is required and will set thesite
attribute to the URI for the remote resource service. -
#patch(path, body = "", headers = {}) ⇒ Object
Executes a PATCH request (see HTTP protocol documentation if unfamiliar).
-
#post(path, body = "", headers = {}) ⇒ Object
Executes a POST request.
-
#put(path, body = "", headers = {}) ⇒ Object
Executes a PUT request (see HTTP protocol documentation if unfamiliar).
Constructor Details
#initialize(site, format = ActiveResource::Formats::JsonFormat, logger: nil) ⇒ Connection
The site
parameter is required and will set the site
attribute to the URI for the remote resource service.
35 36 37 38 39 40 41 |
# File 'lib/active_resource/connection.rb', line 35 def initialize(site, format = ActiveResource::Formats::JsonFormat, logger: nil) raise ArgumentError, "Missing site URI" unless site @proxy = @user = @password = @bearer_token = nil self.site = site self.format = format self.logger = logger end |
Instance Attribute Details
#auth_type ⇒ Object
Returns the value of attribute auth_type.
24 25 26 |
# File 'lib/active_resource/connection.rb', line 24 def auth_type @auth_type end |
#bearer_token ⇒ Object
Returns the value of attribute bearer_token.
24 25 26 |
# File 'lib/active_resource/connection.rb', line 24 def bearer_token @bearer_token end |
#format ⇒ Object
Returns the value of attribute format.
25 26 27 |
# File 'lib/active_resource/connection.rb', line 25 def format @format end |
#logger ⇒ Object
Returns the value of attribute logger.
25 26 27 |
# File 'lib/active_resource/connection.rb', line 25 def logger @logger end |
#open_timeout ⇒ Object
Returns the value of attribute open_timeout.
24 25 26 |
# File 'lib/active_resource/connection.rb', line 24 def open_timeout @open_timeout end |
#password ⇒ Object
Returns the value of attribute password.
24 25 26 |
# File 'lib/active_resource/connection.rb', line 24 def password @password end |
#proxy ⇒ Object
Returns the value of attribute proxy.
24 25 26 |
# File 'lib/active_resource/connection.rb', line 24 def proxy @proxy end |
#read_timeout ⇒ Object
Returns the value of attribute read_timeout.
24 25 26 |
# File 'lib/active_resource/connection.rb', line 24 def read_timeout @read_timeout end |
#site ⇒ Object
Returns the value of attribute site.
24 25 26 |
# File 'lib/active_resource/connection.rb', line 24 def site @site end |
#ssl_options ⇒ Object
Returns the value of attribute ssl_options.
24 25 26 |
# File 'lib/active_resource/connection.rb', line 24 def @ssl_options end |
#timeout ⇒ Object
Returns the value of attribute timeout.
24 25 26 |
# File 'lib/active_resource/connection.rb', line 24 def timeout @timeout end |
#user ⇒ Object
Returns the value of attribute user.
24 25 26 |
# File 'lib/active_resource/connection.rb', line 24 def user @user end |
Class Method Details
.requests ⇒ Object
28 29 30 |
# File 'lib/active_resource/connection.rb', line 28 def requests @@requests ||= [] end |
Instance Method Details
#delete(path, headers = {}) ⇒ Object
Executes a DELETE request (see HTTP protocol documentation if unfamiliar). Used to delete resources.
90 91 92 |
# File 'lib/active_resource/connection.rb', line 90 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.
84 85 86 |
# File 'lib/active_resource/connection.rb', line 84 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).
114 115 116 |
# File 'lib/active_resource/connection.rb', line 114 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 (see HTTP protocol documentation if unfamiliar). Used to update resources.
96 97 98 |
# File 'lib/active_resource/connection.rb', line 96 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.
108 109 110 |
# File 'lib/active_resource/connection.rb', line 108 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.
102 103 104 |
# File 'lib/active_resource/connection.rb', line 102 def put(path, body = "", headers = {}) with_auth { request(:put, path, body.to_s, build_request_headers(headers, :put, self.site.merge(path))) } end |