yoyle439587298 0.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (91) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +10 -0
  3. data/.travis.yml +7 -0
  4. data/Gemfile +14 -0
  5. data/Guardfile +16 -0
  6. data/History +303 -0
  7. data/LICENSE +21 -0
  8. data/MIT-LICENSE +20 -0
  9. data/README.md +80 -0
  10. data/Rakefile +12 -0
  11. data/bin/httparty +117 -0
  12. data/cucumber.yml +1 -0
  13. data/examples/aaws.rb +32 -0
  14. data/examples/basic.rb +32 -0
  15. data/examples/crack.rb +19 -0
  16. data/examples/custom_parsers.rb +67 -0
  17. data/examples/delicious.rb +37 -0
  18. data/examples/google.rb +16 -0
  19. data/examples/headers_and_user_agents.rb +6 -0
  20. data/examples/nokogiri_html_parser.rb +22 -0
  21. data/examples/rubyurl.rb +14 -0
  22. data/examples/tripit_sign_in.rb +33 -0
  23. data/examples/twitter.rb +31 -0
  24. data/examples/whoismyrep.rb +10 -0
  25. data/features/basic_authentication.feature +20 -0
  26. data/features/command_line.feature +7 -0
  27. data/features/deals_with_http_error_codes.feature +26 -0
  28. data/features/digest_authentication.feature +20 -0
  29. data/features/handles_compressed_responses.feature +27 -0
  30. data/features/handles_multiple_formats.feature +57 -0
  31. data/features/steps/env.rb +22 -0
  32. data/features/steps/httparty_response_steps.rb +52 -0
  33. data/features/steps/httparty_steps.rb +35 -0
  34. data/features/steps/mongrel_helper.rb +94 -0
  35. data/features/steps/remote_service_steps.rb +74 -0
  36. data/features/supports_redirection.feature +22 -0
  37. data/features/supports_timeout_option.feature +13 -0
  38. data/httparty.gemspec +26 -0
  39. data/lib/httparty.rb +578 -0
  40. data/lib/httparty/connection_adapter.rb +176 -0
  41. data/lib/httparty/cookie_hash.rb +22 -0
  42. data/lib/httparty/core_extensions.rb +32 -0
  43. data/lib/httparty/exceptions.rb +29 -0
  44. data/lib/httparty/hash_conversions.rb +51 -0
  45. data/lib/httparty/logger/apache_logger.rb +22 -0
  46. data/lib/httparty/logger/curl_logger.rb +48 -0
  47. data/lib/httparty/logger/logger.rb +18 -0
  48. data/lib/httparty/module_inheritable_attributes.rb +56 -0
  49. data/lib/httparty/net_digest_auth.rb +84 -0
  50. data/lib/httparty/parser.rb +141 -0
  51. data/lib/httparty/request.rb +330 -0
  52. data/lib/httparty/response.rb +72 -0
  53. data/lib/httparty/response/headers.rb +31 -0
  54. data/lib/httparty/version.rb +3 -0
  55. data/script/release +42 -0
  56. data/spec/fixtures/delicious.xml +23 -0
  57. data/spec/fixtures/empty.xml +0 -0
  58. data/spec/fixtures/google.html +3 -0
  59. data/spec/fixtures/ssl/generate.sh +29 -0
  60. data/spec/fixtures/ssl/generated/1fe462c2.0 +16 -0
  61. data/spec/fixtures/ssl/generated/bogushost.crt +13 -0
  62. data/spec/fixtures/ssl/generated/ca.crt +16 -0
  63. data/spec/fixtures/ssl/generated/ca.key +15 -0
  64. data/spec/fixtures/ssl/generated/selfsigned.crt +14 -0
  65. data/spec/fixtures/ssl/generated/server.crt +13 -0
  66. data/spec/fixtures/ssl/generated/server.key +15 -0
  67. data/spec/fixtures/ssl/openssl-exts.cnf +9 -0
  68. data/spec/fixtures/twitter.csv +2 -0
  69. data/spec/fixtures/twitter.json +1 -0
  70. data/spec/fixtures/twitter.xml +403 -0
  71. data/spec/fixtures/undefined_method_add_node_for_nil.xml +2 -0
  72. data/spec/httparty/connection_adapter_spec.rb +298 -0
  73. data/spec/httparty/cookie_hash_spec.rb +83 -0
  74. data/spec/httparty/exception_spec.rb +23 -0
  75. data/spec/httparty/logger/apache_logger_spec.rb +26 -0
  76. data/spec/httparty/logger/curl_logger_spec.rb +18 -0
  77. data/spec/httparty/logger/logger_spec.rb +22 -0
  78. data/spec/httparty/net_digest_auth_spec.rb +152 -0
  79. data/spec/httparty/parser_spec.rb +165 -0
  80. data/spec/httparty/request_spec.rb +631 -0
  81. data/spec/httparty/response_spec.rb +221 -0
  82. data/spec/httparty/ssl_spec.rb +74 -0
  83. data/spec/httparty_spec.rb +764 -0
  84. data/spec/spec.opts +2 -0
  85. data/spec/spec_helper.rb +37 -0
  86. data/spec/support/ssl_test_helper.rb +47 -0
  87. data/spec/support/ssl_test_server.rb +80 -0
  88. data/spec/support/stub_response.rb +43 -0
  89. data/website/css/common.css +47 -0
  90. data/website/index.html +73 -0
  91. metadata +208 -0
@@ -0,0 +1,22 @@
1
+ Feature: Supports Redirection
2
+
3
+ As a developer
4
+ I want to work with services that may redirect me
5
+ And I want it to follow a reasonable number of redirects
6
+ Because sometimes web services do that
7
+
8
+ Scenario: A service that redirects once
9
+ Given a remote service that returns 'Service Response'
10
+ And that service is accessed at the path '/landing_service.html'
11
+ And the url '/redirector.html' redirects to '/landing_service.html'
12
+ When I call HTTParty#get with '/redirector.html'
13
+ Then the return value should match 'Service Response'
14
+
15
+ # TODO: Look in to why this actually fails...
16
+ Scenario: A service that redirects to a relative URL
17
+
18
+ Scenario: A service that redirects infinitely
19
+ Given the url '/first.html' redirects to '/second.html'
20
+ And the url '/second.html' redirects to '/first.html'
21
+ When I call HTTParty#get with '/first.html'
22
+ Then it should raise an HTTParty::RedirectionTooDeep exception
@@ -0,0 +1,13 @@
1
+ Feature: Supports the timeout option
2
+ In order to handle inappropriately slow response times
3
+ As a developer
4
+ I want my request to raise an exception after my specified timeout as elapsed
5
+
6
+ Scenario: A long running response
7
+ Given a remote service that returns '<h1>Some HTML</h1>'
8
+ And that service is accessed at the path '/long_running_service.html'
9
+ And that service takes 2 seconds to generate a response
10
+ When I set my HTTParty timeout option to 1
11
+ And I call HTTParty#get with '/long_running_service.html'
12
+ Then it should raise a Timeout::Error exception
13
+ And I wait for the server to recover
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "httparty/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "yoyle439587298"
7
+ s.version = HTTParty::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["John Nunemaker", "Sandro Turriate"]
10
+ s.email = ["nunemaker@gmail.com"]
11
+ s.homepage = "http://jnunemaker.github.com/httparty"
12
+ s.summary = %q{Makes http fun! Also, makes consuming restful web services dead easy.}
13
+ s.description = %q{Makes http fun! Also, makes consuming restful web services dead easy.}
14
+
15
+ s.required_ruby_version = '>= 1.9.3'
16
+
17
+ s.add_dependency 'json', "~> 1.8"
18
+ s.add_dependency 'multi_xml', ">= 0.5.2"
19
+
20
+ s.post_install_message = "When you HTTParty, you must party hard!"
21
+
22
+ s.files = `git ls-files`.split("\n")
23
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
24
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
25
+ s.require_paths = ["lib"]
26
+ end
@@ -0,0 +1,578 @@
1
+ require 'pathname'
2
+ require 'net/http'
3
+ require 'net/https'
4
+ require 'uri'
5
+ require 'zlib'
6
+ require 'multi_xml'
7
+ require 'json'
8
+ require 'csv'
9
+
10
+ require 'httparty/module_inheritable_attributes'
11
+ require 'httparty/cookie_hash'
12
+ require 'httparty/net_digest_auth'
13
+ require 'httparty/version'
14
+ require 'httparty/connection_adapter'
15
+ require 'httparty/logger/logger'
16
+
17
+ # @see HTTParty::ClassMethods
18
+ module HTTParty
19
+ module AllowedFormatsDeprecation
20
+ def const_missing(const)
21
+ if const.to_s =~ /AllowedFormats$/
22
+ Kernel.warn("Deprecated: Use HTTParty::Parser::SupportedFormats")
23
+ HTTParty::Parser::SupportedFormats
24
+ else
25
+ super
26
+ end
27
+ end
28
+ end
29
+
30
+ extend AllowedFormatsDeprecation
31
+
32
+ def self.included(base)
33
+ base.extend ClassMethods
34
+ base.send :include, HTTParty::ModuleInheritableAttributes
35
+ base.send(:mattr_inheritable, :default_options)
36
+ base.send(:mattr_inheritable, :default_cookies)
37
+ base.instance_variable_set("@default_options", {})
38
+ base.instance_variable_set("@default_cookies", CookieHash.new)
39
+ end
40
+
41
+ # == Common Request Options
42
+ # Request methods (get, post, patch, put, delete, head, options) all take a common set of options. These are:
43
+ #
44
+ # [:+body+:] Body of the request. If passed a Hash, will try to normalize it first, by default passing it to ActiveSupport::to_params. Any other kind of object will get used as-is.
45
+ # [:+http_proxyaddr+:] Address of proxy server to use.
46
+ # [:+http_proxyport+:] Port of proxy server to use.
47
+ # [:+http_proxyuser+:] User for proxy server authentication.
48
+ # [:+http_proxypass+:] Password for proxy server authentication.
49
+ # [:+limit+:] Maximum number of redirects to follow. Takes precedences over :+no_follow+.
50
+ # [:+query+:] Query string, or a Hash representing it. Normalized according to the same rules as :+body+. If you specify this on a POST, you must use a Hash. See also HTTParty::ClassMethods.default_params.
51
+ # [:+timeout+:] Timeout for opening connection and reading data.
52
+ # [:+local_host:] Local address to bind to before connecting.
53
+ # [:+local_port:] Local port to bind to before connecting.
54
+ #
55
+ # There are also another set of options with names corresponding to various class methods. The methods in question are those that let you set a class-wide default, and the options override the defaults on a request-by-request basis. Those options are:
56
+ # * :+base_uri+: see HTTParty::ClassMethods.base_uri.
57
+ # * :+basic_auth+: see HTTParty::ClassMethods.basic_auth. Only one of :+basic_auth+ and :+digest_auth+ can be used at a time; if you try using both, you'll get an ArgumentError.
58
+ # * :+debug_output+: see HTTParty::ClassMethods.debug_output.
59
+ # * :+digest_auth+: see HTTParty::ClassMethods.digest_auth. Only one of :+basic_auth+ and :+digest_auth+ can be used at a time; if you try using both, you'll get an ArgumentError.
60
+ # * :+format+: see HTTParty::ClassMethods.format.
61
+ # * :+headers+: see HTTParty::ClassMethods.headers. Must be a Hash.
62
+ # * :+maintain_method_across_redirects+: see HTTParty::ClassMethods.maintain_method_across_redirects.
63
+ # * :+no_follow+: see HTTParty::ClassMethods.no_follow.
64
+ # * :+parser+: see HTTParty::ClassMethods.parser.
65
+ # * :+connection_adapter+: see HTTParty::ClassMethods.connection_adapter.
66
+ # * :+pem+: see HTTParty::ClassMethods.pem.
67
+ # * :+query_string_normalizer+: see HTTParty::ClassMethods.query_string_normalizer
68
+ # * :+ssl_ca_file+: see HTTParty::ClassMethods.ssl_ca_file.
69
+ # * :+ssl_ca_path+: see HTTParty::ClassMethods.ssl_ca_path.
70
+
71
+ module ClassMethods
72
+
73
+ extend AllowedFormatsDeprecation
74
+
75
+ # Turns on logging
76
+ #
77
+ # class Foo
78
+ # include HTTParty
79
+ # logger Logger.new('http_logger'), :info, :apache
80
+ # end
81
+ def logger(logger, level=:info, format=:apache)
82
+ default_options[:logger] = logger
83
+ default_options[:log_level] = level
84
+ default_options[:log_format] = format
85
+ end
86
+
87
+ # Allows setting http proxy information to be used
88
+ #
89
+ # class Foo
90
+ # include HTTParty
91
+ # http_proxy 'http://foo.com', 80, 'user', 'pass'
92
+ # end
93
+ def http_proxy(addr=nil, port=nil, user=nil, pass=nil)
94
+ default_options[:http_proxyaddr] = addr
95
+ default_options[:http_proxyport] = port
96
+ default_options[:http_proxyuser] = user
97
+ default_options[:http_proxypass] = pass
98
+ end
99
+
100
+ # Allows setting a base uri to be used for each request.
101
+ # Will normalize uri to include http, etc.
102
+ #
103
+ # class Foo
104
+ # include HTTParty
105
+ # base_uri 'twitter.com'
106
+ # end
107
+ def base_uri(uri=nil)
108
+ return default_options[:base_uri] unless uri
109
+ default_options[:base_uri] = HTTParty.normalize_base_uri(uri)
110
+ end
111
+
112
+ # Allows setting basic authentication username and password.
113
+ #
114
+ # class Foo
115
+ # include HTTParty
116
+ # basic_auth 'username', 'password'
117
+ # end
118
+ def basic_auth(u, p)
119
+ default_options[:basic_auth] = {:username => u, :password => p}
120
+ end
121
+
122
+ # Allows setting digest authentication username and password.
123
+ #
124
+ # class Foo
125
+ # include HTTParty
126
+ # digest_auth 'username', 'password'
127
+ # end
128
+ def digest_auth(u, p)
129
+ default_options[:digest_auth] = {:username => u, :password => p}
130
+ end
131
+
132
+ # Do not send rails style query strings.
133
+ # Specically, don't use bracket notation when sending an array
134
+ #
135
+ # For a query:
136
+ # get '/', :query => {:selected_ids => [1,2,3]}
137
+ #
138
+ # The default query string looks like this:
139
+ # /?selected_ids[]=1&selected_ids[]=2&selected_ids[]=3
140
+ #
141
+ # Call `disable_rails_query_string_format` to transform the query string
142
+ # into:
143
+ # /?selected_ids=1&selected_ids=2&selected_ids=3
144
+ #
145
+ # @example
146
+ # class Foo
147
+ # include HTTParty
148
+ # disable_rails_query_string_format
149
+ # end
150
+ def disable_rails_query_string_format
151
+ query_string_normalizer Request::NON_RAILS_QUERY_STRING_NORMALIZER
152
+ end
153
+
154
+ # Allows setting default parameters to be appended to each request.
155
+ # Great for api keys and such.
156
+ #
157
+ # class Foo
158
+ # include HTTParty
159
+ # default_params :api_key => 'secret', :another => 'foo'
160
+ # end
161
+ def default_params(h={})
162
+ raise ArgumentError, 'Default params must be a hash' unless h.is_a?(Hash)
163
+ default_options[:default_params] ||= {}
164
+ default_options[:default_params].merge!(h)
165
+ end
166
+
167
+ # Allows setting a default timeout for all HTTP calls
168
+ # Timeout is specified in seconds.
169
+ #
170
+ # class Foo
171
+ # include HTTParty
172
+ # default_timeout 10
173
+ # end
174
+ def default_timeout(t)
175
+ raise ArgumentError, 'Timeout must be an integer or float' unless t && (t.is_a?(Integer) || t.is_a?(Float))
176
+ default_options[:timeout] = t
177
+ end
178
+
179
+ # Set an output stream for debugging, defaults to $stderr.
180
+ # The output stream is passed on to Net::HTTP#set_debug_output.
181
+ #
182
+ # class Foo
183
+ # include HTTParty
184
+ # debug_output $stderr
185
+ # end
186
+ def debug_output(stream = $stderr)
187
+ default_options[:debug_output] = stream
188
+ end
189
+
190
+ # Allows setting HTTP headers to be used for each request.
191
+ #
192
+ # class Foo
193
+ # include HTTParty
194
+ # headers 'Accept' => 'text/html'
195
+ # end
196
+ def headers(h={})
197
+ raise ArgumentError, 'Headers must be a hash' unless h.is_a?(Hash)
198
+ default_options[:headers] ||= {}
199
+ default_options[:headers].merge!(h)
200
+ end
201
+
202
+ def cookies(h={})
203
+ raise ArgumentError, 'Cookies must be a hash' unless h.is_a?(Hash)
204
+ default_cookies.add_cookies(h)
205
+ end
206
+
207
+ # Proceed to the location header when an HTTP response dictates a redirect.
208
+ # Redirects are always followed by default.
209
+ #
210
+ # @example
211
+ # class Foo
212
+ # include HTTParty
213
+ # base_uri 'http://google.com'
214
+ # follow_redirects true
215
+ # end
216
+ def follow_redirects(value = true)
217
+ default_options[:follow_redirects] = value
218
+ end
219
+
220
+ # Allows setting the format with which to parse.
221
+ # Must be one of the allowed formats ie: json, xml
222
+ #
223
+ # class Foo
224
+ # include HTTParty
225
+ # format :json
226
+ # end
227
+ def format(f = nil)
228
+ if f.nil?
229
+ default_options[:format]
230
+ else
231
+ parser(Parser) if parser.nil?
232
+ default_options[:format] = f
233
+ validate_format
234
+ end
235
+ end
236
+
237
+ # Declare whether or not to follow redirects. When true, an
238
+ # {HTTParty::RedirectionTooDeep} error will raise upon encountering a
239
+ # redirect. You can then gain access to the response object via
240
+ # HTTParty::RedirectionTooDeep#response.
241
+ #
242
+ # @see HTTParty::ResponseError#response
243
+ #
244
+ # @example
245
+ # class Foo
246
+ # include HTTParty
247
+ # base_uri 'http://google.com'
248
+ # no_follow true
249
+ # end
250
+ #
251
+ # begin
252
+ # Foo.get('/')
253
+ # rescue HTTParty::RedirectionTooDeep => e
254
+ # puts e.response.body
255
+ # end
256
+ def no_follow(value = false)
257
+ default_options[:no_follow] = value
258
+ end
259
+
260
+ # Declare that you wish to maintain the chosen HTTP method across redirects.
261
+ # The default behavior is to follow redirects via the GET method.
262
+ # If you wish to maintain the original method, you can set this option to true.
263
+ #
264
+ # @example
265
+ # class Foo
266
+ # include HTTParty
267
+ # base_uri 'http://google.com'
268
+ # maintain_method_across_redirects true
269
+ # end
270
+
271
+ def maintain_method_across_redirects(value = true)
272
+ default_options[:maintain_method_across_redirects] = value
273
+ end
274
+
275
+ # Allows setting a PEM file to be used
276
+ #
277
+ # class Foo
278
+ # include HTTParty
279
+ # pem File.read('/home/user/my.pem'), "optional password"
280
+ # end
281
+ def pem(pem_contents, password=nil)
282
+ default_options[:pem] = pem_contents
283
+ default_options[:pem_password] = password
284
+ end
285
+
286
+ # Allows setting a PKCS12 file to be used
287
+ #
288
+ # class Foo
289
+ # include HTTParty
290
+ # pkcs12 File.read('/home/user/my.p12'), "password"
291
+ # end
292
+ def pkcs12(p12_contents, password)
293
+ default_options[:p12] = p12_contents
294
+ default_options[:p12_password] = password
295
+ end
296
+
297
+ # Override the way query strings are normalized.
298
+ # Helpful for overriding the default rails normalization of Array queries.
299
+ #
300
+ # For a query:
301
+ # get '/', :query => {:selected_ids => [1,2,3]}
302
+ #
303
+ # The default query string normalizer returns:
304
+ # /?selected_ids[]=1&selected_ids[]=2&selected_ids[]=3
305
+ #
306
+ # Let's change it to this:
307
+ # /?selected_ids=1&selected_ids=2&selected_ids=3
308
+ #
309
+ # Pass a Proc to the query normalizer which accepts the yielded query.
310
+ #
311
+ # @example Modifying Array query strings
312
+ # class ServiceWrapper
313
+ # include HTTParty
314
+ #
315
+ # query_string_normalizer proc { |query|
316
+ # query.map do |key, value|
317
+ # value.map {|v| "#{key}=#{v}"}
318
+ # end.join('&')
319
+ # }
320
+ # end
321
+ #
322
+ # @param [Proc] normalizer custom query string normalizer.
323
+ # @yield [Hash, String] query string
324
+ # @yieldreturn [Array] an array that will later be joined with '&'
325
+ def query_string_normalizer(normalizer)
326
+ default_options[:query_string_normalizer] = normalizer
327
+ end
328
+
329
+ # Allows setting of SSL version to use. This only works in Ruby 1.9+.
330
+ # You can get a list of valid versions from OpenSSL::SSL::SSLContext::METHODS.
331
+ #
332
+ # class Foo
333
+ # include HTTParty
334
+ # ssl_version :SSLv3
335
+ # end
336
+ def ssl_version(version)
337
+ default_options[:ssl_version] = version
338
+ end
339
+
340
+ # Allows setting of SSL ciphers to use. This only works in Ruby 1.9+.
341
+ # You can get a list of valid specific ciphers from OpenSSL::Cipher.ciphers.
342
+ # You also can specify a cipher suite here, listed here at openssl.org:
343
+ # http://www.openssl.org/docs/apps/ciphers.html#CIPHER_SUITE_NAMES
344
+ #
345
+ # class Foo
346
+ # include HTTParty
347
+ # ciphers "RC4-SHA"
348
+ # end
349
+ def ciphers(cipher_names)
350
+ default_options[:ciphers] = cipher_names
351
+ end
352
+
353
+ # Allows setting an OpenSSL certificate authority file. The file
354
+ # should contain one or more certificates in PEM format.
355
+ #
356
+ # Setting this option enables certificate verification. All
357
+ # certificates along a chain must be available in ssl_ca_file or
358
+ # ssl_ca_path for verification to succeed.
359
+ #
360
+ #
361
+ # class Foo
362
+ # include HTTParty
363
+ # ssl_ca_file '/etc/ssl/certs/ca-certificates.crt'
364
+ # end
365
+ def ssl_ca_file(path)
366
+ default_options[:ssl_ca_file] = path
367
+ end
368
+
369
+ # Allows setting an OpenSSL certificate authority path (directory).
370
+ #
371
+ # Setting this option enables certificate verification. All
372
+ # certificates along a chain must be available in ssl_ca_file or
373
+ # ssl_ca_path for verification to succeed.
374
+ #
375
+ # class Foo
376
+ # include HTTParty
377
+ # ssl_ca_path '/etc/ssl/certs/'
378
+ # end
379
+ def ssl_ca_path(path)
380
+ default_options[:ssl_ca_path] = path
381
+ end
382
+
383
+ # Allows setting a custom parser for the response.
384
+ #
385
+ # class Foo
386
+ # include HTTParty
387
+ # parser Proc.new {|data| ...}
388
+ # end
389
+ def parser(custom_parser = nil)
390
+ if custom_parser.nil?
391
+ default_options[:parser]
392
+ else
393
+ default_options[:parser] = custom_parser
394
+ validate_format
395
+ end
396
+ end
397
+
398
+ # Allows setting a custom connection_adapter for the http connections
399
+ #
400
+ # @example
401
+ # class Foo
402
+ # include HTTParty
403
+ # connection_adapter Proc.new {|uri, options| ... }
404
+ # end
405
+ #
406
+ # @example provide optional configuration for your connection_adapter
407
+ # class Foo
408
+ # include HTTParty
409
+ # connection_adapter Proc.new {|uri, options| ... }, {:foo => :bar}
410
+ # end
411
+ #
412
+ # @see HTTParty::ConnectionAdapter
413
+ def connection_adapter(custom_adapter = nil, options = nil)
414
+ if custom_adapter.nil?
415
+ default_options[:connection_adapter]
416
+ else
417
+ default_options[:connection_adapter] = custom_adapter
418
+ default_options[:connection_adapter_options] = options
419
+ end
420
+ end
421
+
422
+ # Allows making a get request to a url.
423
+ #
424
+ # class Foo
425
+ # include HTTParty
426
+ # end
427
+ #
428
+ # # Simple get with full url
429
+ # Foo.get('http://foo.com/resource.json')
430
+ #
431
+ # # Simple get with full url and query parameters
432
+ # # ie: http://foo.com/resource.json?limit=10
433
+ # Foo.get('http://foo.com/resource.json', :query => {:limit => 10})
434
+ def get(path, options={}, &block)
435
+ perform_request Net::HTTP::Get, path, options, &block
436
+ end
437
+
438
+ # Allows making a post request to a url.
439
+ #
440
+ # class Foo
441
+ # include HTTParty
442
+ # end
443
+ #
444
+ # # Simple post with full url and setting the body
445
+ # Foo.post('http://foo.com/resources', :body => {:bar => 'baz'})
446
+ #
447
+ # # Simple post with full url using :query option,
448
+ # # which gets set as form data on the request.
449
+ # Foo.post('http://foo.com/resources', :query => {:bar => 'baz'})
450
+ def post(path, options={}, &block)
451
+ perform_request Net::HTTP::Post, path, options, &block
452
+ end
453
+
454
+ # Perform a PATCH request to a path
455
+ def patch(path, options={}, &block)
456
+ perform_request Net::HTTP::Patch, path, options, &block
457
+ end
458
+
459
+ # Perform a PUT request to a path
460
+ def put(path, options={}, &block)
461
+ perform_request Net::HTTP::Put, path, options, &block
462
+ end
463
+
464
+ # Perform a DELETE request to a path
465
+ def delete(path, options={}, &block)
466
+ perform_request Net::HTTP::Delete, path, options, &block
467
+ end
468
+
469
+ # Perform a MOVE request to a path
470
+ def move(path, options={}, &block)
471
+ perform_request Net::HTTP::Move, path, options, &block
472
+ end
473
+
474
+ # Perform a COPY request to a path
475
+ def copy(path, options={}, &block)
476
+ perform_request Net::HTTP::Copy, path, options, &block
477
+ end
478
+
479
+ # Perform a HEAD request to a path
480
+ def head(path, options={}, &block)
481
+ perform_request Net::HTTP::Head, path, options, &block
482
+ end
483
+
484
+ # Perform an OPTIONS request to a path
485
+ def options(path, options={}, &block)
486
+ perform_request Net::HTTP::Options, path, options, &block
487
+ end
488
+
489
+ def default_options #:nodoc:
490
+ @default_options
491
+ end
492
+
493
+ private
494
+
495
+ def perform_request(http_method, path, options, &block) #:nodoc:
496
+ options = default_options.merge(options)
497
+ process_headers(options)
498
+ process_cookies(options)
499
+ Request.new(http_method, path, options).perform(&block)
500
+ end
501
+
502
+ def process_headers(options)
503
+ if options[:headers] && headers.any?
504
+ options[:headers] = headers.merge(options[:headers])
505
+ end
506
+ end
507
+
508
+ def process_cookies(options) #:nodoc:
509
+ return unless options[:cookies] || default_cookies.any?
510
+ options[:headers] ||= headers.dup
511
+ options[:headers]["cookie"] = cookies.merge(options.delete(:cookies) || {}).to_cookie_string
512
+ end
513
+
514
+ def validate_format
515
+ if format && parser.respond_to?(:supports_format?) && !parser.supports_format?(format)
516
+ raise UnsupportedFormat, "'#{format.inspect}' Must be one of: #{parser.supported_formats.map{|f| f.to_s}.sort.join(', ')}"
517
+ end
518
+ end
519
+ end
520
+
521
+ def self.normalize_base_uri(url) #:nodoc:
522
+ normalized_url = url.dup
523
+ use_ssl = (normalized_url =~ /^https/) || (normalized_url =~ /:443\b/)
524
+ ends_with_slash = normalized_url =~ /\/$/
525
+
526
+ normalized_url.chop! if ends_with_slash
527
+ normalized_url.gsub!(/^https?:\/\//i, '')
528
+
529
+ "http#{'s' if use_ssl}://#{normalized_url}"
530
+ end
531
+
532
+ class Basement #:nodoc:
533
+ include HTTParty
534
+ end
535
+
536
+ def self.get(*args, &block)
537
+ Basement.get(*args, &block)
538
+ end
539
+
540
+ def self.post(*args, &block)
541
+ Basement.post(*args, &block)
542
+ end
543
+
544
+ def self.patch(*args, &block)
545
+ Basement.patch(*args, &block)
546
+ end
547
+
548
+ def self.put(*args, &block)
549
+ Basement.put(*args, &block)
550
+ end
551
+
552
+ def self.delete(*args, &block)
553
+ Basement.delete(*args, &block)
554
+ end
555
+
556
+ def self.move(*args, &block)
557
+ Basement.move(*args, &block)
558
+ end
559
+
560
+ def self.copy(*args, &block)
561
+ Basement.copy(*args, &block)
562
+ end
563
+
564
+ def self.head(*args, &block)
565
+ Basement.head(*args, &block)
566
+ end
567
+
568
+ def self.options(*args, &block)
569
+ Basement.options(*args, &block)
570
+ end
571
+ end
572
+
573
+ require 'httparty/core_extensions'
574
+ require 'httparty/hash_conversions'
575
+ require 'httparty/exceptions'
576
+ require 'httparty/parser'
577
+ require 'httparty/request'
578
+ require 'httparty/response'