Class: Typhoeus::RemoteProxyObject

Inherits:
Object
  • Object
show all
Defined in:
lib/typhoeus/remote_proxy_object.rb

Instance Method Summary collapse

Constructor Details

#initialize(clear_memoized_store_proc, easy, options = {}) ⇒ RemoteProxyObject

Returns a new instance of RemoteProxyObject.



5
6
7
8
9
10
11
12
13
14
# File 'lib/typhoeus/remote_proxy_object.rb', line 5

def initialize(clear_memoized_store_proc, easy, options = {})
  @clear_memoized_store_proc = clear_memoized_store_proc
  @easy      = easy
  @success   = options[:on_success]
  @failure   = options[:on_failure]
  @cache     = options.delete(:cache)
  @cache_key = options.delete(:cache_key)
  @timeout   = options.delete(:cache_timeout)
  Typhoeus.add_easy_request(@easy)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/typhoeus/remote_proxy_object.rb', line 16

def method_missing(sym, *args, &block)
  unless @proxied_object
    if @cache && @cache_key
      @proxied_object = @cache.get(@cache_key) rescue nil
    end

    unless @proxied_object
      Typhoeus.perform_easy_requests
      response = Response.new(:code => @easy.response_code,
                              :curl_return_code => @easy.curl_return_code,
                              :curl_error_message => @easy.curl_error_message,
                              :headers => @easy.response_header,
                              :body => @easy.response_body,
                              :time => @easy.total_time_taken,
                              :requested_url => @easy.url,
                              :requested_http_method => @easy.method,
                              :start_time => @easy.start_time)
      if @easy.response_code >= 200 && @easy.response_code < 300
        Typhoeus.release_easy_object(@easy)
        @proxied_object = @success.nil? ? response : @success.call(response)

        if @cache && @cache_key
          @cache.set(@cache_key, @proxied_object, @timeout)
        end
      else
        @proxied_object = @failure.nil? ? response : @failure.call(response)
      end
     @clear_memoized_store_proc.call
   end
  end

  @proxied_object.__send__(sym, *args, &block)
end