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