piko-clean-pkg 0.0.1
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.
- checksums.yaml +7 -0
- data/httparty-0.24.2/CONTRIBUTING.md +23 -0
- data/httparty-0.24.2/Changelog.md +624 -0
- data/httparty-0.24.2/Gemfile +27 -0
- data/httparty-0.24.2/Guardfile +17 -0
- data/httparty-0.24.2/MIT-LICENSE +20 -0
- data/httparty-0.24.2/README.md +79 -0
- data/httparty-0.24.2/Rakefile +10 -0
- data/httparty-0.24.2/bin/httparty +123 -0
- data/httparty-0.24.2/cucumber.yml +1 -0
- data/httparty-0.24.2/docs/README.md +223 -0
- data/httparty-0.24.2/examples/README.md +90 -0
- data/httparty-0.24.2/examples/aaws.rb +36 -0
- data/httparty-0.24.2/examples/basic.rb +28 -0
- data/httparty-0.24.2/examples/body_stream.rb +14 -0
- data/httparty-0.24.2/examples/crack.rb +19 -0
- data/httparty-0.24.2/examples/custom_parsers.rb +68 -0
- data/httparty-0.24.2/examples/delicious.rb +37 -0
- data/httparty-0.24.2/examples/google.rb +16 -0
- data/httparty-0.24.2/examples/headers_and_user_agents.rb +10 -0
- data/httparty-0.24.2/examples/idn.rb +10 -0
- data/httparty-0.24.2/examples/logging.rb +36 -0
- data/httparty-0.24.2/examples/microsoft_graph.rb +52 -0
- data/httparty-0.24.2/examples/multipart.rb +35 -0
- data/httparty-0.24.2/examples/nokogiri_html_parser.rb +19 -0
- data/httparty-0.24.2/examples/party_foul_mode.rb +90 -0
- data/httparty-0.24.2/examples/peer_cert.rb +9 -0
- data/httparty-0.24.2/examples/rescue_json.rb +17 -0
- data/httparty-0.24.2/examples/rubyurl.rb +14 -0
- data/httparty-0.24.2/examples/stackexchange.rb +24 -0
- data/httparty-0.24.2/examples/stream_download.rb +26 -0
- data/httparty-0.24.2/examples/tripit_sign_in.rb +44 -0
- data/httparty-0.24.2/examples/twitter.rb +31 -0
- data/httparty-0.24.2/examples/whoismyrep.rb +10 -0
- data/httparty-0.24.2/httparty.gemspec +32 -0
- data/httparty-0.24.2/lib/httparty/connection_adapter.rb +237 -0
- data/httparty-0.24.2/lib/httparty/cookie_hash.rb +23 -0
- data/httparty-0.24.2/lib/httparty/decompressor.rb +102 -0
- data/httparty-0.24.2/lib/httparty/exceptions.rb +66 -0
- data/httparty-0.24.2/lib/httparty/hash_conversions.rb +71 -0
- data/httparty-0.24.2/lib/httparty/headers_processor.rb +32 -0
- data/httparty-0.24.2/lib/httparty/logger/apache_formatter.rb +47 -0
- data/httparty-0.24.2/lib/httparty/logger/curl_formatter.rb +93 -0
- data/httparty-0.24.2/lib/httparty/logger/logger.rb +30 -0
- data/httparty-0.24.2/lib/httparty/logger/logstash_formatter.rb +62 -0
- data/httparty-0.24.2/lib/httparty/module_inheritable_attributes.rb +56 -0
- data/httparty-0.24.2/lib/httparty/net_digest_auth.rb +135 -0
- data/httparty-0.24.2/lib/httparty/parser.rb +157 -0
- data/httparty-0.24.2/lib/httparty/request/body.rb +125 -0
- data/httparty-0.24.2/lib/httparty/request/multipart_boundary.rb +13 -0
- data/httparty-0.24.2/lib/httparty/request/streaming_multipart_body.rb +190 -0
- data/httparty-0.24.2/lib/httparty/request.rb +466 -0
- data/httparty-0.24.2/lib/httparty/response/headers.rb +35 -0
- data/httparty-0.24.2/lib/httparty/response.rb +156 -0
- data/httparty-0.24.2/lib/httparty/response_fragment.rb +21 -0
- data/httparty-0.24.2/lib/httparty/text_encoder.rb +72 -0
- data/httparty-0.24.2/lib/httparty/utils.rb +13 -0
- data/httparty-0.24.2/lib/httparty/version.rb +5 -0
- data/httparty-0.24.2/lib/httparty.rb +699 -0
- data/httparty-0.24.2/script/release +42 -0
- data/httparty-0.24.2/website/css/common.css +47 -0
- data/httparty-0.24.2/website/index.html +73 -0
- data/piko-clean-pkg.gemspec +12 -0
- metadata +103 -0
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
rspec_options = {
|
|
2
|
+
all_after_pass: false,
|
|
3
|
+
all_on_start: false,
|
|
4
|
+
failed_mode: :keep,
|
|
5
|
+
cmd: 'bundle exec rspec',
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
guard 'rspec', rspec_options do
|
|
9
|
+
watch(%r{^spec/.+_spec\.rb$})
|
|
10
|
+
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
|
|
11
|
+
watch('spec/spec_helper.rb') { "spec" }
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
guard 'bundler' do
|
|
15
|
+
watch('Gemfile')
|
|
16
|
+
watch(/^.+\.gemspec/)
|
|
17
|
+
end
|
|
@@ -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.
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
# httparty
|
|
2
|
+
|
|
3
|
+
[](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.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```
|
|
10
|
+
gem install httparty
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Requirements
|
|
14
|
+
|
|
15
|
+
- Ruby 2.7.0 or higher
|
|
16
|
+
- You like to party!
|
|
17
|
+
|
|
18
|
+
## Examples
|
|
19
|
+
|
|
20
|
+
```ruby
|
|
21
|
+
# Use the class methods to get down to business quickly
|
|
22
|
+
response = HTTParty.get('http://api.stackexchange.com/2.2/questions?site=stackoverflow')
|
|
23
|
+
|
|
24
|
+
puts response.body, response.code, response.message, response.headers.inspect
|
|
25
|
+
|
|
26
|
+
# Or wrap things up in your own class
|
|
27
|
+
class StackExchange
|
|
28
|
+
include HTTParty
|
|
29
|
+
base_uri 'api.stackexchange.com'
|
|
30
|
+
|
|
31
|
+
def initialize(service, page)
|
|
32
|
+
@options = { query: { site: service, page: page } }
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def questions
|
|
36
|
+
self.class.get("/2.2/questions", @options)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def users
|
|
40
|
+
self.class.get("/2.2/users", @options)
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
stack_exchange = StackExchange.new("stackoverflow", 1)
|
|
45
|
+
puts stack_exchange.questions
|
|
46
|
+
puts stack_exchange.users
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
See the [examples directory](http://github.com/jnunemaker/httparty/tree/main/examples) for even more goodies.
|
|
50
|
+
|
|
51
|
+
## Command Line Interface
|
|
52
|
+
|
|
53
|
+
httparty also includes the executable `httparty` which can be
|
|
54
|
+
used to query web services and examine the resulting output. By default
|
|
55
|
+
it will output the response as a pretty-printed Ruby object (useful for
|
|
56
|
+
grokking the structure of output). This can also be overridden to output
|
|
57
|
+
formatted XML or JSON. Execute `httparty --help` for all the
|
|
58
|
+
options. Below is an example of how easy it is.
|
|
59
|
+
|
|
60
|
+
```
|
|
61
|
+
httparty "https://api.stackexchange.com/2.2/questions?site=stackoverflow"
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## Help and Docs
|
|
65
|
+
|
|
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
|
|
69
|
+
|
|
70
|
+
## Contributing
|
|
71
|
+
|
|
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.
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require "optparse"
|
|
4
|
+
require "pp"
|
|
5
|
+
|
|
6
|
+
$LOAD_PATH.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: #{$PROGRAM_NAME} [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
|
+
|
|
66
|
+
o.on("--version", "Show HTTParty version") do |ver|
|
|
67
|
+
puts "Version: #{HTTParty::VERSION}"
|
|
68
|
+
exit
|
|
69
|
+
end
|
|
70
|
+
end.parse!
|
|
71
|
+
|
|
72
|
+
if ARGV.empty?
|
|
73
|
+
STDERR.puts "You need to provide a URL"
|
|
74
|
+
STDERR.puts "USAGE: #{$PROGRAM_NAME} [options] [url]"
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def dump_headers(response)
|
|
78
|
+
resp_type = Net::HTTPResponse::CODE_TO_OBJ[response.code.to_s]
|
|
79
|
+
puts "#{response.code} #{resp_type.to_s.sub(/^Net::HTTP/, '')}"
|
|
80
|
+
response.headers.each do |n, v|
|
|
81
|
+
puts "#{n}: #{v}"
|
|
82
|
+
end
|
|
83
|
+
puts
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
if opts[:verbose]
|
|
87
|
+
puts "#{opts[:action].to_s.upcase} #{ARGV.first}"
|
|
88
|
+
opts[:headers].each do |n, v|
|
|
89
|
+
puts "#{n}: #{v}"
|
|
90
|
+
end
|
|
91
|
+
puts
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
response = HTTParty.send(opts[:action], ARGV.first, opts)
|
|
95
|
+
if opts[:output_format].nil?
|
|
96
|
+
dump_headers(response) if opts[:verbose]
|
|
97
|
+
pp response
|
|
98
|
+
else
|
|
99
|
+
print_format = opts[:output_format]
|
|
100
|
+
dump_headers(response) if opts[:verbose]
|
|
101
|
+
|
|
102
|
+
case opts[:output_format]
|
|
103
|
+
when :json
|
|
104
|
+
begin
|
|
105
|
+
require 'json'
|
|
106
|
+
puts JSON.pretty_generate(response.parsed_response)
|
|
107
|
+
rescue LoadError
|
|
108
|
+
puts YAML.dump(response)
|
|
109
|
+
rescue JSON::JSONError
|
|
110
|
+
puts response.inspect
|
|
111
|
+
end
|
|
112
|
+
when :xml
|
|
113
|
+
require 'rexml/document'
|
|
114
|
+
REXML::Document.new(response.body).write(STDOUT, 2)
|
|
115
|
+
puts
|
|
116
|
+
when :csv
|
|
117
|
+
require 'csv'
|
|
118
|
+
puts CSV.parse(response.body).map(&:to_s)
|
|
119
|
+
else
|
|
120
|
+
puts response
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
exit false if opts[:response_code] && response.code >= 400
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
default: features --format progress
|
|
@@ -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
|
+
```
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
## Examples
|
|
2
|
+
|
|
3
|
+
* [Amazon Book Search](aaws.rb)
|
|
4
|
+
* Httparty included into poro class
|
|
5
|
+
* Uses `get` requests
|
|
6
|
+
* Transforms query params to uppercased params
|
|
7
|
+
|
|
8
|
+
* [Google Search](google.rb)
|
|
9
|
+
* Httparty included into poro class
|
|
10
|
+
* Uses `get` requests
|
|
11
|
+
|
|
12
|
+
* [Crack Custom Parser](crack.rb)
|
|
13
|
+
* Creates a custom parser for XML using crack gem
|
|
14
|
+
* Uses `get` request
|
|
15
|
+
|
|
16
|
+
* [Create HTML Nokogiri parser](nokogiri_html_parser.rb)
|
|
17
|
+
* Adds Html as a format
|
|
18
|
+
* passed the body of request to Nokogiri
|
|
19
|
+
|
|
20
|
+
* [More Custom Parsers](custom_parsers.rb)
|
|
21
|
+
* Create an additional parser for atom or make it the ONLY parser
|
|
22
|
+
|
|
23
|
+
* [Basic Auth, Delicious](delicious.rb)
|
|
24
|
+
* Basic Auth, shows how to merge those into options
|
|
25
|
+
* Uses `get` requests
|
|
26
|
+
|
|
27
|
+
* [Passing Headers, User Agent](headers_and_user_agents.rb)
|
|
28
|
+
* Use the class method of Httparty
|
|
29
|
+
* Pass the User-Agent in the headers
|
|
30
|
+
* Uses `get` requests
|
|
31
|
+
|
|
32
|
+
* [Basic Post Request](basic.rb)
|
|
33
|
+
* Httparty included into poro class
|
|
34
|
+
* Uses `post` requests
|
|
35
|
+
|
|
36
|
+
* [Access Rubyurl Shortener](rubyurl.rb)
|
|
37
|
+
* Httparty included into poro class
|
|
38
|
+
* Uses `post` requests
|
|
39
|
+
|
|
40
|
+
* [Add a custom log file](logging.rb)
|
|
41
|
+
* create a log file and have httparty log requests
|
|
42
|
+
|
|
43
|
+
* [Accessing StackExchange](stackexchange.rb)
|
|
44
|
+
* Httparty included into poro class
|
|
45
|
+
* Creates methods for different endpoints
|
|
46
|
+
* Uses `get` requests
|
|
47
|
+
|
|
48
|
+
* [Accessing Tripit](tripit_sign_in.rb)
|
|
49
|
+
* Httparty included into poro class
|
|
50
|
+
* Example of using `debug_output` to see headers/urls passed
|
|
51
|
+
* Getting and using Cookies
|
|
52
|
+
* Uses `get` requests
|
|
53
|
+
|
|
54
|
+
* [Accessing Twitter](twitter.rb)
|
|
55
|
+
* Httparty included into poro class
|
|
56
|
+
* Basic Auth
|
|
57
|
+
* Loads settings from a config file
|
|
58
|
+
* Uses `get` requests
|
|
59
|
+
* Uses `post` requests
|
|
60
|
+
|
|
61
|
+
* [Accessing WhoIsMyRep](whoismyrep.rb)
|
|
62
|
+
* Httparty included into poro class
|
|
63
|
+
* Uses `get` requests
|
|
64
|
+
* Two ways to pass params to get, inline on the url or in query hash
|
|
65
|
+
|
|
66
|
+
* [Rescue Json Error](rescue_json.rb)
|
|
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.
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
require 'rubygems'
|
|
2
|
+
require 'active_support'
|
|
3
|
+
require 'active_support/core_ext/hash'
|
|
4
|
+
require 'active_support/core_ext/string'
|
|
5
|
+
|
|
6
|
+
dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
|
|
7
|
+
require File.join(dir, 'httparty')
|
|
8
|
+
require 'pp'
|
|
9
|
+
config = YAML.load(File.read(File.join(ENV['HOME'], '.aaws')))
|
|
10
|
+
|
|
11
|
+
module AAWS
|
|
12
|
+
class Book
|
|
13
|
+
include HTTParty
|
|
14
|
+
base_uri 'http://ecs.amazonaws.com'
|
|
15
|
+
default_params Service: 'AWSECommerceService', Operation: 'ItemSearch', SearchIndex: 'Books'
|
|
16
|
+
|
|
17
|
+
def initialize(key)
|
|
18
|
+
@auth = { AWSAccessKeyId: key }
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def search(options = {})
|
|
22
|
+
raise ArgumentError, 'You must search for something' if options[:query].blank?
|
|
23
|
+
|
|
24
|
+
# amazon uses nasty camelized query params
|
|
25
|
+
options[:query] = options[:query]
|
|
26
|
+
.reverse_merge(@auth)
|
|
27
|
+
.transform_keys { |k| k.to_s.camelize }
|
|
28
|
+
|
|
29
|
+
# make a request and return the items (NOTE: this doesn't handle errors at this point)
|
|
30
|
+
self.class.get('/onca/xml', options)['ItemSearchResponse']['Items']
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
aaws = AAWS::Book.new(config[:access_key])
|
|
36
|
+
pp aaws.search(query: { title: 'Ruby On Rails' })
|
|
@@ -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)
|
|
@@ -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
|
+
)
|
|
@@ -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 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 })
|