Sha256: d3203c9f93e46e3857c5af25fe066e8dab5ced12cb1d5ac6029a7d6a41e79bd7

Contents?: true

Size: 1.77 KB

Versions: 37

Compression:

Stored size: 1.77 KB

Contents

module Typhoeus
  class RemoteProxyObject
    instance_methods.each { |m| undef_method m unless m =~ /^__|object_id/ }

    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,
                                  :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
  end
end

Version data entries

37 entries across 37 versions & 6 rubygems

Version Path
typhoeus-0.2.0 lib/typhoeus/remote_proxy_object.rb
fblee-typhoeus-0.1.31 lib/typhoeus/remote_proxy_object.rb
tech-angels-typhoeus-0.1.36 lib/typhoeus/remote_proxy_object.rb
typhoeus-0.1.31 lib/typhoeus/remote_proxy_object.rb
typhoeus-0.1.30 lib/typhoeus/remote_proxy_object.rb
typhoeus-0.1.29 lib/typhoeus/remote_proxy_object.rb
gravis-typhoeus-0.1.36 lib/typhoeus/remote_proxy_object.rb
typhoeus-0.1.28 lib/typhoeus/remote_proxy_object.rb
gravis-typhoeus-0.1.35 lib/typhoeus/remote_proxy_object.rb
gravis-typhoeus-0.1.34 lib/typhoeus/remote_proxy_object.rb
gravis-typhoeus-0.1.33 lib/typhoeus/remote_proxy_object.rb
gravis-typhoeus-0.1.32 lib/typhoeus/remote_proxy_object.rb
gravis-typhoeus-0.1.31 lib/typhoeus/remote_proxy_object.rb
gravis-typhoeus-0.1.30 lib/typhoeus/remote_proxy_object.rb
gravis-typhoeus-0.1.29 lib/typhoeus/remote_proxy_object.rb
typhoeus-0.1.27 lib/typhoeus/remote_proxy_object.rb
typhoeus-0.1.26 lib/typhoeus/remote_proxy_object.rb
typhoeus-0.1.25 lib/typhoeus/remote_proxy_object.rb
typhoeus-0.1.24 lib/typhoeus/remote_proxy_object.rb
typhoeus-0.1.23 lib/typhoeus/remote_proxy_object.rb