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
16
# 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 }
  @debug      = options.delete(:debug)
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



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

Examples:

Interpolate a url

interpolate("http://example.com/planet/:identifier", {:identifier => "mars"}) #=> "http://example.com/planet/mars"

Parameters:

  • url (String)

    to be interpolated

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

    to perform interpolation

Returns:

  • (String)

    the interpolated url



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

Parameters:

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

    for interpolation and request options

Returns:

  • (Response, Object)

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



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 = @options.smart_merge(args)
  url     = interpolate(base_uri + path, options)
  request = Typhoeus::Request.new(url, options)
  Orthrus::Logger.debug("request to #{url}\n  with options #{options.inspect}") if @debug
  handle_response(request)
  if options[:return_request]
    request
  else
    Typhoeus::Hydra.hydra.queue request
    Typhoeus::Hydra.hydra.run
    request.handled_response
  end
end