httparty 0.13.7 → 0.24.2

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 (108) hide show
  1. checksums.yaml +5 -5
  2. data/.editorconfig +18 -0
  3. data/.github/dependabot.yml +6 -0
  4. data/.github/workflows/ci.yml +24 -0
  5. data/.gitignore +3 -0
  6. data/.rubocop_todo.yml +1 -1
  7. data/Changelog.md +624 -0
  8. data/Gemfile +11 -3
  9. data/Guardfile +3 -2
  10. data/README.md +18 -17
  11. data/bin/httparty +3 -1
  12. data/docs/README.md +223 -0
  13. data/examples/README.md +35 -12
  14. data/examples/aaws.rb +7 -3
  15. data/examples/body_stream.rb +14 -0
  16. data/examples/crack.rb +1 -1
  17. data/examples/custom_parsers.rb +5 -1
  18. data/examples/delicious.rb +4 -4
  19. data/examples/headers_and_user_agents.rb +7 -3
  20. data/examples/idn.rb +10 -0
  21. data/examples/logging.rb +4 -4
  22. data/examples/microsoft_graph.rb +52 -0
  23. data/examples/multipart.rb +35 -0
  24. data/examples/party_foul_mode.rb +90 -0
  25. data/examples/peer_cert.rb +9 -0
  26. data/examples/stackexchange.rb +1 -1
  27. data/examples/stream_download.rb +26 -0
  28. data/examples/tripit_sign_in.rb +17 -6
  29. data/examples/twitter.rb +2 -2
  30. data/examples/whoismyrep.rb +1 -1
  31. data/httparty.gemspec +9 -5
  32. data/lib/httparty/connection_adapter.rb +71 -24
  33. data/lib/httparty/cookie_hash.rb +10 -8
  34. data/lib/httparty/decompressor.rb +102 -0
  35. data/lib/httparty/exceptions.rb +42 -5
  36. data/lib/httparty/hash_conversions.rb +30 -8
  37. data/lib/httparty/headers_processor.rb +32 -0
  38. data/lib/httparty/logger/apache_formatter.rb +31 -6
  39. data/lib/httparty/logger/curl_formatter.rb +68 -23
  40. data/lib/httparty/logger/logger.rb +5 -1
  41. data/lib/httparty/logger/logstash_formatter.rb +62 -0
  42. data/lib/httparty/module_inheritable_attributes.rb +9 -9
  43. data/lib/httparty/net_digest_auth.rb +23 -21
  44. data/lib/httparty/parser.rb +28 -14
  45. data/lib/httparty/request/body.rb +125 -0
  46. data/lib/httparty/request/multipart_boundary.rb +13 -0
  47. data/lib/httparty/request/streaming_multipart_body.rb +190 -0
  48. data/lib/httparty/request.rb +224 -122
  49. data/lib/httparty/response/headers.rb +23 -19
  50. data/lib/httparty/response.rb +92 -13
  51. data/lib/httparty/response_fragment.rb +21 -0
  52. data/lib/httparty/text_encoder.rb +72 -0
  53. data/lib/httparty/utils.rb +13 -0
  54. data/lib/httparty/version.rb +3 -1
  55. data/lib/httparty.rb +118 -42
  56. data/script/release +4 -4
  57. data/website/css/common.css +1 -1
  58. metadata +50 -112
  59. data/.simplecov +0 -1
  60. data/.travis.yml +0 -7
  61. data/History +0 -390
  62. data/features/basic_authentication.feature +0 -20
  63. data/features/command_line.feature +0 -95
  64. data/features/deals_with_http_error_codes.feature +0 -26
  65. data/features/digest_authentication.feature +0 -30
  66. data/features/handles_compressed_responses.feature +0 -27
  67. data/features/handles_multiple_formats.feature +0 -57
  68. data/features/steps/env.rb +0 -27
  69. data/features/steps/httparty_response_steps.rb +0 -52
  70. data/features/steps/httparty_steps.rb +0 -43
  71. data/features/steps/mongrel_helper.rb +0 -127
  72. data/features/steps/remote_service_steps.rb +0 -90
  73. data/features/supports_read_timeout_option.feature +0 -13
  74. data/features/supports_redirection.feature +0 -22
  75. data/features/supports_timeout_option.feature +0 -13
  76. data/spec/fixtures/delicious.xml +0 -23
  77. data/spec/fixtures/empty.xml +0 -0
  78. data/spec/fixtures/google.html +0 -3
  79. data/spec/fixtures/ssl/generate.sh +0 -29
  80. data/spec/fixtures/ssl/generated/1fe462c2.0 +0 -16
  81. data/spec/fixtures/ssl/generated/bogushost.crt +0 -13
  82. data/spec/fixtures/ssl/generated/ca.crt +0 -16
  83. data/spec/fixtures/ssl/generated/ca.key +0 -15
  84. data/spec/fixtures/ssl/generated/selfsigned.crt +0 -14
  85. data/spec/fixtures/ssl/generated/server.crt +0 -13
  86. data/spec/fixtures/ssl/generated/server.key +0 -15
  87. data/spec/fixtures/ssl/openssl-exts.cnf +0 -9
  88. data/spec/fixtures/twitter.csv +0 -2
  89. data/spec/fixtures/twitter.json +0 -1
  90. data/spec/fixtures/twitter.xml +0 -403
  91. data/spec/fixtures/undefined_method_add_node_for_nil.xml +0 -2
  92. data/spec/httparty/connection_adapter_spec.rb +0 -468
  93. data/spec/httparty/cookie_hash_spec.rb +0 -83
  94. data/spec/httparty/exception_spec.rb +0 -38
  95. data/spec/httparty/hash_conversions_spec.rb +0 -41
  96. data/spec/httparty/logger/apache_formatter_spec.rb +0 -41
  97. data/spec/httparty/logger/curl_formatter_spec.rb +0 -18
  98. data/spec/httparty/logger/logger_spec.rb +0 -38
  99. data/spec/httparty/net_digest_auth_spec.rb +0 -230
  100. data/spec/httparty/parser_spec.rb +0 -173
  101. data/spec/httparty/request_spec.rb +0 -1073
  102. data/spec/httparty/response_spec.rb +0 -241
  103. data/spec/httparty/ssl_spec.rb +0 -74
  104. data/spec/httparty_spec.rb +0 -850
  105. data/spec/spec_helper.rb +0 -59
  106. data/spec/support/ssl_test_helper.rb +0 -47
  107. data/spec/support/ssl_test_server.rb +0 -80
  108. data/spec/support/stub_response.rb +0 -49
data/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
  # httparty
2
2
 
3
- Makes http fun again!
3
+ [![CI](https://github.com/jnunemaker/httparty/actions/workflows/ci.yml/badge.svg)](https://github.com/jnunemaker/httparty/actions/workflows/ci.yml)
4
+
5
+ Makes http fun again! Ain't no party like a httparty, because a httparty don't stop.
4
6
 
5
7
  ## Install
6
8
 
@@ -10,9 +12,8 @@ gem install httparty
10
12
 
11
13
  ## Requirements
12
14
 
13
- * Ruby 1.9.3 or higher
14
- * multi_xml
15
- * You like to party!
15
+ - Ruby 2.7.0 or higher
16
+ - You like to party!
16
17
 
17
18
  ## Examples
18
19
 
@@ -28,7 +29,7 @@ class StackExchange
28
29
  base_uri 'api.stackexchange.com'
29
30
 
30
31
  def initialize(service, page)
31
- @options = { query: {site: service, page: page} }
32
+ @options = { query: { site: service, page: page } }
32
33
  end
33
34
 
34
35
  def questions
@@ -45,7 +46,7 @@ puts stack_exchange.questions
45
46
  puts stack_exchange.users
46
47
  ```
47
48
 
48
- See the [examples directory](http://github.com/jnunemaker/httparty/tree/master/examples) for even more goodies.
49
+ See the [examples directory](http://github.com/jnunemaker/httparty/tree/main/examples) for even more goodies.
49
50
 
50
51
  ## Command Line Interface
51
52
 
@@ -62,17 +63,17 @@ httparty "https://api.stackexchange.com/2.2/questions?site=stackoverflow"
62
63
 
63
64
  ## Help and Docs
64
65
 
65
- * https://groups.google.com/forum/#!forum/httparty-gem
66
- * http://rdoc.info/projects/jnunemaker/httparty
67
- * http://stackoverflow.com/questions/tagged/httparty
66
+ - [Docs](https://github.com/jnunemaker/httparty/tree/main/docs)
67
+ - https://github.com/jnunemaker/httparty/discussions
68
+ - https://www.rubydoc.info/github/jnunemaker/httparty
68
69
 
69
70
  ## Contributing
70
71
 
71
- * Fork the project.
72
- * Run `bundle`
73
- * Run `bundle exec rake`
74
- * Make your feature addition or bug fix.
75
- * Add tests for it. This is important so I don't break it in a future version unintentionally.
76
- * Run `bundle exec rake` (No, REALLY :))
77
- * 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)
78
- * Send me a pull request. Bonus points for topic branches.
72
+ - Fork the project.
73
+ - Run `bundle`
74
+ - Run `bundle exec rake`
75
+ - Make your feature addition or bug fix.
76
+ - Add tests for it. This is important so I don't break it in a future version unintentionally.
77
+ - Run `bundle exec rake` (No, REALLY :))
78
+ - 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)
79
+ - Send me a pull request. Bonus points for topic branches.
data/bin/httparty CHANGED
@@ -103,9 +103,11 @@ else
103
103
  when :json
104
104
  begin
105
105
  require 'json'
106
- puts JSON.pretty_generate(response)
106
+ puts JSON.pretty_generate(response.parsed_response)
107
107
  rescue LoadError
108
108
  puts YAML.dump(response)
109
+ rescue JSON::JSONError
110
+ puts response.inspect
109
111
  end
110
112
  when :xml
111
113
  require 'rexml/document'
data/docs/README.md ADDED
@@ -0,0 +1,223 @@
1
+ # httparty
2
+
3
+ Makes http fun again!
4
+
5
+ ## Table of contents
6
+ - [Parsing JSON](#parsing-json)
7
+ - [File Uploads (Multipart)](#file-uploads-multipart)
8
+ - [Working with SSL](#working-with-ssl)
9
+
10
+ ## Parsing JSON
11
+ 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:
12
+
13
+ ```ruby
14
+ response = HTTParty.get('http://example.com', format: :plain)
15
+ JSON.parse response, symbolize_names: true
16
+ ```
17
+
18
+ ## Posting JSON
19
+ When using Content Type `application/json` with `POST`, `PUT` or `PATCH` requests, the body should be a string of valid JSON:
20
+
21
+ ```ruby
22
+ # With written JSON
23
+ HTTParty.post('http://example.com', body: "{\"foo\":\"bar\"}", headers: { 'Content-Type' => 'application/json' })
24
+
25
+ # Using JSON.generate
26
+ HTTParty.post('http://example.com', body: JSON.generate({ foo: 'bar' }), headers: { 'Content-Type' => 'application/json' })
27
+
28
+ # Using object.to_json
29
+ HTTParty.post('http://example.com', body: { foo: 'bar' }.to_json, headers: { 'Content-Type' => 'application/json' })
30
+ ```
31
+
32
+ ## File Uploads (Multipart)
33
+
34
+ When you include a `File` object in the body, HTTParty automatically uses `multipart/form-data` encoding:
35
+
36
+ ```ruby
37
+ HTTParty.post('http://example.com/upload',
38
+ body: {
39
+ name: 'Foo Bar',
40
+ avatar: File.open('/path/to/avatar.jpg')
41
+ }
42
+ )
43
+ ```
44
+
45
+ ### Streaming Uploads for Large Files
46
+
47
+ For large file uploads, you can enable streaming mode to reduce memory usage. Instead of loading the entire file into memory, HTTParty will stream the file in chunks:
48
+
49
+ ```ruby
50
+ HTTParty.post('http://example.com/upload',
51
+ body: {
52
+ name: 'Foo Bar',
53
+ avatar: File.open('/path/to/large_file.zip')
54
+ },
55
+ stream_body: true
56
+ )
57
+ ```
58
+
59
+ **Note:** Some servers may not handle streaming uploads correctly. If you encounter issues (e.g., 400 errors), try without the `stream_body` option.
60
+
61
+ ## Working with SSL
62
+
63
+ You can use this guide to work with SSL certificates.
64
+
65
+ #### Using `pem` option
66
+
67
+ ```ruby
68
+ # Use this example if you are using a pem file
69
+ # - cert.pem must contain the content of a PEM file having the private key appended (separated from the cert by a newline \n)
70
+ # - Use an empty string for the password if the cert is not password protected
71
+
72
+ class Client
73
+ include HTTParty
74
+
75
+ base_uri "https://example.com"
76
+ pem File.read("#{File.expand_path('.')}/path/to/certs/cert.pem"), "123456"
77
+ end
78
+ ```
79
+
80
+ #### Using `pkcs12` option
81
+
82
+ ```ruby
83
+ # Use this example if you are using a pkcs12 file
84
+
85
+ class Client
86
+ include HTTParty
87
+
88
+ base_uri "https://example.com"
89
+ pkcs12 File.read("#{File.expand_path('.')}/path/to/certs/cert.p12"), "123456"
90
+ end
91
+ ```
92
+
93
+ #### Using `ssl_ca_file` option
94
+
95
+ ```ruby
96
+ # Use this example if you are using a pkcs12 file
97
+
98
+ class Client
99
+ include HTTParty
100
+
101
+ base_uri "https://example.com"
102
+ ssl_ca_file "#{File.expand_path('.')}/path/to/certs/cert.pem"
103
+ end
104
+ ```
105
+
106
+ #### Using `ssl_ca_path` option
107
+
108
+ ```ruby
109
+ # Use this example if you are using a pkcs12 file
110
+
111
+ class Client
112
+ include HTTParty
113
+
114
+ base_uri "https://example.com"
115
+ ssl_ca_path '/path/to/certs'
116
+ end
117
+ ```
118
+
119
+ You can also include all of these options with the call:
120
+
121
+ ```ruby
122
+ class Client
123
+ include HTTParty
124
+
125
+ base_uri "https://example.com"
126
+
127
+ def self.fetch
128
+ get("/resources", pem: File.read("#{File.expand_path('.')}/path/to/certs/cert.pem"), pem_password: "123456")
129
+ end
130
+ end
131
+ ```
132
+
133
+ ### Avoid SSL verification
134
+
135
+ 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:
136
+
137
+ ```ruby
138
+ # Skips SSL certificate verification
139
+
140
+ class Client
141
+ include HTTParty
142
+
143
+ base_uri "https://example.com"
144
+ pem File.read("#{File.expand_path('.')}/path/to/certs/cert.pem"), "123456"
145
+
146
+ def self.fetch
147
+ get("/resources", verify: false)
148
+ # You can also use something like:
149
+ # get("resources", verify_peer: false)
150
+ end
151
+ end
152
+ ```
153
+
154
+ ### HTTP Compression
155
+
156
+ The `Accept-Encoding` request header and `Content-Encoding` response header
157
+ are used to control compression (gzip, etc.) over the wire. Refer to
158
+ [RFC-2616](https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html) for details.
159
+ (For clarity: these headers are **not** used for character encoding i.e. `utf-8`
160
+ which is specified in the `Accept` and `Content-Type` headers.)
161
+
162
+ Unless you have specific requirements otherwise, we recommend to **not** set
163
+ set the `Accept-Encoding` header on HTTParty requests. In this case, `Net::HTTP`
164
+ will set a sensible default compression scheme and automatically decompress the response.
165
+
166
+ If you explicitly set `Accept-Encoding`, there be dragons:
167
+
168
+ * If the HTTP response `Content-Encoding` received on the wire is `gzip` or `deflate`,
169
+ `Net::HTTP` will automatically decompress it, and will omit `Content-Encoding`
170
+ from your `HTTParty::Response` headers.
171
+
172
+ * For the following encodings, HTTParty will automatically decompress them if you include
173
+ the required gem into your project. Similar to above, if decompression succeeds,
174
+ `Content-Encoding` will be omitted from your `HTTParty::Response` headers.
175
+ **Warning:** Support for these encodings is experimental and not fully battle-tested.
176
+
177
+ | Content-Encoding | Required Gem |
178
+ | --- | --- |
179
+ | `br` (Brotli) | [brotli](https://rubygems.org/gems/brotli) |
180
+ | `compress` (LZW) | [ruby-lzws](https://rubygems.org/gems/ruby-lzws) |
181
+ | `zstd` (Zstandard) | [zstd-ruby](https://rubygems.org/gems/zstd-ruby) |
182
+
183
+ * For other encodings, `HTTParty::Response#body` will return the raw uncompressed byte string,
184
+ and you'll need to inspect the `Content-Encoding` response header and decompress it yourself.
185
+ In this case, `HTTParty::Response#parsed_response` will be `nil`.
186
+
187
+ * Lastly, you may use the `skip_decompression` option to disable all automatic decompression
188
+ and always get `HTTParty::Response#body` in its raw form along with the `Content-Encoding` header.
189
+
190
+ ```ruby
191
+ # Accept-Encoding=gzip,deflate can be safely assumed to be auto-decompressed
192
+
193
+ res = HTTParty.get('https://example.com/test.json', headers: { 'Accept-Encoding' => 'gzip,deflate,identity' })
194
+ JSON.parse(res.body) # safe
195
+
196
+
197
+ # Accept-Encoding=br,compress requires third-party gems
198
+
199
+ require 'brotli'
200
+ require 'lzws'
201
+ require 'zstd-ruby'
202
+ res = HTTParty.get('https://example.com/test.json', headers: { 'Accept-Encoding' => 'br,compress,zstd' })
203
+ JSON.parse(res.body)
204
+
205
+
206
+ # Accept-Encoding=* may return unhandled Content-Encoding
207
+
208
+ res = HTTParty.get('https://example.com/test.json', headers: { 'Accept-Encoding' => '*' })
209
+ encoding = res.headers['Content-Encoding']
210
+ if encoding
211
+ JSON.parse(your_decompression_handling(res.body, encoding))
212
+ else
213
+ # Content-Encoding not present implies decompressed
214
+ JSON.parse(res.body)
215
+ end
216
+
217
+
218
+ # Gimme the raw data!
219
+
220
+ res = HTTParty.get('https://example.com/test.json', skip_decompression: true)
221
+ encoding = res.headers['Content-Encoding']
222
+ JSON.parse(your_decompression_handling(res.body, encoding))
223
+ ```
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,24 +44,47 @@
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)
67
- * Rescue errors due to parsing response
67
+ * Rescue errors due to parsing response
68
+
69
+ * [Download file using stream mode](stream_download.rb)
70
+ * Uses `get` requests
71
+ * Uses `stream_body` mode
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
+ * Streaming uploads for large files with `stream_body: true`
82
+
83
+ * [Uploading File](body_stream.rb)
84
+ * Uses `body_stream` to upload file
85
+
86
+ * [Accessing x509 Peer Certificate](peer_cert.rb)
87
+ * Provides access to the server's TLS certificate
88
+
89
+ * [Accessing IDNs](idn.rb)
90
+ * 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']
@@ -29,4 +33,4 @@ module AAWS
29
33
  end
30
34
 
31
35
  aaws = AAWS::Book.new(config[:access_key])
32
- pp aaws.search(query: {title: 'Ruby On Rails'})
36
+ pp aaws.search(query: { title: 'Ruby On Rails' })
@@ -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/crack.rb CHANGED
@@ -16,4 +16,4 @@ class Rep
16
16
  end
17
17
 
18
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})
19
+ pp Rep.get('http://whoismyrepresentative.com/getall_mems.php', query: { zip: 46544 })
@@ -1,3 +1,7 @@
1
+ dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ require File.join(dir, 'httparty')
3
+ require 'pp'
4
+
1
5
  class ParseAtom
2
6
  include HTTParty
3
7
 
@@ -21,7 +25,7 @@ class OnlyParseAtom
21
25
 
22
26
  # Only support Atom
23
27
  class Parser::OnlyAtom < HTTParty::Parser
24
- SupportedFormats = {"application/atom+xml" => :atom}
28
+ SupportedFormats = { "application/atom+xml" => :atom }
25
29
 
26
30
  protected
27
31
 
@@ -8,7 +8,7 @@ class Delicious
8
8
  base_uri 'https://api.del.icio.us/v1'
9
9
 
10
10
  def initialize(u, p)
11
- @auth = {username: u, password: p}
11
+ @auth = { username: u, password: p }
12
12
  end
13
13
 
14
14
  # query params that filter the posts are:
@@ -17,7 +17,7 @@ class Delicious
17
17
  # url (optional). Filter by this url.
18
18
  # ie: posts(query: {tag: 'ruby'})
19
19
  def posts(options = {})
20
- options.merge!({basic_auth: @auth})
20
+ options.merge!({ basic_auth: @auth })
21
21
  self.class.get('/posts/get', options)
22
22
  end
23
23
 
@@ -25,13 +25,13 @@ class Delicious
25
25
  # tag (optional). Filter by this tag.
26
26
  # count (optional). Number of items to retrieve (Default:15, Maximum:100).
27
27
  def recent(options = {})
28
- options.merge!({basic_auth: @auth})
28
+ options.merge!({ basic_auth: @auth })
29
29
  self.class.get('/posts/recent', options)
30
30
  end
31
31
  end
32
32
 
33
33
  delicious = Delicious.new(config['username'], config['password'])
34
- pp delicious.posts(query: {tag: 'ruby'})
34
+ pp delicious.posts(query: { tag: 'ruby' })
35
35
  pp delicious.recent
36
36
 
37
37
  delicious.recent['posts']['post'].each { |post| puts post['href'] }
@@ -1,6 +1,10 @@
1
1
  # To send custom user agents to identify your application to a web service (or mask as a specific browser for testing), send "User-Agent" as a hash to headers as shown below.
2
2
 
3
- require 'httparty'
3
+ dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
4
+ require File.join(dir, 'httparty')
5
+ require 'pp'
4
6
 
5
- APPLICATION_NAME = "Httparty"
6
- response = HTTParty.get('http://example.com', headers: {"User-Agent" => APPLICATION_NAME})
7
+ response = HTTParty.get('http://example.com', {
8
+ headers: {"User-Agent" => "Httparty"},
9
+ debug_output: STDOUT, # To show that User-Agent is Httparty
10
+ })
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")
data/examples/logging.rb CHANGED
@@ -3,7 +3,7 @@ require File.join(dir, 'httparty')
3
3
  require 'logger'
4
4
  require 'pp'
5
5
 
6
- my_logger = Logger.new "httparty.log"
6
+ my_logger = Logger.new STDOUT
7
7
 
8
8
  my_logger.info "Logging can be used on the main HTTParty class. It logs redirects too."
9
9
  HTTParty.get "http://google.com", logger: my_logger
@@ -14,7 +14,7 @@ my_logger.info "It can be used also on a custom class."
14
14
 
15
15
  class Google
16
16
  include HTTParty
17
- logger ::Logger.new "httparty.log"
17
+ logger ::Logger.new STDOUT
18
18
  end
19
19
 
20
20
  Google.get "http://google.com"
@@ -22,7 +22,7 @@ Google.get "http://google.com"
22
22
  my_logger.info '*' * 70
23
23
 
24
24
  my_logger.info "The default formatter is :apache. The :curl formatter can also be used."
25
- my_logger.info "You can tell wich method to call on the logger too. It is info by default."
25
+ my_logger.info "You can tell which method to call on the logger too. It is info by default."
26
26
  HTTParty.get "http://google.com", logger: my_logger, log_level: :debug, log_format: :curl
27
27
 
28
28
  my_logger.info '*' * 70
@@ -30,7 +30,7 @@ my_logger.info '*' * 70
30
30
  my_logger.info "These configs are also available on custom classes."
31
31
  class Google
32
32
  include HTTParty
33
- logger ::Logger.new("httparty.log"), :debug, :curl
33
+ logger ::Logger.new(STDOUT), :debug, :curl
34
34
  end
35
35
 
36
36
  Google.get "http://google.com"
@@ -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,35 @@
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
+ )
23
+
24
+
25
+ # For large file uploads, use stream_body: true to reduce memory usage.
26
+ # Instead of loading the entire file into memory, HTTParty will stream it in chunks.
27
+ # Note: Some servers may not handle streaming uploads correctly.
28
+
29
+ HTTParty.post(
30
+ 'http://localhost:3000/upload',
31
+ body: {
32
+ document: File.open('/full/path/to/large_file.zip')
33
+ },
34
+ stream_body: true
35
+ )