Module: Typhoeus::ClassMethods

Defined in:
lib/typhoeus/remote.rb

Instance Method Summary collapse

Instance Method Details

#allow_net_connectObject



11
12
13
14
# File 'lib/typhoeus/remote.rb', line 11

def allow_net_connect
  @allow_net_connect = true if @allow_net_connect.nil?
  @allow_net_connect
end

#allow_net_connect=(value) ⇒ Object



16
17
18
# File 'lib/typhoeus/remote.rb', line 16

def allow_net_connect=(value)
  @allow_net_connect = value
end

#cache=(cache) ⇒ Object



274
275
276
# File 'lib/typhoeus/remote.rb', line 274

def cache=(cache)
  @cache = cache
end

#call_remote_method(method_name, args) ⇒ Object



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/typhoeus/remote.rb', line 216

def call_remote_method(method_name, args)
  m = @remote_methods[method_name]
  
  base_uri = args.delete(:base_uri) || m.base_uri || ""

  if args.has_key? :path
    path = args.delete(:path)
  else
    path = m.interpolate_path_with_arguments(args)
  end
  path ||= ""
  
  http_method = m.http_method
  url         = base_uri + path
  options     = m.merge_options(args)
  
  # proxy_object = memoized_proxy_object(http_method, url, options)
  # return proxy_object unless proxy_object.nil?
  # 
  # if m.cache_responses?
  #   object = @cache.get(get_memcache_response_key(method_name, args))
  #   if object
  #     set_memoized_proxy_object(http_method, url, options, object)
  #     return object
  #   end
  # end

  proxy = memoized_proxy_object(http_method, url, options)
  unless proxy
    if m.cache_responses?
      options[:cache] = @cache
      options[:cache_key] = get_memcache_response_key(method_name, args)
      options[:cache_timeout] = m.cache_ttl
    end
    proxy = send(http_method, url, options)
  end
  proxy
end

#check_expected_headers!(response_args, options) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/typhoeus/remote.rb', line 104

def check_expected_headers!(response_args, options)
  missing_headers = {}

  response_args[:expected_headers].each do |key, value|
    if options[:headers].nil?
      missing_headers[key] = [value, nil]
    elsif ((options[:headers][key] && value != :anything) &&
       options[:headers][key] != value)

      missing_headers[key] = [value, options[:headers][key]]
    end
  end

  unless missing_headers.empty?
    raise headers_error_summary(response_args, options, missing_headers, 'expected')
  end
end

#check_unexpected_headers!(response_args, options) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/typhoeus/remote.rb', line 122

def check_unexpected_headers!(response_args, options)
  bad_headers = {}
  response_args[:unexpected_headers].each do |key, value|
    if (options[:headers][key] && value == :anything) ||
       (options[:headers][key] == value)
      bad_headers[key] = [value, options[:headers][key]]
    end
  end

  unless bad_headers.empty?
    raise headers_error_summary(response_args, options, bad_headers, 'did not expect')
  end
end

#clear_memoized_proxy_objectsObject



265
266
267
# File 'lib/typhoeus/remote.rb', line 265

def clear_memoized_proxy_objects
  lambda { @memoized_proxy_objects = {} }
end

#define_remote_method(name, args = {}) ⇒ Object



278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/typhoeus/remote.rb', line 278

def define_remote_method(name, args = {})
  @remote_defaults  ||= {}
  args[:method]     ||= @remote_defaults[:method]
  args[:on_success] ||= @remote_defaults[:on_success]
  args[:on_failure] ||= @remote_defaults[:on_failure]
  args[:base_uri]   ||= @remote_defaults[:base_uri]
  args[:path]       ||= @remote_defaults[:path]
  m = RemoteMethod.new(args)

  @remote_methods ||= {}
  @remote_methods[name] = m

  class_eval <<-SRC
    def self.#{name.to_s}(args = {})
      call_remote_method(:#{name.to_s}, args)
    end
  SRC
end

#enforce_allow_net_connect!(http_verb, url, params = nil) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/typhoeus/remote.rb', line 89

def enforce_allow_net_connect!(http_verb, url, params = nil)
  if !allow_net_connect
    message = "Real HTTP connections are disabled. Unregistered request: " <<
              "#{http_verb.to_s.upcase} #{url}\n" <<
              "  Try: mock(:#{http_verb}, :url => \"#{url}\""
    if params
      message << ",\n            :params => #{params.inspect}"
    end

    message << ")"

    raise MockExpectedError, message
  end
end

#flatten_and_sort_hash(params) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/typhoeus/remote.rb', line 51

def flatten_and_sort_hash(params)
  params = params.dup

  # Flatten any sub-hashes to a single string.
  params.keys.each do |key|
    if params[key].is_a?(Hash)
      params[key] = params[key].sort_by { |k, v| k.to_s.downcase }.to_s
    end
  end

  params.sort_by { |k, v| k.to_s.downcase }
end

#get_memcache_response_key(remote_method_name, args) ⇒ Object



269
270
271
272
# File 'lib/typhoeus/remote.rb', line 269

def get_memcache_response_key(remote_method_name, args)
  result = "#{remote_method_name.to_s}-#{args.to_s}"
  (Digest::SHA2.new << result).to_s
end

#get_mock(method, url, options) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/typhoeus/remote.rb', line 64

def get_mock(method, url, options)
  return nil unless @remote_mocks
  if @remote_mocks.has_key? method
    extra_response_args = { :requested_http_method => method,
                            :requested_url => url,
                            :start_time => Time.now }
    mock_key = mock_key_for(url, options[:params])
    if @remote_mocks[method].has_key? mock_key
      get_mock_and_run_handlers(method,
                                @remote_mocks[method][mock_key].merge(
                                  extra_response_args),
                                options)
    elsif @remote_mocks[method].has_key? :catch_all
      get_mock_and_run_handlers(method,
                                @remote_mocks[method][:catch_all].merge(
                                  extra_response_args),
                                options)
    else
      nil
    end
  else
    nil
  end
end

#get_mock_and_run_handlers(method, response_args, options) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/typhoeus/remote.rb', line 148

def get_mock_and_run_handlers(method, response_args, options)
  response = Response.new(response_args)
 
  if response_args.has_key? :expected_body
    raise "#{method} expected body of \"#{response_args[:expected_body]}\" but received #{options[:body]}" if response_args[:expected_body] != options[:body]
  end
  
  if response_args.has_key? :expected_headers
    check_expected_headers!(response_args, options)
  end

  if response_args.has_key? :unexpected_headers
    check_unexpected_headers!(response_args, options)
  end

  if response.code >= 200 && response.code < 300 && options.has_key?(:on_success)
    response = options[:on_success].call(response)
  elsif options.has_key?(:on_failure)
    response = options[:on_failure].call(response)
  end

  encode_nil_response(response)
end

#inherited(child) ⇒ Object

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



212
213
214
# File 'lib/typhoeus/remote.rb', line 212

def inherited(child)
  child.__send__(:remote_defaults, @remote_defaults)
end

#memoized_proxy_object(http_method, url, options) ⇒ Object



260
261
262
263
# File 'lib/typhoeus/remote.rb', line 260

def memoized_proxy_object(http_method, url, options)
  @memoized_proxy_objects ||= {}
  @memoized_proxy_objects["#{http_method}_#{url}_#{options.to_s}"]
end

#mock(method, args = {}) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/typhoeus/remote.rb', line 20

def mock(method, args = {})
  @remote_mocks ||= {}
  @remote_mocks[method] ||= {}
  args[:code]    ||= 200
  args[:body]    ||= ""
  args[:headers] ||= ""
  args[:time]    ||= 0
  url = args.delete(:url)
  url ||= :catch_all
  params = args.delete(:params)

  key = mock_key_for(url, params)

  @remote_mocks[method][key] = args
end

#mock_key_for(url, params = nil) ⇒ Object

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



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/typhoeus/remote.rb', line 39

def mock_key_for(url, params = nil)
  if url == :catch_all
    url
  else
    key = url
    if params and !params.empty?
      key += flatten_and_sort_hash(params).to_s
    end
    key
  end
end

#remote_defaults(options) ⇒ Object



204
205
206
207
208
# File 'lib/typhoeus/remote.rb', line 204

def remote_defaults(options)
  @remote_defaults ||= {}
  @remote_defaults.merge!(options) if options
  @remote_defaults
end

#remote_proxy_object(url, method, options) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/typhoeus/remote.rb', line 188

def remote_proxy_object(url, method, options)
  easy = Typhoeus.get_easy_object
  
  easy.url                   = url
  easy.method                = method
  easy.headers               = options[:headers] if options.has_key?(:headers)
  easy.headers["User-Agent"] = (options[:user_agent] || Typhoeus::USER_AGENT)
  easy.params                = options[:params] if options[:params]
  easy.request_body          = options[:body] if options[:body]
  easy.timeout               = options[:timeout] if options[:timeout]
  easy.set_headers
  
  proxy = Typhoeus::RemoteProxyObject.new(clear_memoized_proxy_objects, easy, options)
  set_memoized_proxy_object(method, url, options, proxy)
end

#set_memoized_proxy_object(http_method, url, options, object) ⇒ Object



255
256
257
258
# File 'lib/typhoeus/remote.rb', line 255

def set_memoized_proxy_object(http_method, url, options, object)
  @memoized_proxy_objects ||= {}
  @memoized_proxy_objects["#{http_method}_#{url}_#{options.to_s}"] = object
end