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, data, 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.
12 13 14 15 |
# File 'lib/flexirest/connection.rb', line 12 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.
10 11 12 |
# File 'lib/flexirest/connection.rb', line 10 def base_url @base_url end |
#session ⇒ Object
Returns the value of attribute session.
10 11 12 |
# File 'lib/flexirest/connection.rb', line 10 def session @session end |
Instance Method Details
#delete(path, data, options = {}) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/flexirest/connection.rb', line 91 def delete(path, data, ={}) 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]) req.body = data sign_request(req, [:api_auth]) end end end |
#get(path, options = {}) ⇒ Object
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/flexirest/connection.rb', line 44 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
21 22 23 |
# File 'lib/flexirest/connection.rb', line 21 def headers @session.headers end |
#make_safe_request(path, &block) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/flexirest/connection.rb', line 25 def make_safe_request(path, &block) block.call rescue Faraday::TimeoutError raise Flexirest::TimeoutException.new("Timed out getting #{full_url(path)}") rescue Faraday::ConnectionFailed => e1 if e1.respond_to?(:cause) && e1.cause.is_a?(Net::OpenTimeout) raise Flexirest::TimeoutException.new("Timed out getting #{full_url(path)}") end begin reconnect block.call rescue Faraday::ConnectionFailed => e2 if e2.respond_to?(:cause) && e2.cause.is_a?(Net::OpenTimeout) raise Flexirest::TimeoutException.new("Timed out getting #{full_url(path)}") end raise Flexirest::ConnectionFailedException.new("Unable to connect to #{full_url(path)}") end end |
#patch(path, data, options = {}) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/flexirest/connection.rb', line 67 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
79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/flexirest/connection.rb', line 79 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
55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/flexirest/connection.rb', line 55 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
17 18 19 |
# File 'lib/flexirest/connection.rb', line 17 def reconnect @session = new_session end |