Class: ActiveRestClient::Connection
- Inherits:
-
Object
- Object
- ActiveRestClient::Connection
- Defined in:
- lib/active_rest_client/connection.rb
Instance Attribute Summary collapse
-
#base_url ⇒ Object
Returns the value of attribute base_url.
-
#session ⇒ Object
Returns the value of attribute session.
Instance Method Summary collapse
- #delete(path, options = {}) ⇒ Object
- #get(path, options = {}) ⇒ Object
- #headers ⇒ Object
-
#initialize(base_url) ⇒ Connection
constructor
A new instance of Connection.
- #make_safe_request(path, &block) ⇒ Object
- #post(path, data, options = {}) ⇒ Object
- #put(path, data, options = {}) ⇒ Object
- #reconnect ⇒ Object
Constructor Details
#initialize(base_url) ⇒ Connection
Returns a new instance of Connection.
11 12 13 14 |
# File 'lib/active_rest_client/connection.rb', line 11 def initialize(base_url) @base_url = base_url @session = new_session end |
Instance Attribute Details
#base_url ⇒ Object
Returns the value of attribute base_url.
9 10 11 |
# File 'lib/active_rest_client/connection.rb', line 9 def base_url @base_url end |
#session ⇒ Object
Returns the value of attribute session.
9 10 11 |
# File 'lib/active_rest_client/connection.rb', line 9 def session @session end |
Instance Method Details
#delete(path, options = {}) ⇒ Object
69 70 71 72 73 74 75 76 77 |
# File 'lib/active_rest_client/connection.rb', line 69 def delete(path, ={}) set_defaults() make_safe_request(path) do @session.delete(path) do |req| req.headers = req.headers.merge([:headers]) sign_request(req, [:api_auth]) end end end |
#get(path, options = {}) ⇒ Object
37 38 39 40 41 42 43 44 45 |
# File 'lib/active_rest_client/connection.rb', line 37 def get(path, ={}) set_defaults() make_safe_request(path) do @session.get(path) do |req| req.headers = req.headers.merge([:headers]) sign_request(req, [:api_auth]) end end end |
#headers ⇒ Object
20 21 22 |
# File 'lib/active_rest_client/connection.rb', line 20 def headers @session.headers end |
#make_safe_request(path, &block) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/active_rest_client/connection.rb', line 24 def make_safe_request(path, &block) block.call rescue Faraday::Error::TimeoutError raise ActiveRestClient::TimeoutException.new("Timed out getting #{full_url(path)}") rescue Faraday::Error::ConnectionFailed begin reconnect block.call rescue Faraday::Error::ConnectionFailed raise ActiveRestClient::ConnectionFailedException.new("Unable to connect to #{full_url(path)}") end end |
#post(path, data, options = {}) ⇒ Object
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/active_rest_client/connection.rb', line 58 def post(path, data, ={}) set_defaults() make_safe_request(path) do @session.post(path) do |req| req.headers = req.headers.merge([:headers]) req.body = data sign_request(req, [:api_auth]) end end end |
#put(path, data, options = {}) ⇒ Object
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/active_rest_client/connection.rb', line 47 def put(path, data, ={}) set_defaults() make_safe_request(path) do @session.put(path) do |req| req.headers = req.headers.merge([:headers]) req.body = data sign_request(req, [:api_auth]) end end end |
#reconnect ⇒ Object
16 17 18 |
# File 'lib/active_rest_client/connection.rb', line 16 def reconnect @session = new_session end |