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
@@ -0,0 +1,190 @@
|
|
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
|
+
# Some things that are probably interesting are as follows:
|
42
|
+
# * :+timeout+: timeout in seconds
|
43
|
+
# * :+open_timeout+: http connection open_timeout in seconds, overrides timeout if set
|
44
|
+
# * :+read_timeout+: http connection read_timeout in seconds, overrides timeout if set
|
45
|
+
# * :+debug_output+: see HTTParty::ClassMethods.debug_output.
|
46
|
+
# * :+pem+: contains pem data. see HTTParty::ClassMethods.pem.
|
47
|
+
# * :+verify+: verify the server’s certificate against the ca certificate.
|
48
|
+
# * :+verify_peer+: set to false to turn off server verification but still send client certificate
|
49
|
+
# * :+ssl_ca_file+: see HTTParty::ClassMethods.ssl_ca_file.
|
50
|
+
# * :+ssl_ca_path+: see HTTParty::ClassMethods.ssl_ca_path.
|
51
|
+
# * :+connection_adapter_options+: contains the hash you passed to HTTParty.connection_adapter when you configured your connection adapter
|
52
|
+
class ConnectionAdapter
|
53
|
+
# Private: Regex used to strip brackets from IPv6 URIs.
|
54
|
+
StripIpv6BracketsRegex = /\A\[(.*)\]\z/
|
55
|
+
|
56
|
+
# Public
|
57
|
+
def self.call(uri, options)
|
58
|
+
new(uri, options).connection
|
59
|
+
end
|
60
|
+
|
61
|
+
attr_reader :uri, :options
|
62
|
+
|
63
|
+
def initialize(uri, options = {})
|
64
|
+
uri_adapter = options[:uri_adapter] || URI
|
65
|
+
raise ArgumentError, "uri must be a #{uri_adapter}, not a #{uri.class}" unless uri.is_a? uri_adapter
|
66
|
+
|
67
|
+
@uri = uri
|
68
|
+
@options = options
|
69
|
+
end
|
70
|
+
|
71
|
+
def connection
|
72
|
+
host = clean_host(uri.host)
|
73
|
+
port = uri.port || (uri.scheme == 'https' ? 443 : 80)
|
74
|
+
if options[:http_proxyaddr]
|
75
|
+
http = Net::HTTP.new(host, port, options[:http_proxyaddr], options[:http_proxyport], options[:http_proxyuser], options[:http_proxypass])
|
76
|
+
else
|
77
|
+
http = Net::HTTP.new(host, port)
|
78
|
+
end
|
79
|
+
|
80
|
+
http.use_ssl = ssl_implied?(uri)
|
81
|
+
|
82
|
+
attach_ssl_certificates(http, options)
|
83
|
+
|
84
|
+
if options[:timeout] && (options[:timeout].is_a?(Integer) || options[:timeout].is_a?(Float))
|
85
|
+
http.open_timeout = options[:timeout]
|
86
|
+
http.read_timeout = options[:timeout]
|
87
|
+
end
|
88
|
+
|
89
|
+
if options[:read_timeout] && (options[:read_timeout].is_a?(Integer) || options[:read_timeout].is_a?(Float))
|
90
|
+
http.read_timeout = options[:read_timeout]
|
91
|
+
end
|
92
|
+
|
93
|
+
if options[:open_timeout] && (options[:open_timeout].is_a?(Integer) || options[:open_timeout].is_a?(Float))
|
94
|
+
http.open_timeout = options[:open_timeout]
|
95
|
+
end
|
96
|
+
|
97
|
+
if options[:debug_output]
|
98
|
+
http.set_debug_output(options[:debug_output])
|
99
|
+
end
|
100
|
+
|
101
|
+
if options[:ciphers]
|
102
|
+
http.ciphers = options[:ciphers]
|
103
|
+
end
|
104
|
+
|
105
|
+
# Bind to a specific local address or port
|
106
|
+
#
|
107
|
+
# @see https://bugs.ruby-lang.org/issues/6617
|
108
|
+
if options[:local_host]
|
109
|
+
if RUBY_VERSION >= "2.0.0"
|
110
|
+
http.local_host = options[:local_host]
|
111
|
+
else
|
112
|
+
Kernel.warn("Warning: option :local_host requires Ruby version 2.0 or later")
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
if options[:local_port]
|
117
|
+
if RUBY_VERSION >= "2.0.0"
|
118
|
+
http.local_port = options[:local_port]
|
119
|
+
else
|
120
|
+
Kernel.warn("Warning: option :local_port requires Ruby version 2.0 or later")
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
http
|
125
|
+
end
|
126
|
+
|
127
|
+
private
|
128
|
+
|
129
|
+
def clean_host(host)
|
130
|
+
strip_ipv6_brackets(host)
|
131
|
+
end
|
132
|
+
|
133
|
+
def strip_ipv6_brackets(host)
|
134
|
+
StripIpv6BracketsRegex =~ host ? $1 : host
|
135
|
+
end
|
136
|
+
|
137
|
+
def ssl_implied?(uri)
|
138
|
+
uri.port == 443 || uri.scheme == 'https'
|
139
|
+
end
|
140
|
+
|
141
|
+
def attach_ssl_certificates(http, options)
|
142
|
+
if http.use_ssl?
|
143
|
+
if options.fetch(:verify, true)
|
144
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
|
145
|
+
if options[:cert_store]
|
146
|
+
http.cert_store = options[:cert_store]
|
147
|
+
else
|
148
|
+
# Use the default cert store by default, i.e. system ca certs
|
149
|
+
http.cert_store = OpenSSL::X509::Store.new
|
150
|
+
http.cert_store.set_default_paths
|
151
|
+
end
|
152
|
+
else
|
153
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
154
|
+
end
|
155
|
+
|
156
|
+
# Client certificate authentication
|
157
|
+
# Note: options[:pem] must contain the content of a PEM file having the private key appended
|
158
|
+
if options[:pem]
|
159
|
+
http.cert = OpenSSL::X509::Certificate.new(options[:pem])
|
160
|
+
http.key = OpenSSL::PKey::RSA.new(options[:pem], options[:pem_password])
|
161
|
+
http.verify_mode = options[:verify_peer] == false ? OpenSSL::SSL::VERIFY_NONE : OpenSSL::SSL::VERIFY_PEER
|
162
|
+
end
|
163
|
+
|
164
|
+
# PKCS12 client certificate authentication
|
165
|
+
if options[:p12]
|
166
|
+
p12 = OpenSSL::PKCS12.new(options[:p12], options[:p12_password])
|
167
|
+
http.cert = p12.certificate
|
168
|
+
http.key = p12.key
|
169
|
+
http.verify_mode = options[:verify_peer] == false ? OpenSSL::SSL::VERIFY_NONE : OpenSSL::SSL::VERIFY_PEER
|
170
|
+
end
|
171
|
+
|
172
|
+
# SSL certificate authority file and/or directory
|
173
|
+
if options[:ssl_ca_file]
|
174
|
+
http.ca_file = options[:ssl_ca_file]
|
175
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
|
176
|
+
end
|
177
|
+
|
178
|
+
if options[:ssl_ca_path]
|
179
|
+
http.ca_path = options[:ssl_ca_path]
|
180
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
|
181
|
+
end
|
182
|
+
|
183
|
+
# This is only Ruby 1.9+
|
184
|
+
if options[:ssl_version] && http.respond_to?(:ssl_version=)
|
185
|
+
http.ssl_version = options[:ssl_version]
|
186
|
+
end
|
187
|
+
end
|
188
|
+
end
|
189
|
+
end
|
190
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
class HTTParty::CookieHash < Hash #:nodoc:
|
2
|
+
CLIENT_COOKIES = %w(path expires domain path secure httponly)
|
3
|
+
|
4
|
+
def add_cookies(value)
|
5
|
+
case value
|
6
|
+
when Hash
|
7
|
+
merge!(value)
|
8
|
+
when String
|
9
|
+
value.split('; ').each do |cookie|
|
10
|
+
array = cookie.split('=', 2)
|
11
|
+
self[array[0].to_sym] = array[1]
|
12
|
+
end
|
13
|
+
else
|
14
|
+
raise "add_cookies only takes a Hash or a String"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def to_cookie_string
|
19
|
+
delete_if { |k, v| CLIENT_COOKIES.include?(k.to_s.downcase) }.collect { |k, v| "#{k}=#{v}" }.join("; ")
|
20
|
+
end
|
21
|
+
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,49 @@
|
|
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
|
+
hash.to_hash.map { |k, v| normalize_param(k, v) }.join.chop
|
16
|
+
end
|
17
|
+
|
18
|
+
# @param key<Object> The key for the param.
|
19
|
+
# @param value<Object> The value for the param.
|
20
|
+
#
|
21
|
+
# @return <String> This key value pair as a param
|
22
|
+
#
|
23
|
+
# @example normalize_param(:name, "Bob Jones") #=> "name=Bob%20Jones&"
|
24
|
+
def self.normalize_param(key, value)
|
25
|
+
param = ''
|
26
|
+
stack = []
|
27
|
+
|
28
|
+
if value.respond_to?(:to_ary)
|
29
|
+
param << value.to_ary.map { |element| normalize_param("#{key}[]", element) }.join
|
30
|
+
elsif value.respond_to?(:to_hash)
|
31
|
+
stack << [key, value.to_hash]
|
32
|
+
else
|
33
|
+
param << "#{key}=#{ERB::Util.url_encode(value.to_s)}&"
|
34
|
+
end
|
35
|
+
|
36
|
+
stack.each do |parent, hash|
|
37
|
+
hash.each do |k, v|
|
38
|
+
if v.respond_to?(:to_hash)
|
39
|
+
stack << ["#{parent}[#{k}]", v.to_hash]
|
40
|
+
else
|
41
|
+
param << normalize_param("#{parent}[#{k}]", v)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
param
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module HTTParty
|
2
|
+
module Logger
|
3
|
+
class ApacheFormatter #: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.respond_to?(:headers) ? response.headers['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 CurlFormatter #: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,26 @@
|
|
1
|
+
require 'httparty/logger/apache_formatter'
|
2
|
+
require 'httparty/logger/curl_formatter'
|
3
|
+
|
4
|
+
module HTTParty
|
5
|
+
module Logger
|
6
|
+
def self.formatters
|
7
|
+
@formatters ||= {
|
8
|
+
:curl => Logger::CurlFormatter,
|
9
|
+
:apache => Logger::ApacheFormatter
|
10
|
+
}
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.add_formatter(name, formatter)
|
14
|
+
raise HTTParty::Error.new("Log Formatter with name #{name} already exists") if formatters.include?(name)
|
15
|
+
formatters.merge!(name.to_sym => formatter)
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.build(logger, level, formatter)
|
19
|
+
level ||= :info
|
20
|
+
formatter ||= :apache
|
21
|
+
|
22
|
+
logger_klass = formatters[formatter] || Logger::ApacheFormatter
|
23
|
+
logger_klass.new(logger, level)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
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
|