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.
-
#interpolated_path(args = {}) ⇒ String
Interpolate parts of the path marked through color.
-
#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 |
# 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 } 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
48 49 50 51 52 53 54 55 56 |
# File 'lib/orthrus/remote_method.rb', line 48 def handle_response(request) request.on_complete do |response| if response.success? @on_success.call(response) else @on_failure.call(response) end end end |
#interpolated_path(args = {}) ⇒ String
Interpolate parts of the path marked through color
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/orthrus/remote_method.rb', line 35 def interpolated_path(args = {}) interpolated_path = @path args.each do |key, value| if interpolated_path.include?(":#{key}") interpolated_path.sub!(":#{key}", value.to_s) args.delete(key) end end interpolated_path end |
#run(args = {}) ⇒ Response, Object
Perform the request, handle response and return the result
20 21 22 23 24 25 26 27 |
# File 'lib/orthrus/remote_method.rb', line 20 def run(args = {}) url = base_uri + interpolated_path(args) request = Typhoeus::Request.new(url, @options.smart_merge(args)) handle_response(request) Typhoeus::Hydra.hydra.queue request Typhoeus::Hydra.hydra.run request.handled_response end |