httparty 0.16.2 → 0.20.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of httparty might be problematic. Click here for more details.

Files changed (92) hide show
  1. checksums.yaml +5 -5
  2. data/.editorconfig +18 -0
  3. data/.github/workflows/ci.yml +23 -0
  4. data/.gitignore +1 -0
  5. data/.rubocop_todo.yml +1 -1
  6. data/Changelog.md +72 -0
  7. data/Gemfile +5 -0
  8. data/README.md +5 -5
  9. data/docs/README.md +70 -5
  10. data/examples/README.md +28 -11
  11. data/examples/aaws.rb +6 -2
  12. data/examples/body_stream.rb +14 -0
  13. data/examples/idn.rb +10 -0
  14. data/examples/microsoft_graph.rb +52 -0
  15. data/examples/multipart.rb +22 -0
  16. data/examples/peer_cert.rb +9 -0
  17. data/examples/stream_download.rb +8 -2
  18. data/httparty.gemspec +3 -3
  19. data/lib/httparty/connection_adapter.rb +59 -16
  20. data/lib/httparty/cookie_hash.rb +10 -8
  21. data/lib/httparty/decompressor.rb +92 -0
  22. data/lib/httparty/exceptions.rb +3 -1
  23. data/lib/httparty/hash_conversions.rb +10 -4
  24. data/lib/httparty/headers_processor.rb +32 -0
  25. data/lib/httparty/logger/apache_formatter.rb +31 -6
  26. data/lib/httparty/logger/curl_formatter.rb +9 -7
  27. data/lib/httparty/logger/logger.rb +5 -1
  28. data/lib/httparty/logger/logstash_formatter.rb +61 -0
  29. data/lib/httparty/module_inheritable_attributes.rb +6 -4
  30. data/lib/httparty/net_digest_auth.rb +15 -15
  31. data/lib/httparty/parser.rb +9 -5
  32. data/lib/httparty/request/body.rb +46 -27
  33. data/lib/httparty/request/multipart_boundary.rb +2 -0
  34. data/lib/httparty/request.rb +75 -96
  35. data/lib/httparty/response/headers.rb +4 -2
  36. data/lib/httparty/response.rb +51 -8
  37. data/lib/httparty/response_fragment.rb +21 -0
  38. data/lib/httparty/text_encoder.rb +72 -0
  39. data/lib/httparty/utils.rb +13 -0
  40. data/lib/httparty/version.rb +3 -1
  41. data/lib/httparty.rb +70 -24
  42. data/website/css/common.css +1 -1
  43. metadata +33 -104
  44. data/.travis.yml +0 -10
  45. data/features/basic_authentication.feature +0 -20
  46. data/features/command_line.feature +0 -95
  47. data/features/deals_with_http_error_codes.feature +0 -26
  48. data/features/digest_authentication.feature +0 -30
  49. data/features/handles_compressed_responses.feature +0 -27
  50. data/features/handles_multiple_formats.feature +0 -57
  51. data/features/steps/env.rb +0 -27
  52. data/features/steps/httparty_response_steps.rb +0 -56
  53. data/features/steps/httparty_steps.rb +0 -43
  54. data/features/steps/mongrel_helper.rb +0 -127
  55. data/features/steps/remote_service_steps.rb +0 -92
  56. data/features/supports_read_timeout_option.feature +0 -13
  57. data/features/supports_redirection.feature +0 -22
  58. data/features/supports_timeout_option.feature +0 -13
  59. data/spec/fixtures/delicious.xml +0 -23
  60. data/spec/fixtures/empty.xml +0 -0
  61. data/spec/fixtures/google.html +0 -3
  62. data/spec/fixtures/ssl/generate.sh +0 -29
  63. data/spec/fixtures/ssl/generated/bogushost.crt +0 -13
  64. data/spec/fixtures/ssl/generated/ca.crt +0 -16
  65. data/spec/fixtures/ssl/generated/ca.key +0 -15
  66. data/spec/fixtures/ssl/generated/selfsigned.crt +0 -14
  67. data/spec/fixtures/ssl/generated/server.crt +0 -13
  68. data/spec/fixtures/ssl/generated/server.key +0 -15
  69. data/spec/fixtures/ssl/openssl-exts.cnf +0 -9
  70. data/spec/fixtures/tiny.gif +0 -0
  71. data/spec/fixtures/twitter.csv +0 -2
  72. data/spec/fixtures/twitter.json +0 -1
  73. data/spec/fixtures/twitter.xml +0 -403
  74. data/spec/fixtures/undefined_method_add_node_for_nil.xml +0 -2
  75. data/spec/httparty/connection_adapter_spec.rb +0 -498
  76. data/spec/httparty/cookie_hash_spec.rb +0 -100
  77. data/spec/httparty/exception_spec.rb +0 -45
  78. data/spec/httparty/hash_conversions_spec.rb +0 -56
  79. data/spec/httparty/logger/apache_formatter_spec.rb +0 -41
  80. data/spec/httparty/logger/curl_formatter_spec.rb +0 -119
  81. data/spec/httparty/logger/logger_spec.rb +0 -38
  82. data/spec/httparty/net_digest_auth_spec.rb +0 -270
  83. data/spec/httparty/parser_spec.rb +0 -190
  84. data/spec/httparty/request/body_spec.rb +0 -60
  85. data/spec/httparty/request_spec.rb +0 -1312
  86. data/spec/httparty/response_spec.rb +0 -347
  87. data/spec/httparty/ssl_spec.rb +0 -74
  88. data/spec/httparty_spec.rb +0 -896
  89. data/spec/spec_helper.rb +0 -51
  90. data/spec/support/ssl_test_helper.rb +0 -47
  91. data/spec/support/ssl_test_server.rb +0 -80
  92. data/spec/support/stub_response.rb +0 -49
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 255d54a6f0cad04486f09e887f8787725421fece
4
- data.tar.gz: d1b74c09b4ef625e5fc4ef791dc896c792a79e19
2
+ SHA256:
3
+ metadata.gz: bde34a85d8b010341759c1b2b42dfe58cad693c39f047b9803ac90afbc13290b
4
+ data.tar.gz: b5248420c90ea545dfa9e6e234d6c597b4c7306dfc8d58ff94dca82e405e0ed1
5
5
  SHA512:
6
- metadata.gz: 6ba5c551dec574810d6e2fe84b639660d7f6c36b2ee667bcf91cae9eb978567d0a076eaff3d80886c0d8b9f225c018561d28f51a09ebbe7102b62fcff17ec8f1
7
- data.tar.gz: e5a1beb085744c890aff8cb42f238a1f2d5e041328d94cc2a5fafab75ca6f21385d4d4fa50a60c4033cedbcf611e10d4809c6337de2b98ce4a42fe32719773a4
6
+ metadata.gz: 13316910a85a75d1204c64be41e995475b26abf9aad1165c06349c296d0b3f0f6ae644b00d8bcf14adc3d9a11659bffd060a5101dc88718af74d0324adde9e30
7
+ data.tar.gz: 668fac0ca6a513e64dead9b633ecb12c4ff926cb017e4e7e244249cfb361f93e1aa515ef3fb6b80e6245264e7da38d0599abec9931c5b33b88d7235b966b0aa1
data/.editorconfig ADDED
@@ -0,0 +1,18 @@
1
+ ; This file is for unifying the coding style for different editors and IDEs.
2
+ ; More information at http://EditorConfig.org
3
+
4
+ root = true
5
+ [*]
6
+ end_of_line = lf
7
+ trim_trailing_whitespace = true
8
+
9
+ [**.rb]
10
+ indent_size = 2
11
+ indent_style = spaces
12
+ insert_final_newline = true
13
+
14
+ [**.xml]
15
+ trim_trailing_whitespace = false
16
+
17
+ [**.html]
18
+ trim_trailing_whitespace = false
@@ -0,0 +1,23 @@
1
+ name: CI
2
+ on: [push, pull_request]
3
+ jobs:
4
+ build:
5
+ runs-on: ubuntu-latest
6
+ strategy:
7
+ matrix:
8
+ ruby:
9
+ - 2.3
10
+ - 2.4
11
+ - 2.5
12
+ - 2.6
13
+ - 2.7
14
+ steps:
15
+ - name: Check out repository code
16
+ uses: actions/checkout@v2
17
+ - name: Set up Ruby
18
+ uses: ruby/setup-ruby@v1
19
+ with:
20
+ ruby-version: ${{ matrix.ruby }}
21
+ bundler-cache: true # Run "bundle install", and cache the result automatically.
22
+ - name: Run Rake
23
+ run: bundle exec rake
data/.gitignore CHANGED
@@ -10,3 +10,4 @@ pkg/
10
10
  .rvmrc
11
11
  coverage
12
12
  *.gem
13
+ .idea
data/.rubocop_todo.yml CHANGED
@@ -15,7 +15,7 @@ Lint/EndAlignment:
15
15
  Enabled: false
16
16
 
17
17
  # Offense count: 1
18
- Lint/HandleExceptions:
18
+ Lint/SuppressedException:
19
19
  Enabled: false
20
20
 
21
21
  # Offense count: 5
data/Changelog.md CHANGED
@@ -1,3 +1,75 @@
1
+ ## 0.20.0
2
+
3
+ Breaking changes
4
+
5
+ * Require Ruby >= 2.3.0
6
+
7
+ Fixes
8
+
9
+ * [`Marshal.dump` fails on response objects when request option `:logger` is set or `:parser` is a proc](https://github.com/jnunemaker/httparty/pull/714)
10
+ * [Switch `:pem` option to to `OpenSSL::PKey.read` to support other algorithms](https://github.com/jnunemaker/httparty/pull/720)
11
+
12
+ ## 0.19.1
13
+
14
+ * [Remove use of unary + method for creating non-frozen string to increase compatibility with older versions of ruby](https://github.com/jnunemaker/httparty/commit/4416141d37fd71bdba4f37589ec265f55aa446ce)
15
+
16
+ ## 0.19.0
17
+
18
+ * [Multipart/Form-Data: rewind files after read](https://github.com/jnunemaker/httparty/pull/709)
19
+ * [add frozen_string_literal pragma to all files](https://github.com/jnunemaker/httparty/pull/711)
20
+ * [Better handling of Accept-Encoding / Content-Encoding decompression (fixes #562)](https://github.com/jnunemaker/httparty/pull/729)
21
+
22
+ ## 0.18.1
23
+
24
+ * [Rename cop Lint/HandleExceptions to Lint/SuppressedException](https://github.com/jnunemaker/httparty/pull/699).
25
+ * [Encode keys in query params](https://github.com/jnunemaker/httparty/pull/698).
26
+ * [Fixed SSL doc example](https://github.com/jnunemaker/httparty/pull/692).
27
+ * [Add a build status badge](https://github.com/jnunemaker/httparty/pull/701).
28
+
29
+
30
+ ## 0.18.0
31
+
32
+ * [Support gzip/deflate transfer encoding when explicit headers are set](https://github.com/jnunemaker/httparty/pull/678).
33
+ * [Support edge case cookie format with a blank attribute](https://github.com/jnunemaker/httparty/pull/685).
34
+
35
+ ## 0.17.3
36
+
37
+ 0.17.2 is broken https://github.com/jnunemaker/httparty/issues/681
38
+
39
+ ## 0.17.2
40
+
41
+ * [Add Response#nil? deprecetion warning](https://github.com/jnunemaker/httparty/pull/680)
42
+
43
+ ## 0.17.1
44
+
45
+ * [Pass options to dynamic block headers](https://github.com/jnunemaker/httparty/pull/661)
46
+ * [Normalize urls with URI adapter to allow International Domain Names support](https://github.com/jnunemaker/httparty/pull/668)
47
+ * [Add max_retries support](https://github.com/jnunemaker/httparty/pull/660)
48
+ * [Minize gem size by removing test files](https://github.com/jnunemaker/httparty/pull/658)
49
+
50
+ ## 0.17.0
51
+
52
+ * [Fix encoding of streamed chunk](https://github.com/jnunemaker/httparty/pull/644)
53
+ * [Avoid modifying frozen strings](https://github.com/jnunemaker/httparty/pull/649)
54
+ * [Expose .connection on fragment block param](https://github.com/jnunemaker/httparty/pull/648)
55
+ * [Add support for `Net::HTTP#write_timeout` method (Ruby 2.6.0)](https://github.com/jnunemaker/httparty/pull/647)
56
+
57
+ ## 0.16.4
58
+ * [Add support for Ruby 2.6](https://github.com/jnunemaker/httparty/pull/636)
59
+ * [Fix a few multipart issues](https://github.com/jnunemaker/httparty/pull/626)
60
+ * [Improve a memory usage for https requests](https://github.com/jnunemaker/httparty/pull/625)
61
+ * [Add response code to streamed body](https://github.com/jnunemaker/httparty/pull/588)
62
+
63
+ ## 0.16.3
64
+ * [Add Logstash-compatible formatter](https://github.com/jnunemaker/httparty/pull/612)
65
+ * [Add support for headers specified with symbols](https://github.com/jnunemaker/httparty/pull/622)
66
+ * [Fix response object marshalling](https://github.com/jnunemaker/httparty/pull/618)
67
+ * [Add ability to send multipart, without passing file](https://github.com/jnunemaker/httparty/pull/615)
68
+ * [Fix detection of content_type for multipart payload](https://github.com/jnunemaker/httparty/pull/616)
69
+ * [Process dynamic headers before making actual request](https://github.com/jnunemaker/httparty/pull/606)
70
+ * [Fix multipart uploads with ActionDispatch::Http::UploadedFile TempFile by using original_filename](https://github.com/jnunemaker/httparty/pull/598)
71
+ * [Added support for lock and unlock http requests](https://github.com/jnunemaker/httparty/pull/596)
72
+
1
73
  ## 0.16.2
2
74
 
3
75
  * [Support ActionDispatch::Http::UploadedFile again](https://github.com/jnunemaker/httparty/pull/585)
data/Gemfile CHANGED
@@ -16,4 +16,9 @@ group :test do
16
16
  gem 'aruba'
17
17
  gem 'cucumber', '~> 2.3'
18
18
  gem 'webmock'
19
+ gem 'addressable'
20
+ end
21
+
22
+ group :development, :test do
23
+ gem 'pry'
19
24
  end
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # httparty
2
2
 
3
+ [![Build Status](https://travis-ci.org/jnunemaker/httparty.svg?branch=master)](https://travis-ci.org/jnunemaker/httparty)
4
+
3
5
  Makes http fun again! Ain't no party like a httparty, because a httparty don't stop.
4
6
 
5
7
  ## Install
@@ -10,7 +12,7 @@ gem install httparty
10
12
 
11
13
  ## Requirements
12
14
 
13
- * Ruby 2.0.0 or higher
15
+ * Ruby 2.3.0 or higher
14
16
  * multi_xml
15
17
  * You like to party!
16
18
 
@@ -46,7 +48,6 @@ puts stack_exchange.users
46
48
  ```
47
49
 
48
50
  See the [examples directory](http://github.com/jnunemaker/httparty/tree/master/examples) for even more goodies.
49
-
50
51
  ## Command Line Interface
51
52
 
52
53
  httparty also includes the executable `httparty` which can be
@@ -63,9 +64,8 @@ httparty "https://api.stackexchange.com/2.2/questions?site=stackoverflow"
63
64
  ## Help and Docs
64
65
 
65
66
  * [Docs](https://github.com/jnunemaker/httparty/tree/master/docs)
66
- * https://groups.google.com/forum/#!forum/httparty-gem
67
- * http://rdoc.info/projects/jnunemaker/httparty
68
- * http://stackoverflow.com/questions/tagged/httparty
67
+ * https://github.com/jnunemaker/httparty/discussions
68
+ * https://www.rubydoc.info/github/jnunemaker/httparty
69
69
 
70
70
  ## Contributing
71
71
 
data/docs/README.md CHANGED
@@ -9,7 +9,7 @@ Makes http fun again!
9
9
  ## Parsing JSON
10
10
  If the response Content Type is `application/json`, HTTParty will parse the response and return Ruby objects such as a hash or array. The default behavior for parsing JSON will return keys as strings. This can be supressed with the `format` option. To get hash keys as symbols:
11
11
 
12
- ```
12
+ ```ruby
13
13
  response = HTTParty.get('http://example.com', format: :plain)
14
14
  JSON.parse response, symbolize_names: true
15
15
  ```
@@ -70,7 +70,7 @@ class Client
70
70
  end
71
71
  ```
72
72
 
73
- You can also include this options with the call:
73
+ You can also include all of these options with the call:
74
74
 
75
75
  ```ruby
76
76
  class Client
@@ -79,14 +79,14 @@ class Client
79
79
  base_uri "https://example.com"
80
80
 
81
81
  def self.fetch
82
- get("/resources", pem: (File.read("#{File.expand_path('.')}/path/to/certs/cert.pem"), "123456")
82
+ get("/resources", pem: File.read("#{File.expand_path('.')}/path/to/certs/cert.pem"), pem_password: "123456")
83
83
  end
84
84
  end
85
85
  ```
86
86
 
87
87
  ### Avoid SSL verification
88
88
 
89
- In some cases you may want to skip SSL verification, because the entity that issue the certificate is not a valid one, but you still want to work with it. You can achieve this through:
89
+ In some cases you may want to skip SSL verification, because the entity that issued the certificate is not a valid one, but you still want to work with it. You can achieve this through:
90
90
 
91
91
  ```ruby
92
92
  # Skips SSL certificate verification
@@ -103,4 +103,69 @@ class Client
103
103
  # get("resources", verify_peer: false)
104
104
  end
105
105
  end
106
- ```
106
+ ```
107
+
108
+ ### HTTP Compression
109
+
110
+ The `Accept-Encoding` request header and `Content-Encoding` response header
111
+ are used to control compression (gzip, etc.) over the wire. Refer to
112
+ [RFC-2616](https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html) for details.
113
+ (For clarity: these headers are **not** used for character encoding i.e. `utf-8`
114
+ which is specified in the `Accept` and `Content-Type` headers.)
115
+
116
+ Unless you have specific requirements otherwise, we recommend to **not** set
117
+ set the `Accept-Encoding` header on HTTParty requests. In this case, `Net::HTTP`
118
+ will set a sensible default compression scheme and automatically decompress the response.
119
+
120
+ If you explicitly set `Accept-Encoding`, there be dragons:
121
+
122
+ * If the HTTP response `Content-Encoding` received on the wire is `gzip` or `deflate`,
123
+ `Net::HTTP` will automatically decompress it, and will omit `Content-Encoding`
124
+ from your `HTTParty::Response` headers.
125
+
126
+ * For encodings `br` (Brotli) or `compress` (LZW), HTTParty will automatically
127
+ decompress if you include the `brotli` or `ruby-lzws` gems respectively into your project.
128
+ **Warning:** Support for these encodings is experimental and not fully battle-tested.
129
+ Similar to above, if decompression succeeds, `Content-Encoding` will be omitted
130
+ from your `HTTParty::Response` headers.
131
+
132
+ * For other encodings, `HTTParty::Response#body` will return the raw uncompressed byte string,
133
+ and you'll need to inspect the `Content-Encoding` response header and decompress it yourself.
134
+ In this case, `HTTParty::Response#parsed_response` will be `nil`.
135
+
136
+ * Lastly, you may use the `skip_decompression` option to disable all automatic decompression
137
+ and always get `HTTParty::Response#body` in its raw form along with the `Content-Encoding` header.
138
+
139
+ ```ruby
140
+ # Accept-Encoding=gzip,deflate can be safely assumed to be auto-decompressed
141
+
142
+ res = HTTParty.get('https://example.com/test.json', headers: { 'Accept-Encoding' => 'gzip,deflate,identity' })
143
+ JSON.parse(res.body) # safe
144
+
145
+
146
+ # Accept-Encoding=br,compress requires third-party gems
147
+
148
+ require 'brotli'
149
+ require 'lzws'
150
+ res = HTTParty.get('https://example.com/test.json', headers: { 'Accept-Encoding' => 'br,compress' })
151
+ JSON.parse(res.body)
152
+
153
+
154
+ # Accept-Encoding=* may return unhandled Content-Encoding
155
+
156
+ res = HTTParty.get('https://example.com/test.json', headers: { 'Accept-Encoding' => '*' })
157
+ encoding = res.headers['Content-Encoding']
158
+ if encoding
159
+ JSON.parse(your_decompression_handling(res.body, encoding))
160
+ else
161
+ # Content-Encoding not present implies decompressed
162
+ JSON.parse(res.body)
163
+ end
164
+
165
+
166
+ # Gimme the raw data!
167
+
168
+ res = HTTParty.get('https://example.com/test.json', skip_decompression: true)
169
+ encoding = res.headers['Content-Encoding']
170
+ JSON.parse(your_decompression_handling(res.body, encoding))
171
+ ```
data/examples/README.md CHANGED
@@ -13,22 +13,22 @@
13
13
  * Creates a custom parser for XML using crack gem
14
14
  * Uses `get` request
15
15
 
16
- * [Create HTML Nokogiri parser](nokogiri_html_parser.rb)
16
+ * [Create HTML Nokogiri parser](nokogiri_html_parser.rb)
17
17
  * Adds Html as a format
18
18
  * passed the body of request to Nokogiri
19
-
19
+
20
20
  * [More Custom Parsers](custom_parsers.rb)
21
21
  * Create an additional parser for atom or make it the ONLY parser
22
-
22
+
23
23
  * [Basic Auth, Delicious](delicious.rb)
24
24
  * Basic Auth, shows how to merge those into options
25
25
  * Uses `get` requests
26
-
26
+
27
27
  * [Passing Headers, User Agent](headers_and_user_agents.rb)
28
28
  * Use the class method of Httparty
29
29
  * Pass the User-Agent in the headers
30
30
  * Uses `get` requests
31
-
31
+
32
32
  * [Basic Post Request](basic.rb)
33
33
  * Httparty included into poro class
34
34
  * Uses `post` requests
@@ -36,7 +36,7 @@
36
36
  * [Access Rubyurl Shortener](rubyurl.rb)
37
37
  * Httparty included into poro class
38
38
  * Uses `post` requests
39
-
39
+
40
40
  * [Add a custom log file](logging.rb)
41
41
  * create a log file and have httparty log requests
42
42
 
@@ -44,23 +44,23 @@
44
44
  * Httparty included into poro class
45
45
  * Creates methods for different endpoints
46
46
  * Uses `get` requests
47
-
47
+
48
48
  * [Accessing Tripit](tripit_sign_in.rb)
49
49
  * Httparty included into poro class
50
50
  * Example of using `debug_output` to see headers/urls passed
51
51
  * Getting and using Cookies
52
52
  * Uses `get` requests
53
-
53
+
54
54
  * [Accessing Twitter](twitter.rb)
55
55
  * Httparty included into poro class
56
56
  * Basic Auth
57
- * Loads settings from a config file
57
+ * Loads settings from a config file
58
58
  * Uses `get` requests
59
59
  * Uses `post` requests
60
-
60
+
61
61
  * [Accessing WhoIsMyRep](whoismyrep.rb)
62
62
  * Httparty included into poro class
63
- * Uses `get` requests
63
+ * Uses `get` requests
64
64
  * Two ways to pass params to get, inline on the url or in query hash
65
65
 
66
66
  * [Rescue Json Error](rescue_json.rb)
@@ -70,3 +70,20 @@
70
70
  * Uses `get` requests
71
71
  * Uses `stream_body` mode
72
72
  * Download file without using the memory
73
+
74
+ * [Microsoft graph](microsoft_graph.rb)
75
+ * Basic Auth
76
+ * Uses `post` requests
77
+ * Uses multipart
78
+
79
+ * [Multipart](multipart.rb)
80
+ * Multipart data upload _(with and without file)_
81
+
82
+ * [Uploading File](body_stream.rb)
83
+ * Uses `body_stream` to upload file
84
+
85
+ * [Accessing x509 Peer Certificate](peer_cert.rb)
86
+ * Provides access to the server's TLS certificate
87
+
88
+ * [Accessing IDNs](idn.rb)
89
+ * Uses a `get` request with an International domain names, which are Urls with emojis and non-ASCII characters such as accented letters.
data/examples/aaws.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  require 'rubygems'
2
2
  require 'active_support'
3
+ require 'active_support/core_ext/hash'
4
+ require 'active_support/core_ext/string'
3
5
 
4
6
  dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
5
7
  require File.join(dir, 'httparty')
@@ -13,14 +15,16 @@ module AAWS
13
15
  default_params Service: 'AWSECommerceService', Operation: 'ItemSearch', SearchIndex: 'Books'
14
16
 
15
17
  def initialize(key)
16
- self.class.default_params AWSAccessKeyId: key
18
+ @auth = { AWSAccessKeyId: key }
17
19
  end
18
20
 
19
21
  def search(options = {})
20
22
  raise ArgumentError, 'You must search for something' if options[:query].blank?
21
23
 
22
24
  # amazon uses nasty camelized query params
23
- options[:query] = options[:query].inject({}) { |h, q| h[q[0].to_s.camelize] = q[1]; h }
25
+ options[:query] = options[:query]
26
+ .reverse_merge(@auth)
27
+ .transform_keys { |k| k.to_s.camelize }
24
28
 
25
29
  # make a request and return the items (NOTE: this doesn't handle errors at this point)
26
30
  self.class.get('/onca/xml', options)['ItemSearchResponse']['Items']
@@ -0,0 +1,14 @@
1
+ # To upload file to a server use :body_stream
2
+
3
+ HTTParty.put(
4
+ 'http://localhost:3000/train',
5
+ body_stream: File.open('sample_configs/config_train_server_md.yml', 'r')
6
+ )
7
+
8
+
9
+ # Actually, it works with any IO object
10
+
11
+ HTTParty.put(
12
+ 'http://localhost:3000/train',
13
+ body_stream: StringIO.new('foo')
14
+ )
data/examples/idn.rb ADDED
@@ -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 Idn
6
+ include HTTParty
7
+ uri_adapter Addressable::URI
8
+ end
9
+
10
+ pp Idn.get("https://i❤️.ws/emojidomain/💎?format=json")
@@ -0,0 +1,52 @@
1
+ require 'httparty'
2
+
3
+ class MicrosoftGraph
4
+ MS_BASE_URL = "https://login.microsoftonline.com".freeze
5
+ TOKEN_REQUEST_PATH = "oauth2/v2.0/token".freeze
6
+
7
+ def initialize(tenant_id)
8
+ @tenant_id = tenant_id
9
+ end
10
+
11
+ # Make a request to the Microsoft Graph API, for instance https://graph.microsoft.com/v1.0/users
12
+ def request(url)
13
+ return false unless (token = bearer_token)
14
+
15
+ response = HTTParty.get(
16
+ url,
17
+ headers: {
18
+ Authorization: "Bearer #{token}"
19
+ }
20
+ )
21
+
22
+ return false unless response.code == 200
23
+
24
+ return JSON.parse(response.body)
25
+ end
26
+
27
+ private
28
+
29
+ # A post to the Microsoft Graph to get a bearer token for the specified tenant. In this example
30
+ # our Rails application has already been given permission to request these tokens by the admin of
31
+ # the specified tenant_id.
32
+ #
33
+ # See here for more information https://developer.microsoft.com/en-us/graph/docs/concepts/auth_v2_service
34
+ #
35
+ # This request also makes use of the multipart/form-data post body.
36
+ def bearer_token
37
+ response = HTTParty.post(
38
+ "#{MS_BASE_URL}/#{@tenant_id}/#{TOKEN_REQUEST_PATH}",
39
+ multipart: true,
40
+ body: {
41
+ client_id: Rails.application.credentials[Rails.env.to_sym][:microsoft_client_id],
42
+ client_secret: Rails.application.credentials[Rails.env.to_sym][:microsoft_client_secret],
43
+ scope: 'https://graph.microsoft.com/.default',
44
+ grant_type: 'client_credentials'
45
+ }
46
+ )
47
+
48
+ return false unless response.code == 200
49
+
50
+ JSON.parse(response.body)['access_token']
51
+ end
52
+ end
@@ -0,0 +1,22 @@
1
+ # If you are uploading file in params, multipart will used as content-type automatically
2
+
3
+ HTTParty.post(
4
+ 'http://localhost:3000/user',
5
+ body: {
6
+ name: 'Foo Bar',
7
+ email: 'example@email.com',
8
+ avatar: File.open('/full/path/to/avatar.jpg')
9
+ }
10
+ )
11
+
12
+
13
+ # However, you can force it yourself
14
+
15
+ HTTParty.post(
16
+ 'http://localhost:3000/user',
17
+ multipart: true,
18
+ body: {
19
+ name: 'Foo Bar',
20
+ email: 'example@email.com'
21
+ }
22
+ )
@@ -0,0 +1,9 @@
1
+ dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ require File.join(dir, 'httparty')
3
+
4
+ peer_cert = nil
5
+ HTTParty.get("https://www.example.com") do |fragment|
6
+ peer_cert ||= fragment.connection.peer_cert
7
+ end
8
+
9
+ puts "The server's certificate expires #{peer_cert.not_after}"
@@ -9,8 +9,14 @@ url = "https://cdn.kernel.org/pub/linux/kernel/v4.x/#{filename}"
9
9
 
10
10
  File.open(filename, "w") do |file|
11
11
  response = HTTParty.get(url, stream_body: true) do |fragment|
12
- print "."
13
- file.write(fragment)
12
+ if [301, 302].include?(fragment.code)
13
+ print "skip writing for redirect"
14
+ elsif fragment.code == 200
15
+ print "."
16
+ file.write(fragment)
17
+ else
18
+ raise StandardError, "Non-success status code while streaming #{fragment.code}"
19
+ end
14
20
  end
15
21
  end
16
22
  puts
data/httparty.gemspec CHANGED
@@ -9,13 +9,14 @@ Gem::Specification.new do |s|
9
9
  s.licenses = ['MIT']
10
10
  s.authors = ["John Nunemaker", "Sandro Turriate"]
11
11
  s.email = ["nunemaker@gmail.com"]
12
- s.homepage = "http://jnunemaker.github.com/httparty"
12
+ s.homepage = "https://github.com/jnunemaker/httparty"
13
13
  s.summary = 'Makes http fun! Also, makes consuming restful web services dead easy.'
14
14
  s.description = 'Makes http fun! Also, makes consuming restful web services dead easy.'
15
15
 
16
- s.required_ruby_version = '>= 2.0.0'
16
+ s.required_ruby_version = '>= 2.3.0'
17
17
 
18
18
  s.add_dependency 'multi_xml', ">= 0.5.2"
19
+ s.add_dependency('mime-types', "~> 3.0")
19
20
 
20
21
  # If this line is removed, all hard partying will cease.
21
22
  s.post_install_message = "When you HTTParty, you must party hard!"
@@ -24,7 +25,6 @@ Gem::Specification.new do |s|
24
25
  test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
25
26
 
26
27
  s.files = all_files - test_files
27
- s.test_files = test_files
28
28
  s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
29
29
  s.require_paths = ["lib"]
30
30
  end