httparty-responsibly 0.17.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (65) hide show
  1. checksums.yaml +7 -0
  2. data/.editorconfig +18 -0
  3. data/.gitignore +13 -0
  4. data/.rubocop.yml +92 -0
  5. data/.rubocop_todo.yml +124 -0
  6. data/.simplecov +1 -0
  7. data/.travis.yml +11 -0
  8. data/CONTRIBUTING.md +23 -0
  9. data/Changelog.md +509 -0
  10. data/Gemfile +24 -0
  11. data/Guardfile +16 -0
  12. data/MIT-LICENSE +20 -0
  13. data/README.md +78 -0
  14. data/Rakefile +10 -0
  15. data/bin/httparty +123 -0
  16. data/cucumber.yml +1 -0
  17. data/docs/README.md +106 -0
  18. data/examples/README.md +86 -0
  19. data/examples/aaws.rb +32 -0
  20. data/examples/basic.rb +28 -0
  21. data/examples/body_stream.rb +14 -0
  22. data/examples/crack.rb +19 -0
  23. data/examples/custom_parsers.rb +68 -0
  24. data/examples/delicious.rb +37 -0
  25. data/examples/google.rb +16 -0
  26. data/examples/headers_and_user_agents.rb +10 -0
  27. data/examples/logging.rb +36 -0
  28. data/examples/microsoft_graph.rb +52 -0
  29. data/examples/multipart.rb +22 -0
  30. data/examples/nokogiri_html_parser.rb +19 -0
  31. data/examples/peer_cert.rb +9 -0
  32. data/examples/rescue_json.rb +17 -0
  33. data/examples/rubyurl.rb +14 -0
  34. data/examples/stackexchange.rb +24 -0
  35. data/examples/stream_download.rb +26 -0
  36. data/examples/tripit_sign_in.rb +44 -0
  37. data/examples/twitter.rb +31 -0
  38. data/examples/whoismyrep.rb +10 -0
  39. data/httparty-responsibly.gemspec +27 -0
  40. data/lib/httparty.rb +668 -0
  41. data/lib/httparty/connection_adapter.rb +254 -0
  42. data/lib/httparty/cookie_hash.rb +21 -0
  43. data/lib/httparty/exceptions.rb +33 -0
  44. data/lib/httparty/hash_conversions.rb +69 -0
  45. data/lib/httparty/headers_processor.rb +30 -0
  46. data/lib/httparty/logger/apache_formatter.rb +45 -0
  47. data/lib/httparty/logger/curl_formatter.rb +91 -0
  48. data/lib/httparty/logger/logger.rb +28 -0
  49. data/lib/httparty/logger/logstash_formatter.rb +59 -0
  50. data/lib/httparty/module_inheritable_attributes.rb +56 -0
  51. data/lib/httparty/net_digest_auth.rb +136 -0
  52. data/lib/httparty/parser.rb +150 -0
  53. data/lib/httparty/request.rb +386 -0
  54. data/lib/httparty/request/body.rb +84 -0
  55. data/lib/httparty/request/multipart_boundary.rb +11 -0
  56. data/lib/httparty/response.rb +140 -0
  57. data/lib/httparty/response/headers.rb +33 -0
  58. data/lib/httparty/response_fragment.rb +19 -0
  59. data/lib/httparty/text_encoder.rb +70 -0
  60. data/lib/httparty/utils.rb +11 -0
  61. data/lib/httparty/version.rb +3 -0
  62. data/script/release +42 -0
  63. data/website/css/common.css +47 -0
  64. data/website/index.html +73 -0
  65. metadata +138 -0
@@ -0,0 +1,44 @@
1
+ dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ require File.join(dir, 'httparty')
3
+
4
+ class TripIt
5
+ include HTTParty
6
+ base_uri 'https://www.tripit.com'
7
+ debug_output
8
+
9
+ def initialize(email, password)
10
+ @email = email
11
+ get_response = self.class.get('/account/login')
12
+ get_response_cookie = parse_cookie(get_response.headers['Set-Cookie'])
13
+
14
+ post_response = self.class.post(
15
+ '/account/login',
16
+ body: {
17
+ login_email_address: email,
18
+ login_password: password
19
+ },
20
+ headers: {'Cookie' => get_response_cookie.to_cookie_string }
21
+ )
22
+
23
+ @cookie = parse_cookie(post_response.headers['Set-Cookie'])
24
+ end
25
+
26
+ def account_settings
27
+ self.class.get('/account/edit', headers: { 'Cookie' => @cookie.to_cookie_string })
28
+ end
29
+
30
+ def logged_in?
31
+ account_settings.include? "You're logged in as #{@email}"
32
+ end
33
+
34
+ private
35
+
36
+ def parse_cookie(resp)
37
+ cookie_hash = CookieHash.new
38
+ resp.get_fields('Set-Cookie').each { |c| cookie_hash.add_cookies(c) }
39
+ cookie_hash
40
+ end
41
+ end
42
+
43
+ tripit = TripIt.new('email', 'password')
44
+ puts "Logged in: #{tripit.logged_in?}"
@@ -0,0 +1,31 @@
1
+ dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ require File.join(dir, 'httparty')
3
+ require 'pp'
4
+ config = YAML.load(File.read(File.join(ENV['HOME'], '.twitter')))
5
+
6
+ class Twitter
7
+ include HTTParty
8
+ base_uri 'twitter.com'
9
+
10
+ def initialize(u, p)
11
+ @auth = {username: u, password: p}
12
+ end
13
+
14
+ # which can be :friends, :user or :public
15
+ # options[:query] can be things like since, since_id, count, etc.
16
+ def timeline(which = :friends, options = {})
17
+ options.merge!({ basic_auth: @auth })
18
+ self.class.get("/statuses/#{which}_timeline.json", options)
19
+ end
20
+
21
+ def post(text)
22
+ options = { query: { status: text }, basic_auth: @auth }
23
+ self.class.post('/statuses/update.json', options)
24
+ end
25
+ end
26
+
27
+ twitter = Twitter.new(config['email'], config['password'])
28
+ pp twitter.timeline
29
+ # pp twitter.timeline(:friends, query: {since_id: 868482746})
30
+ # pp twitter.timeline(:friends, query: 'since_id=868482746')
31
+ # pp twitter.post('this is a test of 0.2.0')
@@ -0,0 +1,10 @@
1
+ dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ require File.join(dir, 'httparty')
3
+ require 'pp'
4
+
5
+ class Rep
6
+ include HTTParty
7
+ end
8
+
9
+ pp Rep.get('http://whoismyrepresentative.com/getall_mems.php?zip=46544')
10
+ pp Rep.get('http://whoismyrepresentative.com/getall_mems.php', query: { zip: 46544 })
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $LOAD_PATH.push File.expand_path("../lib", __FILE__)
3
+ require "httparty/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "httparty-responsibly"
7
+ s.version = HTTParty::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.licenses = ['MIT']
10
+ s.authors = ["John Nunemaker", "Sandro Turriate"]
11
+ s.email = ["nunemaker@gmail.com", "james@denness.org"]
12
+ s.homepage = "https://github.com/scarybot/httparty-responsibly"
13
+ s.summary = "An up-to-date fork of jnunemaker's httparty, without the post-install nonsense."
14
+ s.description = "An up-to-date fork of jnunemaker's httparty, without the post-install nonsense."
15
+
16
+ s.required_ruby_version = '>= 2.0.0'
17
+
18
+ s.add_dependency 'multi_xml', ">= 0.5.2"
19
+ s.add_dependency('mime-types', "~> 3.0")
20
+
21
+ all_files = `git ls-files`.split("\n")
22
+ test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
23
+
24
+ s.files = all_files - test_files
25
+ s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
26
+ s.require_paths = ["lib"]
27
+ end
@@ -0,0 +1,668 @@
1
+ require 'pathname'
2
+ require 'net/http'
3
+ require 'net/https'
4
+ require 'uri'
5
+ require 'zlib'
6
+ require 'multi_xml'
7
+ require 'mime/types'
8
+ require 'json'
9
+ require 'csv'
10
+
11
+ require 'httparty/module_inheritable_attributes'
12
+ require 'httparty/cookie_hash'
13
+ require 'httparty/net_digest_auth'
14
+ require 'httparty/version'
15
+ require 'httparty/connection_adapter'
16
+ require 'httparty/logger/logger'
17
+ require 'httparty/request/body'
18
+ require 'httparty/response_fragment'
19
+ require 'httparty/text_encoder'
20
+ require 'httparty/headers_processor'
21
+
22
+ # @see HTTParty::ClassMethods
23
+ module HTTParty
24
+ def self.included(base)
25
+ base.extend ClassMethods
26
+ base.send :include, ModuleInheritableAttributes
27
+ base.send(:mattr_inheritable, :default_options)
28
+ base.send(:mattr_inheritable, :default_cookies)
29
+ base.instance_variable_set("@default_options", {})
30
+ base.instance_variable_set("@default_cookies", CookieHash.new)
31
+ end
32
+
33
+ # == Common Request Options
34
+ # Request methods (get, post, patch, put, delete, head, options) all take a common set of options. These are:
35
+ #
36
+ # [:+body+:] Body of the request. If passed an object that responds to #to_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.
37
+ # [:+http_proxyaddr+:] Address of proxy server to use.
38
+ # [:+http_proxyport+:] Port of proxy server to use.
39
+ # [:+http_proxyuser+:] User for proxy server authentication.
40
+ # [:+http_proxypass+:] Password for proxy server authentication.
41
+ # [:+limit+:] Maximum number of redirects to follow. Takes precedences over :+no_follow+.
42
+ # [:+query+:] Query string, or an object that responds to #to_hash representing it. Normalized according to the same rules as :+body+. If you specify this on a POST, you must use an object which responds to #to_hash. See also HTTParty::ClassMethods.default_params.
43
+ # [:+timeout+:] Timeout for opening connection and reading data.
44
+ # [:+local_host:] Local address to bind to before connecting.
45
+ # [:+local_port:] Local port to bind to before connecting.
46
+ # [:+body_stream:] Allow streaming to a REST server to specify a body_stream.
47
+ # [:+stream_body:] Allow for streaming large files without loading them into memory.
48
+ # [:+multipart:] Force content-type to be multipart
49
+ #
50
+ # 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:
51
+ # * :+base_uri+: see HTTParty::ClassMethods.base_uri.
52
+ # * :+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.
53
+ # * :+debug_output+: see HTTParty::ClassMethods.debug_output.
54
+ # * :+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.
55
+ # * :+format+: see HTTParty::ClassMethods.format.
56
+ # * :+headers+: see HTTParty::ClassMethods.headers. Must be a an object which responds to #to_hash.
57
+ # * :+maintain_method_across_redirects+: see HTTParty::ClassMethods.maintain_method_across_redirects.
58
+ # * :+no_follow+: see HTTParty::ClassMethods.no_follow.
59
+ # * :+parser+: see HTTParty::ClassMethods.parser.
60
+ # * :+uri_adapter+: see HTTParty::ClassMethods.uri_adapter
61
+ # * :+connection_adapter+: see HTTParty::ClassMethods.connection_adapter.
62
+ # * :+pem+: see HTTParty::ClassMethods.pem.
63
+ # * :+query_string_normalizer+: see HTTParty::ClassMethods.query_string_normalizer
64
+ # * :+ssl_ca_file+: see HTTParty::ClassMethods.ssl_ca_file.
65
+ # * :+ssl_ca_path+: see HTTParty::ClassMethods.ssl_ca_path.
66
+
67
+ module ClassMethods
68
+ # Turns on logging
69
+ #
70
+ # class Foo
71
+ # include HTTParty
72
+ # logger Logger.new('http_logger'), :info, :apache
73
+ # end
74
+ def logger(logger, level = :info, format = :apache)
75
+ default_options[:logger] = logger
76
+ default_options[:log_level] = level
77
+ default_options[:log_format] = format
78
+ end
79
+
80
+ # Raises HTTParty::ResponseError if response's code matches this statuses
81
+ #
82
+ # class Foo
83
+ # include HTTParty
84
+ # raise_on [404, 500]
85
+ # end
86
+ def raise_on(codes = [])
87
+ default_options[:raise_on] = *codes
88
+ end
89
+
90
+ # Allows setting http proxy information to be used
91
+ #
92
+ # class Foo
93
+ # include HTTParty
94
+ # http_proxy 'http://foo.com', 80, 'user', 'pass'
95
+ # end
96
+ def http_proxy(addr = nil, port = nil, user = nil, pass = nil)
97
+ default_options[:http_proxyaddr] = addr
98
+ default_options[:http_proxyport] = port
99
+ default_options[:http_proxyuser] = user
100
+ default_options[:http_proxypass] = pass
101
+ end
102
+
103
+ # Allows setting a base uri to be used for each request.
104
+ # Will normalize uri to include http, etc.
105
+ #
106
+ # class Foo
107
+ # include HTTParty
108
+ # base_uri 'twitter.com'
109
+ # end
110
+ def base_uri(uri = nil)
111
+ return default_options[:base_uri] unless uri
112
+ default_options[:base_uri] = HTTParty.normalize_base_uri(uri)
113
+ end
114
+
115
+ # Allows setting basic authentication username and password.
116
+ #
117
+ # class Foo
118
+ # include HTTParty
119
+ # basic_auth 'username', 'password'
120
+ # end
121
+ def basic_auth(u, p)
122
+ default_options[:basic_auth] = {username: u, password: p}
123
+ end
124
+
125
+ # Allows setting digest authentication username and password.
126
+ #
127
+ # class Foo
128
+ # include HTTParty
129
+ # digest_auth 'username', 'password'
130
+ # end
131
+ def digest_auth(u, p)
132
+ default_options[:digest_auth] = {username: u, password: p}
133
+ end
134
+
135
+ # Do not send rails style query strings.
136
+ # Specifically, don't use bracket notation when sending an array
137
+ #
138
+ # For a query:
139
+ # get '/', query: {selected_ids: [1,2,3]}
140
+ #
141
+ # The default query string looks like this:
142
+ # /?selected_ids[]=1&selected_ids[]=2&selected_ids[]=3
143
+ #
144
+ # Call `disable_rails_query_string_format` to transform the query string
145
+ # into:
146
+ # /?selected_ids=1&selected_ids=2&selected_ids=3
147
+ #
148
+ # @example
149
+ # class Foo
150
+ # include HTTParty
151
+ # disable_rails_query_string_format
152
+ # end
153
+ def disable_rails_query_string_format
154
+ query_string_normalizer Request::NON_RAILS_QUERY_STRING_NORMALIZER
155
+ end
156
+
157
+ # Allows setting default parameters to be appended to each request.
158
+ # Great for api keys and such.
159
+ #
160
+ # class Foo
161
+ # include HTTParty
162
+ # default_params api_key: 'secret', another: 'foo'
163
+ # end
164
+ def default_params(h = {})
165
+ raise ArgumentError, 'Default params must be an object which responds to #to_hash' unless h.respond_to?(:to_hash)
166
+ default_options[:default_params] ||= {}
167
+ default_options[:default_params].merge!(h)
168
+ end
169
+
170
+ # Allows setting a default timeout for all HTTP calls
171
+ # Timeout is specified in seconds.
172
+ #
173
+ # class Foo
174
+ # include HTTParty
175
+ # default_timeout 10
176
+ # end
177
+ def default_timeout(value)
178
+ validate_timeout_argument(__method__, value)
179
+ default_options[:timeout] = value
180
+ end
181
+
182
+ # Allows setting a default open_timeout for all HTTP calls in seconds
183
+ #
184
+ # class Foo
185
+ # include HTTParty
186
+ # open_timeout 10
187
+ # end
188
+ def open_timeout(value)
189
+ validate_timeout_argument(__method__, value)
190
+ default_options[:open_timeout] = value
191
+ end
192
+
193
+ # Allows setting a default read_timeout for all HTTP calls in seconds
194
+ #
195
+ # class Foo
196
+ # include HTTParty
197
+ # read_timeout 10
198
+ # end
199
+ def read_timeout(value)
200
+ validate_timeout_argument(__method__, value)
201
+ default_options[:read_timeout] = value
202
+ end
203
+
204
+ # Allows setting a default write_timeout for all HTTP calls in seconds
205
+ # Supported by Ruby > 2.6.0
206
+ #
207
+ # class Foo
208
+ # include HTTParty
209
+ # write_timeout 10
210
+ # end
211
+ def write_timeout(value)
212
+ validate_timeout_argument(__method__, value)
213
+ default_options[:write_timeout] = value
214
+ end
215
+
216
+
217
+ # Set an output stream for debugging, defaults to $stderr.
218
+ # The output stream is passed on to Net::HTTP#set_debug_output.
219
+ #
220
+ # class Foo
221
+ # include HTTParty
222
+ # debug_output $stderr
223
+ # end
224
+ def debug_output(stream = $stderr)
225
+ default_options[:debug_output] = stream
226
+ end
227
+
228
+ # Allows setting HTTP headers to be used for each request.
229
+ #
230
+ # class Foo
231
+ # include HTTParty
232
+ # headers 'Accept' => 'text/html'
233
+ # end
234
+ def headers(h = nil)
235
+ if h
236
+ raise ArgumentError, 'Headers must be an object which responds to #to_hash' unless h.respond_to?(:to_hash)
237
+ default_options[:headers] ||= {}
238
+ default_options[:headers].merge!(h.to_hash)
239
+ else
240
+ default_options[:headers] || {}
241
+ end
242
+ end
243
+
244
+ def cookies(h = {})
245
+ raise ArgumentError, 'Cookies must be an object which responds to #to_hash' unless h.respond_to?(:to_hash)
246
+ default_cookies.add_cookies(h)
247
+ end
248
+
249
+ # Proceed to the location header when an HTTP response dictates a redirect.
250
+ # Redirects are always followed by default.
251
+ #
252
+ # @example
253
+ # class Foo
254
+ # include HTTParty
255
+ # base_uri 'http://google.com'
256
+ # follow_redirects true
257
+ # end
258
+ def follow_redirects(value = true)
259
+ default_options[:follow_redirects] = value
260
+ end
261
+
262
+ # Allows setting the format with which to parse.
263
+ # Must be one of the allowed formats ie: json, xml
264
+ #
265
+ # class Foo
266
+ # include HTTParty
267
+ # format :json
268
+ # end
269
+ def format(f = nil)
270
+ if f.nil?
271
+ default_options[:format]
272
+ else
273
+ parser(Parser) if parser.nil?
274
+ default_options[:format] = f
275
+ validate_format
276
+ end
277
+ end
278
+
279
+ # Declare whether or not to follow redirects. When true, an
280
+ # {HTTParty::RedirectionTooDeep} error will raise upon encountering a
281
+ # redirect. You can then gain access to the response object via
282
+ # HTTParty::RedirectionTooDeep#response.
283
+ #
284
+ # @see HTTParty::ResponseError#response
285
+ #
286
+ # @example
287
+ # class Foo
288
+ # include HTTParty
289
+ # base_uri 'http://google.com'
290
+ # no_follow true
291
+ # end
292
+ #
293
+ # begin
294
+ # Foo.get('/')
295
+ # rescue HTTParty::RedirectionTooDeep => e
296
+ # puts e.response.body
297
+ # end
298
+ def no_follow(value = false)
299
+ default_options[:no_follow] = value
300
+ end
301
+
302
+ # Declare that you wish to maintain the chosen HTTP method across redirects.
303
+ # The default behavior is to follow redirects via the GET method, except
304
+ # if you are making a HEAD request, in which case the default is to
305
+ # follow all redirects with HEAD requests.
306
+ # If you wish to maintain the original method, you can set this option to true.
307
+ #
308
+ # @example
309
+ # class Foo
310
+ # include HTTParty
311
+ # base_uri 'http://google.com'
312
+ # maintain_method_across_redirects true
313
+ # end
314
+
315
+ def maintain_method_across_redirects(value = true)
316
+ default_options[:maintain_method_across_redirects] = value
317
+ end
318
+
319
+ # Declare that you wish to resend the full HTTP request across redirects,
320
+ # even on redirects that should logically become GET requests.
321
+ # A 303 redirect in HTTP signifies that the redirected url should normally
322
+ # retrieved using a GET request, for instance, it is the output of a previous
323
+ # POST. maintain_method_across_redirects respects this behavior, but you
324
+ # can force HTTParty to resend_on_redirect even on 303 responses.
325
+ #
326
+ # @example
327
+ # class Foo
328
+ # include HTTParty
329
+ # base_uri 'http://google.com'
330
+ # resend_on_redirect
331
+ # end
332
+
333
+ def resend_on_redirect(value = true)
334
+ default_options[:resend_on_redirect] = value
335
+ end
336
+
337
+ # Allows setting a PEM file to be used
338
+ #
339
+ # class Foo
340
+ # include HTTParty
341
+ # pem File.read('/home/user/my.pem'), "optional password"
342
+ # end
343
+ def pem(pem_contents, password = nil)
344
+ default_options[:pem] = pem_contents
345
+ default_options[:pem_password] = password
346
+ end
347
+
348
+ # Allows setting a PKCS12 file to be used
349
+ #
350
+ # class Foo
351
+ # include HTTParty
352
+ # pkcs12 File.read('/home/user/my.p12'), "password"
353
+ # end
354
+ def pkcs12(p12_contents, password)
355
+ default_options[:p12] = p12_contents
356
+ default_options[:p12_password] = password
357
+ end
358
+
359
+ # Override the way query strings are normalized.
360
+ # Helpful for overriding the default rails normalization of Array queries.
361
+ #
362
+ # For a query:
363
+ # get '/', query: {selected_ids: [1,2,3]}
364
+ #
365
+ # The default query string normalizer returns:
366
+ # /?selected_ids[]=1&selected_ids[]=2&selected_ids[]=3
367
+ #
368
+ # Let's change it to this:
369
+ # /?selected_ids=1&selected_ids=2&selected_ids=3
370
+ #
371
+ # Pass a Proc to the query normalizer which accepts the yielded query.
372
+ #
373
+ # @example Modifying Array query strings
374
+ # class ServiceWrapper
375
+ # include HTTParty
376
+ #
377
+ # query_string_normalizer proc { |query|
378
+ # query.map do |key, value|
379
+ # value.map {|v| "#{key}=#{v}"}
380
+ # end.join('&')
381
+ # }
382
+ # end
383
+ #
384
+ # @param [Proc] normalizer custom query string normalizer.
385
+ # @yield [Hash, String] query string
386
+ # @yieldreturn [Array] an array that will later be joined with '&'
387
+ def query_string_normalizer(normalizer)
388
+ default_options[:query_string_normalizer] = normalizer
389
+ end
390
+
391
+ # Allows setting of SSL version to use. This only works in Ruby 1.9+.
392
+ # You can get a list of valid versions from OpenSSL::SSL::SSLContext::METHODS.
393
+ #
394
+ # class Foo
395
+ # include HTTParty
396
+ # ssl_version :SSLv3
397
+ # end
398
+ def ssl_version(version)
399
+ default_options[:ssl_version] = version
400
+ end
401
+
402
+ # Allows setting of SSL ciphers to use. This only works in Ruby 1.9+.
403
+ # You can get a list of valid specific ciphers from OpenSSL::Cipher.ciphers.
404
+ # You also can specify a cipher suite here, listed here at openssl.org:
405
+ # http://www.openssl.org/docs/apps/ciphers.html#CIPHER_SUITE_NAMES
406
+ #
407
+ # class Foo
408
+ # include HTTParty
409
+ # ciphers "RC4-SHA"
410
+ # end
411
+ def ciphers(cipher_names)
412
+ default_options[:ciphers] = cipher_names
413
+ end
414
+
415
+ # Allows setting an OpenSSL certificate authority file. The file
416
+ # should contain one or more certificates in PEM format.
417
+ #
418
+ # Setting this option enables certificate verification. All
419
+ # certificates along a chain must be available in ssl_ca_file or
420
+ # ssl_ca_path for verification to succeed.
421
+ #
422
+ #
423
+ # class Foo
424
+ # include HTTParty
425
+ # ssl_ca_file '/etc/ssl/certs/ca-certificates.crt'
426
+ # end
427
+ def ssl_ca_file(path)
428
+ default_options[:ssl_ca_file] = path
429
+ end
430
+
431
+ # Allows setting an OpenSSL certificate authority path (directory).
432
+ #
433
+ # Setting this option enables certificate verification. All
434
+ # certificates along a chain must be available in ssl_ca_file or
435
+ # ssl_ca_path for verification to succeed.
436
+ #
437
+ # class Foo
438
+ # include HTTParty
439
+ # ssl_ca_path '/etc/ssl/certs/'
440
+ # end
441
+ def ssl_ca_path(path)
442
+ default_options[:ssl_ca_path] = path
443
+ end
444
+
445
+ # Allows setting a custom parser for the response.
446
+ #
447
+ # class Foo
448
+ # include HTTParty
449
+ # parser Proc.new {|data| ...}
450
+ # end
451
+ def parser(custom_parser = nil)
452
+ if custom_parser.nil?
453
+ default_options[:parser]
454
+ else
455
+ default_options[:parser] = custom_parser
456
+ validate_format
457
+ end
458
+ end
459
+
460
+ # Allows setting a custom URI adapter.
461
+ #
462
+ # class Foo
463
+ # include HTTParty
464
+ # uri_adapter Addressable::URI
465
+ # end
466
+ def uri_adapter(uri_adapter)
467
+ raise ArgumentError, 'The URI adapter should respond to #parse' unless uri_adapter.respond_to?(:parse)
468
+ default_options[:uri_adapter] = uri_adapter
469
+ end
470
+
471
+ # Allows setting a custom connection_adapter for the http connections
472
+ #
473
+ # @example
474
+ # class Foo
475
+ # include HTTParty
476
+ # connection_adapter Proc.new {|uri, options| ... }
477
+ # end
478
+ #
479
+ # @example provide optional configuration for your connection_adapter
480
+ # class Foo
481
+ # include HTTParty
482
+ # connection_adapter Proc.new {|uri, options| ... }, {foo: :bar}
483
+ # end
484
+ #
485
+ # @see HTTParty::ConnectionAdapter
486
+ def connection_adapter(custom_adapter = nil, options = nil)
487
+ if custom_adapter.nil?
488
+ default_options[:connection_adapter]
489
+ else
490
+ default_options[:connection_adapter] = custom_adapter
491
+ default_options[:connection_adapter_options] = options
492
+ end
493
+ end
494
+
495
+ # Allows making a get request to a url.
496
+ #
497
+ # class Foo
498
+ # include HTTParty
499
+ # end
500
+ #
501
+ # # Simple get with full url
502
+ # Foo.get('http://foo.com/resource.json')
503
+ #
504
+ # # Simple get with full url and query parameters
505
+ # # ie: http://foo.com/resource.json?limit=10
506
+ # Foo.get('http://foo.com/resource.json', query: {limit: 10})
507
+ def get(path, options = {}, &block)
508
+ perform_request Net::HTTP::Get, path, options, &block
509
+ end
510
+
511
+ # Allows making a post request to a url.
512
+ #
513
+ # class Foo
514
+ # include HTTParty
515
+ # end
516
+ #
517
+ # # Simple post with full url and setting the body
518
+ # Foo.post('http://foo.com/resources', body: {bar: 'baz'})
519
+ #
520
+ # # Simple post with full url using :query option,
521
+ # # which appends the parameters to the URI.
522
+ # Foo.post('http://foo.com/resources', query: {bar: 'baz'})
523
+ def post(path, options = {}, &block)
524
+ perform_request Net::HTTP::Post, path, options, &block
525
+ end
526
+
527
+ # Perform a PATCH request to a path
528
+ def patch(path, options = {}, &block)
529
+ perform_request Net::HTTP::Patch, path, options, &block
530
+ end
531
+
532
+ # Perform a PUT request to a path
533
+ def put(path, options = {}, &block)
534
+ perform_request Net::HTTP::Put, path, options, &block
535
+ end
536
+
537
+ # Perform a DELETE request to a path
538
+ def delete(path, options = {}, &block)
539
+ perform_request Net::HTTP::Delete, path, options, &block
540
+ end
541
+
542
+ # Perform a MOVE request to a path
543
+ def move(path, options = {}, &block)
544
+ perform_request Net::HTTP::Move, path, options, &block
545
+ end
546
+
547
+ # Perform a COPY request to a path
548
+ def copy(path, options = {}, &block)
549
+ perform_request Net::HTTP::Copy, path, options, &block
550
+ end
551
+
552
+ # Perform a HEAD request to a path
553
+ def head(path, options = {}, &block)
554
+ ensure_method_maintained_across_redirects options
555
+ perform_request Net::HTTP::Head, path, options, &block
556
+ end
557
+
558
+ # Perform an OPTIONS request to a path
559
+ def options(path, options = {}, &block)
560
+ perform_request Net::HTTP::Options, path, options, &block
561
+ end
562
+
563
+ # Perform a MKCOL request to a path
564
+ def mkcol(path, options = {}, &block)
565
+ perform_request Net::HTTP::Mkcol, path, options, &block
566
+ end
567
+
568
+ def lock(path, options = {}, &block)
569
+ perform_request Net::HTTP::Lock, path, options, &block
570
+ end
571
+
572
+ def unlock(path, options = {}, &block)
573
+ perform_request Net::HTTP::Unlock, path, options, &block
574
+ end
575
+
576
+ attr_reader :default_options
577
+
578
+ private
579
+
580
+ def validate_timeout_argument(timeout_type, value)
581
+ raise ArgumentError, "#{ timeout_type } must be an integer or float" unless value && (value.is_a?(Integer) || value.is_a?(Float))
582
+ end
583
+
584
+ def ensure_method_maintained_across_redirects(options)
585
+ unless options.key?(:maintain_method_across_redirects)
586
+ options[:maintain_method_across_redirects] = true
587
+ end
588
+ end
589
+
590
+ def perform_request(http_method, path, options, &block) #:nodoc:
591
+ options = ModuleInheritableAttributes.hash_deep_dup(default_options).merge(options)
592
+ HeadersProcessor.new(headers, options).call
593
+ process_cookies(options)
594
+ Request.new(http_method, path, options).perform(&block)
595
+ end
596
+
597
+ def process_cookies(options) #:nodoc:
598
+ return unless options[:cookies] || default_cookies.any?
599
+ options[:headers] ||= headers.dup
600
+ options[:headers]["cookie"] = cookies.merge(options.delete(:cookies) || {}).to_cookie_string
601
+ end
602
+
603
+ def validate_format
604
+ if format && parser.respond_to?(:supports_format?) && !parser.supports_format?(format)
605
+ supported_format_names = parser.supported_formats.map(&:to_s).sort.join(', ')
606
+ raise UnsupportedFormat, "'#{format.inspect}' Must be one of: #{supported_format_names}"
607
+ end
608
+ end
609
+ end
610
+
611
+ def self.normalize_base_uri(url) #:nodoc:
612
+ normalized_url = url.dup
613
+ use_ssl = (normalized_url =~ /^https/) || (normalized_url =~ /:443\b/)
614
+ ends_with_slash = normalized_url =~ /\/$/
615
+
616
+ normalized_url.chop! if ends_with_slash
617
+ normalized_url.gsub!(/^https?:\/\//i, '')
618
+
619
+ "http#{'s' if use_ssl}://#{normalized_url}"
620
+ end
621
+
622
+ class Basement #:nodoc:
623
+ include HTTParty
624
+ end
625
+
626
+ def self.get(*args, &block)
627
+ Basement.get(*args, &block)
628
+ end
629
+
630
+ def self.post(*args, &block)
631
+ Basement.post(*args, &block)
632
+ end
633
+
634
+ def self.patch(*args, &block)
635
+ Basement.patch(*args, &block)
636
+ end
637
+
638
+ def self.put(*args, &block)
639
+ Basement.put(*args, &block)
640
+ end
641
+
642
+ def self.delete(*args, &block)
643
+ Basement.delete(*args, &block)
644
+ end
645
+
646
+ def self.move(*args, &block)
647
+ Basement.move(*args, &block)
648
+ end
649
+
650
+ def self.copy(*args, &block)
651
+ Basement.copy(*args, &block)
652
+ end
653
+
654
+ def self.head(*args, &block)
655
+ Basement.head(*args, &block)
656
+ end
657
+
658
+ def self.options(*args, &block)
659
+ Basement.options(*args, &block)
660
+ end
661
+ end
662
+
663
+ require 'httparty/hash_conversions'
664
+ require 'httparty/utils'
665
+ require 'httparty/exceptions'
666
+ require 'httparty/parser'
667
+ require 'httparty/request'
668
+ require 'httparty/response'