xenda-typhoeus 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (65) hide show
  1. data/.gitignore +4 -0
  2. data/CHANGELOG.markdown +70 -0
  3. data/Gemfile +3 -0
  4. data/Gemfile.lock +38 -0
  5. data/LICENSE +20 -0
  6. data/README.textile +397 -0
  7. data/Rakefile +56 -0
  8. data/VERSION +1 -0
  9. data/benchmarks/profile.rb +25 -0
  10. data/benchmarks/vs_nethttp.rb +35 -0
  11. data/examples/file.rb +12 -0
  12. data/examples/times.rb +40 -0
  13. data/examples/twitter.rb +21 -0
  14. data/ext/typhoeus/.gitignore +7 -0
  15. data/ext/typhoeus/extconf.rb +65 -0
  16. data/ext/typhoeus/native.c +12 -0
  17. data/ext/typhoeus/native.h +22 -0
  18. data/ext/typhoeus/typhoeus_easy.c +232 -0
  19. data/ext/typhoeus/typhoeus_easy.h +20 -0
  20. data/ext/typhoeus/typhoeus_form.c +59 -0
  21. data/ext/typhoeus/typhoeus_form.h +13 -0
  22. data/ext/typhoeus/typhoeus_multi.c +217 -0
  23. data/ext/typhoeus/typhoeus_multi.h +16 -0
  24. data/lib/typhoeus.rb +58 -0
  25. data/lib/typhoeus/.gitignore +1 -0
  26. data/lib/typhoeus/easy.rb +379 -0
  27. data/lib/typhoeus/filter.rb +28 -0
  28. data/lib/typhoeus/form.rb +32 -0
  29. data/lib/typhoeus/hydra.rb +247 -0
  30. data/lib/typhoeus/hydra/callbacks.rb +24 -0
  31. data/lib/typhoeus/hydra/connect_options.rb +61 -0
  32. data/lib/typhoeus/hydra/stubbing.rb +52 -0
  33. data/lib/typhoeus/hydra_mock.rb +131 -0
  34. data/lib/typhoeus/multi.rb +37 -0
  35. data/lib/typhoeus/normalized_header_hash.rb +58 -0
  36. data/lib/typhoeus/remote.rb +306 -0
  37. data/lib/typhoeus/remote_method.rb +108 -0
  38. data/lib/typhoeus/remote_proxy_object.rb +50 -0
  39. data/lib/typhoeus/request.rb +203 -0
  40. data/lib/typhoeus/response.rb +91 -0
  41. data/lib/typhoeus/service.rb +20 -0
  42. data/lib/typhoeus/utils.rb +63 -0
  43. data/profilers/valgrind.rb +24 -0
  44. data/spec/fixtures/placeholder.gif +0 -0
  45. data/spec/fixtures/placeholder.txt +1 -0
  46. data/spec/fixtures/placeholder.ukn +0 -0
  47. data/spec/fixtures/result_set.xml +60 -0
  48. data/spec/servers/app.rb +94 -0
  49. data/spec/spec.opts +3 -0
  50. data/spec/spec_helper.rb +14 -0
  51. data/spec/typhoeus/easy_spec.rb +343 -0
  52. data/spec/typhoeus/filter_spec.rb +35 -0
  53. data/spec/typhoeus/form_spec.rb +117 -0
  54. data/spec/typhoeus/hydra_mock_spec.rb +300 -0
  55. data/spec/typhoeus/hydra_spec.rb +574 -0
  56. data/spec/typhoeus/multi_spec.rb +74 -0
  57. data/spec/typhoeus/normalized_header_hash_spec.rb +41 -0
  58. data/spec/typhoeus/remote_method_spec.rb +141 -0
  59. data/spec/typhoeus/remote_proxy_object_spec.rb +65 -0
  60. data/spec/typhoeus/remote_spec.rb +695 -0
  61. data/spec/typhoeus/request_spec.rb +300 -0
  62. data/spec/typhoeus/response_spec.rb +151 -0
  63. data/spec/typhoeus/utils_spec.rb +22 -0
  64. data/xenda-typhoeus.gemspec +139 -0
  65. metadata +203 -0
@@ -0,0 +1,108 @@
1
+ module Typhoeus
2
+ class RemoteMethod
3
+ attr_accessor :http_method, :options, :base_uri, :path, :on_success, :on_failure, :cache_ttl
4
+
5
+ def initialize(options = {})
6
+ @http_method = options.delete(:method) || :get
7
+ @options = options
8
+ @base_uri = options.delete(:base_uri)
9
+ @path = options.delete(:path)
10
+ @on_success = options[:on_success]
11
+ @on_failure = options[:on_failure]
12
+ @cache_responses = options.delete(:cache_responses)
13
+ @memoize_responses = options.delete(:memoize_responses) || @cache_responses
14
+ @cache_ttl = @cache_responses == true ? 0 : @cache_responses
15
+ @keys = nil
16
+
17
+ clear_cache
18
+ end
19
+
20
+ def cache_responses?
21
+ @cache_responses
22
+ end
23
+
24
+ def memoize_responses?
25
+ @memoize_responses
26
+ end
27
+
28
+ def args_options_key(args, options)
29
+ "#{args.to_s}+#{options.to_s}"
30
+ end
31
+
32
+ def calling(args, options)
33
+ @called_methods[args_options_key(args, options)] = true
34
+ end
35
+
36
+ def already_called?(args, options)
37
+ @called_methods.has_key? args_options_key(args, options)
38
+ end
39
+
40
+ def add_response_block(block, args, options)
41
+ @response_blocks[args_options_key(args, options)] << block
42
+ end
43
+
44
+ def call_response_blocks(result, args, options)
45
+ key = args_options_key(args, options)
46
+ @response_blocks[key].each {|block| block.call(result)}
47
+ @response_blocks.delete(key)
48
+ @called_methods.delete(key)
49
+ end
50
+
51
+ def clear_cache
52
+ @response_blocks = Hash.new {|h, k| h[k] = []}
53
+ @called_methods = {}
54
+ end
55
+
56
+ def merge_options(new_options)
57
+ merged = options.merge(new_options)
58
+ if options.has_key?(:params) && new_options.has_key?(:params)
59
+ merged[:params] = options[:params].merge(new_options[:params])
60
+ end
61
+ argument_names.each {|a| merged.delete(a)}
62
+ merged.delete(:on_success) if merged[:on_success].nil?
63
+ merged.delete(:on_failure) if merged[:on_failure].nil?
64
+ merged
65
+ end
66
+
67
+ def interpolate_path_with_arguments(args)
68
+ interpolated_path = @path
69
+ argument_names.each do |arg|
70
+ interpolated_path = interpolated_path.gsub(":#{arg}", args[arg].to_s)
71
+ end
72
+ interpolated_path
73
+ end
74
+
75
+ def argument_names
76
+ return @keys if @keys
77
+ pattern, keys = compile(@path)
78
+ @keys = keys.collect {|k| k.to_sym}
79
+ end
80
+
81
+ # rippped from Sinatra. clean up stuff we don't need later
82
+ def compile(path)
83
+ path ||= ""
84
+ keys = []
85
+ if path.respond_to? :to_str
86
+ special_chars = %w{. + ( )}
87
+ pattern =
88
+ path.gsub(/((:\w+)|[\*#{special_chars.join}])/) do |match|
89
+ case match
90
+ when "*"
91
+ keys << 'splat'
92
+ "(.*?)"
93
+ when *special_chars
94
+ Regexp.escape(match)
95
+ else
96
+ keys << $2[1..-1]
97
+ "([^/?&#]+)"
98
+ end
99
+ end
100
+ [/^#{pattern}$/, keys]
101
+ elsif path.respond_to? :match
102
+ [path, keys]
103
+ else
104
+ raise TypeError, path
105
+ end
106
+ end
107
+ end
108
+ end
@@ -0,0 +1,50 @@
1
+ module Typhoeus
2
+ class RemoteProxyObject
3
+ instance_methods.each { |m| undef_method m unless m =~ /^__|object_id/ }
4
+
5
+ def initialize(clear_memoized_store_proc, easy, options = {})
6
+ @clear_memoized_store_proc = clear_memoized_store_proc
7
+ @easy = easy
8
+ @success = options[:on_success]
9
+ @failure = options[:on_failure]
10
+ @cache = options.delete(:cache)
11
+ @cache_key = options.delete(:cache_key)
12
+ @timeout = options.delete(:cache_timeout)
13
+ Typhoeus.add_easy_request(@easy)
14
+ end
15
+
16
+ def method_missing(sym, *args, &block)
17
+ unless @proxied_object
18
+ if @cache && @cache_key
19
+ @proxied_object = @cache.get(@cache_key) rescue nil
20
+ end
21
+
22
+ unless @proxied_object
23
+ Typhoeus.perform_easy_requests
24
+ response = Response.new(:code => @easy.response_code,
25
+ :curl_return_code => @easy.curl_return_code,
26
+ :curl_error_message => @easy.curl_error_message,
27
+ :headers => @easy.response_header,
28
+ :body => @easy.response_body,
29
+ :time => @easy.total_time_taken,
30
+ :requested_url => @easy.url,
31
+ :requested_http_method => @easy.method,
32
+ :start_time => @easy.start_time)
33
+ if @easy.response_code >= 200 && @easy.response_code < 300
34
+ Typhoeus.release_easy_object(@easy)
35
+ @proxied_object = @success.nil? ? response : @success.call(response)
36
+
37
+ if @cache && @cache_key
38
+ @cache.set(@cache_key, @proxied_object, @timeout)
39
+ end
40
+ else
41
+ @proxied_object = @failure.nil? ? response : @failure.call(response)
42
+ end
43
+ @clear_memoized_store_proc.call
44
+ end
45
+ end
46
+
47
+ @proxied_object.__send__(sym, *args, &block)
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,203 @@
1
+ require 'uri'
2
+
3
+ module Typhoeus
4
+ class Request
5
+ attr_reader :url
6
+ attr_writer :headers
7
+ attr_accessor :method, :params, :body, :connect_timeout, :timeout,
8
+ :user_agent, :response, :cache_timeout, :follow_location,
9
+ :max_redirects, :proxy, :proxy_username,:proxy_password,
10
+ :disable_ssl_peer_verification, :interface,
11
+ :ssl_cert, :ssl_cert_type, :ssl_key, :ssl_key_type,
12
+ :ssl_key_password, :ssl_cacert, :ssl_capath, :verbose,
13
+ :username, :password, :auth_method, :user_agent,
14
+ :proxy_auth_method, :proxy_type
15
+
16
+ # Initialize a new Request
17
+ #
18
+ # Options:
19
+ # * +url+ : Endpoint (URL) of the request
20
+ # * +options+ : A hash containing options among :
21
+ # ** +:method+ : :get (default) / :post / :put
22
+ # ** +:params+ : params as a Hash
23
+ # ** +:body+
24
+ # ** +:timeout+ : timeout (ms)
25
+ # ** +:interface+ : interface or ip address (string)
26
+ # ** +:connect_timeout+ : connect timeout (ms)
27
+ # ** +:headers+ : headers as Hash
28
+ # ** +:user_agent+ : user agent (string)
29
+ # ** +:cache_timeout+ : cache timeout (ms)
30
+ # ** +:follow_location
31
+ # ** +:max_redirects
32
+ # ** +:proxy
33
+ # ** +:disable_ssl_peer_verification
34
+ # ** +:ssl_cert
35
+ # ** +:ssl_cert_type
36
+ # ** +:ssl_key
37
+ # ** +:ssl_key_type
38
+ # ** +:ssl_key_password
39
+ # ** +:ssl_cacert
40
+ # ** +:ssl_capath
41
+ # ** +:verbose
42
+ # ** +:username
43
+ # ** +:password
44
+ # ** +:auth_method
45
+ #
46
+ def initialize(url, options = {})
47
+ @method = options[:method] || :get
48
+ @params = options[:params]
49
+ @body = options[:body]
50
+ @timeout = options[:timeout]
51
+ @connect_timeout = options[:connect_timeout]
52
+ @interface = options[:interface]
53
+ @headers = options[:headers] || {}
54
+ @user_agent = options[:user_agent] || Typhoeus::USER_AGENT
55
+ @cache_timeout = options[:cache_timeout]
56
+ @follow_location = options[:follow_location]
57
+ @max_redirects = options[:max_redirects]
58
+ @proxy = options[:proxy]
59
+ @proxy_type = options[:proxy_type]
60
+ @proxy_username = options[:proxy_username]
61
+ @proxy_password = options[:proxy_password]
62
+ @proxy_auth_method = options[:proxy_auth_method]
63
+ @disable_ssl_peer_verification = options[:disable_ssl_peer_verification]
64
+ @ssl_cert = options[:ssl_cert]
65
+ @ssl_cert_type = options[:ssl_cert_type]
66
+ @ssl_key = options[:ssl_key]
67
+ @ssl_key_type = options[:ssl_key_type]
68
+ @ssl_key_password = options[:ssl_key_password]
69
+ @ssl_cacert = options[:ssl_cacert]
70
+ @ssl_capath = options[:ssl_capath]
71
+ @verbose = options[:verbose]
72
+ @username = options[:username]
73
+ @password = options[:password]
74
+ @auth_method = options[:auth_method]
75
+
76
+ if @method == :post
77
+ @url = url
78
+ else
79
+ @url = @params ? "#{url}?#{params_string}" : url
80
+ end
81
+
82
+ @parsed_uri = URI.parse(@url)
83
+
84
+ @on_complete = nil
85
+ @after_complete = nil
86
+ @handled_response = nil
87
+ end
88
+
89
+ LOCALHOST_ALIASES = %w[ localhost 127.0.0.1 0.0.0.0 ]
90
+
91
+ def localhost?
92
+ LOCALHOST_ALIASES.include?(@parsed_uri.host)
93
+ end
94
+
95
+ def host
96
+ slash_location = @url.index('/', 8)
97
+ if slash_location
98
+ @url.slice(0, slash_location)
99
+ else
100
+ query_string_location = @url.index('?')
101
+ return query_string_location ? @url.slice(0, query_string_location) : @url
102
+ end
103
+ end
104
+
105
+ def host_domain
106
+ @parsed_uri.host
107
+ end
108
+
109
+ def headers
110
+ @headers["User-Agent"] = @user_agent
111
+ @headers
112
+ end
113
+
114
+ def params_string
115
+ traversal = Typhoeus::Utils.traverse_params_hash(params)
116
+ Typhoeus::Utils.traversal_to_param_string(traversal)
117
+ end
118
+
119
+ def on_complete(&block)
120
+ @on_complete = block
121
+ end
122
+
123
+ def on_complete=(proc)
124
+ @on_complete = proc
125
+ end
126
+
127
+ def after_complete(&block)
128
+ @after_complete = block
129
+ end
130
+
131
+ def after_complete=(proc)
132
+ @after_complete = proc
133
+ end
134
+
135
+ def call_handlers
136
+ if @on_complete
137
+ @handled_response = @on_complete.call(response)
138
+ call_after_complete
139
+ end
140
+ end
141
+
142
+ def call_after_complete
143
+ @after_complete.call(@handled_response) if @after_complete
144
+ end
145
+
146
+ def handled_response=(val)
147
+ @handled_response = val
148
+ end
149
+
150
+ def handled_response
151
+ @handled_response || response
152
+ end
153
+
154
+ def inspect
155
+ result = ":method => #{self.method.inspect},\n" <<
156
+ "\t:url => #{URI.parse(self.url).to_s}"
157
+ if self.body and !self.body.empty?
158
+ result << ",\n\t:body => #{self.body.inspect}"
159
+ end
160
+
161
+ if self.params and !self.params.empty?
162
+ result << ",\n\t:params => #{self.params.inspect}"
163
+ end
164
+
165
+ if self.headers and !self.headers.empty?
166
+ result << ",\n\t:headers => #{self.headers.inspect}"
167
+ end
168
+
169
+ result
170
+ end
171
+
172
+ def cache_key
173
+ Digest::SHA1.hexdigest(url)
174
+ end
175
+
176
+ def self.run(url, params)
177
+ r = new(url, params)
178
+ Typhoeus::Hydra.hydra.queue r
179
+ Typhoeus::Hydra.hydra.run
180
+ r.response
181
+ end
182
+
183
+ def self.get(url, params = {})
184
+ run(url, params.merge(:method => :get))
185
+ end
186
+
187
+ def self.post(url, params = {})
188
+ run(url, params.merge(:method => :post))
189
+ end
190
+
191
+ def self.put(url, params = {})
192
+ run(url, params.merge(:method => :put))
193
+ end
194
+
195
+ def self.delete(url, params = {})
196
+ run(url, params.merge(:method => :delete))
197
+ end
198
+
199
+ def self.head(url, params = {})
200
+ run(url, params.merge(:method => :head))
201
+ end
202
+ end
203
+ end
@@ -0,0 +1,91 @@
1
+ module Typhoeus
2
+ class Response
3
+ attr_accessor :request, :mock
4
+ attr_reader :code, :headers, :body, :time,
5
+ :requested_url, :requested_remote_method,
6
+ :requested_http_method, :start_time,
7
+ :effective_url, :start_transfer_time,
8
+ :app_connect_time, :pretransfer_time,
9
+ :connect_time, :name_lookup_time,
10
+ :curl_return_code, :curl_error_message
11
+
12
+ attr_writer :headers_hash
13
+
14
+ def initialize(params = {})
15
+ @code = params[:code]
16
+ @curl_return_code = params[:curl_return_code]
17
+ @curl_error_message = params[:curl_error_message]
18
+ @status_message = params[:status_message]
19
+ @http_version = params[:http_version]
20
+ @headers = params[:headers] || ''
21
+ @body = params[:body]
22
+ @time = params[:time]
23
+ @requested_url = params[:requested_url]
24
+ @requested_http_method = params[:requested_http_method]
25
+ @start_time = params[:start_time]
26
+ @start_transfer_time = params[:start_transfer_time]
27
+ @app_connect_time = params[:app_connect_time]
28
+ @pretransfer_time = params[:pretransfer_time]
29
+ @connect_time = params[:connect_time]
30
+ @name_lookup_time = params[:name_lookup_time]
31
+ @request = params[:request]
32
+ @effective_url = params[:effective_url]
33
+ @mock = params[:mock] || false # default
34
+ @headers_hash = NormalizedHeaderHash.new(params[:headers_hash]) if params[:headers_hash]
35
+ end
36
+
37
+ # Returns true if this is a mock response.
38
+ def mock?
39
+ @mock
40
+ end
41
+
42
+ def headers_hash
43
+ @headers_hash ||= begin
44
+ headers.split("\n").map {|o| o.strip}.inject(Typhoeus::NormalizedHeaderHash.new) do |hash, o|
45
+ if o.empty? || o =~ /^HTTP\/[\d\.]+/
46
+ hash
47
+ else
48
+ i = o.index(":") || o.size
49
+ key = o.slice(0, i)
50
+ value = o.slice(i + 1, o.size)
51
+ value = value.strip unless value.nil?
52
+ if hash.has_key? key
53
+ hash[key] = [hash[key], value].flatten
54
+ else
55
+ hash[key] = value
56
+ end
57
+
58
+ hash
59
+ end
60
+ end
61
+ end
62
+ end
63
+
64
+ def status_message
65
+ # http://rubular.com/r/eAr1oVYsVa
66
+ @status_message ||= first_header_line ? first_header_line[/\d{3} (.*)$/, 1].chomp : nil
67
+ end
68
+
69
+ def http_version
70
+ @http_version ||= first_header_line ? first_header_line[/HTTP\/(\S+)/, 1] : nil
71
+ end
72
+
73
+ def success?
74
+ @code >= 200 && @code < 300
75
+ end
76
+
77
+ def modified?
78
+ @code != 304
79
+ end
80
+
81
+ def timed_out?
82
+ curl_return_code == 28
83
+ end
84
+
85
+ private
86
+
87
+ def first_header_line
88
+ @first_header_line ||= headers.split("\n").first
89
+ end
90
+ end
91
+ end