Class: Orthrus::RemoteMethod

Inherits:
Object
  • Object
show all
Defined in:
lib/orthrus/remote_method.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ RemoteMethod

Extract request options to class variables. All other options will be passed through to Typhoeus.

Parameters:

  • options (Hash) (defaults to: {})

    for the request



8
9
10
11
12
13
14
15
# File 'lib/orthrus/remote_method.rb', line 8

def initialize(options = {})
  options[:method] ||= :get
  @options    = options === RemoteOptions ? options : RemoteOptions.new(options)
  @base_uri   = options.delete(:base_uri)
  @path       = options.delete(:path)
  @on_success = options[:on_success] || lambda { |response| response }
  @on_failure = options[:on_failure] || lambda { |response| response }
end

Instance Attribute Details

#base_uriObject

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_failureObject

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_successObject

Returns the value of attribute on_success.



3
4
5
# File 'lib/orthrus/remote_method.rb', line 3

def on_success
  @on_success
end

#optionsObject

Returns the value of attribute options.



3
4
5
# File 'lib/orthrus/remote_method.rb', line 3

def options
  @options
end

#pathObject

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

Parameters:

  • request (Typhoeus::Request)

    that needs success or failure handling



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

Examples:

Interpolate a path

path = "/planet/:identifier"
interpolated_path({:identifier => "mars"}) #=> "/planet/mars"

Parameters:

  • args (Hash) (defaults to: {})

    to perform interpolation

Returns:

  • (String)

    the interpolated path



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

Parameters:

  • args (Hash) (defaults to: {})

    for interpolation and request options

Returns:

  • (Response, Object)

    the Typhoeus::Response or the result of the on_complete block



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