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 put request.
- #head(path, headers = self.headers) ⇒ Object
-
#initialize(site, format = ApiResource::Formats::JsonFormat, headers) ⇒ Connection
constructor
The
siteparameter is required and will set thesiteattribute 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 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 |
# 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 put 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 format.decode(request(:get, path, headers)) end end |
#head(path, headers = self.headers) ⇒ Object
74 75 76 |
# File 'lib/api_resource/connection.rb', line 74 def head(path, headers = self.headers) request(:head, path, build_request_headers(headers, :head, self.site.merge(path))) end |
#post(path, body = {}, headers = self.headers) ⇒ String
make a post request
99 100 101 102 103 104 105 106 107 108 |
# File 'lib/api_resource/connection.rb', line 99 def post(path, body = {}, headers = self.headers) format.decode( request( :post, path, body, build_request_headers(headers, :post, self.site.merge(path)) ) ) end |
#put(path, body = {}, headers = self.headers) ⇒ String
make a put request
83 84 85 86 87 88 89 90 91 92 |
# File 'lib/api_resource/connection.rb', line 83 def put(path, body = {}, headers = self.headers) format.decode( request( :put, path, body, build_request_headers(headers, :put, self.site.merge(path)) ) ) end |