httparty-responsibly 0.17.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.editorconfig +18 -0
- data/.gitignore +13 -0
- data/.rubocop.yml +92 -0
- data/.rubocop_todo.yml +124 -0
- data/.simplecov +1 -0
- data/.travis.yml +11 -0
- data/CONTRIBUTING.md +23 -0
- data/Changelog.md +509 -0
- data/Gemfile +24 -0
- data/Guardfile +16 -0
- data/MIT-LICENSE +20 -0
- data/README.md +78 -0
- data/Rakefile +10 -0
- data/bin/httparty +123 -0
- data/cucumber.yml +1 -0
- data/docs/README.md +106 -0
- data/examples/README.md +86 -0
- data/examples/aaws.rb +32 -0
- data/examples/basic.rb +28 -0
- data/examples/body_stream.rb +14 -0
- data/examples/crack.rb +19 -0
- data/examples/custom_parsers.rb +68 -0
- data/examples/delicious.rb +37 -0
- data/examples/google.rb +16 -0
- data/examples/headers_and_user_agents.rb +10 -0
- data/examples/logging.rb +36 -0
- data/examples/microsoft_graph.rb +52 -0
- data/examples/multipart.rb +22 -0
- data/examples/nokogiri_html_parser.rb +19 -0
- data/examples/peer_cert.rb +9 -0
- data/examples/rescue_json.rb +17 -0
- data/examples/rubyurl.rb +14 -0
- data/examples/stackexchange.rb +24 -0
- data/examples/stream_download.rb +26 -0
- data/examples/tripit_sign_in.rb +44 -0
- data/examples/twitter.rb +31 -0
- data/examples/whoismyrep.rb +10 -0
- data/httparty-responsibly.gemspec +27 -0
- data/lib/httparty.rb +668 -0
- data/lib/httparty/connection_adapter.rb +254 -0
- data/lib/httparty/cookie_hash.rb +21 -0
- data/lib/httparty/exceptions.rb +33 -0
- data/lib/httparty/hash_conversions.rb +69 -0
- data/lib/httparty/headers_processor.rb +30 -0
- data/lib/httparty/logger/apache_formatter.rb +45 -0
- data/lib/httparty/logger/curl_formatter.rb +91 -0
- data/lib/httparty/logger/logger.rb +28 -0
- data/lib/httparty/logger/logstash_formatter.rb +59 -0
- data/lib/httparty/module_inheritable_attributes.rb +56 -0
- data/lib/httparty/net_digest_auth.rb +136 -0
- data/lib/httparty/parser.rb +150 -0
- data/lib/httparty/request.rb +386 -0
- data/lib/httparty/request/body.rb +84 -0
- data/lib/httparty/request/multipart_boundary.rb +11 -0
- data/lib/httparty/response.rb +140 -0
- data/lib/httparty/response/headers.rb +33 -0
- data/lib/httparty/response_fragment.rb +19 -0
- data/lib/httparty/text_encoder.rb +70 -0
- data/lib/httparty/utils.rb +11 -0
- data/lib/httparty/version.rb +3 -0
- data/script/release +42 -0
- data/website/css/common.css +47 -0
- data/website/index.html +73 -0
- metadata +138 -0
data/Gemfile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
gemspec
|
3
|
+
|
4
|
+
gem 'rake'
|
5
|
+
gem 'mongrel', '1.2.0.pre2'
|
6
|
+
|
7
|
+
group :development do
|
8
|
+
gem 'guard'
|
9
|
+
gem 'guard-rspec'
|
10
|
+
gem 'guard-bundler'
|
11
|
+
end
|
12
|
+
|
13
|
+
group :test do
|
14
|
+
gem 'rspec', '~> 3.4'
|
15
|
+
gem 'simplecov', require: false
|
16
|
+
gem 'aruba'
|
17
|
+
gem 'cucumber', '~> 2.3'
|
18
|
+
gem 'webmock'
|
19
|
+
gem 'addressable'
|
20
|
+
end
|
21
|
+
|
22
|
+
group :development, :test do
|
23
|
+
gem 'pry'
|
24
|
+
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/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,78 @@
|
|
1
|
+
# httparty
|
2
|
+
|
3
|
+
An up-to-date fork of httparty, without the post-install nonsense.
|
4
|
+
|
5
|
+
## Install
|
6
|
+
|
7
|
+
```
|
8
|
+
gem install httparty
|
9
|
+
```
|
10
|
+
|
11
|
+
## Requirements
|
12
|
+
|
13
|
+
* Ruby 2.0.0 or higher
|
14
|
+
* multi_xml
|
15
|
+
|
16
|
+
## Examples
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
# Use the class methods to get down to business quickly
|
20
|
+
response = HTTParty.get('http://api.stackexchange.com/2.2/questions?site=stackoverflow')
|
21
|
+
|
22
|
+
puts response.body, response.code, response.message, response.headers.inspect
|
23
|
+
|
24
|
+
# Or wrap things up in your own class
|
25
|
+
class StackExchange
|
26
|
+
include HTTParty
|
27
|
+
base_uri 'api.stackexchange.com'
|
28
|
+
|
29
|
+
def initialize(service, page)
|
30
|
+
@options = { query: { site: service, page: page } }
|
31
|
+
end
|
32
|
+
|
33
|
+
def questions
|
34
|
+
self.class.get("/2.2/questions", @options)
|
35
|
+
end
|
36
|
+
|
37
|
+
def users
|
38
|
+
self.class.get("/2.2/users", @options)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
stack_exchange = StackExchange.new("stackoverflow", 1)
|
43
|
+
puts stack_exchange.questions
|
44
|
+
puts stack_exchange.users
|
45
|
+
```
|
46
|
+
|
47
|
+
See the [examples directory](http://github.com/jnunemaker/httparty/tree/master/examples) for even more goodies.
|
48
|
+
|
49
|
+
## Command Line Interface
|
50
|
+
|
51
|
+
httparty also includes the executable `httparty` which can be
|
52
|
+
used to query web services and examine the resulting output. By default
|
53
|
+
it will output the response as a pretty-printed Ruby object (useful for
|
54
|
+
grokking the structure of output). This can also be overridden to output
|
55
|
+
formatted XML or JSON. Execute `httparty --help` for all the
|
56
|
+
options. Below is an example of how easy it is.
|
57
|
+
|
58
|
+
```
|
59
|
+
httparty "https://api.stackexchange.com/2.2/questions?site=stackoverflow"
|
60
|
+
```
|
61
|
+
|
62
|
+
## Help and Docs
|
63
|
+
|
64
|
+
* [Docs](https://github.com/jnunemaker/httparty/tree/master/docs)
|
65
|
+
* https://groups.google.com/forum/#!forum/httparty-gem
|
66
|
+
* https://www.rubydoc.info/github/jnunemaker/httparty
|
67
|
+
* http://stackoverflow.com/questions/tagged/httparty
|
68
|
+
|
69
|
+
## Contributing
|
70
|
+
|
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.
|
data/Rakefile
ADDED
data/bin/httparty
ADDED
@@ -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
|
data/cucumber.yml
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
default: features --format progress
|
data/docs/README.md
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
# httparty
|
2
|
+
|
3
|
+
Makes http fun again!
|
4
|
+
|
5
|
+
## Table of contents
|
6
|
+
- [Parsing JSON](#parsing-json)
|
7
|
+
- [Working with SSL](#working-with-ssl)
|
8
|
+
|
9
|
+
## Parsing JSON
|
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
|
+
|
12
|
+
```ruby
|
13
|
+
response = HTTParty.get('http://example.com', format: :plain)
|
14
|
+
JSON.parse response, symbolize_names: true
|
15
|
+
```
|
16
|
+
|
17
|
+
## Working with SSL
|
18
|
+
|
19
|
+
You can use this guide to work with SSL certificates.
|
20
|
+
|
21
|
+
#### Using `pem` option
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
# Use this example if you are using a pem file
|
25
|
+
|
26
|
+
class Client
|
27
|
+
include HTTParty
|
28
|
+
|
29
|
+
base_uri "https://example.com"
|
30
|
+
pem File.read("#{File.expand_path('.')}/path/to/certs/cert.pem"), "123456"
|
31
|
+
end
|
32
|
+
```
|
33
|
+
|
34
|
+
#### Using `pkcs12` option
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
# Use this example if you are using a pkcs12 file
|
38
|
+
|
39
|
+
class Client
|
40
|
+
include HTTParty
|
41
|
+
|
42
|
+
base_uri "https://example.com"
|
43
|
+
pkcs12 File.read("#{File.expand_path('.')}/path/to/certs/cert.p12"), "123456"
|
44
|
+
end
|
45
|
+
```
|
46
|
+
|
47
|
+
#### Using `ssl_ca_file` option
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
# Use this example if you are using a pkcs12 file
|
51
|
+
|
52
|
+
class Client
|
53
|
+
include HTTParty
|
54
|
+
|
55
|
+
base_uri "https://example.com"
|
56
|
+
ssl_ca_file "#{File.expand_path('.')}/path/to/certs/cert.pem"
|
57
|
+
end
|
58
|
+
```
|
59
|
+
|
60
|
+
#### Using `ssl_ca_path` option
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
# Use this example if you are using a pkcs12 file
|
64
|
+
|
65
|
+
class Client
|
66
|
+
include HTTParty
|
67
|
+
|
68
|
+
base_uri "https://example.com"
|
69
|
+
ssl_ca_path '/path/to/certs'
|
70
|
+
end
|
71
|
+
```
|
72
|
+
|
73
|
+
You can also include this options with the call:
|
74
|
+
|
75
|
+
```ruby
|
76
|
+
class Client
|
77
|
+
include HTTParty
|
78
|
+
|
79
|
+
base_uri "https://example.com"
|
80
|
+
|
81
|
+
def self.fetch
|
82
|
+
get("/resources", pem: (File.read("#{File.expand_path('.')}/path/to/certs/cert.pem"), "123456")
|
83
|
+
end
|
84
|
+
end
|
85
|
+
```
|
86
|
+
|
87
|
+
### Avoid SSL verification
|
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:
|
90
|
+
|
91
|
+
```ruby
|
92
|
+
# Skips SSL certificate verification
|
93
|
+
|
94
|
+
class Client
|
95
|
+
include HTTParty
|
96
|
+
|
97
|
+
base_uri "https://example.com"
|
98
|
+
pem File.read("#{File.expand_path('.')}/path/to/certs/cert.pem"), "123456"
|
99
|
+
|
100
|
+
def self.fetch
|
101
|
+
get("/resources", verify: false)
|
102
|
+
# You can also use something like:
|
103
|
+
# get("resources", verify_peer: false)
|
104
|
+
end
|
105
|
+
end
|
106
|
+
```
|
data/examples/README.md
ADDED
@@ -0,0 +1,86 @@
|
|
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
|
+
|
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
|