Class: Vidocq::Connection
- Inherits:
-
Object
- Object
- Vidocq::Connection
- Defined in:
- lib/vidocq/connection.rb
Instance Method Summary collapse
-
#call(opts = {}) ⇒ Object
Finds an endpoint and calls it with the given options.
- #get_endpoints ⇒ Object
-
#initialize(sid, version, opts = {}) ⇒ Connection
constructor
A new instance of Connection.
Constructor Details
#initialize(sid, version, opts = {}) ⇒ Connection
Returns a new instance of Connection.
7 8 9 10 |
# File 'lib/vidocq/connection.rb', line 7 def initialize(sid, version, opts = {}) @fallbacks = opts.delete(:fallbacks) || [] @cache = Cache.new(sid, version, opts) end |
Instance Method Details
#call(opts = {}) ⇒ Object
Finds an endpoint and calls it with the given options. If there is a connection failure, new attempts are made with new endpoints.
Two failure modes are handled:
-
There are no registered endpoints: NoEndpointError
-
None of the registered endpoints respond: NoResponseError
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/vidocq/connection.rb', line 19 def call(opts = {}) resource_id = opts.delete(:id) endpoints = get_endpoints raise NoEndpointError if endpoints.empty? Vidocq.logger.info "Vidocq endpoints: #{endpoints}" begin endpoint = endpoints.slice!(rand(endpoints.size)) path = [endpoint, resource_id].compact.join('/') url = "#{path}?#{build_querystring(opts)}" begin return HTTParty.get(url) rescue Exception => e Vidocq.logger.warn "Error requesting '#{url}': #{e}." end end while endpoints.any? Vidocq.logger.warn "Vidocq: Unable to reach any of the endpoints" raise NoResponseError end |
#get_endpoints ⇒ Object
41 42 43 44 |
# File 'lib/vidocq/connection.rb', line 41 def get_endpoints endpoints = @cache.endpoints || [] endpoints.empty? ? @fallbacks : endpoints end |