httsoiree 0.13.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (94) 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/MIT-LICENSE +20 -0
  8. data/README.md +77 -0
  9. data/Rakefile +12 -0
  10. data/bin/httparty +117 -0
  11. data/cucumber.yml +1 -0
  12. data/examples/aaws.rb +32 -0
  13. data/examples/basic.rb +28 -0
  14. data/examples/crack.rb +19 -0
  15. data/examples/custom_parsers.rb +67 -0
  16. data/examples/delicious.rb +37 -0
  17. data/examples/google.rb +16 -0
  18. data/examples/headers_and_user_agents.rb +6 -0
  19. data/examples/logging.rb +38 -0
  20. data/examples/nokogiri_html_parser.rb +22 -0
  21. data/examples/rubyurl.rb +14 -0
  22. data/examples/stackexchange.rb +24 -0
  23. data/examples/tripit_sign_in.rb +33 -0
  24. data/examples/twitter.rb +31 -0
  25. data/examples/whoismyrep.rb +10 -0
  26. data/features/basic_authentication.feature +20 -0
  27. data/features/command_line.feature +7 -0
  28. data/features/deals_with_http_error_codes.feature +26 -0
  29. data/features/digest_authentication.feature +20 -0
  30. data/features/handles_compressed_responses.feature +27 -0
  31. data/features/handles_multiple_formats.feature +57 -0
  32. data/features/steps/env.rb +22 -0
  33. data/features/steps/httparty_response_steps.rb +52 -0
  34. data/features/steps/httparty_steps.rb +43 -0
  35. data/features/steps/mongrel_helper.rb +94 -0
  36. data/features/steps/remote_service_steps.rb +74 -0
  37. data/features/supports_read_timeout_option.feature +13 -0
  38. data/features/supports_redirection.feature +22 -0
  39. data/features/supports_timeout_option.feature +13 -0
  40. data/httparty.gemspec +25 -0
  41. data/lib/httparty/connection_adapter.rb +188 -0
  42. data/lib/httparty/cookie_hash.rb +22 -0
  43. data/lib/httparty/core_extensions.rb +32 -0
  44. data/lib/httparty/exceptions.rb +29 -0
  45. data/lib/httparty/hash_conversions.rb +51 -0
  46. data/lib/httparty/logger/apache_logger.rb +22 -0
  47. data/lib/httparty/logger/curl_logger.rb +48 -0
  48. data/lib/httparty/logger/logger.rb +18 -0
  49. data/lib/httparty/module_inheritable_attributes.rb +56 -0
  50. data/lib/httparty/net_digest_auth.rb +84 -0
  51. data/lib/httparty/parser.rb +141 -0
  52. data/lib/httparty/request.rb +339 -0
  53. data/lib/httparty/response/headers.rb +31 -0
  54. data/lib/httparty/response.rb +72 -0
  55. data/lib/httparty/version.rb +3 -0
  56. data/lib/httparty.rb +618 -0
  57. data/lib/httsoiree.rb +3 -0
  58. data/script/release +42 -0
  59. data/spec/fixtures/delicious.xml +23 -0
  60. data/spec/fixtures/empty.xml +0 -0
  61. data/spec/fixtures/google.html +3 -0
  62. data/spec/fixtures/ssl/generate.sh +29 -0
  63. data/spec/fixtures/ssl/generated/1fe462c2.0 +16 -0
  64. data/spec/fixtures/ssl/generated/bogushost.crt +13 -0
  65. data/spec/fixtures/ssl/generated/ca.crt +16 -0
  66. data/spec/fixtures/ssl/generated/ca.key +15 -0
  67. data/spec/fixtures/ssl/generated/selfsigned.crt +14 -0
  68. data/spec/fixtures/ssl/generated/server.crt +13 -0
  69. data/spec/fixtures/ssl/generated/server.key +15 -0
  70. data/spec/fixtures/ssl/openssl-exts.cnf +9 -0
  71. data/spec/fixtures/twitter.csv +2 -0
  72. data/spec/fixtures/twitter.json +1 -0
  73. data/spec/fixtures/twitter.xml +403 -0
  74. data/spec/fixtures/undefined_method_add_node_for_nil.xml +2 -0
  75. data/spec/httparty/connection_adapter_spec.rb +370 -0
  76. data/spec/httparty/cookie_hash_spec.rb +83 -0
  77. data/spec/httparty/exception_spec.rb +23 -0
  78. data/spec/httparty/logger/apache_logger_spec.rb +41 -0
  79. data/spec/httparty/logger/curl_logger_spec.rb +18 -0
  80. data/spec/httparty/logger/logger_spec.rb +22 -0
  81. data/spec/httparty/net_digest_auth_spec.rb +152 -0
  82. data/spec/httparty/parser_spec.rb +165 -0
  83. data/spec/httparty/request_spec.rb +774 -0
  84. data/spec/httparty/response_spec.rb +221 -0
  85. data/spec/httparty/ssl_spec.rb +74 -0
  86. data/spec/httparty_spec.rb +783 -0
  87. data/spec/spec.opts +2 -0
  88. data/spec/spec_helper.rb +37 -0
  89. data/spec/support/ssl_test_helper.rb +47 -0
  90. data/spec/support/ssl_test_server.rb +80 -0
  91. data/spec/support/stub_response.rb +43 -0
  92. data/website/css/common.css +47 -0
  93. data/website/index.html +73 -0
  94. metadata +215 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 491c39f8ca9fcbace9d294dfc6ee776ffcd9b14b
4
+ data.tar.gz: 40856244d0cc91af78cb241f7aa9cceef5b2ba52
5
+ SHA512:
6
+ metadata.gz: 3ef38e2ec560c2ab049369beb70093c74dc99817e9937e9d60428efce64e9b7b814ae0e065d812176a4957aea4ccd3ca76216887d063b78757ef19cb8ae19552
7
+ data.tar.gz: 80bfb94747033c9b6e56f0b384d21fc909ab529ca93cbf189c2cfc0b6b2c9216198396922889104c519527129d87459b8bb0d91d5f5a42b328f51f9bd0b01171
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ Gemfile.lock
2
+ .DS_Store
3
+ .yardoc/
4
+ doc/
5
+ tmp/
6
+ log/
7
+ pkg/
8
+ *.swp
9
+ /.bundle
10
+ .rvmrc
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ notifications:
6
+ email: false
7
+ bundler_args: --without development
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
3
+
4
+ gem 'rake'
5
+ gem 'cucumber', '~> 0.7'
6
+ gem 'fakeweb', '~> 1.2'
7
+ gem 'rspec', '~> 1.3'
8
+ gem 'mongrel', '1.2.0.pre2'
9
+
10
+ group :development do
11
+ gem 'guard'
12
+ gem 'guard-rspec'
13
+ gem 'guard-bundler'
14
+ end
data/Guardfile ADDED
@@ -0,0 +1,16 @@
1
+ rspec_options = {
2
+ version: 1,
3
+ all_after_pass: false,
4
+ all_on_start: false,
5
+ }
6
+
7
+ guard 'rspec', rspec_options do
8
+ watch(%r{^spec/.+_spec\.rb$})
9
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
10
+ watch('spec/spec_helper.rb') { "spec" }
11
+ end
12
+
13
+ guard 'bundler' do
14
+ watch('Gemfile')
15
+ watch(/^.+\.gemspec/)
16
+ end
data/History ADDED
@@ -0,0 +1,303 @@
1
+ == 0.10.1 2013-01-26
2
+
3
+ * new
4
+ * [added support for MOVE requests](https://github.com/jnunemaker/httparty/pull/183)
5
+ * [bump multi xml version](https://github.com/jnunemaker/httparty/pull/181)
6
+
7
+ == 0.10.0 2013-01-10
8
+ * changes
9
+ * removed yaml support because of security risk (see rails yaml issues)
10
+
11
+ == 0.9.0 2012-09-07
12
+ * new
13
+ * [support for connection adapters](https://github.com/jnunemaker/httparty/pull/157)
14
+ * [allow ssl_version on ruby 1.9](https://github.com/jnunemaker/httparty/pull/159)
15
+
16
+ * bug fixes
17
+ * [don't treat port 4430 as ssl](https://github.com/jnunemaker/httparty/commit/a296b1c97f83d7dcc6ef85720a43664c265685ac)
18
+ * [deep clone default options](https://github.com/jnunemaker/httparty/commit/f74227d30f9389b4b23a888c9af49fb9b8248e1f)
19
+ * a few net digest auth fixes
20
+
21
+ == 0.8.3 2012-04-21
22
+ * new
23
+ * [lazy parsing of responses](https://github.com/jnunemaker/httparty/commit/9fd5259c8dab00e426082b66af44ede2c9068f45)
24
+ * [add support for PATCH requests](https://github.com/jnunemaker/httparty/commit/7ab6641e37a9e31517e46f6124f38c615395d38a)
25
+ * bug fixes
26
+ * [subclasses no longer override superclass options](https://github.com/jnunemaker/httparty/commit/682af8fbf672e7b3009e650da776c85cdfe78d39)
27
+
28
+ == 0.8.2 2012-04-12
29
+ * new
30
+ * add -r to make CLI return failure code if status >= 400
31
+ * allow blank username from CLI
32
+ * bug fixes
33
+ * return nil for null body
34
+ * automatically deflate responses with a Content-Encoding: x-gzip header
35
+ * Do not HEAD on POST request with digest authentication
36
+ * add support for proxy authentication
37
+ * fix posting data with CLI
38
+ * require rexml/document if xml format from CLI
39
+ * support for fragmented responses
40
+
41
+ == 0.8.1 2011-10-05
42
+ * bug fixes
43
+ * content-encoding header should be removed when automatically inflating the body
44
+
45
+ == 0.8.0 2011-09-13
46
+ * new
47
+ * switch to multi json/xml for parsing by default
48
+ * bug fixes
49
+ * fix redirects to relative uri's
50
+
51
+ == 0.7.8 2011-06-06
52
+ * bug fix
53
+ * Make response honor respond to
54
+ * net http timeout can also be a float
55
+
56
+ == 0.7.7 2011-04-16
57
+ * bug fix
58
+ * Fix NoMethodError when using the NON_RAILS_QUERY_STRING_NORMALIZER with a hash whose key is a symbol and value is nil
59
+
60
+ == 0.7.5 2011-04-16
61
+ * bug fix
62
+ * caused issue with latest rubygems
63
+
64
+ == 0.7.4 2011-02-13
65
+ * bug fixes
66
+ * Set VERIFY_NONE when using https. Ruby 1.9.2 no longer sets this for us. gh-67
67
+
68
+ == 0.7.3 2011-01-20
69
+ * bug fixes
70
+ * Fix digest auth for unspecified quality of protection (bjoernalbers, mtrudel, dwo)
71
+
72
+ == 0.7.2 2011-01-20
73
+ * bug fixes
74
+ * Fix gem dependencies
75
+
76
+ == 0.7.1 2011-01-19
77
+ * bug fixes
78
+ * Fix uninitialized constant HTTParty::Response::Net in 1.9.2 (cap10morgan)
79
+ * Other fixes for 1.9.2, full suite still fails (cap10morgan)
80
+
81
+ == 0.7.0 2011-01-18
82
+ * minor enhancements
83
+ * Added query methods for HTTP status codes, i.e. response.success?
84
+ response.created? (thanks citizenparker)
85
+ * Added support for ssl_ca_file and ssl_ca_path (dlitz)
86
+ * Allow custom query string normalization. gh-8
87
+ * Unlock private keys with password (freerange)
88
+ * Added high level request documentation (phildarnowsky)
89
+ * Added basic post example (pbuckley)
90
+ * Response object has access to its corresponding request object
91
+ * Added example of siginin into tripit.com
92
+ * Added option to follow redirects (rkj). gh-56
93
+ * bug fixes
94
+ * Fixed superclass mismatch exception while running tests
95
+ (thanks dlitz http://github.com/dlitz/httparty/commit/48224f0615b32133afcff4718ad426df7a4b401b)
96
+
97
+ == 0.6.1 2010-07-07
98
+ * minor enhancements
99
+ * updated to crack 0.1.8
100
+ * bug fixes
101
+ * subclasses always merge into the parent's default_options and
102
+ default_cookies (l4rk).
103
+ * subclasses play nicely with grand parents. gh-49
104
+
105
+ == 0.6.0 2010-06-13
106
+ * major enhancements
107
+ * Digest Auth (bartiaco, sbecker, gilles, and aaronrussell)
108
+ * Maintain HTTP method across redirects (bartiaco and sbecker)
109
+ * HTTParty::Response#response returns the Net::HTTPResponse object
110
+ * HTTParty::Response#headers returns a HTTParty::Response::Headers object
111
+ which quacks like a Hash + Net::HTTPHeader. The #headers method continues
112
+ to be backwards-compatible with the old Hash return value but may become
113
+ deprecated in the future.
114
+
115
+ * minor enhancements
116
+ * Update crack requirement to version 0.1.7
117
+ You may still get a warning because Crack's version constant is out of date
118
+ * Timeout option can be set for all requests using HTTParty.default_timeout (taazza)
119
+ * Closed #38 "headers hash should downcase keys so canonical header name can be used"
120
+ * Closed #40 "Gzip response" wherein gziped and deflated responses are
121
+ automatically inflated. (carsonmcdonald)
122
+
123
+ == 0.5.2 2010-01-31
124
+ * minor enhancements
125
+ * Update crack requirement to version 0.1.6
126
+
127
+ == 0.5.1 2010-01-30
128
+ * bug fixes
129
+ * Handle 304 response correctly by returning the HTTParty::Response object instead of redirecting (seth and hellvinz)
130
+ * Only redirect 300 responses if the header contains a Location
131
+ * Don't append empty query strings to the uri. Closes #31
132
+ * When no_follow is enabled, only raise the RedirectionTooDeep exception when a response tries redirecting. Closes #28
133
+
134
+ * major enhancements
135
+ * Removed rubygems dependency. I suggest adding rubygems to RUBYOPT if this causes problems for you.
136
+ $ export RUBYOPT='rubygems'
137
+ * HTTParty#debug_output prints debugging information for the current request (iwarshak)
138
+ * HTTParty#no_follow now available as a class-level option. Sets whether or not to follow redirects.
139
+
140
+ * minor enhancements
141
+ * HTTParty::VERSION now available
142
+ * Update crack requirement to version 0.1.5
143
+
144
+ == 0.5.0 2009-12-07
145
+ * bug fixes
146
+ * inheritable attributes no longer mutable by subclasses (yyyc514)
147
+ * namespace BasicObject within HTTParty to avoid class name collisions (eric)
148
+
149
+ * major enhancements
150
+ * Custom Parsers via class or proc
151
+ * Deprecation warning on HTTParty::AllowedFormats
152
+ moved to HTTParty::Parser::SupportedFormats
153
+
154
+ * minor enhancements
155
+ * Curl inspired output when using the binary in verbose mode (alexvollmer)
156
+ * raise UnsupportedURIScheme when scheme is not HTTP or HTTPS (djspinmonkey)
157
+ * Allow SSL for ports other than 443 when scheme is HTTPS (stefankroes)
158
+ * Accept PEM certificates via HTTParty#pem (chrislo)
159
+ * Support HEAD and OPTION verbs (grempe)
160
+ * Verify SSL certificates when providing a PEM file (collectiveidea/danielmorrison)
161
+
162
+ == 0.4.5 2009-09-12
163
+ * bug fixes
164
+ * Fixed class-level headers overwritten by cookie management code. Closes #19
165
+ * Fixed "superclass mismatch for class BlankSlate" error. Closes #20
166
+ * Fixed reading files as post data from the command line (vesan)
167
+
168
+ * minor enhancements
169
+ * Timeout option added; will raise a Timeout::Error after the timeout has elapsed (attack). Closes #17
170
+ HTTParty.get "http://github.com", timeout: 1
171
+ * Building gem with Jeweler
172
+
173
+ == 0.4.4 2009-07-19
174
+ * 2 minor update
175
+ * :query no longer sets form data. Use body and set content type to application/x-www-form-urlencoded if you need it. :query was wrong for that.
176
+ * Fixed a bug in the cookies class method that caused cookies to be forgotten after the first request.
177
+ * Also, some general cleanup of tests and such.
178
+
179
+ == 0.4.3 2009-04-23
180
+ * 1 minor update
181
+ * added message to the response object
182
+
183
+ == 0.4.2 2009-03-30
184
+ * 2 minor changes
185
+ * response code now returns an integer instead of a string (jqr)
186
+ * rubyforge project setup for crack so i'm now depending on that instead of jnunemaker-crack
187
+
188
+ == 0.4.1 2009-03-29
189
+ * 1 minor fix
190
+ * gem 'jnunemaker-crack' instead of gem 'crack'
191
+
192
+ == 0.4.0 2009-03-29
193
+ * 1 minor change
194
+ * Switched xml and json parsing to crack (same code as before just moved to gem for easier reuse in other projects)
195
+
196
+ == 0.3.1 2009-02-10
197
+ * 1 minor fix, 1 minor enhancement
198
+ * Fixed unescaping umlauts (siebertm)
199
+ * Added yaml response parsing (Miha Filej)
200
+
201
+ == 0.3.0 2009-01-31
202
+ * 1 major enhancement, 1 bug fix
203
+ * JSON gem no longer a requirement. It was conflicting with rails json stuff so I just stole ActiveSupport's json decoding and bundled it with HTTParty.
204
+ * Fixed bug where query strings were being duplicated on redirects
205
+ * Added a bunch of specs and moved some code around.
206
+
207
+ == 0.2.10 2009-01-29
208
+ * 1 minor enhancement
209
+ * Made encoding on query parameters treat everything except URI::PATTERN::UNRESERVED as UNSAFE to force encoding of '+' character (Julian Russell)
210
+
211
+ == 0.2.9 2009-01-29
212
+ * 3 minor enhancements
213
+ * Added a 'headers' accessor to the response with a hash of any HTTP headers. (Don Peterson)
214
+ * Add support for a ":cookies" option to be used at the class level, or as an option on any individual call. It should be passed a hash, which will be converted to the proper format and added to the request headers when the call is made. (Don Peterson)
215
+ * Refactored several specs and added a full suite of cucumber features (Don Peterson)
216
+
217
+ == 0.2.8 2009-01-28
218
+ * 1 major fix
219
+ * fixed major bug with response where it wouldn't iterate or really work at all with parsed responses
220
+
221
+ == 0.2.7 2009-01-28
222
+ * 2 minor fixes, 2 minor enhancements, 2 major enhancements
223
+ * fixed undefined method add_node for nil class error that occasionally happened (juliocesar)
224
+ * Handle nil or unexpected values better when typecasting. (Brian Landau)
225
+ * More robust handling of mime types (Alex Vollmer)
226
+ * Fixed support for specifying headers and added support for basic auth to CLI. (Alex Vollmer)
227
+ * Added first class response object that includes original body and status code (Alex Vollmer)
228
+ * Now parsing all response types as some non-200 responses provide important information, this means no more exception raising (Alex Vollmer)
229
+
230
+ == 0.2.6 2009-01-05
231
+ * 1 minor bug fix
232
+ * added explicit require of time as Time#parse failed outside of rails (willcodeforfoo)
233
+
234
+ == 0.2.5 2009-01-05
235
+ * 1 major enhancement
236
+ * Add command line interface to HTTParty (Alex Vollmer)
237
+
238
+ == 0.2.4 2008-12-23
239
+ * 1 bug fix
240
+ * Fixed that mimetype detection was failing if no mimetype was returned from service (skippy)
241
+ == 0.2.3 2008-12-23
242
+ * 1 bug fix
243
+ * Fixed typecasting class variable naming issue
244
+
245
+ == 0.2.2 2008-12-08
246
+ * 1 bug fix
247
+ * Added the missing core extension hash method to_xml_attributes
248
+
249
+ == 0.2.1 2008-12-08
250
+ * 1 bug fix
251
+ * Fixed that HTTParty was borking ActiveSupport and as such Rails (thanks to Rob Sanheim)
252
+
253
+ == 0.2.0 2008-12-07
254
+ * 1 major enhancement
255
+ * Removed ActiveSupport as a dependency. Now requires json gem for json deserialization and uses an included class to do the xml parsing.
256
+
257
+ == 0.1.8 2008-11-30
258
+ * 3 major enhancements
259
+ * Moved base_uri normalization into request class and out of httparty module, fixing
260
+ the problem where base_uri was not always being normalized.
261
+ * Stupid simple support for HTTParty.get/post/put/delete. (jqr)
262
+ * Switched gem management to Echoe from newgem.
263
+
264
+ == 0.1.7 2008-11-30
265
+ * 1 major enhancement
266
+ * fixed multiple class definitions overriding each others options
267
+
268
+ == 0.1.6 2008-11-26
269
+ * 1 major enhancement
270
+ * now passing :query to set_form_data if post request to avoid content length errors
271
+
272
+ == 0.1.5 2008-11-14
273
+ * 2 major enhancements
274
+ * Refactored send request method out into its own object.
275
+ * Added :html format if you just want to do that.
276
+
277
+ == 0.1.4 2008-11-08
278
+ * 3 major enhancements:
279
+ * Removed some cruft
280
+ * Added ability to follow redirects automatically and turn that off (Alex Vollmer)
281
+
282
+ == 0.1.3 2008-08-22
283
+
284
+ * 3 major enhancements:
285
+ * Added http_proxy key for setting proxy server and port (francxk@gmail.com)
286
+ * Now raises exception when http error occurs (francxk@gmail.com)
287
+ * Changed auto format detection from file extension to response content type (Jay Pignata)
288
+
289
+ == 0.1.2 2008-08-09
290
+
291
+ * 1 major enhancement:
292
+ * default_params were not being appended to query string if option[:query] was blank
293
+
294
+ == 0.1.1 2008-07-30
295
+
296
+ * 2 major enhancement:
297
+ * Added :basic_auth key for options when making a request
298
+ * :query and :body both now work with query string or hash
299
+
300
+ == 0.1.0 2008-07-27
301
+
302
+ * 1 major enhancement:
303
+ * Initial release
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 John Nunemaker
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,77 @@
1
+ # httparty
2
+
3
+ Makes http fun again!
4
+
5
+ ## Install
6
+
7
+ ```
8
+ gem install httparty
9
+ ```
10
+
11
+ ## Requirements
12
+
13
+ * Ruby 1.9.3 or higher
14
+ * multi_xml
15
+ * You like to party!
16
+
17
+ ## Examples
18
+
19
+ ```ruby
20
+ # Use the class methods to get down to business quickly
21
+ response = HTTParty.get('https://api.stackexchange.com/2.2/questions?site=stackoverflow')
22
+
23
+ puts response.body, response.code, response.message, response.headers.inspect
24
+
25
+ # Or wrap things up in your own class
26
+ class StackExchange
27
+ include HTTParty
28
+ base_uri 'api.stackexchange.com'
29
+
30
+ def initialize(service, page)
31
+ @options = { query: {site: service, page: page} }
32
+ end
33
+
34
+ def questions
35
+ self.class.get("/2.2/questions", @options)
36
+ end
37
+
38
+ def users
39
+ self.class.get("/2.2/users", @options)
40
+ end
41
+ end
42
+
43
+ stack_exchange = StackExchange.new("stackoverflow", 1)
44
+ puts stack_exchange.questions
45
+ puts stack_exchange.users
46
+ ```
47
+
48
+ See the [examples directory](http://github.com/jnunemaker/httparty/tree/master/examples) for even more goodies.
49
+
50
+ ## Command Line Interface
51
+
52
+ httparty also includes the executable `httparty` which can be
53
+ used to query web services and examine the resulting output. By default
54
+ it will output the response as a pretty-printed Ruby object (useful for
55
+ grokking the structure of output). This can also be overridden to output
56
+ formatted XML or JSON. Execute `httparty --help` for all the
57
+ options. Below is an example of how easy it is.
58
+
59
+ ```
60
+ httparty "https://api.stackexchange.com/2.2/questions?site=stackoverflow"
61
+ ```
62
+
63
+ ## Help and Docs
64
+
65
+ * https://groups.google.com/forum/#!forum/httparty-gem
66
+ * http://rdoc.info/projects/jnunemaker/httparty
67
+
68
+ ## Contributing
69
+
70
+ * Fork the project.
71
+ * Run `bundle`
72
+ * Run `bundle exec rake`
73
+ * Make your feature addition or bug fix.
74
+ * Add tests for it. This is important so I don't break it in a future version unintentionally.
75
+ * Run `bundle exec rake` (No, REALLY :))
76
+ * Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself in another branch so I can ignore when I pull)
77
+ * Send me a pull request. Bonus points for topic branches.
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ require 'spec/rake/spectask'
2
+ Spec::Rake::SpecTask.new(:spec) do |spec|
3
+ spec.ruby_opts << '-rubygems'
4
+ spec.libs << 'lib' << 'spec'
5
+ spec.spec_files = FileList['spec/**/*_spec.rb']
6
+ spec.spec_opts = ['--options', 'spec/spec.opts']
7
+ end
8
+
9
+ require 'cucumber/rake/task'
10
+ Cucumber::Rake::Task.new(:features)
11
+
12
+ task default: [:spec, :features]
data/bin/httparty ADDED
@@ -0,0 +1,117 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "optparse"
4
+ require "pp"
5
+
6
+ $:.unshift(File.join(File.dirname(__FILE__), "/../lib"))
7
+ require "httparty"
8
+
9
+ opts = {
10
+ action: :get,
11
+ headers: {},
12
+ verbose: false
13
+ }
14
+
15
+ OptionParser.new do |o|
16
+ o.banner = "USAGE: #{$0} [options] [url]"
17
+
18
+ o.on("-f",
19
+ "--format [FORMAT]",
20
+ "Output format to use instead of pretty-print ruby: " +
21
+ "plain, csv, json or xml") do |f|
22
+ opts[:output_format] = f.downcase.to_sym
23
+ end
24
+
25
+ o.on("-a",
26
+ "--action [ACTION]",
27
+ "HTTP action: get (default), post, put, delete, head, or options") do |a|
28
+ opts[:action] = a.downcase.to_sym
29
+ end
30
+
31
+ o.on("-d",
32
+ "--data [BODY]",
33
+ "Data to put in request body (prefix with '@' for file)") do |d|
34
+ if d =~ /^@/
35
+ opts[:body] = open(d[1..-1]).read
36
+ else
37
+ opts[:body] = d
38
+ end
39
+ end
40
+
41
+ o.on("-H", "--header [NAME:VALUE]", "Additional HTTP headers in NAME:VALUE form") do |h|
42
+ abort "Invalid header specification, should be Name:Value" unless h =~ /.+:.+/
43
+ name, value = h.split(':')
44
+ opts[:headers][name.strip] = value.strip
45
+ end
46
+
47
+ o.on("-v", "--verbose", "If set, print verbose output") do |v|
48
+ opts[:verbose] = true
49
+ end
50
+
51
+ o.on("-u", "--user [CREDS]", "Use basic authentication. Value should be user:password") do |u|
52
+ abort "Invalid credentials format. Must be user:password" unless u =~ /.*:.+/
53
+ user, password = u.split(':')
54
+ opts[:basic_auth] = { username: user, password: password }
55
+ end
56
+
57
+ o.on("-r", "--response-code", "Command fails if response code >= 400") do
58
+ opts[:response_code] = true
59
+ end
60
+
61
+ o.on("-h", "--help", "Show help documentation") do |h|
62
+ puts o
63
+ exit
64
+ end
65
+ end.parse!
66
+
67
+
68
+ if ARGV.empty?
69
+ STDERR.puts "You need to provide a URL"
70
+ STDERR.puts "USAGE: #{$0} [options] [url]"
71
+ end
72
+
73
+ def dump_headers(response)
74
+ resp_type = Net::HTTPResponse::CODE_TO_OBJ[response.code.to_s]
75
+ puts "#{response.code} #{resp_type.to_s.sub(/^Net::HTTP/, '')}"
76
+ response.headers.each do |n,v|
77
+ puts "#{n}: #{v}"
78
+ end
79
+ puts
80
+ end
81
+
82
+ if opts[:verbose]
83
+ puts "#{opts[:action].to_s.upcase} #{ARGV.first}"
84
+ opts[:headers].each do |n,v|
85
+ puts "#{n}: #{v}"
86
+ end
87
+ puts
88
+ end
89
+
90
+ response = HTTParty.send(opts[:action], ARGV.first, opts)
91
+ if opts[:output_format].nil?
92
+ dump_headers(response) if opts[:verbose]
93
+ pp response
94
+ else
95
+ print_format = opts[:output_format]
96
+ dump_headers(response) if opts[:verbose]
97
+
98
+ case opts[:output_format]
99
+ when :json
100
+ begin
101
+ require 'json'
102
+ puts JSON.pretty_generate(response.delegate)
103
+ rescue LoadError
104
+ puts YAML.dump(response.delegate)
105
+ end
106
+ when :xml
107
+ require 'rexml/document'
108
+ REXML::Document.new(response.body).write(STDOUT, 2)
109
+ puts
110
+ when :csv
111
+ require 'csv'
112
+ puts CSV.parse(response.body).map{|row| row.to_s }
113
+ else
114
+ puts response
115
+ end
116
+ end
117
+ exit false if opts[:response_code] && response.code >= 400
data/cucumber.yml ADDED
@@ -0,0 +1 @@
1
+ default: features --format progress
data/examples/aaws.rb ADDED
@@ -0,0 +1,32 @@
1
+ require 'rubygems'
2
+ require 'active_support'
3
+
4
+ dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
5
+ require File.join(dir, 'httparty')
6
+ require 'pp'
7
+ config = YAML::load(File.read(File.join(ENV['HOME'], '.aaws')))
8
+
9
+ module AAWS
10
+ class Book
11
+ include HTTParty
12
+ base_uri 'http://ecs.amazonaws.com'
13
+ default_params Service: 'AWSECommerceService', Operation: 'ItemSearch', SearchIndex: 'Books'
14
+
15
+ def initialize(key)
16
+ self.class.default_params AWSAccessKeyId: key
17
+ end
18
+
19
+ def search(options={})
20
+ raise ArgumentError, 'You must search for something' if options[:query].blank?
21
+
22
+ # amazon uses nasty camelized query params
23
+ options[:query] = options[:query].inject({}) { |h, q| h[q[0].to_s.camelize] = q[1]; h }
24
+
25
+ # make a request and return the items (NOTE: this doesn't handle errors at this point)
26
+ self.class.get('/onca/xml', options)['ItemSearchResponse']['Items']
27
+ end
28
+ end
29
+ end
30
+
31
+ aaws = AAWS::Book.new(config[:access_key])
32
+ pp aaws.search(query: {title: 'Ruby On Rails'})
data/examples/basic.rb ADDED
@@ -0,0 +1,28 @@
1
+ dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ require File.join(dir, 'httparty')
3
+ require 'pp'
4
+
5
+ # You can also use post, put, delete, head, options in the same fashion
6
+ response = HTTParty.get('https://api.stackexchange.com/2.2/questions?site=stackoverflow')
7
+ puts response.body, response.code, response.message, response.headers.inspect
8
+
9
+ # An example post to a minimal rails app in the development environment
10
+ # Note that "skip_before_filter :verify_authenticity_token" must be set in the
11
+ # "pears" controller for this example
12
+
13
+ class Partay
14
+ include HTTParty
15
+ base_uri 'http://localhost:3000'
16
+ end
17
+
18
+ options = {
19
+ body: {
20
+ pear: { # your resource
21
+ foo: '123', # your columns/data
22
+ bar: 'second',
23
+ baz: 'last thing'
24
+ }
25
+ }
26
+ }
27
+
28
+ pp Partay.post('/pears.xml', options)
data/examples/crack.rb ADDED
@@ -0,0 +1,19 @@
1
+ require 'rubygems'
2
+ require 'crack'
3
+
4
+ dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
5
+ require File.join(dir, 'httparty')
6
+ require 'pp'
7
+
8
+ class Rep
9
+ include HTTParty
10
+
11
+ parser(
12
+ Proc.new do |body, format|
13
+ Crack::XML.parse(body)
14
+ end
15
+ )
16
+ end
17
+
18
+ pp Rep.get('http://whoismyrepresentative.com/getall_mems.php?zip=46544')
19
+ pp Rep.get('http://whoismyrepresentative.com/getall_mems.php', query: {zip: 46544})