httsoiree 0.13.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.travis.yml +7 -0
- data/Gemfile +14 -0
- data/Guardfile +16 -0
- data/History +303 -0
- data/MIT-LICENSE +20 -0
- data/README.md +77 -0
- data/Rakefile +12 -0
- data/bin/httparty +117 -0
- data/cucumber.yml +1 -0
- data/examples/aaws.rb +32 -0
- data/examples/basic.rb +28 -0
- data/examples/crack.rb +19 -0
- data/examples/custom_parsers.rb +67 -0
- data/examples/delicious.rb +37 -0
- data/examples/google.rb +16 -0
- data/examples/headers_and_user_agents.rb +6 -0
- data/examples/logging.rb +38 -0
- data/examples/nokogiri_html_parser.rb +22 -0
- data/examples/rubyurl.rb +14 -0
- data/examples/stackexchange.rb +24 -0
- data/examples/tripit_sign_in.rb +33 -0
- data/examples/twitter.rb +31 -0
- data/examples/whoismyrep.rb +10 -0
- data/features/basic_authentication.feature +20 -0
- data/features/command_line.feature +7 -0
- data/features/deals_with_http_error_codes.feature +26 -0
- data/features/digest_authentication.feature +20 -0
- data/features/handles_compressed_responses.feature +27 -0
- data/features/handles_multiple_formats.feature +57 -0
- data/features/steps/env.rb +22 -0
- data/features/steps/httparty_response_steps.rb +52 -0
- data/features/steps/httparty_steps.rb +43 -0
- data/features/steps/mongrel_helper.rb +94 -0
- data/features/steps/remote_service_steps.rb +74 -0
- data/features/supports_read_timeout_option.feature +13 -0
- data/features/supports_redirection.feature +22 -0
- data/features/supports_timeout_option.feature +13 -0
- data/httparty.gemspec +25 -0
- data/lib/httparty/connection_adapter.rb +188 -0
- data/lib/httparty/cookie_hash.rb +22 -0
- data/lib/httparty/core_extensions.rb +32 -0
- data/lib/httparty/exceptions.rb +29 -0
- data/lib/httparty/hash_conversions.rb +51 -0
- data/lib/httparty/logger/apache_logger.rb +22 -0
- data/lib/httparty/logger/curl_logger.rb +48 -0
- data/lib/httparty/logger/logger.rb +18 -0
- data/lib/httparty/module_inheritable_attributes.rb +56 -0
- data/lib/httparty/net_digest_auth.rb +84 -0
- data/lib/httparty/parser.rb +141 -0
- data/lib/httparty/request.rb +339 -0
- data/lib/httparty/response/headers.rb +31 -0
- data/lib/httparty/response.rb +72 -0
- data/lib/httparty/version.rb +3 -0
- data/lib/httparty.rb +618 -0
- data/lib/httsoiree.rb +3 -0
- data/script/release +42 -0
- data/spec/fixtures/delicious.xml +23 -0
- data/spec/fixtures/empty.xml +0 -0
- data/spec/fixtures/google.html +3 -0
- data/spec/fixtures/ssl/generate.sh +29 -0
- data/spec/fixtures/ssl/generated/1fe462c2.0 +16 -0
- data/spec/fixtures/ssl/generated/bogushost.crt +13 -0
- data/spec/fixtures/ssl/generated/ca.crt +16 -0
- data/spec/fixtures/ssl/generated/ca.key +15 -0
- data/spec/fixtures/ssl/generated/selfsigned.crt +14 -0
- data/spec/fixtures/ssl/generated/server.crt +13 -0
- data/spec/fixtures/ssl/generated/server.key +15 -0
- data/spec/fixtures/ssl/openssl-exts.cnf +9 -0
- data/spec/fixtures/twitter.csv +2 -0
- data/spec/fixtures/twitter.json +1 -0
- data/spec/fixtures/twitter.xml +403 -0
- data/spec/fixtures/undefined_method_add_node_for_nil.xml +2 -0
- data/spec/httparty/connection_adapter_spec.rb +370 -0
- data/spec/httparty/cookie_hash_spec.rb +83 -0
- data/spec/httparty/exception_spec.rb +23 -0
- data/spec/httparty/logger/apache_logger_spec.rb +41 -0
- data/spec/httparty/logger/curl_logger_spec.rb +18 -0
- data/spec/httparty/logger/logger_spec.rb +22 -0
- data/spec/httparty/net_digest_auth_spec.rb +152 -0
- data/spec/httparty/parser_spec.rb +165 -0
- data/spec/httparty/request_spec.rb +774 -0
- data/spec/httparty/response_spec.rb +221 -0
- data/spec/httparty/ssl_spec.rb +74 -0
- data/spec/httparty_spec.rb +783 -0
- data/spec/spec.opts +2 -0
- data/spec/spec_helper.rb +37 -0
- data/spec/support/ssl_test_helper.rb +47 -0
- data/spec/support/ssl_test_server.rb +80 -0
- data/spec/support/stub_response.rb +43 -0
- data/website/css/common.css +47 -0
- data/website/index.html +73 -0
- metadata +215 -0
@@ -0,0 +1,72 @@
|
|
1
|
+
module HTTParty
|
2
|
+
class Response < HTTParty::BasicObject #:nodoc:
|
3
|
+
def self.underscore(string)
|
4
|
+
string.gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').gsub(/([a-z])([A-Z])/,'\1_\2').downcase
|
5
|
+
end
|
6
|
+
|
7
|
+
attr_reader :request, :response, :body, :headers
|
8
|
+
|
9
|
+
def initialize(request, response, parsed_block, options={})
|
10
|
+
@request = request
|
11
|
+
@response = response
|
12
|
+
@body = options[:body] || response.body
|
13
|
+
@parsed_block = parsed_block
|
14
|
+
@headers = Headers.new(response.to_hash)
|
15
|
+
|
16
|
+
if request.options[:logger]
|
17
|
+
logger = ::HTTParty::Logger.build(request.options[:logger], request.options[:log_level], request.options[:log_format])
|
18
|
+
logger.format(request, self)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def parsed_response
|
23
|
+
@parsed_response ||= @parsed_block.call
|
24
|
+
end
|
25
|
+
|
26
|
+
def class
|
27
|
+
Response
|
28
|
+
end
|
29
|
+
|
30
|
+
def code
|
31
|
+
response.code.to_i
|
32
|
+
end
|
33
|
+
|
34
|
+
def inspect
|
35
|
+
inspect_id = "%x" % (object_id * 2)
|
36
|
+
%(#<#{self.class}:0x#{inspect_id} parsed_response=#{parsed_response.inspect}, @response=#{response.inspect}, @headers=#{headers.inspect}>)
|
37
|
+
end
|
38
|
+
|
39
|
+
CODES_TO_OBJ = ::Net::HTTPResponse::CODE_CLASS_TO_OBJ.merge ::Net::HTTPResponse::CODE_TO_OBJ
|
40
|
+
|
41
|
+
CODES_TO_OBJ.each do |response_code, klass|
|
42
|
+
name = klass.name.sub("Net::HTTP", '')
|
43
|
+
define_method("#{underscore(name)}?") do
|
44
|
+
klass === response
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
# Support old multiple_choice? method from pre 2.0.0 era.
|
49
|
+
if ::RUBY_VERSION >= "2.0.0" && ::RUBY_PLATFORM != "java"
|
50
|
+
alias_method :multiple_choice?, :multiple_choices?
|
51
|
+
end
|
52
|
+
|
53
|
+
def respond_to?(name, include_all = false)
|
54
|
+
return true if [:request, :response, :parsed_response, :body, :headers].include?(name)
|
55
|
+
parsed_response.respond_to?(name, include_all) || response.respond_to?(name, include_all)
|
56
|
+
end
|
57
|
+
|
58
|
+
protected
|
59
|
+
|
60
|
+
def method_missing(name, *args, &block)
|
61
|
+
if parsed_response.respond_to?(name)
|
62
|
+
parsed_response.send(name, *args, &block)
|
63
|
+
elsif response.respond_to?(name)
|
64
|
+
response.send(name, *args, &block)
|
65
|
+
else
|
66
|
+
super
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
require 'httparty/response/headers'
|
data/lib/httparty.rb
ADDED
@@ -0,0 +1,618 @@
|
|
1
|
+
require 'pathname'
|
2
|
+
require 'net/http'
|
3
|
+
require 'net/https'
|
4
|
+
require 'uri'
|
5
|
+
require 'zlib'
|
6
|
+
require 'multi_xml'
|
7
|
+
require 'json'
|
8
|
+
require 'csv'
|
9
|
+
|
10
|
+
require 'httparty/module_inheritable_attributes'
|
11
|
+
require 'httparty/cookie_hash'
|
12
|
+
require 'httparty/net_digest_auth'
|
13
|
+
require 'httparty/version'
|
14
|
+
require 'httparty/connection_adapter'
|
15
|
+
require 'httparty/logger/logger'
|
16
|
+
|
17
|
+
# @see HTTParty::ClassMethods
|
18
|
+
module HTTParty
|
19
|
+
module AllowedFormatsDeprecation
|
20
|
+
def const_missing(const)
|
21
|
+
if const.to_s =~ /AllowedFormats$/
|
22
|
+
Kernel.warn("Deprecated: Use HTTParty::Parser::SupportedFormats")
|
23
|
+
HTTParty::Parser::SupportedFormats
|
24
|
+
else
|
25
|
+
super
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
extend AllowedFormatsDeprecation
|
31
|
+
|
32
|
+
def self.included(base)
|
33
|
+
base.extend ClassMethods
|
34
|
+
base.send :include, HTTParty::ModuleInheritableAttributes
|
35
|
+
base.send(:mattr_inheritable, :default_options)
|
36
|
+
base.send(:mattr_inheritable, :default_cookies)
|
37
|
+
base.instance_variable_set("@default_options", {})
|
38
|
+
base.instance_variable_set("@default_cookies", CookieHash.new)
|
39
|
+
end
|
40
|
+
|
41
|
+
# == Common Request Options
|
42
|
+
# Request methods (get, post, patch, put, delete, head, options) all take a common set of options. These are:
|
43
|
+
#
|
44
|
+
# [:+body+:] Body of the request. If passed an object that responds to #to_hash, will try to normalize it first, by default passing it to ActiveSupport::to_params. Any other kind of object will get used as-is.
|
45
|
+
# [:+http_proxyaddr+:] Address of proxy server to use.
|
46
|
+
# [:+http_proxyport+:] Port of proxy server to use.
|
47
|
+
# [:+http_proxyuser+:] User for proxy server authentication.
|
48
|
+
# [:+http_proxypass+:] Password for proxy server authentication.
|
49
|
+
# [:+limit+:] Maximum number of redirects to follow. Takes precedences over :+no_follow+.
|
50
|
+
# [:+query+:] Query string, or an object that responds to #to_hash representing it. Normalized according to the same rules as :+body+. If you specify this on a POST, you must use an object which responds to #to_hash. See also HTTParty::ClassMethods.default_params.
|
51
|
+
# [:+timeout+:] Timeout for opening connection and reading data.
|
52
|
+
# [:+local_host:] Local address to bind to before connecting.
|
53
|
+
# [:+local_port:] Local port to bind to before connecting.
|
54
|
+
#
|
55
|
+
# There are also another set of options with names corresponding to various class methods. The methods in question are those that let you set a class-wide default, and the options override the defaults on a request-by-request basis. Those options are:
|
56
|
+
# * :+base_uri+: see HTTParty::ClassMethods.base_uri.
|
57
|
+
# * :+basic_auth+: see HTTParty::ClassMethods.basic_auth. Only one of :+basic_auth+ and :+digest_auth+ can be used at a time; if you try using both, you'll get an ArgumentError.
|
58
|
+
# * :+debug_output+: see HTTParty::ClassMethods.debug_output.
|
59
|
+
# * :+digest_auth+: see HTTParty::ClassMethods.digest_auth. Only one of :+basic_auth+ and :+digest_auth+ can be used at a time; if you try using both, you'll get an ArgumentError.
|
60
|
+
# * :+format+: see HTTParty::ClassMethods.format.
|
61
|
+
# * :+headers+: see HTTParty::ClassMethods.headers. Must be a an object which responds to #to_hash.
|
62
|
+
# * :+maintain_method_across_redirects+: see HTTParty::ClassMethods.maintain_method_across_redirects.
|
63
|
+
# * :+no_follow+: see HTTParty::ClassMethods.no_follow.
|
64
|
+
# * :+parser+: see HTTParty::ClassMethods.parser.
|
65
|
+
# * :+connection_adapter+: see HTTParty::ClassMethods.connection_adapter.
|
66
|
+
# * :+pem+: see HTTParty::ClassMethods.pem.
|
67
|
+
# * :+query_string_normalizer+: see HTTParty::ClassMethods.query_string_normalizer
|
68
|
+
# * :+ssl_ca_file+: see HTTParty::ClassMethods.ssl_ca_file.
|
69
|
+
# * :+ssl_ca_path+: see HTTParty::ClassMethods.ssl_ca_path.
|
70
|
+
|
71
|
+
module ClassMethods
|
72
|
+
|
73
|
+
extend AllowedFormatsDeprecation
|
74
|
+
|
75
|
+
# Turns on logging
|
76
|
+
#
|
77
|
+
# class Foo
|
78
|
+
# include HTTParty
|
79
|
+
# logger Logger.new('http_logger'), :info, :apache
|
80
|
+
# end
|
81
|
+
def logger(logger, level=:info, format=:apache)
|
82
|
+
default_options[:logger] = logger
|
83
|
+
default_options[:log_level] = level
|
84
|
+
default_options[:log_format] = format
|
85
|
+
end
|
86
|
+
|
87
|
+
# Allows setting http proxy information to be used
|
88
|
+
#
|
89
|
+
# class Foo
|
90
|
+
# include HTTParty
|
91
|
+
# http_proxy 'http://foo.com', 80, 'user', 'pass'
|
92
|
+
# end
|
93
|
+
def http_proxy(addr=nil, port=nil, user=nil, pass=nil)
|
94
|
+
default_options[:http_proxyaddr] = addr
|
95
|
+
default_options[:http_proxyport] = port
|
96
|
+
default_options[:http_proxyuser] = user
|
97
|
+
default_options[:http_proxypass] = pass
|
98
|
+
end
|
99
|
+
|
100
|
+
# Allows setting a base uri to be used for each request.
|
101
|
+
# Will normalize uri to include http, etc.
|
102
|
+
#
|
103
|
+
# class Foo
|
104
|
+
# include HTTParty
|
105
|
+
# base_uri 'twitter.com'
|
106
|
+
# end
|
107
|
+
def base_uri(uri=nil)
|
108
|
+
return default_options[:base_uri] unless uri
|
109
|
+
default_options[:base_uri] = HTTParty.normalize_base_uri(uri)
|
110
|
+
end
|
111
|
+
|
112
|
+
# Allows setting basic authentication username and password.
|
113
|
+
#
|
114
|
+
# class Foo
|
115
|
+
# include HTTParty
|
116
|
+
# basic_auth 'username', 'password'
|
117
|
+
# end
|
118
|
+
def basic_auth(u, p)
|
119
|
+
default_options[:basic_auth] = {username: u, password: p}
|
120
|
+
end
|
121
|
+
|
122
|
+
# Allows setting digest authentication username and password.
|
123
|
+
#
|
124
|
+
# class Foo
|
125
|
+
# include HTTParty
|
126
|
+
# digest_auth 'username', 'password'
|
127
|
+
# end
|
128
|
+
def digest_auth(u, p)
|
129
|
+
default_options[:digest_auth] = {username: u, password: p}
|
130
|
+
end
|
131
|
+
|
132
|
+
# Do not send rails style query strings.
|
133
|
+
# Specically, don't use bracket notation when sending an array
|
134
|
+
#
|
135
|
+
# For a query:
|
136
|
+
# get '/', query: {selected_ids: [1,2,3]}
|
137
|
+
#
|
138
|
+
# The default query string looks like this:
|
139
|
+
# /?selected_ids[]=1&selected_ids[]=2&selected_ids[]=3
|
140
|
+
#
|
141
|
+
# Call `disable_rails_query_string_format` to transform the query string
|
142
|
+
# into:
|
143
|
+
# /?selected_ids=1&selected_ids=2&selected_ids=3
|
144
|
+
#
|
145
|
+
# @example
|
146
|
+
# class Foo
|
147
|
+
# include HTTParty
|
148
|
+
# disable_rails_query_string_format
|
149
|
+
# end
|
150
|
+
def disable_rails_query_string_format
|
151
|
+
query_string_normalizer Request::NON_RAILS_QUERY_STRING_NORMALIZER
|
152
|
+
end
|
153
|
+
|
154
|
+
# Allows setting default parameters to be appended to each request.
|
155
|
+
# Great for api keys and such.
|
156
|
+
#
|
157
|
+
# class Foo
|
158
|
+
# include HTTParty
|
159
|
+
# default_params api_key: 'secret', another: 'foo'
|
160
|
+
# end
|
161
|
+
def default_params(h={})
|
162
|
+
raise ArgumentError, 'Default params must an object which respond to #to_hash' unless h.respond_to?(:to_hash)
|
163
|
+
default_options[:default_params] ||= {}
|
164
|
+
default_options[:default_params].merge!(h)
|
165
|
+
end
|
166
|
+
|
167
|
+
# Allows setting a default timeout for all HTTP calls
|
168
|
+
# Timeout is specified in seconds.
|
169
|
+
#
|
170
|
+
# class Foo
|
171
|
+
# include HTTParty
|
172
|
+
# default_timeout 10
|
173
|
+
# end
|
174
|
+
def default_timeout(t)
|
175
|
+
raise ArgumentError, 'Timeout must be an integer or float' unless t && (t.is_a?(Integer) || t.is_a?(Float))
|
176
|
+
default_options[:timeout] = t
|
177
|
+
end
|
178
|
+
|
179
|
+
# Allows setting a default open_timeout for all HTTP calls in seconds
|
180
|
+
#
|
181
|
+
# class Foo
|
182
|
+
# include HTTParty
|
183
|
+
# open_timeout 10
|
184
|
+
# end
|
185
|
+
def open_timeout(t)
|
186
|
+
raise ArgumentError, 'open_timeout must be an integer or float' unless t && (t.is_a?(Integer) || t.is_a?(Float))
|
187
|
+
default_options[:open_timeout] = t
|
188
|
+
end
|
189
|
+
|
190
|
+
# Allows setting a default read_timeout for all HTTP calls in seconds
|
191
|
+
#
|
192
|
+
# class Foo
|
193
|
+
# include HTTParty
|
194
|
+
# read_timeout 10
|
195
|
+
# end
|
196
|
+
def read_timeout(t)
|
197
|
+
raise ArgumentError, 'read_timeout must be an integer or float' unless t && (t.is_a?(Integer) || t.is_a?(Float))
|
198
|
+
default_options[:read_timeout] = t
|
199
|
+
end
|
200
|
+
|
201
|
+
# Set an output stream for debugging, defaults to $stderr.
|
202
|
+
# The output stream is passed on to Net::HTTP#set_debug_output.
|
203
|
+
#
|
204
|
+
# class Foo
|
205
|
+
# include HTTParty
|
206
|
+
# debug_output $stderr
|
207
|
+
# end
|
208
|
+
def debug_output(stream = $stderr)
|
209
|
+
default_options[:debug_output] = stream
|
210
|
+
end
|
211
|
+
|
212
|
+
# Allows setting HTTP headers to be used for each request.
|
213
|
+
#
|
214
|
+
# class Foo
|
215
|
+
# include HTTParty
|
216
|
+
# headers 'Accept' => 'text/html'
|
217
|
+
# end
|
218
|
+
def headers(h={})
|
219
|
+
raise ArgumentError, 'Headers must an object which responds to #to_hash' unless h.respond_to?(:to_hash)
|
220
|
+
default_options[:headers] ||= {}
|
221
|
+
default_options[:headers].merge!(h.to_hash)
|
222
|
+
end
|
223
|
+
|
224
|
+
def cookies(h={})
|
225
|
+
raise ArgumentError, 'Cookies must an object which respond to #to_hash' unless h.respond_to?(:to_hash)
|
226
|
+
default_cookies.add_cookies(h)
|
227
|
+
end
|
228
|
+
|
229
|
+
# Proceed to the location header when an HTTP response dictates a redirect.
|
230
|
+
# Redirects are always followed by default.
|
231
|
+
#
|
232
|
+
# @example
|
233
|
+
# class Foo
|
234
|
+
# include HTTParty
|
235
|
+
# base_uri 'http://google.com'
|
236
|
+
# follow_redirects true
|
237
|
+
# end
|
238
|
+
def follow_redirects(value = true)
|
239
|
+
default_options[:follow_redirects] = value
|
240
|
+
end
|
241
|
+
|
242
|
+
# Allows setting the format with which to parse.
|
243
|
+
# Must be one of the allowed formats ie: json, xml
|
244
|
+
#
|
245
|
+
# class Foo
|
246
|
+
# include HTTParty
|
247
|
+
# format :json
|
248
|
+
# end
|
249
|
+
def format(f = nil)
|
250
|
+
if f.nil?
|
251
|
+
default_options[:format]
|
252
|
+
else
|
253
|
+
parser(Parser) if parser.nil?
|
254
|
+
default_options[:format] = f
|
255
|
+
validate_format
|
256
|
+
end
|
257
|
+
end
|
258
|
+
|
259
|
+
# Declare whether or not to follow redirects. When true, an
|
260
|
+
# {HTTParty::RedirectionTooDeep} error will raise upon encountering a
|
261
|
+
# redirect. You can then gain access to the response object via
|
262
|
+
# HTTParty::RedirectionTooDeep#response.
|
263
|
+
#
|
264
|
+
# @see HTTParty::ResponseError#response
|
265
|
+
#
|
266
|
+
# @example
|
267
|
+
# class Foo
|
268
|
+
# include HTTParty
|
269
|
+
# base_uri 'http://google.com'
|
270
|
+
# no_follow true
|
271
|
+
# end
|
272
|
+
#
|
273
|
+
# begin
|
274
|
+
# Foo.get('/')
|
275
|
+
# rescue HTTParty::RedirectionTooDeep => e
|
276
|
+
# puts e.response.body
|
277
|
+
# end
|
278
|
+
def no_follow(value = false)
|
279
|
+
default_options[:no_follow] = value
|
280
|
+
end
|
281
|
+
|
282
|
+
# Declare that you wish to maintain the chosen HTTP method across redirects.
|
283
|
+
# The default behavior is to follow redirects via the GET method.
|
284
|
+
# If you wish to maintain the original method, you can set this option to true.
|
285
|
+
#
|
286
|
+
# @example
|
287
|
+
# class Foo
|
288
|
+
# include HTTParty
|
289
|
+
# base_uri 'http://google.com'
|
290
|
+
# maintain_method_across_redirects true
|
291
|
+
# end
|
292
|
+
|
293
|
+
def maintain_method_across_redirects(value = true)
|
294
|
+
default_options[:maintain_method_across_redirects] = value
|
295
|
+
end
|
296
|
+
|
297
|
+
# Declare that you wish to resend the full HTTP request across redirects,
|
298
|
+
# even on redirects that should logically become GET requests.
|
299
|
+
# A 303 redirect in HTTP signifies that the redirected url should normally
|
300
|
+
# retrieved using a GET request, for instance, it is the output of a previous
|
301
|
+
# POST. maintain_method_across_redirects respects this behavior, but you
|
302
|
+
# can force HTTParty to resend_on_redirect even on 303 responses.
|
303
|
+
#
|
304
|
+
# @example
|
305
|
+
# class Foo
|
306
|
+
# include HTTParty
|
307
|
+
# base_uri 'http://google.com'
|
308
|
+
# resend_on_redirect
|
309
|
+
# end
|
310
|
+
|
311
|
+
def resend_on_redirect(value = true)
|
312
|
+
default_options[:resend_on_redirect] = value
|
313
|
+
end
|
314
|
+
|
315
|
+
# Allows setting a PEM file to be used
|
316
|
+
#
|
317
|
+
# class Foo
|
318
|
+
# include HTTParty
|
319
|
+
# pem File.read('/home/user/my.pem'), "optional password"
|
320
|
+
# end
|
321
|
+
def pem(pem_contents, password=nil)
|
322
|
+
default_options[:pem] = pem_contents
|
323
|
+
default_options[:pem_password] = password
|
324
|
+
end
|
325
|
+
|
326
|
+
# Allows setting a PKCS12 file to be used
|
327
|
+
#
|
328
|
+
# class Foo
|
329
|
+
# include HTTParty
|
330
|
+
# pkcs12 File.read('/home/user/my.p12'), "password"
|
331
|
+
# end
|
332
|
+
def pkcs12(p12_contents, password)
|
333
|
+
default_options[:p12] = p12_contents
|
334
|
+
default_options[:p12_password] = password
|
335
|
+
end
|
336
|
+
|
337
|
+
# Override the way query strings are normalized.
|
338
|
+
# Helpful for overriding the default rails normalization of Array queries.
|
339
|
+
#
|
340
|
+
# For a query:
|
341
|
+
# get '/', query: {selected_ids: [1,2,3]}
|
342
|
+
#
|
343
|
+
# The default query string normalizer returns:
|
344
|
+
# /?selected_ids[]=1&selected_ids[]=2&selected_ids[]=3
|
345
|
+
#
|
346
|
+
# Let's change it to this:
|
347
|
+
# /?selected_ids=1&selected_ids=2&selected_ids=3
|
348
|
+
#
|
349
|
+
# Pass a Proc to the query normalizer which accepts the yielded query.
|
350
|
+
#
|
351
|
+
# @example Modifying Array query strings
|
352
|
+
# class ServiceWrapper
|
353
|
+
# include HTTParty
|
354
|
+
#
|
355
|
+
# query_string_normalizer proc { |query|
|
356
|
+
# query.map do |key, value|
|
357
|
+
# value.map {|v| "#{key}=#{v}"}
|
358
|
+
# end.join('&')
|
359
|
+
# }
|
360
|
+
# end
|
361
|
+
#
|
362
|
+
# @param [Proc] normalizer custom query string normalizer.
|
363
|
+
# @yield [Hash, String] query string
|
364
|
+
# @yieldreturn [Array] an array that will later be joined with '&'
|
365
|
+
def query_string_normalizer(normalizer)
|
366
|
+
default_options[:query_string_normalizer] = normalizer
|
367
|
+
end
|
368
|
+
|
369
|
+
# Allows setting of SSL version to use. This only works in Ruby 1.9+.
|
370
|
+
# You can get a list of valid versions from OpenSSL::SSL::SSLContext::METHODS.
|
371
|
+
#
|
372
|
+
# class Foo
|
373
|
+
# include HTTParty
|
374
|
+
# ssl_version :SSLv3
|
375
|
+
# end
|
376
|
+
def ssl_version(version)
|
377
|
+
default_options[:ssl_version] = version
|
378
|
+
end
|
379
|
+
|
380
|
+
# Allows setting of SSL ciphers to use. This only works in Ruby 1.9+.
|
381
|
+
# You can get a list of valid specific ciphers from OpenSSL::Cipher.ciphers.
|
382
|
+
# You also can specify a cipher suite here, listed here at openssl.org:
|
383
|
+
# http://www.openssl.org/docs/apps/ciphers.html#CIPHER_SUITE_NAMES
|
384
|
+
#
|
385
|
+
# class Foo
|
386
|
+
# include HTTParty
|
387
|
+
# ciphers "RC4-SHA"
|
388
|
+
# end
|
389
|
+
def ciphers(cipher_names)
|
390
|
+
default_options[:ciphers] = cipher_names
|
391
|
+
end
|
392
|
+
|
393
|
+
# Allows setting an OpenSSL certificate authority file. The file
|
394
|
+
# should contain one or more certificates in PEM format.
|
395
|
+
#
|
396
|
+
# Setting this option enables certificate verification. All
|
397
|
+
# certificates along a chain must be available in ssl_ca_file or
|
398
|
+
# ssl_ca_path for verification to succeed.
|
399
|
+
#
|
400
|
+
#
|
401
|
+
# class Foo
|
402
|
+
# include HTTParty
|
403
|
+
# ssl_ca_file '/etc/ssl/certs/ca-certificates.crt'
|
404
|
+
# end
|
405
|
+
def ssl_ca_file(path)
|
406
|
+
default_options[:ssl_ca_file] = path
|
407
|
+
end
|
408
|
+
|
409
|
+
# Allows setting an OpenSSL certificate authority path (directory).
|
410
|
+
#
|
411
|
+
# Setting this option enables certificate verification. All
|
412
|
+
# certificates along a chain must be available in ssl_ca_file or
|
413
|
+
# ssl_ca_path for verification to succeed.
|
414
|
+
#
|
415
|
+
# class Foo
|
416
|
+
# include HTTParty
|
417
|
+
# ssl_ca_path '/etc/ssl/certs/'
|
418
|
+
# end
|
419
|
+
def ssl_ca_path(path)
|
420
|
+
default_options[:ssl_ca_path] = path
|
421
|
+
end
|
422
|
+
|
423
|
+
# Allows setting a custom parser for the response.
|
424
|
+
#
|
425
|
+
# class Foo
|
426
|
+
# include HTTParty
|
427
|
+
# parser Proc.new {|data| ...}
|
428
|
+
# end
|
429
|
+
def parser(custom_parser = nil)
|
430
|
+
if custom_parser.nil?
|
431
|
+
default_options[:parser]
|
432
|
+
else
|
433
|
+
default_options[:parser] = custom_parser
|
434
|
+
validate_format
|
435
|
+
end
|
436
|
+
end
|
437
|
+
|
438
|
+
# Allows setting a custom connection_adapter for the http connections
|
439
|
+
#
|
440
|
+
# @example
|
441
|
+
# class Foo
|
442
|
+
# include HTTParty
|
443
|
+
# connection_adapter Proc.new {|uri, options| ... }
|
444
|
+
# end
|
445
|
+
#
|
446
|
+
# @example provide optional configuration for your connection_adapter
|
447
|
+
# class Foo
|
448
|
+
# include HTTParty
|
449
|
+
# connection_adapter Proc.new {|uri, options| ... }, {foo: :bar}
|
450
|
+
# end
|
451
|
+
#
|
452
|
+
# @see HTTParty::ConnectionAdapter
|
453
|
+
def connection_adapter(custom_adapter = nil, options = nil)
|
454
|
+
if custom_adapter.nil?
|
455
|
+
default_options[:connection_adapter]
|
456
|
+
else
|
457
|
+
default_options[:connection_adapter] = custom_adapter
|
458
|
+
default_options[:connection_adapter_options] = options
|
459
|
+
end
|
460
|
+
end
|
461
|
+
|
462
|
+
# Allows making a get request to a url.
|
463
|
+
#
|
464
|
+
# class Foo
|
465
|
+
# include HTTParty
|
466
|
+
# end
|
467
|
+
#
|
468
|
+
# # Simple get with full url
|
469
|
+
# Foo.get('http://foo.com/resource.json')
|
470
|
+
#
|
471
|
+
# # Simple get with full url and query parameters
|
472
|
+
# # ie: http://foo.com/resource.json?limit=10
|
473
|
+
# Foo.get('http://foo.com/resource.json', query: {limit: 10})
|
474
|
+
def get(path, options={}, &block)
|
475
|
+
perform_request Net::HTTP::Get, path, options, &block
|
476
|
+
end
|
477
|
+
|
478
|
+
# Allows making a post request to a url.
|
479
|
+
#
|
480
|
+
# class Foo
|
481
|
+
# include HTTParty
|
482
|
+
# end
|
483
|
+
#
|
484
|
+
# # Simple post with full url and setting the body
|
485
|
+
# Foo.post('http://foo.com/resources', body: {bar: 'baz'})
|
486
|
+
#
|
487
|
+
# # Simple post with full url using :query option,
|
488
|
+
# # which gets set as form data on the request.
|
489
|
+
# Foo.post('http://foo.com/resources', query: {bar: 'baz'})
|
490
|
+
def post(path, options={}, &block)
|
491
|
+
perform_request Net::HTTP::Post, path, options, &block
|
492
|
+
end
|
493
|
+
|
494
|
+
# Perform a PATCH request to a path
|
495
|
+
def patch(path, options={}, &block)
|
496
|
+
perform_request Net::HTTP::Patch, path, options, &block
|
497
|
+
end
|
498
|
+
|
499
|
+
# Perform a PUT request to a path
|
500
|
+
def put(path, options={}, &block)
|
501
|
+
perform_request Net::HTTP::Put, path, options, &block
|
502
|
+
end
|
503
|
+
|
504
|
+
# Perform a DELETE request to a path
|
505
|
+
def delete(path, options={}, &block)
|
506
|
+
perform_request Net::HTTP::Delete, path, options, &block
|
507
|
+
end
|
508
|
+
|
509
|
+
# Perform a MOVE request to a path
|
510
|
+
def move(path, options={}, &block)
|
511
|
+
perform_request Net::HTTP::Move, path, options, &block
|
512
|
+
end
|
513
|
+
|
514
|
+
# Perform a COPY request to a path
|
515
|
+
def copy(path, options={}, &block)
|
516
|
+
perform_request Net::HTTP::Copy, path, options, &block
|
517
|
+
end
|
518
|
+
|
519
|
+
# Perform a HEAD request to a path
|
520
|
+
def head(path, options={}, &block)
|
521
|
+
perform_request Net::HTTP::Head, path, options, &block
|
522
|
+
end
|
523
|
+
|
524
|
+
# Perform an OPTIONS request to a path
|
525
|
+
def options(path, options={}, &block)
|
526
|
+
perform_request Net::HTTP::Options, path, options, &block
|
527
|
+
end
|
528
|
+
|
529
|
+
def default_options #:nodoc:
|
530
|
+
@default_options
|
531
|
+
end
|
532
|
+
|
533
|
+
private
|
534
|
+
|
535
|
+
def perform_request(http_method, path, options, &block) #:nodoc:
|
536
|
+
options = ModuleInheritableAttributes.hash_deep_dup(default_options).merge(options)
|
537
|
+
process_headers(options)
|
538
|
+
process_cookies(options)
|
539
|
+
Request.new(http_method, path, options).perform(&block)
|
540
|
+
end
|
541
|
+
|
542
|
+
def process_headers(options)
|
543
|
+
if options[:headers] && headers.any?
|
544
|
+
options[:headers] = headers.merge(options[:headers])
|
545
|
+
end
|
546
|
+
end
|
547
|
+
|
548
|
+
def process_cookies(options) #:nodoc:
|
549
|
+
return unless options[:cookies] || default_cookies.any?
|
550
|
+
options[:headers] ||= headers.dup
|
551
|
+
options[:headers]["cookie"] = cookies.merge(options.delete(:cookies) || {}).to_cookie_string
|
552
|
+
end
|
553
|
+
|
554
|
+
def validate_format
|
555
|
+
if format && parser.respond_to?(:supports_format?) && !parser.supports_format?(format)
|
556
|
+
raise UnsupportedFormat, "'#{format.inspect}' Must be one of: #{parser.supported_formats.map{|f| f.to_s}.sort.join(', ')}"
|
557
|
+
end
|
558
|
+
end
|
559
|
+
end
|
560
|
+
|
561
|
+
def self.normalize_base_uri(url) #:nodoc:
|
562
|
+
normalized_url = url.dup
|
563
|
+
use_ssl = (normalized_url =~ /^https/) || (normalized_url =~ /:443\b/)
|
564
|
+
ends_with_slash = normalized_url =~ /\/$/
|
565
|
+
|
566
|
+
normalized_url.chop! if ends_with_slash
|
567
|
+
normalized_url.gsub!(/^https?:\/\//i, '')
|
568
|
+
|
569
|
+
"http#{'s' if use_ssl}://#{normalized_url}"
|
570
|
+
end
|
571
|
+
|
572
|
+
class Basement #:nodoc:
|
573
|
+
include HTTParty
|
574
|
+
end
|
575
|
+
|
576
|
+
def self.get(*args, &block)
|
577
|
+
Basement.get(*args, &block)
|
578
|
+
end
|
579
|
+
|
580
|
+
def self.post(*args, &block)
|
581
|
+
Basement.post(*args, &block)
|
582
|
+
end
|
583
|
+
|
584
|
+
def self.patch(*args, &block)
|
585
|
+
Basement.patch(*args, &block)
|
586
|
+
end
|
587
|
+
|
588
|
+
def self.put(*args, &block)
|
589
|
+
Basement.put(*args, &block)
|
590
|
+
end
|
591
|
+
|
592
|
+
def self.delete(*args, &block)
|
593
|
+
Basement.delete(*args, &block)
|
594
|
+
end
|
595
|
+
|
596
|
+
def self.move(*args, &block)
|
597
|
+
Basement.move(*args, &block)
|
598
|
+
end
|
599
|
+
|
600
|
+
def self.copy(*args, &block)
|
601
|
+
Basement.copy(*args, &block)
|
602
|
+
end
|
603
|
+
|
604
|
+
def self.head(*args, &block)
|
605
|
+
Basement.head(*args, &block)
|
606
|
+
end
|
607
|
+
|
608
|
+
def self.options(*args, &block)
|
609
|
+
Basement.options(*args, &block)
|
610
|
+
end
|
611
|
+
end
|
612
|
+
|
613
|
+
require 'httparty/core_extensions'
|
614
|
+
require 'httparty/hash_conversions'
|
615
|
+
require 'httparty/exceptions'
|
616
|
+
require 'httparty/parser'
|
617
|
+
require 'httparty/request'
|
618
|
+
require 'httparty/response'
|
data/lib/httsoiree.rb
ADDED