Class: Orthrus::RemoteMethod
- Inherits:
-
Object
- Object
- Orthrus::RemoteMethod
- Defined in:
- lib/orthrus/remote_method.rb
Instance Attribute Summary collapse
-
#base_uri ⇒ Object
Returns the value of attribute base_uri.
-
#on_failure ⇒ Object
Returns the value of attribute on_failure.
-
#on_success ⇒ Object
Returns the value of attribute on_success.
-
#options ⇒ Object
Returns the value of attribute options.
-
#path ⇒ Object
Returns the value of attribute path.
Instance Method Summary collapse
-
#handle_response(request) ⇒ Object
Call success and failure handler on request complete.
-
#initialize(options = {}) ⇒ RemoteMethod
constructor
Extract request options to class variables.
-
#interpolate(url, args = {}) ⇒ String
Interpolate parts of the given url which are marked by colon.
-
#run(args = {}) ⇒ Response, Object
Perform the request, handle response and return the result.
Constructor Details
#initialize(options = {}) ⇒ RemoteMethod
Extract request options to class variables. All other options will be passed through to Typhoeus.
8 9 10 11 12 13 14 15 16 |
# File 'lib/orthrus/remote_method.rb', line 8 def initialize( = {}) [:method] ||= :get @options = === RemoteOptions ? : RemoteOptions.new() @base_uri = .delete(:base_uri) @path = .delete(:path) || "" @on_success = [:on_success] || lambda { |response| response } @on_failure = [:on_failure] || lambda { |response| response } @debug = .delete(:debug) end |
Instance Attribute Details
#base_uri ⇒ Object
Returns the value of attribute base_uri.
3 4 5 |
# File 'lib/orthrus/remote_method.rb', line 3 def base_uri @base_uri end |
#on_failure ⇒ Object
Returns the value of attribute on_failure.
3 4 5 |
# File 'lib/orthrus/remote_method.rb', line 3 def on_failure @on_failure end |
#on_success ⇒ Object
Returns the value of attribute on_success.
3 4 5 |
# File 'lib/orthrus/remote_method.rb', line 3 def on_success @on_success end |
#options ⇒ Object
Returns the value of attribute options.
3 4 5 |
# File 'lib/orthrus/remote_method.rb', line 3 def @options end |
#path ⇒ Object
Returns the value of attribute path.
3 4 5 |
# File 'lib/orthrus/remote_method.rb', line 3 def path @path end |
Instance Method Details
#handle_response(request) ⇒ Object
Call success and failure handler on request complete
56 57 58 59 60 61 62 63 64 |
# File 'lib/orthrus/remote_method.rb', line 56 def handle_response(request) request.on_complete do |response| if response.success? @on_success.call(response) else @on_failure.call(response) end end end |
#interpolate(url, args = {}) ⇒ String
Interpolate parts of the given url which are marked by colon
42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/orthrus/remote_method.rb', line 42 def interpolate(url, args = {}) result = url args.each do |key, value| if result.include?(":#{key}") result.sub!(":#{key}", value.to_s) args.delete(key) end end Orthrus::Logger.debug("interpolation result is #{result}") if @debug result end |
#run(args = {}) ⇒ Response, Object
Perform the request, handle response and return the result
21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/orthrus/remote_method.rb', line 21 def run(args = {}) = @options.smart_merge(args) url = interpolate(base_uri + path, ) request = Typhoeus::Request.new(url, ) Orthrus::Logger.debug("request to #{url}\n with options #{.inspect}") if @debug handle_response(request) if [:return_request] request else Typhoeus::Hydra.hydra.queue request Typhoeus::Hydra.hydra.run request.handled_response end end |