Vendoring Rails 2.3.5

This commit is contained in:
Eric Allen 2009-12-07 12:42:42 -05:00
parent 3e83d19299
commit f8779795ce
943 changed files with 56503 additions and 61351 deletions

View file

@ -93,6 +93,8 @@ module ActiveResource
#
# Many REST APIs will require authentication, usually in the form of basic
# HTTP authentication. Authentication can be specified by:
#
# === HTTP Basic Authentication
# * putting the credentials in the URL for the +site+ variable.
#
# class Person < ActiveResource::Base
@ -112,6 +114,19 @@ module ActiveResource
#
# Note: Some values cannot be provided in the URL passed to site. e.g. email addresses
# as usernames. In those situations you should use the separate user and password option.
#
# === Certificate Authentication
#
# * End point uses an X509 certificate for authentication. <tt>See ssl_options=</tt> for all options.
#
# class Person < ActiveResource::Base
# self.site = "https://secure.api.people.com/"
# self.ssl_options = {:cert => OpenSSL::X509::Certificate.new(File.open(pem_file))
# :key => OpenSSL::PKey::RSA.new(File.open(pem_file)),
# :ca_path => "/path/to/OpenSSL/formatted/CA_Certs",
# :verify_mode => OpenSSL::SSL::VERIFY_PEER}
# end
#
# == Errors & Validation
#
# Error handling and validation is handled in much the same manner as you're used to seeing in
@ -138,6 +153,7 @@ module ActiveResource
# * 404 - ActiveResource::ResourceNotFound
# * 405 - ActiveResource::MethodNotAllowed
# * 409 - ActiveResource::ResourceConflict
# * 410 - ActiveResource::ResourceGone
# * 422 - ActiveResource::ResourceInvalid (rescued by save as validation errors)
# * 401..499 - ActiveResource::ClientError
# * 500..599 - ActiveResource::ServerError
@ -158,7 +174,7 @@ module ActiveResource
#
# Active Resource supports validations on resources and will return errors if any these validations fail
# (e.g., "First name can not be blank" and so on). These types of errors are denoted in the response by
# a response code of <tt>422</tt> and an XML representation of the validation errors. The save operation will
# a response code of <tt>422</tt> and an XML or JSON representation of the validation errors. The save operation will
# then fail (with a <tt>false</tt> return value) and the validation errors can be accessed on the resource in question.
#
# ryan = Person.find(1)
@ -167,10 +183,14 @@ module ActiveResource
#
# # When
# # PUT http://api.people.com:3000/people/1.xml
# # or
# # PUT http://api.people.com:3000/people/1.json
# # is requested with invalid values, the response is:
# #
# # Response (422):
# # <errors type="array"><error>First cannot be empty</error></errors>
# # or
# # {"errors":["First cannot be empty"]}
# #
#
# ryan.errors.invalid?(:first) # => true
@ -202,6 +222,8 @@ module ActiveResource
# sets the <tt>read_timeout</tt> of the internal Net::HTTP instance to the same value. The default
# <tt>read_timeout</tt> is 60 seconds on most Ruby implementations.
class Base
##
# :singleton-method:
# The logger for diagnosing and tracing Active Resource calls.
cattr_accessor :logger
@ -244,6 +266,22 @@ module ActiveResource
end
end
# Gets the \proxy variable if a proxy is required
def proxy
# Not using superclass_delegating_reader. See +site+ for explanation
if defined?(@proxy)
@proxy
elsif superclass != Object && superclass.proxy
superclass.proxy.dup.freeze
end
end
# Sets the URI of the http proxy to the value in the +proxy+ argument.
def proxy=(proxy)
@connection = nil
@proxy = proxy.nil? ? nil : create_proxy_uri_from(proxy)
end
# Gets the \user for REST HTTP authentication.
def user
# Not using superclass_delegating_reader. See +site+ for explanation
@ -313,15 +351,42 @@ module ActiveResource
end
end
# Options that will get applied to an SSL connection.
#
# * <tt>:key</tt> - An OpenSSL::PKey::RSA or OpenSSL::PKey::DSA object.
# * <tt>:cert</tt> - An OpenSSL::X509::Certificate object as client certificate
# * <tt>:ca_file</tt> - Path to a CA certification file in PEM format. The file can contrain several CA certificates.
# * <tt>:ca_path</tt> - Path of a CA certification directory containing certifications in PEM format.
# * <tt>:verify_mode</tt> - Flags for server the certification verification at begining of SSL/TLS session. (OpenSSL::SSL::VERIFY_NONE or OpenSSL::SSL::VERIFY_PEER is acceptable)
# * <tt>:verify_callback</tt> - The verify callback for the server certification verification.
# * <tt>:verify_depth</tt> - The maximum depth for the certificate chain verification.
# * <tt>:cert_store</tt> - OpenSSL::X509::Store to verify peer certificate.
# * <tt>:ssl_timeout</tt> -The SSL timeout in seconds.
def ssl_options=(opts={})
@connection = nil
@ssl_options = opts
end
# Returns the SSL options hash.
def ssl_options
if defined?(@ssl_options)
@ssl_options
elsif superclass != Object && superclass.ssl_options
superclass.ssl_options
end
end
# An instance of ActiveResource::Connection that is the base \connection to the remote service.
# The +refresh+ parameter toggles whether or not the \connection is refreshed at every request
# or not (defaults to <tt>false</tt>).
def connection(refresh = false)
if defined?(@connection) || superclass == Object
@connection = Connection.new(site, format) if refresh || @connection.nil?
@connection.proxy = proxy if proxy
@connection.user = user if user
@connection.password = password if password
@connection.timeout = timeout if timeout
@connection.ssl_options = ssl_options if ssl_options
@connection
else
superclass.connection
@ -460,7 +525,7 @@ module ActiveResource
# that_guy.valid? # => false
# that_guy.new? # => true
def create(attributes = {})
returning(self.new(attributes)) { |res| res.save }
self.new(attributes).tap { |resource| resource.save }
end
# Core method for finding resources. Used similarly to Active Record's +find+ method.
@ -555,7 +620,7 @@ module ActiveResource
response.code.to_i == 200
end
# id && !find_single(id, options).nil?
rescue ActiveResource::ResourceNotFound
rescue ActiveResource::ResourceNotFound, ActiveResource::ResourceGone
false
end
@ -598,7 +663,7 @@ module ActiveResource
end
def instantiate_record(record, prefix_options = {})
returning new(record) do |resource|
new(record).tap do |resource|
resource.prefix_options = prefix_options
end
end
@ -609,6 +674,11 @@ module ActiveResource
site.is_a?(URI) ? site.dup : URI.parse(site)
end
# Accepts a URI and creates the proxy URI from that.
def create_proxy_uri_from(proxy)
proxy.is_a?(URI) ? proxy.dup : URI.parse(proxy)
end
# contains a set of the current prefix parameters.
def prefix_parameters
@prefix_parameters ||= prefix_source.scan(/:\w+/).map { |key| key[1..-1].to_sym }.to_set
@ -704,6 +774,7 @@ module ActiveResource
def new?
id.nil?
end
alias :new_record? :new?
# Gets the <tt>\id</tt> attribute of the resource.
def id
@ -743,7 +814,7 @@ module ActiveResource
# # => true
#
def ==(other)
other.equal?(self) || (other.instance_of?(self.class) && !other.new? && other.id == id)
other.equal?(self) || (other.instance_of?(self.class) && other.id == id && other.prefix_options == prefix_options)
end
# Tests for equality (delegates to ==).
@ -770,7 +841,7 @@ module ActiveResource
# my_invoice.customer # => That Company
# next_invoice.customer # => That Company
def dup
returning self.class.new do |resource|
self.class.new.tap do |resource|
resource.attributes = @attributes
resource.prefix_options = @prefix_options
end
@ -829,7 +900,7 @@ module ActiveResource
!new? && self.class.exists?(to_param, :params => prefix_options)
end
# A method to convert the the resource to an XML string.
# Converts the resource to an XML string representation.
#
# ==== Options
# The +options+ parameter is handed off to the +to_xml+ method on each
@ -838,7 +909,14 @@ module ActiveResource
#
# * <tt>:indent</tt> - Set the indent level for the XML output (default is +2+).
# * <tt>:dasherize</tt> - Boolean option to determine whether or not element names should
# replace underscores with dashes (default is <tt>false</tt>).
# replace underscores with dashes. Default is <tt>true</tt>. The default can be set to <tt>false</tt>
# by setting the module attribute <tt>ActiveSupport.dasherize_xml = false</tt> in an initializer. Because save
# uses this method, and there are no options on save, then you will have to set the default if you don't
# want underscores in element names to become dashes when the resource is saved. This is important when
# integrating with non-Rails applications.
# * <tt>:camelize</tt> - Boolean option to determine whether or not element names should be converted
# to camel case, e.g some_name to SomeName. Default is <tt>false</tt>. Like <tt>:dasherize</tt> you can
# change the default by setting the module attribute <tt>ActiveSupport.camelise_xml = true</tt> in an initializer.
# * <tt>:skip_instruct</tt> - Toggle skipping the +instruct!+ call on the XML builder
# that generates the XML declaration (default is <tt>false</tt>).
#
@ -858,8 +936,7 @@ module ActiveResource
attributes.to_xml({:root => self.class.element_name}.merge(options))
end
# Returns a JSON string representing the model. Some configuration is
# available through +options+.
# Coerces to a hash for JSON encoding.
#
# ==== Options
# The +options+ are passed to the +to_json+ method on each
@ -883,8 +960,8 @@ module ActiveResource
#
# person.to_json(:except => ["first_name"])
# # => {"last_name": "Smith"}
def to_json(options={})
attributes.to_json(options)
def as_json(options = nil)
attributes.as_json(options)
end
# Returns the serialized string representation of the resource in the configured
@ -943,7 +1020,13 @@ module ActiveResource
case value
when Array
resource = find_or_create_resource_for_collection(key)
value.map { |attrs| attrs.is_a?(String) ? attrs.dup : resource.new(attrs) }
value.map do |attrs|
if attrs.is_a?(String) || attrs.is_a?(Numeric)
attrs.duplicable? ? attrs.dup : attrs
else
resource.new(attrs)
end
end
when Hash
resource = find_or_create_resource_for(key)
resource.new(value)
@ -982,14 +1065,14 @@ module ActiveResource
# Update the resource on the remote service.
def update
returning connection.put(element_path(prefix_options), encode, self.class.headers) do |response|
connection.put(element_path(prefix_options), encode, self.class.headers).tap do |response|
load_attributes_from_response(response)
end
end
# Create (i.e., \save to the remote service) the \new resource.
def create
returning connection.post(collection_path, encode, self.class.headers) do |response|
connection.post(collection_path, encode, self.class.headers).tap do |response|
self.id = id_from_response(response)
load_attributes_from_response(response)
end
@ -1003,7 +1086,7 @@ module ActiveResource
# Takes a response from a typical create post and pulls the ID out
def id_from_response(response)
response['Location'][/\/([^\/]*?)(\.\w+)?$/, 1]
response['Location'][/\/([^\/]*?)(\.\w+)?$/, 1] if response['Location']
end
def element_path(options = nil)