Typhoeus::ClassMethods

Public Instance Methods

allow_net_connect() click to toggle source
    # File lib/typhoeus/remote.rb, line 11
11:     def allow_net_connect
12:       @allow_net_connect = true if @allow_net_connect.nil?
13:       @allow_net_connect
14:     end
allow_net_connect=(value) click to toggle source
    # File lib/typhoeus/remote.rb, line 16
16:     def allow_net_connect=(value)
17:       @allow_net_connect = value
18:     end
cache=(cache) click to toggle source
     # File lib/typhoeus/remote.rb, line 274
274:     def cache=(cache)
275:       @cache = cache
276:     end
call_remote_method(method_name, args) click to toggle source
     # File lib/typhoeus/remote.rb, line 216
216:     def call_remote_method(method_name, args)
217:       m = @remote_methods[method_name]
218:       
219:       base_uri = args.delete(:base_uri) || m.base_uri || ""
220: 
221:       if args.has_key? :path
222:         path = args.delete(:path)
223:       else
224:         path = m.interpolate_path_with_arguments(args)
225:       end
226:       path ||= ""
227:       
228:       http_method = m.http_method
229:       url         = base_uri + path
230:       options     = m.merge_options(args)
231:       
232:       # proxy_object = memoized_proxy_object(http_method, url, options)
233:       # return proxy_object unless proxy_object.nil?
234:       # 
235:       # if m.cache_responses?
236:       #   object = @cache.get(get_memcache_response_key(method_name, args))
237:       #   if object
238:       #     set_memoized_proxy_object(http_method, url, options, object)
239:       #     return object
240:       #   end
241:       # end
242: 
243:       proxy = memoized_proxy_object(http_method, url, options)
244:       unless proxy
245:         if m.cache_responses?
246:           options[:cache] = @cache
247:           options[:cache_key] = get_memcache_response_key(method_name, args)
248:           options[:cache_timeout] = m.cache_ttl
249:         end
250:         proxy = send(http_method, url, options)
251:       end
252:       proxy
253:     end
check_expected_headers!(response_args, options) click to toggle source
     # File lib/typhoeus/remote.rb, line 104
104:     def check_expected_headers!(response_args, options)
105:       missing_headers = {}
106: 
107:       response_args[:expected_headers].each do |key, value|
108:         if options[:headers].nil?
109:           missing_headers[key] = [value, nil]
110:         elsif ((options[:headers][key] && value != :anything) &&
111:            options[:headers][key] != value)
112: 
113:           missing_headers[key] = [value, options[:headers][key]]
114:         end
115:       end
116: 
117:       unless missing_headers.empty?
118:         raise headers_error_summary(response_args, options, missing_headers, 'expected')
119:       end
120:     end
check_unexpected_headers!(response_args, options) click to toggle source
     # File lib/typhoeus/remote.rb, line 122
122:     def check_unexpected_headers!(response_args, options)
123:       bad_headers = {}
124:       response_args[:unexpected_headers].each do |key, value|
125:         if (options[:headers][key] && value == :anything) ||
126:            (options[:headers][key] == value)
127:           bad_headers[key] = [value, options[:headers][key]]
128:         end
129:       end
130: 
131:       unless bad_headers.empty?
132:         raise headers_error_summary(response_args, options, bad_headers, 'did not expect')
133:       end
134:     end
clear_memoized_proxy_objects() click to toggle source
     # File lib/typhoeus/remote.rb, line 265
265:     def clear_memoized_proxy_objects
266:       lambda { @memoized_proxy_objects = {} }
267:     end
define_remote_method(name, args = {}) click to toggle source
     # File lib/typhoeus/remote.rb, line 278
278:     def define_remote_method(name, args = {})
279:       @remote_defaults  ||= {}
280:       args[:method]     ||= @remote_defaults[:method]
281:       args[:on_success] ||= @remote_defaults[:on_success]
282:       args[:on_failure] ||= @remote_defaults[:on_failure]
283:       args[:base_uri]   ||= @remote_defaults[:base_uri]
284:       args[:path]       ||= @remote_defaults[:path]
285:       m = RemoteMethod.new(args)
286: 
287:       @remote_methods ||= {}
288:       @remote_methods[name] = m
289: 
290:       class_eval         def self.#{name.to_s}(args = {})          call_remote_method(:#{name.to_s}, args)        end
291:     end
enforce_allow_net_connect!(http_verb, url, params = nil) click to toggle source
     # File lib/typhoeus/remote.rb, line 89
 89:     def enforce_allow_net_connect!(http_verb, url, params = nil)
 90:       if !allow_net_connect
 91:         message = "Real HTTP connections are disabled. Unregistered request: " <<
 92:                   "#{http_verb.to_s.upcase} #{url}\n" <<
 93:                   "  Try: mock(:#{http_verb}, :url => \"#{url}\""
 94:         if params
 95:           message << ",\n            :params => #{params.inspect}"
 96:         end
 97: 
 98:         message << ")"
 99: 
100:         raise MockExpectedError, message
101:       end
102:     end
flatten_and_sort_hash(params) click to toggle source
    # File lib/typhoeus/remote.rb, line 51
51:     def flatten_and_sort_hash(params)
52:       params = params.dup
53: 
54:       # Flatten any sub-hashes to a single string.
55:       params.keys.each do |key|
56:         if params[key].is_a?(Hash)
57:           params[key] = params[key].sort_by { |k, v| k.to_s.downcase }.to_s
58:         end
59:       end
60: 
61:       params.sort_by { |k, v| k.to_s.downcase }
62:     end
get_memcache_response_key(remote_method_name, args) click to toggle source
     # File lib/typhoeus/remote.rb, line 269
269:     def get_memcache_response_key(remote_method_name, args)
270:       result = "#{remote_method_name.to_s}-#{args.to_s}"
271:       (Digest::SHA2.new << result).to_s
272:     end
get_mock(method, url, options) click to toggle source
    # File lib/typhoeus/remote.rb, line 64
64:     def get_mock(method, url, options)
65:       return nil unless @remote_mocks
66:       if @remote_mocks.has_key? method
67:         extra_response_args = { :requested_http_method => method,
68:                                 :requested_url => url,
69:                                 :start_time => Time.now }
70:         mock_key = mock_key_for(url, options[:params])
71:         if @remote_mocks[method].has_key? mock_key
72:           get_mock_and_run_handlers(method,
73:                                     @remote_mocks[method][mock_key].merge(
74:                                       extra_response_args),
75:                                     options)
76:         elsif @remote_mocks[method].has_key? :catch_all
77:           get_mock_and_run_handlers(method,
78:                                     @remote_mocks[method][:catch_all].merge(
79:                                       extra_response_args),
80:                                     options)
81:         else
82:           nil
83:         end
84:       else
85:         nil
86:       end
87:     end
get_mock_and_run_handlers(method, response_args, options) click to toggle source
     # File lib/typhoeus/remote.rb, line 148
148:     def get_mock_and_run_handlers(method, response_args, options)
149:       response = Response.new(response_args)
150:      
151:       if response_args.has_key? :expected_body
152:         raise "#{method} expected body of \"#{response_args[:expected_body]}\" but received #{options[:body]}" if response_args[:expected_body] != options[:body]
153:       end
154:       
155:       if response_args.has_key? :expected_headers
156:         check_expected_headers!(response_args, options)
157:       end
158: 
159:       if response_args.has_key? :unexpected_headers
160:         check_unexpected_headers!(response_args, options)
161:       end
162: 
163:       if response.code >= 200 && response.code < 300 && options.has_key?(:on_success)
164:         response = options[:on_success].call(response)
165:       elsif options.has_key?(:on_failure)
166:         response = options[:on_failure].call(response)
167:       end
168: 
169:       encode_nil_response(response)
170:     end
inherited(child) click to toggle source

If we get subclassed, make sure that child inherits the remote defaults of the parent class.

     # File lib/typhoeus/remote.rb, line 212
212:     def inherited(child)
213:       child.__send__(:remote_defaults, @remote_defaults)
214:     end
memoized_proxy_object(http_method, url, options) click to toggle source
     # File lib/typhoeus/remote.rb, line 260
260:     def memoized_proxy_object(http_method, url, options)
261:       @memoized_proxy_objects ||= {}
262:       @memoized_proxy_objects["#{http_method}_#{url}_#{options.to_s}"]
263:     end
mock(method, args = {}) click to toggle source
    # File lib/typhoeus/remote.rb, line 20
20:     def mock(method, args = {})
21:       @remote_mocks ||= {}
22:       @remote_mocks[method] ||= {}
23:       args[:code]    ||= 200
24:       args[:body]    ||= ""
25:       args[:headers] ||= ""
26:       args[:time]    ||= 0
27:       url = args.delete(:url)
28:       url ||= :catch_all
29:       params = args.delete(:params)
30: 
31:       key = mock_key_for(url, params)
32: 
33:       @remote_mocks[method][key] = args
34:     end
mock_key_for(url, params = nil) click to toggle source

Returns a key for a given URL and passed in set of Typhoeus options to be used to store/retrieve a corresponding mock.

    # File lib/typhoeus/remote.rb, line 39
39:     def mock_key_for(url, params = nil)
40:       if url == :catch_all
41:         url
42:       else
43:         key = url
44:         if params and !params.empty?
45:           key += flatten_and_sort_hash(params).to_s
46:         end
47:         key
48:       end
49:     end
remote_defaults(options) click to toggle source
     # File lib/typhoeus/remote.rb, line 204
204:     def remote_defaults(options)
205:       @remote_defaults ||= {}
206:       @remote_defaults.merge!(options) if options
207:       @remote_defaults
208:     end
remote_proxy_object(url, method, options) click to toggle source
     # File lib/typhoeus/remote.rb, line 188
188:     def remote_proxy_object(url, method, options)
189:       easy = Typhoeus.get_easy_object
190:       
191:       easy.url                   = url
192:       easy.method                = method
193:       easy.headers               = options[:headers] if options.has_key?(:headers)
194:       easy.headers["User-Agent"] = (options[:user_agent] || Typhoeus::USER_AGENT)
195:       easy.params                = options[:params] if options[:params]
196:       easy.request_body          = options[:body] if options[:body]
197:       easy.timeout               = options[:timeout] if options[:timeout]
198:       easy.set_headers
199:       
200:       proxy = Typhoeus::RemoteProxyObject.new(clear_memoized_proxy_objects, easy, options)
201:       set_memoized_proxy_object(method, url, options, proxy)
202:     end
set_memoized_proxy_object(http_method, url, options, object) click to toggle source
     # File lib/typhoeus/remote.rb, line 255
255:     def set_memoized_proxy_object(http_method, url, options, object)
256:       @memoized_proxy_objects ||= {}
257:       @memoized_proxy_objects["#{http_method}_#{url}_#{options.to_s}"] = object
258:     end

Private Instance Methods

decode_nil_response(response) click to toggle source
     # File lib/typhoeus/remote.rb, line 302
302:     def decode_nil_response(response)
303:       response == :__nil__ ? nil : response
304:     end
encode_nil_response(response) click to toggle source
     # File lib/typhoeus/remote.rb, line 298
298:     def encode_nil_response(response)
299:       response == nil ? :__nil__ : response
300:     end
headers_error_summary(response_args, options, missing_headers, lead_in) click to toggle source
     # File lib/typhoeus/remote.rb, line 136
136:     def headers_error_summary(response_args, options, missing_headers, lead_in)
137:       error = "#{lead_in} the following headers: #{response_args[:expected_headers].inspect}, but received: #{options[:headers].inspect}\n\n"
138:       error   << "Differences:\n"
139:       error   << "------------\n"
140:       missing_headers.each do |key, values|
141:         error << "  - #{key}: #{lead_in} #{values[0].inspect}, got #{values[1].inspect}\n"
142:       end
143: 
144:       error
145:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.