httpserious 0.13.5.lstoll1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (100) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +11 -0
  3. data/.rubocop.yml +92 -0
  4. data/.rubocop_todo.yml +124 -0
  5. data/.simplecov +1 -0
  6. data/.travis.yml +7 -0
  7. data/CONTRIBUTING.md +23 -0
  8. data/Gemfile +19 -0
  9. data/Guardfile +16 -0
  10. data/History +370 -0
  11. data/MIT-LICENSE +20 -0
  12. data/README.md +78 -0
  13. data/Rakefile +10 -0
  14. data/bin/httparty +116 -0
  15. data/cucumber.yml +1 -0
  16. data/examples/README.md +67 -0
  17. data/examples/aaws.rb +32 -0
  18. data/examples/basic.rb +28 -0
  19. data/examples/crack.rb +19 -0
  20. data/examples/custom_parsers.rb +64 -0
  21. data/examples/delicious.rb +37 -0
  22. data/examples/google.rb +16 -0
  23. data/examples/headers_and_user_agents.rb +6 -0
  24. data/examples/logging.rb +36 -0
  25. data/examples/nokogiri_html_parser.rb +19 -0
  26. data/examples/rescue_json.rb +17 -0
  27. data/examples/rubyurl.rb +14 -0
  28. data/examples/stackexchange.rb +24 -0
  29. data/examples/tripit_sign_in.rb +33 -0
  30. data/examples/twitter.rb +31 -0
  31. data/examples/whoismyrep.rb +10 -0
  32. data/features/basic_authentication.feature +20 -0
  33. data/features/command_line.feature +90 -0
  34. data/features/deals_with_http_error_codes.feature +26 -0
  35. data/features/digest_authentication.feature +20 -0
  36. data/features/handles_compressed_responses.feature +27 -0
  37. data/features/handles_multiple_formats.feature +57 -0
  38. data/features/steps/env.rb +27 -0
  39. data/features/steps/httparty_response_steps.rb +52 -0
  40. data/features/steps/httparty_steps.rb +43 -0
  41. data/features/steps/mongrel_helper.rb +94 -0
  42. data/features/steps/remote_service_steps.rb +86 -0
  43. data/features/supports_read_timeout_option.feature +13 -0
  44. data/features/supports_redirection.feature +22 -0
  45. data/features/supports_timeout_option.feature +13 -0
  46. data/httparty.gemspec +28 -0
  47. data/httpserious.gemspec +25 -0
  48. data/lib/httparty.rb +612 -0
  49. data/lib/httparty/connection_adapter.rb +190 -0
  50. data/lib/httparty/cookie_hash.rb +21 -0
  51. data/lib/httparty/exceptions.rb +29 -0
  52. data/lib/httparty/hash_conversions.rb +49 -0
  53. data/lib/httparty/logger/apache_formatter.rb +22 -0
  54. data/lib/httparty/logger/curl_formatter.rb +48 -0
  55. data/lib/httparty/logger/logger.rb +26 -0
  56. data/lib/httparty/module_inheritable_attributes.rb +56 -0
  57. data/lib/httparty/net_digest_auth.rb +117 -0
  58. data/lib/httparty/parser.rb +141 -0
  59. data/lib/httparty/request.rb +361 -0
  60. data/lib/httparty/response.rb +77 -0
  61. data/lib/httparty/response/headers.rb +31 -0
  62. data/lib/httparty/version.rb +3 -0
  63. data/lib/httpserious.rb +1 -0
  64. data/script/release +42 -0
  65. data/spec/fixtures/delicious.xml +23 -0
  66. data/spec/fixtures/empty.xml +0 -0
  67. data/spec/fixtures/google.html +3 -0
  68. data/spec/fixtures/ssl/generate.sh +29 -0
  69. data/spec/fixtures/ssl/generated/1fe462c2.0 +16 -0
  70. data/spec/fixtures/ssl/generated/bogushost.crt +13 -0
  71. data/spec/fixtures/ssl/generated/ca.crt +16 -0
  72. data/spec/fixtures/ssl/generated/ca.key +15 -0
  73. data/spec/fixtures/ssl/generated/selfsigned.crt +14 -0
  74. data/spec/fixtures/ssl/generated/server.crt +13 -0
  75. data/spec/fixtures/ssl/generated/server.key +15 -0
  76. data/spec/fixtures/ssl/openssl-exts.cnf +9 -0
  77. data/spec/fixtures/twitter.csv +2 -0
  78. data/spec/fixtures/twitter.json +1 -0
  79. data/spec/fixtures/twitter.xml +403 -0
  80. data/spec/fixtures/undefined_method_add_node_for_nil.xml +2 -0
  81. data/spec/httparty/connection_adapter_spec.rb +468 -0
  82. data/spec/httparty/cookie_hash_spec.rb +83 -0
  83. data/spec/httparty/exception_spec.rb +38 -0
  84. data/spec/httparty/hash_conversions_spec.rb +41 -0
  85. data/spec/httparty/logger/apache_formatter_spec.rb +41 -0
  86. data/spec/httparty/logger/curl_formatter_spec.rb +18 -0
  87. data/spec/httparty/logger/logger_spec.rb +38 -0
  88. data/spec/httparty/net_digest_auth_spec.rb +191 -0
  89. data/spec/httparty/parser_spec.rb +167 -0
  90. data/spec/httparty/request_spec.rb +872 -0
  91. data/spec/httparty/response_spec.rb +241 -0
  92. data/spec/httparty/ssl_spec.rb +74 -0
  93. data/spec/httparty_spec.rb +823 -0
  94. data/spec/spec_helper.rb +59 -0
  95. data/spec/support/ssl_test_helper.rb +47 -0
  96. data/spec/support/ssl_test_server.rb +80 -0
  97. data/spec/support/stub_response.rb +43 -0
  98. data/website/css/common.css +47 -0
  99. data/website/index.html +73 -0
  100. metadata +219 -0
@@ -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,78 @@
1
+ # httparty
2
+
3
+ Makes http fun again!
4
+
5
+ ## Install
6
+
7
+ ```
8
+ gem install httparty
9
+ ```
10
+
11
+ ## Requirements
12
+
13
+ * Ruby 1.9.3 or higher
14
+ * multi_xml
15
+ * You like to party!
16
+
17
+ ## Examples
18
+
19
+ ```ruby
20
+ # Use the class methods to get down to business quickly
21
+ response = HTTParty.get('https://api.stackexchange.com/2.2/questions?site=stackoverflow')
22
+
23
+ puts response.body, response.code, response.message, response.headers.inspect
24
+
25
+ # Or wrap things up in your own class
26
+ class StackExchange
27
+ include HTTParty
28
+ base_uri 'api.stackexchange.com'
29
+
30
+ def initialize(service, page)
31
+ @options = { query: {site: service, page: page} }
32
+ end
33
+
34
+ def questions
35
+ self.class.get("/2.2/questions", @options)
36
+ end
37
+
38
+ def users
39
+ self.class.get("/2.2/users", @options)
40
+ end
41
+ end
42
+
43
+ stack_exchange = StackExchange.new("stackoverflow", 1)
44
+ puts stack_exchange.questions
45
+ puts stack_exchange.users
46
+ ```
47
+
48
+ See the [examples directory](http://github.com/jnunemaker/httparty/tree/master/examples) for even more goodies.
49
+
50
+ ## Command Line Interface
51
+
52
+ httparty also includes the executable `httparty` which can be
53
+ used to query web services and examine the resulting output. By default
54
+ it will output the response as a pretty-printed Ruby object (useful for
55
+ grokking the structure of output). This can also be overridden to output
56
+ formatted XML or JSON. Execute `httparty --help` for all the
57
+ options. Below is an example of how easy it is.
58
+
59
+ ```
60
+ httparty "https://api.stackexchange.com/2.2/questions?site=stackoverflow"
61
+ ```
62
+
63
+ ## Help and Docs
64
+
65
+ * https://groups.google.com/forum/#!forum/httparty-gem
66
+ * http://rdoc.info/projects/jnunemaker/httparty
67
+ * 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.
@@ -0,0 +1,10 @@
1
+ begin
2
+ require 'rspec/core/rake_task'
3
+ RSpec::Core::RakeTask.new(:spec)
4
+ rescue LoadError
5
+ end
6
+
7
+ require 'cucumber/rake/task'
8
+ Cucumber::Rake::Task.new(:features)
9
+
10
+ task default: [:spec, :features]
@@ -0,0 +1,116 @@
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
+ end.parse!
66
+
67
+ if ARGV.empty?
68
+ STDERR.puts "You need to provide a URL"
69
+ STDERR.puts "USAGE: #{$PROGRAM_NAME} [options] [url]"
70
+ end
71
+
72
+ def dump_headers(response)
73
+ resp_type = Net::HTTPResponse::CODE_TO_OBJ[response.code.to_s]
74
+ puts "#{response.code} #{resp_type.to_s.sub(/^Net::HTTP/, '')}"
75
+ response.headers.each do |n, v|
76
+ puts "#{n}: #{v}"
77
+ end
78
+ puts
79
+ end
80
+
81
+ if opts[:verbose]
82
+ puts "#{opts[:action].to_s.upcase} #{ARGV.first}"
83
+ opts[:headers].each do |n, v|
84
+ puts "#{n}: #{v}"
85
+ end
86
+ puts
87
+ end
88
+
89
+ response = HTTParty.send(opts[:action], ARGV.first, opts)
90
+ if opts[:output_format].nil?
91
+ dump_headers(response) if opts[:verbose]
92
+ pp response
93
+ else
94
+ print_format = opts[:output_format]
95
+ dump_headers(response) if opts[:verbose]
96
+
97
+ case opts[:output_format]
98
+ when :json
99
+ begin
100
+ require 'json'
101
+ puts JSON.pretty_generate(response)
102
+ rescue LoadError
103
+ puts YAML.dump(response)
104
+ end
105
+ when :xml
106
+ require 'rexml/document'
107
+ REXML::Document.new(response.body).write(STDOUT, 2)
108
+ puts
109
+ when :csv
110
+ require 'csv'
111
+ puts CSV.parse(response.body).map(&:to_s)
112
+ else
113
+ puts response
114
+ end
115
+ end
116
+ exit false if opts[:response_code] && response.code >= 400
@@ -0,0 +1 @@
1
+ default: features --format progress
@@ -0,0 +1,67 @@
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
@@ -0,0 +1,32 @@
1
+ require 'rubygems'
2
+ require 'active_support'
3
+
4
+ dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
5
+ require File.join(dir, 'httparty')
6
+ require 'pp'
7
+ config = YAML.load(File.read(File.join(ENV['HOME'], '.aaws')))
8
+
9
+ module AAWS
10
+ class Book
11
+ include HTTParty
12
+ base_uri 'http://ecs.amazonaws.com'
13
+ default_params Service: 'AWSECommerceService', Operation: 'ItemSearch', SearchIndex: 'Books'
14
+
15
+ def initialize(key)
16
+ self.class.default_params AWSAccessKeyId: key
17
+ end
18
+
19
+ def search(options = {})
20
+ raise ArgumentError, 'You must search for something' if options[:query].blank?
21
+
22
+ # amazon uses nasty camelized query params
23
+ options[:query] = options[:query].inject({}) { |h, q| h[q[0].to_s.camelize] = q[1]; h }
24
+
25
+ # make a request and return the items (NOTE: this doesn't handle errors at this point)
26
+ self.class.get('/onca/xml', options)['ItemSearchResponse']['Items']
27
+ end
28
+ end
29
+ end
30
+
31
+ aaws = AAWS::Book.new(config[:access_key])
32
+ pp aaws.search(query: {title: 'Ruby On Rails'})
@@ -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,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})
@@ -0,0 +1,64 @@
1
+ class ParseAtom
2
+ include HTTParty
3
+
4
+ # Support Atom along with the default parsers: xml, json, etc.
5
+ class Parser::Atom < HTTParty::Parser
6
+ SupportedFormats.merge!({"application/atom+xml" => :atom})
7
+
8
+ protected
9
+
10
+ # perform atom parsing on body
11
+ def atom
12
+ body.to_atom
13
+ end
14
+ end
15
+
16
+ parser Parser::Atom
17
+ end
18
+
19
+ class OnlyParseAtom
20
+ include HTTParty
21
+
22
+ # Only support Atom
23
+ class Parser::OnlyAtom < HTTParty::Parser
24
+ SupportedFormats = {"application/atom+xml" => :atom}
25
+
26
+ protected
27
+
28
+ # perform atom parsing on body
29
+ def atom
30
+ body.to_atom
31
+ end
32
+ end
33
+
34
+ parser Parser::OnlyAtom
35
+ end
36
+
37
+ class SkipParsing
38
+ include HTTParty
39
+
40
+ # Parse the response body however you like
41
+ class Parser::Simple < HTTParty::Parser
42
+ def parse
43
+ body
44
+ end
45
+ end
46
+
47
+ parser Parser::Simple
48
+ end
49
+
50
+ class AdHocParsing
51
+ include HTTParty
52
+ parser(
53
+ proc do |body, format|
54
+ case format
55
+ when :json
56
+ body.to_json
57
+ when :xml
58
+ body.to_xml
59
+ else
60
+ body
61
+ end
62
+ end
63
+ )
64
+ end