Class: Flexirest::Connection
- Inherits:
-
Object
- Object
- Flexirest::Connection
- Defined in:
- lib/flexirest/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
- #patch(path, data, options = {}) ⇒ 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/flexirest/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/flexirest/connection.rb', line 9 def base_url @base_url end |
#session ⇒ Object
Returns the value of attribute session.
9 10 11 |
# File 'lib/flexirest/connection.rb', line 9 def session @session end |
Instance Method Details
#delete(path, options = {}) ⇒ Object
84 85 86 87 88 89 90 91 92 93 |
# File 'lib/flexirest/connection.rb', line 84 def delete(path, ={}) set_defaults() make_safe_request(path) do @session.delete(path) do |req| set_per_request_timeout(req, ) if [:timeout] 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 46 |
# File 'lib/flexirest/connection.rb', line 37 def get(path, ={}) set_defaults() make_safe_request(path) do @session.get(path) do |req| set_per_request_timeout(req, ) if [:timeout] req.headers = req.headers.merge([:headers]) sign_request(req, [:api_auth]) end end end |
#headers ⇒ Object
20 21 22 |
# File 'lib/flexirest/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/flexirest/connection.rb', line 24 def make_safe_request(path, &block) block.call rescue Faraday::Error::TimeoutError raise Flexirest::TimeoutException.new("Timed out getting #{full_url(path)}") rescue Faraday::Error::ConnectionFailed begin reconnect block.call rescue Faraday::Error::ConnectionFailed raise Flexirest::ConnectionFailedException.new("Unable to connect to #{full_url(path)}") end end |
#patch(path, data, options = {}) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/flexirest/connection.rb', line 60 def patch(path, data, ={}) set_defaults() make_safe_request(path) do @session.patch(path) do |req| set_per_request_timeout(req, ) if [:timeout] req.headers = req.headers.merge([:headers]) req.body = data sign_request(req, [:api_auth]) end end end |
#post(path, data, options = {}) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/flexirest/connection.rb', line 72 def post(path, data, ={}) set_defaults() make_safe_request(path) do @session.post(path) do |req| set_per_request_timeout(req, ) if [:timeout] req.headers = req.headers.merge([:headers]) req.body = data sign_request(req, [:api_auth]) end end end |
#put(path, data, options = {}) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/flexirest/connection.rb', line 48 def put(path, data, ={}) set_defaults() make_safe_request(path) do @session.put(path) do |req| set_per_request_timeout(req, ) if [:timeout] req.headers = req.headers.merge([:headers]) req.body = data sign_request(req, [:api_auth]) end end end |
#reconnect ⇒ Object
16 17 18 |
# File 'lib/flexirest/connection.rb', line 16 def reconnect @session = new_session end |