Class: ApiResource::Connection
- Inherits:
-
Object
- Object
- ApiResource::Connection
- Defined in:
- lib/api_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', :delete => 'Accept', :head => 'Accept' }
Instance Attribute Summary collapse
-
#auth_type ⇒ Object
readonly
Returns the value of attribute auth_type.
-
#format ⇒ Object
Returns the value of attribute format.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#password ⇒ Object
readonly
Returns the value of attribute password.
-
#proxy ⇒ Object
readonly
Returns the value of attribute proxy.
-
#site ⇒ Object
Returns the value of attribute site.
-
#ssl_options ⇒ Object
readonly
Returns the value of attribute ssl_options.
-
#timeout ⇒ Object
Returns the value of attribute timeout.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Class Method Summary collapse
Instance Method Summary collapse
- #delete(path, headers = self.headers) ⇒ Object
-
#get(path, headers = self.headers) ⇒ String
make a get request.
- #head(path, headers = self.headers) ⇒ Object
-
#initialize(site, format = ApiResource::Formats::JsonFormat, headers) ⇒ Connection
constructor
The +site+ parameter is required and will set the +site+ attribute to the URI for the remote resource service.
-
#post(path, body = {}, headers = self.headers) ⇒ String
make a post request.
-
#put(path, body = {}, headers = self.headers) ⇒ String
make a put request.
Constructor Details
#initialize(site, format = ApiResource::Formats::JsonFormat, headers) ⇒ Connection
The +site+ parameter is required and will set the +site+ attribute to the URI for the remote resource service.
33 34 35 36 37 38 39 40 |
# File 'lib/api_resource/connection.rb', line 33 def initialize(site, format = ApiResource::Formats::JsonFormat, headers) 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 self.headers = headers end |
Instance Attribute Details
#auth_type ⇒ Object (readonly)
Returns the value of attribute auth_type.
22 23 24 |
# File 'lib/api_resource/connection.rb', line 22 def auth_type @auth_type end |
#format ⇒ Object
Returns the value of attribute format.
23 24 25 |
# File 'lib/api_resource/connection.rb', line 23 def format @format end |
#headers ⇒ Object
Returns the value of attribute headers.
23 24 25 |
# File 'lib/api_resource/connection.rb', line 23 def headers @headers end |
#password ⇒ Object (readonly)
Returns the value of attribute password.
22 23 24 |
# File 'lib/api_resource/connection.rb', line 22 def password @password end |
#proxy ⇒ Object (readonly)
Returns the value of attribute proxy.
22 23 24 |
# File 'lib/api_resource/connection.rb', line 22 def proxy @proxy end |
#site ⇒ Object
Returns the value of attribute site.
22 23 24 |
# File 'lib/api_resource/connection.rb', line 22 def site @site end |
#ssl_options ⇒ Object (readonly)
Returns the value of attribute ssl_options.
22 23 24 |
# File 'lib/api_resource/connection.rb', line 22 def @ssl_options end |
#timeout ⇒ Object
Returns the value of attribute timeout.
22 23 24 |
# File 'lib/api_resource/connection.rb', line 22 def timeout @timeout end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
22 23 24 |
# File 'lib/api_resource/connection.rb', line 22 def user @user end |
Class Method Details
.requests ⇒ Object
26 27 28 |
# File 'lib/api_resource/connection.rb', line 26 def requests @@requests ||= [] end |
Instance Method Details
#delete(path, headers = self.headers) ⇒ Object
69 70 71 72 73 74 75 76 77 |
# File 'lib/api_resource/connection.rb', line 69 def delete(path, headers = self.headers) request( :delete, path, {}, build_request_headers(headers, :delete, self.site.merge(path)) ) return true end |
#get(path, headers = self.headers) ⇒ String
make a get request
59 60 61 62 63 64 65 66 67 |
# File 'lib/api_resource/connection.rb', line 59 def get(path, headers = self.headers) # our site and headers for this request site = self.site.merge(path) headers = build_request_headers(headers, :get, site) self.with_caching(path, headers) do request(:get, path, {}, headers) end end |
#head(path, headers = self.headers) ⇒ Object
79 80 81 82 83 84 85 86 87 |
# File 'lib/api_resource/connection.rb', line 79 def head(path, headers = self.headers) request( :head, path, {}, build_request_headers(headers, :head, self.site.merge(path)) ) return true end |
#post(path, body = {}, headers = self.headers) ⇒ String
make a post request
120 121 122 123 124 125 126 127 |
# File 'lib/api_resource/connection.rb', line 120 def post(path, body = {}, headers = self.headers) request( :post, path, format.encode(body), build_request_headers(headers, :post, self.site.merge(path)) ) end |
#put(path, body = {}, headers = self.headers) ⇒ String
make a put request
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/api_resource/connection.rb', line 94 def put(path, body = {}, headers = self.headers) response = request( :put, path, format.encode(body), build_request_headers(headers, :put, self.site.merge(path)) ) # handle blank response and return true if response.blank? return {} # we used to decode JSON in the response, but we don't want to # do that anymore - we will issue a warning but keep the behavior else ApiResource.logger.warn( "[DEPRECATION] Returning a response body from a PUT " + "is deprecated. \n#{response.pretty_inspect} was returned." ) return response end end |