yoyle439587298 0.13.0

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 (91) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +10 -0
  3. data/.travis.yml +7 -0
  4. data/Gemfile +14 -0
  5. data/Guardfile +16 -0
  6. data/History +303 -0
  7. data/LICENSE +21 -0
  8. data/MIT-LICENSE +20 -0
  9. data/README.md +80 -0
  10. data/Rakefile +12 -0
  11. data/bin/httparty +117 -0
  12. data/cucumber.yml +1 -0
  13. data/examples/aaws.rb +32 -0
  14. data/examples/basic.rb +32 -0
  15. data/examples/crack.rb +19 -0
  16. data/examples/custom_parsers.rb +67 -0
  17. data/examples/delicious.rb +37 -0
  18. data/examples/google.rb +16 -0
  19. data/examples/headers_and_user_agents.rb +6 -0
  20. data/examples/nokogiri_html_parser.rb +22 -0
  21. data/examples/rubyurl.rb +14 -0
  22. data/examples/tripit_sign_in.rb +33 -0
  23. data/examples/twitter.rb +31 -0
  24. data/examples/whoismyrep.rb +10 -0
  25. data/features/basic_authentication.feature +20 -0
  26. data/features/command_line.feature +7 -0
  27. data/features/deals_with_http_error_codes.feature +26 -0
  28. data/features/digest_authentication.feature +20 -0
  29. data/features/handles_compressed_responses.feature +27 -0
  30. data/features/handles_multiple_formats.feature +57 -0
  31. data/features/steps/env.rb +22 -0
  32. data/features/steps/httparty_response_steps.rb +52 -0
  33. data/features/steps/httparty_steps.rb +35 -0
  34. data/features/steps/mongrel_helper.rb +94 -0
  35. data/features/steps/remote_service_steps.rb +74 -0
  36. data/features/supports_redirection.feature +22 -0
  37. data/features/supports_timeout_option.feature +13 -0
  38. data/httparty.gemspec +26 -0
  39. data/lib/httparty.rb +578 -0
  40. data/lib/httparty/connection_adapter.rb +176 -0
  41. data/lib/httparty/cookie_hash.rb +22 -0
  42. data/lib/httparty/core_extensions.rb +32 -0
  43. data/lib/httparty/exceptions.rb +29 -0
  44. data/lib/httparty/hash_conversions.rb +51 -0
  45. data/lib/httparty/logger/apache_logger.rb +22 -0
  46. data/lib/httparty/logger/curl_logger.rb +48 -0
  47. data/lib/httparty/logger/logger.rb +18 -0
  48. data/lib/httparty/module_inheritable_attributes.rb +56 -0
  49. data/lib/httparty/net_digest_auth.rb +84 -0
  50. data/lib/httparty/parser.rb +141 -0
  51. data/lib/httparty/request.rb +330 -0
  52. data/lib/httparty/response.rb +72 -0
  53. data/lib/httparty/response/headers.rb +31 -0
  54. data/lib/httparty/version.rb +3 -0
  55. data/script/release +42 -0
  56. data/spec/fixtures/delicious.xml +23 -0
  57. data/spec/fixtures/empty.xml +0 -0
  58. data/spec/fixtures/google.html +3 -0
  59. data/spec/fixtures/ssl/generate.sh +29 -0
  60. data/spec/fixtures/ssl/generated/1fe462c2.0 +16 -0
  61. data/spec/fixtures/ssl/generated/bogushost.crt +13 -0
  62. data/spec/fixtures/ssl/generated/ca.crt +16 -0
  63. data/spec/fixtures/ssl/generated/ca.key +15 -0
  64. data/spec/fixtures/ssl/generated/selfsigned.crt +14 -0
  65. data/spec/fixtures/ssl/generated/server.crt +13 -0
  66. data/spec/fixtures/ssl/generated/server.key +15 -0
  67. data/spec/fixtures/ssl/openssl-exts.cnf +9 -0
  68. data/spec/fixtures/twitter.csv +2 -0
  69. data/spec/fixtures/twitter.json +1 -0
  70. data/spec/fixtures/twitter.xml +403 -0
  71. data/spec/fixtures/undefined_method_add_node_for_nil.xml +2 -0
  72. data/spec/httparty/connection_adapter_spec.rb +298 -0
  73. data/spec/httparty/cookie_hash_spec.rb +83 -0
  74. data/spec/httparty/exception_spec.rb +23 -0
  75. data/spec/httparty/logger/apache_logger_spec.rb +26 -0
  76. data/spec/httparty/logger/curl_logger_spec.rb +18 -0
  77. data/spec/httparty/logger/logger_spec.rb +22 -0
  78. data/spec/httparty/net_digest_auth_spec.rb +152 -0
  79. data/spec/httparty/parser_spec.rb +165 -0
  80. data/spec/httparty/request_spec.rb +631 -0
  81. data/spec/httparty/response_spec.rb +221 -0
  82. data/spec/httparty/ssl_spec.rb +74 -0
  83. data/spec/httparty_spec.rb +764 -0
  84. data/spec/spec.opts +2 -0
  85. data/spec/spec_helper.rb +37 -0
  86. data/spec/support/ssl_test_helper.rb +47 -0
  87. data/spec/support/ssl_test_server.rb +80 -0
  88. data/spec/support/stub_response.rb +43 -0
  89. data/website/css/common.css +47 -0
  90. data/website/index.html +73 -0
  91. metadata +208 -0
@@ -0,0 +1,176 @@
1
+ module HTTParty
2
+ # Default connection adapter that returns a new Net::HTTP each time
3
+ #
4
+ # == Custom Connection Factories
5
+ #
6
+ # If you like to implement your own connection adapter, subclassing
7
+ # HTTPParty::ConnectionAdapter will make it easier. Just override
8
+ # the #connection method. The uri and options attributes will have
9
+ # all the info you need to construct your http connection. Whatever
10
+ # you return from your connection method needs to adhere to the
11
+ # Net::HTTP interface as this is what HTTParty expects.
12
+ #
13
+ # @example log the uri and options
14
+ # class LoggingConnectionAdapter < HTTParty::ConnectionAdapter
15
+ # def connection
16
+ # puts uri
17
+ # puts options
18
+ # Net::HTTP.new(uri)
19
+ # end
20
+ # end
21
+ #
22
+ # @example count number of http calls
23
+ # class CountingConnectionAdapter < HTTParty::ConnectionAdapter
24
+ # @@count = 0
25
+ #
26
+ # self.count
27
+ # @@count
28
+ # end
29
+ #
30
+ # def connection
31
+ # self.count += 1
32
+ # super
33
+ # end
34
+ # end
35
+ #
36
+ # === Configuration
37
+ # There is lots of configuration data available for your connection adapter
38
+ # in the #options attribute. It is up to you to interpret them within your
39
+ # connection adapter. Take a look at the implementation of
40
+ # HTTParty::ConnectionAdapter#connection for examples of how they are used.
41
+ # Something are probably interesting are as follows:
42
+ # * :+timeout+: timeout in seconds
43
+ # * :+debug_output+: see HTTParty::ClassMethods.debug_output.
44
+ # * :+pem+: contains pem data. see HTTParty::ClassMethods.pem.
45
+ # * :+ssl_ca_file+: see HTTParty::ClassMethods.ssl_ca_file.
46
+ # * :+ssl_ca_path+: see HTTParty::ClassMethods.ssl_ca_path.
47
+ # * :+connection_adapter_options+: contains the hash your passed to HTTParty.connection_adapter when you configured your connection adapter
48
+ class ConnectionAdapter
49
+
50
+ # Private: Regex used to strip brackets from IPv6 URIs.
51
+ StripIpv6BracketsRegex = /\A\[(.*)\]\z/
52
+
53
+ # Public
54
+ def self.call(uri, options)
55
+ new(uri, options).connection
56
+ end
57
+
58
+ attr_reader :uri, :options
59
+
60
+ def initialize(uri, options={})
61
+ raise ArgumentError, "uri must be a URI, not a #{uri.class}" unless uri.kind_of? URI
62
+
63
+ @uri = uri
64
+ @options = options
65
+ end
66
+
67
+ def connection
68
+ host = clean_host(uri.host)
69
+ if options[:http_proxyaddr]
70
+ http = Net::HTTP.new(host, uri.port, options[:http_proxyaddr], options[:http_proxyport], options[:http_proxyuser], options[:http_proxypass])
71
+ else
72
+ http = Net::HTTP.new(host, uri.port)
73
+ end
74
+
75
+ http.use_ssl = ssl_implied?(uri)
76
+
77
+ attach_ssl_certificates(http, options)
78
+
79
+ if options[:timeout] && (options[:timeout].is_a?(Integer) || options[:timeout].is_a?(Float))
80
+ http.open_timeout = options[:timeout]
81
+ http.read_timeout = options[:timeout]
82
+ end
83
+
84
+ if options[:debug_output]
85
+ http.set_debug_output(options[:debug_output])
86
+ end
87
+
88
+ if options[:ciphers]
89
+ http.ciphers = options[:ciphers]
90
+ end
91
+
92
+ # Bind to a specific local address or port
93
+ #
94
+ # @see https://bugs.ruby-lang.org/issues/6617
95
+ if options[:local_host]
96
+ if RUBY_VERSION >= "2.0.0"
97
+ http.local_host = options[:local_host]
98
+ else
99
+ Kernel.warn("Warning: option :local_host requires Ruby version 2.0 or later")
100
+ end
101
+ end
102
+
103
+ if options[:local_port]
104
+ if RUBY_VERSION >= "2.0.0"
105
+ http.local_port = options[:local_port]
106
+ else
107
+ Kernel.warn("Warning: option :local_port requires Ruby version 2.0 or later")
108
+ end
109
+ end
110
+
111
+ return http
112
+ end
113
+
114
+ private
115
+
116
+ def clean_host(host)
117
+ strip_ipv6_brackets(host)
118
+ end
119
+
120
+ def strip_ipv6_brackets(host)
121
+ StripIpv6BracketsRegex =~ host ? $1 : host
122
+ end
123
+
124
+ def ssl_implied?(uri)
125
+ uri.port == 443 || uri.instance_of?(URI::HTTPS)
126
+ end
127
+
128
+ def attach_ssl_certificates(http, options)
129
+ if http.use_ssl?
130
+ if options.fetch(:verify, true)
131
+ http.verify_mode = OpenSSL::SSL::VERIFY_PEER
132
+ if options[:cert_store]
133
+ http.cert_store = options[:cert_store]
134
+ else
135
+ # Use the default cert store by default, i.e. system ca certs
136
+ http.cert_store = OpenSSL::X509::Store.new
137
+ http.cert_store.set_default_paths
138
+ end
139
+ else
140
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
141
+ end
142
+
143
+ # Client certificate authentication
144
+ if options[:pem]
145
+ http.cert = OpenSSL::X509::Certificate.new(options[:pem])
146
+ http.key = OpenSSL::PKey::RSA.new(options[:pem], options[:pem_password])
147
+ http.verify_mode = OpenSSL::SSL::VERIFY_PEER
148
+ end
149
+
150
+ # PKCS12 client certificate authentication
151
+ if options[:p12]
152
+ p12 = OpenSSL::PKCS12.new(options[:p12], options[:p12_password])
153
+ http.cert = p12.certificate
154
+ http.key = p12.key
155
+ http.verify_mode = OpenSSL::SSL::VERIFY_PEER
156
+ end
157
+
158
+ # SSL certificate authority file and/or directory
159
+ if options[:ssl_ca_file]
160
+ http.ca_file = options[:ssl_ca_file]
161
+ http.verify_mode = OpenSSL::SSL::VERIFY_PEER
162
+ end
163
+
164
+ if options[:ssl_ca_path]
165
+ http.ca_path = options[:ssl_ca_path]
166
+ http.verify_mode = OpenSSL::SSL::VERIFY_PEER
167
+ end
168
+
169
+ # This is only Ruby 1.9+
170
+ if options[:ssl_version] && http.respond_to?(:ssl_version=)
171
+ http.ssl_version = options[:ssl_version]
172
+ end
173
+ end
174
+ end
175
+ end
176
+ end
@@ -0,0 +1,22 @@
1
+ class HTTParty::CookieHash < Hash #:nodoc:
2
+
3
+ CLIENT_COOKIES = %w{path expires domain path secure HTTPOnly}
4
+
5
+ def add_cookies(value)
6
+ case value
7
+ when Hash
8
+ merge!(value)
9
+ when String
10
+ value.split('; ').each do |cookie|
11
+ array = cookie.split('=',2)
12
+ self[array[0].to_sym] = array[1]
13
+ end
14
+ else
15
+ raise "add_cookies only takes a Hash or a String"
16
+ end
17
+ end
18
+
19
+ def to_cookie_string
20
+ delete_if { |k, v| CLIENT_COOKIES.include?(k.to_s.downcase) }.collect { |k, v| "#{k}=#{v}" }.join("; ")
21
+ end
22
+ end
@@ -0,0 +1,32 @@
1
+ module HTTParty
2
+ if defined?(::BasicObject)
3
+ BasicObject = ::BasicObject #:nodoc:
4
+ else
5
+ class BasicObject #:nodoc:
6
+ instance_methods.each { |m| undef_method m unless m =~ /^__|instance_eval/ }
7
+ end
8
+ end
9
+
10
+ unless defined?(Net::HTTP::Patch)
11
+ class Net::HTTP
12
+ def patch(path, data, initheader = nil, dest = nil, &block) #:nodoc:
13
+ res = nil
14
+ request(Patch.new(path, initheader), data) {|r|
15
+ r.read_body dest, &block
16
+ res = r
17
+ }
18
+ unless @newimpl
19
+ res.value
20
+ return res, res.body
21
+ end
22
+ res
23
+ end
24
+
25
+ class Patch < Net::HTTPRequest
26
+ METHOD = 'PATCH'
27
+ REQUEST_HAS_BODY = true
28
+ RESPONSE_HAS_BODY = true
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,29 @@
1
+ module HTTParty
2
+ # @abstact Exceptions raised by HTTParty inherit from Error
3
+ class Error < StandardError; end
4
+
5
+ # Exception raised when you attempt to set a non-existant format
6
+ class UnsupportedFormat < Error; end
7
+
8
+ # Exception raised when using a URI scheme other than HTTP or HTTPS
9
+ class UnsupportedURIScheme < Error; end
10
+
11
+ # @abstract Exceptions which inherit from ResponseError contain the Net::HTTP
12
+ # response object accessible via the {#response} method.
13
+ class ResponseError < Error
14
+ # Returns the response of the last request
15
+ # @return [Net::HTTPResponse] A subclass of Net::HTTPResponse, e.g.
16
+ # Net::HTTPOK
17
+ attr_reader :response
18
+
19
+ # Instantiate an instance of ResponseError with a Net::HTTPResponse object
20
+ # @param [Net::HTTPResponse]
21
+ def initialize(response)
22
+ @response = response
23
+ end
24
+ end
25
+
26
+ # Exception that is raised when request has redirected too many times.
27
+ # Calling {#response} returns the Net:HTTP response object.
28
+ class RedirectionTooDeep < ResponseError; end
29
+ end
@@ -0,0 +1,51 @@
1
+ module HTTParty
2
+ module HashConversions
3
+ # @return <String> This hash as a query string
4
+ #
5
+ # @example
6
+ # { :name => "Bob",
7
+ # :address => {
8
+ # :street => '111 Ruby Ave.',
9
+ # :city => 'Ruby Central',
10
+ # :phones => ['111-111-1111', '222-222-2222']
11
+ # }
12
+ # }.to_params
13
+ # #=> "name=Bob&address[city]=Ruby Central&address[phones][]=111-111-1111&address[phones][]=222-222-2222&address[street]=111 Ruby Ave."
14
+ def self.to_params(hash)
15
+ params = hash.map { |k,v| normalize_param(k,v) }.join
16
+ params.chop! # trailing &
17
+ params
18
+ end
19
+
20
+ # @param key<Object> The key for the param.
21
+ # @param value<Object> The value for the param.
22
+ #
23
+ # @return <String> This key value pair as a param
24
+ #
25
+ # @example normalize_param(:name, "Bob Jones") #=> "name=Bob%20Jones&"
26
+ def self.normalize_param(key, value)
27
+ param = ''
28
+ stack = []
29
+
30
+ if value.is_a?(Array)
31
+ param << value.map { |element| normalize_param("#{key}[]", element) }.join
32
+ elsif value.is_a?(Hash)
33
+ stack << [key,value]
34
+ else
35
+ param << "#{key}=#{URI.encode(value.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))}&"
36
+ end
37
+
38
+ stack.each do |parent, hash|
39
+ hash.each do |k, v|
40
+ if v.is_a?(Hash)
41
+ stack << ["#{parent}[#{k}]", v]
42
+ else
43
+ param << normalize_param("#{parent}[#{k}]", v)
44
+ end
45
+ end
46
+ end
47
+
48
+ param
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,22 @@
1
+ module HTTParty
2
+ module Logger
3
+ class ApacheLogger #:nodoc:
4
+ TAG_NAME = HTTParty.name
5
+
6
+ attr_accessor :level, :logger, :current_time
7
+
8
+ def initialize(logger, level)
9
+ @logger = logger
10
+ @level = level.to_sym
11
+ end
12
+
13
+ def format(request, response)
14
+ current_time = Time.now.strftime("%Y-%m-%d %H:%M:%S %z")
15
+ http_method = request.http_method.name.split("::").last.upcase
16
+ path = request.path.to_s
17
+ content_length = response['Content-Length']
18
+ @logger.send @level, "[#{TAG_NAME}] [#{current_time}] #{response.code} \"#{http_method} #{path}\" #{content_length || "-"} "
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,48 @@
1
+ module HTTParty
2
+ module Logger
3
+ class CurlLogger #:nodoc:
4
+ TAG_NAME = HTTParty.name
5
+ OUT = ">"
6
+ IN = "<"
7
+
8
+ attr_accessor :level, :logger, :current_time
9
+
10
+ def initialize(logger, level)
11
+ @logger = logger
12
+ @level = level.to_sym
13
+ end
14
+
15
+ def format(request, response)
16
+ messages = []
17
+ time = Time.now.strftime("%Y-%m-%d %H:%M:%S %z")
18
+ http_method = request.http_method.name.split("::").last.upcase
19
+ path = request.path.to_s
20
+
21
+ messages << print(time, OUT, "#{http_method} #{path}")
22
+
23
+ if request.options[:headers] && request.options[:headers].size > 0
24
+ request.options[:headers].each do |k, v|
25
+ messages << print(time, OUT, "#{k}: #{v}")
26
+ end
27
+ end
28
+
29
+ messages << print(time, OUT, request.raw_body)
30
+ messages << print(time, OUT, "")
31
+ messages << print(time, IN, "HTTP/#{response.http_version} #{response.code}")
32
+
33
+ headers = response.respond_to?(:headers) ? response.headers : response
34
+ response.each_header do |response_header|
35
+ messages << print(time, IN, "#{response_header.capitalize}: #{headers[response_header]}")
36
+ end
37
+
38
+ messages << print(time, IN, "\n#{response.body}")
39
+
40
+ @logger.send @level, messages.join("\n")
41
+ end
42
+
43
+ def print(time, direction, line)
44
+ "[#{TAG_NAME}] [#{time}] #{direction} #{line}"
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,18 @@
1
+ require 'httparty/logger/apache_logger'
2
+ require 'httparty/logger/curl_logger'
3
+
4
+ module HTTParty
5
+ module Logger
6
+ def self.build(logger, level, formatter)
7
+ level ||= :info
8
+ format ||= :apache
9
+
10
+ case formatter
11
+ when :curl
12
+ Logger::CurlLogger.new(logger, level)
13
+ else
14
+ Logger::ApacheLogger.new(logger, level)
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,56 @@
1
+ module HTTParty
2
+ module ModuleInheritableAttributes #:nodoc:
3
+ def self.included(base)
4
+ base.extend(ClassMethods)
5
+ end
6
+
7
+ # borrowed from Rails 3.2 ActiveSupport
8
+ def self.hash_deep_dup(hash)
9
+ duplicate = hash.dup
10
+
11
+ duplicate.each_pair do |key, value|
12
+ duplicate[key] = if value.is_a?(Hash)
13
+ hash_deep_dup(value)
14
+ elsif value.is_a?(Proc)
15
+ duplicate[key] = value.dup
16
+ else
17
+ value
18
+ end
19
+ end
20
+
21
+ duplicate
22
+ end
23
+
24
+ module ClassMethods #:nodoc:
25
+ def mattr_inheritable(*args)
26
+ @mattr_inheritable_attrs ||= [:mattr_inheritable_attrs]
27
+ @mattr_inheritable_attrs += args
28
+
29
+ args.each do |arg|
30
+ module_eval %(class << self; attr_accessor :#{arg} end)
31
+ end
32
+
33
+ @mattr_inheritable_attrs
34
+ end
35
+
36
+ def inherited(subclass)
37
+ super
38
+ @mattr_inheritable_attrs.each do |inheritable_attribute|
39
+ ivar = "@#{inheritable_attribute}"
40
+ subclass.instance_variable_set(ivar, instance_variable_get(ivar).clone)
41
+
42
+ if instance_variable_get(ivar).respond_to?(:merge)
43
+ method = <<-EOM
44
+ def self.#{inheritable_attribute}
45
+ duplicate = ModuleInheritableAttributes.hash_deep_dup(#{ivar})
46
+ #{ivar} = superclass.#{inheritable_attribute}.merge(duplicate)
47
+ end
48
+ EOM
49
+
50
+ subclass.class_eval method
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end