Sha256: f20e851a13cbf685d474da4a17f82a4b13e23283ee040daa98fa31d32ed83cf4

Contents?: true

Size: 1.73 KB

Versions: 7

Compression:

Stored size: 1.73 KB

Contents

module Typhoeus
  class RemoteProxyObject
    instance_methods.each { |m| undef_method m unless m =~ /^__/ }
    
    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
    
    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,
                                  :headers => @easy.response_header,
                                  :body => @easy.response_body,
                                  :time => @easy.total_time_taken,
                                  :requested_url => @easy.url,
                                  :requested_http_method => @easy.method)
          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
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
pauldix-typhoeus-0.0.13 lib/typhoeus/remote_proxy_object.rb
pauldix-typhoeus-0.0.14 lib/typhoeus/remote_proxy_object.rb
pauldix-typhoeus-0.0.15 lib/typhoeus/remote_proxy_object.rb
pauldix-typhoeus-0.0.16 lib/typhoeus/remote_proxy_object.rb
pauldix-typhoeus-0.0.17 lib/typhoeus/remote_proxy_object.rb
pauldix-typhoeus-0.0.18 lib/typhoeus/remote_proxy_object.rb
pauldix-typhoeus-0.0.19 lib/typhoeus/remote_proxy_object.rb