mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge branch 'master' of git://github.com/rails/rails
This commit is contained in:
commit
118b19a9fa
12 changed files with 105 additions and 102 deletions
|
@ -1,12 +1,3 @@
|
|||
begin
|
||||
require 'active_support'
|
||||
rescue LoadError
|
||||
activesupport_path = "#{File.dirname(__FILE__)}/../../../activesupport/lib"
|
||||
if File.directory?(activesupport_path)
|
||||
$:.unshift activesupport_path
|
||||
require 'active_support'
|
||||
end
|
||||
end
|
||||
|
||||
# So far, we only need the string inflections and not the rest of ActiveSupport.
|
||||
activesupport_path = "#{File.dirname(__FILE__)}/../../../activesupport/lib"
|
||||
$:.unshift(activesupport_path) if File.directory?(activesupport_path)
|
||||
require 'active_support/inflector'
|
||||
|
|
|
@ -21,25 +21,10 @@
|
|||
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
#++
|
||||
|
||||
begin
|
||||
require 'active_support'
|
||||
rescue LoadError
|
||||
activesupport_path = "#{File.dirname(__FILE__)}/../../activesupport/lib"
|
||||
if File.directory?(activesupport_path)
|
||||
$:.unshift activesupport_path
|
||||
require 'active_support'
|
||||
end
|
||||
end
|
||||
require 'active_support/core/all'
|
||||
|
||||
require 'active_resource/formats'
|
||||
require 'active_resource/base'
|
||||
require 'active_resource/validations'
|
||||
require 'active_resource/custom_methods'
|
||||
activesupport_path = "#{File.dirname(__FILE__)}/../../activesupport/lib"
|
||||
$:.unshift(activesupport_path) if File.directory?(activesupport_path)
|
||||
require 'active_support'
|
||||
|
||||
module ActiveResource
|
||||
Base.class_eval do
|
||||
include Validations
|
||||
include CustomMethods
|
||||
end
|
||||
autoload :Base, 'active_resource/base'
|
||||
end
|
||||
|
|
|
@ -1,8 +1,14 @@
|
|||
require 'active_resource/connection'
|
||||
require 'cgi'
|
||||
require 'active_support/core_ext/class/attribute_accessors'
|
||||
require 'active_support/core_ext/class/inheritable_attributes'
|
||||
require 'active_support/core_ext/module/attr_accessor_with_default'
|
||||
require 'active_support/core_ext/module/delegation'
|
||||
require 'active_support/core_ext/module/aliasing'
|
||||
require 'set'
|
||||
|
||||
module ActiveResource
|
||||
autoload :Formats, 'active_resource/formats'
|
||||
autoload :Connection, 'active_resource/connection'
|
||||
|
||||
# ActiveResource::Base is the main class for mapping RESTful resources as models in a Rails application.
|
||||
#
|
||||
# For an outline of what Active Resource is capable of, see link:files/vendor/rails/activeresource/README.html.
|
||||
|
@ -298,7 +304,7 @@ module ActiveResource
|
|||
|
||||
# Returns the current format, default is ActiveResource::Formats::XmlFormat.
|
||||
def format
|
||||
read_inheritable_attribute(:format) || ActiveResource::Formats[:xml]
|
||||
read_inheritable_attribute(:format) || ActiveResource::Formats::XmlFormat
|
||||
end
|
||||
|
||||
# Sets the number of seconds after which requests to the REST API should time out.
|
||||
|
@ -895,7 +901,7 @@ module ActiveResource
|
|||
# applicable depend on the configured encoding format.
|
||||
def encode(options={})
|
||||
case self.class.format
|
||||
when ActiveResource::Formats[:xml]
|
||||
when ActiveResource::Formats::XmlFormat
|
||||
self.class.format.encode(attributes, {:root => self.class.element_name}.merge(options))
|
||||
else
|
||||
self.class.format.encode(attributes, options)
|
||||
|
@ -1080,3 +1086,6 @@ module ActiveResource
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
require 'active_resource/validations'
|
||||
require 'active_resource/custom_methods'
|
||||
|
|
|
@ -1,64 +1,12 @@
|
|||
require 'active_resource/exceptions'
|
||||
require 'active_resource/formats'
|
||||
require 'active_support/core_ext/benchmark'
|
||||
require 'net/https'
|
||||
require 'date'
|
||||
require 'time'
|
||||
require 'uri'
|
||||
require 'benchmark'
|
||||
|
||||
module ActiveResource
|
||||
class ConnectionError < StandardError # :nodoc:
|
||||
attr_reader :response
|
||||
|
||||
def initialize(response, message = nil)
|
||||
@response = response
|
||||
@message = message
|
||||
end
|
||||
|
||||
def to_s
|
||||
"Failed with #{response.code} #{response.message if response.respond_to?(:message)}"
|
||||
end
|
||||
end
|
||||
|
||||
# Raised when a Timeout::Error occurs.
|
||||
class TimeoutError < ConnectionError
|
||||
def initialize(message)
|
||||
@message = message
|
||||
end
|
||||
def to_s; @message ;end
|
||||
end
|
||||
|
||||
# 3xx Redirection
|
||||
class Redirection < ConnectionError # :nodoc:
|
||||
def to_s; response['Location'] ? "#{super} => #{response['Location']}" : super; end
|
||||
end
|
||||
|
||||
# 4xx Client Error
|
||||
class ClientError < ConnectionError; end # :nodoc:
|
||||
|
||||
# 400 Bad Request
|
||||
class BadRequest < ClientError; end # :nodoc
|
||||
|
||||
# 401 Unauthorized
|
||||
class UnauthorizedAccess < ClientError; end # :nodoc
|
||||
|
||||
# 403 Forbidden
|
||||
class ForbiddenAccess < ClientError; end # :nodoc
|
||||
|
||||
# 404 Not Found
|
||||
class ResourceNotFound < ClientError; end # :nodoc:
|
||||
|
||||
# 409 Conflict
|
||||
class ResourceConflict < ClientError; end # :nodoc:
|
||||
|
||||
# 5xx Server Error
|
||||
class ServerError < ConnectionError; end # :nodoc:
|
||||
|
||||
# 405 Method Not Allowed
|
||||
class MethodNotAllowed < ClientError # :nodoc:
|
||||
def allowed_methods
|
||||
@response['Allow'].split(',').map { |verb| verb.strip.downcase.to_sym }
|
||||
end
|
||||
end
|
||||
|
||||
# Class to handle connections to remote web services.
|
||||
# This class is used by ActiveResource::Base to interface with REST
|
||||
# services.
|
||||
|
@ -81,7 +29,7 @@ module ActiveResource
|
|||
|
||||
# The +site+ parameter is required and will set the +site+
|
||||
# attribute to the URI for the remote resource service.
|
||||
def initialize(site, format = ActiveResource::Formats[:xml])
|
||||
def initialize(site, format = ActiveResource::Formats::XmlFormat)
|
||||
raise ArgumentError, 'Missing site URI' unless site
|
||||
@user = @password = nil
|
||||
self.site = site
|
||||
|
|
|
@ -117,4 +117,8 @@ module ActiveResource
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
class Base
|
||||
include CustomMethods
|
||||
end
|
||||
end
|
||||
|
|
55
activeresource/lib/active_resource/exceptions.rb
Normal file
55
activeresource/lib/active_resource/exceptions.rb
Normal file
|
@ -0,0 +1,55 @@
|
|||
module ActiveResource
|
||||
class ConnectionError < StandardError # :nodoc:
|
||||
attr_reader :response
|
||||
|
||||
def initialize(response, message = nil)
|
||||
@response = response
|
||||
@message = message
|
||||
end
|
||||
|
||||
def to_s
|
||||
"Failed with #{response.code} #{response.message if response.respond_to?(:message)}"
|
||||
end
|
||||
end
|
||||
|
||||
# Raised when a Timeout::Error occurs.
|
||||
class TimeoutError < ConnectionError
|
||||
def initialize(message)
|
||||
@message = message
|
||||
end
|
||||
def to_s; @message ;end
|
||||
end
|
||||
|
||||
# 3xx Redirection
|
||||
class Redirection < ConnectionError # :nodoc:
|
||||
def to_s; response['Location'] ? "#{super} => #{response['Location']}" : super; end
|
||||
end
|
||||
|
||||
# 4xx Client Error
|
||||
class ClientError < ConnectionError; end # :nodoc:
|
||||
|
||||
# 400 Bad Request
|
||||
class BadRequest < ClientError; end # :nodoc
|
||||
|
||||
# 401 Unauthorized
|
||||
class UnauthorizedAccess < ClientError; end # :nodoc
|
||||
|
||||
# 403 Forbidden
|
||||
class ForbiddenAccess < ClientError; end # :nodoc
|
||||
|
||||
# 404 Not Found
|
||||
class ResourceNotFound < ClientError; end # :nodoc:
|
||||
|
||||
# 409 Conflict
|
||||
class ResourceConflict < ClientError; end # :nodoc:
|
||||
|
||||
# 5xx Server Error
|
||||
class ServerError < ConnectionError; end # :nodoc:
|
||||
|
||||
# 405 Method Not Allowed
|
||||
class MethodNotAllowed < ClientError # :nodoc:
|
||||
def allowed_methods
|
||||
@response['Allow'].split(',').map { |verb| verb.strip.downcase.to_sym }
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,14 +1,14 @@
|
|||
module ActiveResource
|
||||
module Formats
|
||||
autoload :XmlFormat, 'active_resource/formats/xml_format'
|
||||
autoload :JsonFormat, 'active_resource/formats/json_format'
|
||||
|
||||
# Lookup the format class from a mime type reference symbol. Example:
|
||||
#
|
||||
# ActiveResource::Formats[:xml] # => ActiveResource::Formats::XmlFormat
|
||||
# ActiveResource::Formats[:json] # => ActiveResource::Formats::JsonFormat
|
||||
def self.[](mime_type_reference)
|
||||
ActiveResource::Formats.const_get(mime_type_reference.to_s.camelize + "Format")
|
||||
ActiveResource::Formats.const_get(ActiveSupport::Inflector.camelize(mime_type_reference.to_s) + "Format")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
require 'active_resource/formats/xml_format'
|
||||
require 'active_resource/formats/json_format'
|
|
@ -1,3 +1,5 @@
|
|||
require 'active_support/core_ext/hash/conversions'
|
||||
|
||||
module ActiveResource
|
||||
module Formats
|
||||
module XmlFormat
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
require 'active_resource/connection'
|
||||
require 'active_support/core_ext/kernel/reporting'
|
||||
|
||||
module ActiveResource
|
||||
class InvalidRequestError < StandardError; end #:nodoc:
|
||||
|
@ -129,7 +130,11 @@ module ActiveResource
|
|||
def #{method}(path, #{'body, ' if has_body}headers)
|
||||
request = ActiveResource::Request.new(:#{method}, path, #{has_body ? 'body, ' : 'nil, '}headers)
|
||||
self.class.requests << request
|
||||
self.class.responses.assoc(request).try(:second) || raise(InvalidRequestError.new("No response recorded for \#{request}"))
|
||||
if response = self.class.responses.assoc(request)
|
||||
response[1]
|
||||
else
|
||||
raise InvalidRequestError.new("No response recorded for \#{request}")
|
||||
end
|
||||
end
|
||||
EOE
|
||||
end
|
||||
|
|
|
@ -1,3 +1,6 @@
|
|||
require 'active_resource/exceptions'
|
||||
require 'active_support/core_ext/array/wrap'
|
||||
|
||||
module ActiveResource
|
||||
class ResourceInvalid < ClientError #:nodoc:
|
||||
end
|
||||
|
@ -272,4 +275,8 @@ module ActiveResource
|
|||
@errors ||= Errors.new(self)
|
||||
end
|
||||
end
|
||||
|
||||
class Base
|
||||
include Validations
|
||||
end
|
||||
end
|
||||
|
|
|
@ -5,19 +5,16 @@ gem 'mocha', '>= 0.9.5'
|
|||
require 'mocha'
|
||||
|
||||
$:.unshift "#{File.dirname(__FILE__)}/../lib"
|
||||
$:.unshift "#{File.dirname(__FILE__)}/../../activesupport/lib"
|
||||
require 'active_resource'
|
||||
require 'active_resource/http_mock'
|
||||
|
||||
$:.unshift "#{File.dirname(__FILE__)}/../test"
|
||||
require 'setter_trap'
|
||||
|
||||
require 'logger'
|
||||
ActiveResource::Base.logger = Logger.new("#{File.dirname(__FILE__)}/debug.log")
|
||||
|
||||
def uses_gem(gem_name, test_name, version = '> 0')
|
||||
gem gem_name.to_s, version
|
||||
require gem_name.to_s
|
||||
yield
|
||||
begin
|
||||
require 'ruby-debug'
|
||||
rescue LoadError
|
||||
$stderr.puts "Skipping #{test_name} tests. `gem install #{gem_name}` and try again."
|
||||
end
|
||||
|
|
|
@ -853,7 +853,7 @@ class BaseTest < Test::Unit::TestCase
|
|||
def test_to_xml
|
||||
matz = Person.find(1)
|
||||
xml = matz.encode
|
||||
assert xml.starts_with?('<?xml version="1.0" encoding="UTF-8"?>')
|
||||
assert xml.include?('<?xml version="1.0" encoding="UTF-8"?>')
|
||||
assert xml.include?('<name>Matz</name>')
|
||||
assert xml.include?('<id type="integer">1</id>')
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue