Skip to content

Commit

Permalink
corrected some further tests and docs for the ARes change to json change
Browse files Browse the repository at this point in the history
  • Loading branch information
joshk committed May 18, 2011
1 parent b641ab5 commit 0095869
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 108 deletions.
72 changes: 36 additions & 36 deletions activeresource/lib/active_resource/base.rb
Expand Up @@ -81,19 +81,19 @@ module ActiveResource
# <tt>post</tt>, <tt>put</tt> and <tt>\delete</tt> methods where you can specify a custom REST method
# name to invoke.
#
# # POST to the custom 'register' REST method, i.e. POST /people/new/register.xml.
# # POST to the custom 'register' REST method, i.e. POST /people/new/register.json.
# Person.new(:name => 'Ryan').post(:register)
# # => { :id => 1, :name => 'Ryan', :position => 'Clerk' }
#
# # PUT an update by invoking the 'promote' REST method, i.e. PUT /people/1/promote.xml?position=Manager.
# # PUT an update by invoking the 'promote' REST method, i.e. PUT /people/1/promote.json?position=Manager.
# Person.find(1).put(:promote, :position => 'Manager')
# # => { :id => 1, :name => 'Ryan', :position => 'Manager' }
#
# # GET all the positions available, i.e. GET /people/positions.xml.
# # GET all the positions available, i.e. GET /people/positions.json.
# Person.get(:positions)
# # => [{:name => 'Manager'}, {:name => 'Clerk'}]
#
# # DELETE to 'fire' a person, i.e. DELETE /people/1/fire.xml.
# # DELETE to 'fire' a person, i.e. DELETE /people/1/fire.json.
# Person.find(1).delete(:fire)
#
# For more information on using custom REST methods, see the
Expand Down Expand Up @@ -164,7 +164,7 @@ module ActiveResource
# response code will be returned from the server which will raise an ActiveResource::ResourceNotFound
# exception.
#
# # GET http://api.people.com:3000/people/999.xml
# # GET http://api.people.com:3000/people/999.json
# ryan = Person.find(999) # 404, raises ActiveResource::ResourceNotFound
#
#
Expand Down Expand Up @@ -218,7 +218,7 @@ module ActiveResource
# ryan.save # => false
#
# # When
# # PUT http://api.people.com:3000/people/1.xml
# # PUT http://api.people.com:3000/people/1.json
# # or
# # PUT http://api.people.com:3000/people/1.json
# # is requested with invalid values, the response is:
Expand Down Expand Up @@ -489,7 +489,7 @@ def auth_type=(auth_type)
# Person.format = ActiveResource::Formats::XmlFormat
# Person.find(1) # => GET /people/1.xml
#
# Default format is <tt>:xml</tt>.
# Default format is <tt>:json</tt>.
def format=(mime_type_reference_or_format)
format = mime_type_reference_or_format.is_a?(Symbol) ?
ActiveResource::Formats[mime_type_reference_or_format] : mime_type_reference_or_format
Expand Down Expand Up @@ -570,7 +570,7 @@ def headers

attr_accessor_with_default(:primary_key, 'id') #:nodoc:

# Gets the \prefix for a resource's nested URL (e.g., <tt>prefix/collectionname/1.xml</tt>)
# Gets the \prefix for a resource's nested URL (e.g., <tt>prefix/collectionname/1.json</tt>)
# This method is regenerated at runtime based on what the \prefix is set to.
def prefix(options={})
default = site.path
Expand All @@ -587,7 +587,7 @@ def prefix_source
prefix_source
end

# Sets the \prefix for a resource's nested URL (e.g., <tt>prefix/collectionname/1.xml</tt>).
# Sets the \prefix for a resource's nested URL (e.g., <tt>prefix/collectionname/1.json</tt>).
# Default value is <tt>site.path</tt>.
def prefix=(value = '/')
# Replace :placeholders with '#{embedded options[:lookups]}'
Expand Down Expand Up @@ -618,21 +618,21 @@ def prefix(options={}) "#{prefix_call}" end
#
# ==== Options
# +prefix_options+ - A \hash to add a \prefix to the request for nested URLs (e.g., <tt>:account_id => 19</tt>
# would yield a URL like <tt>/accounts/19/purchases.xml</tt>).
# would yield a URL like <tt>/accounts/19/purchases.json</tt>).
# +query_options+ - A \hash to add items to the query string for the request.
#
# ==== Examples
# Post.element_path(1)
# # => /posts/1.xml
# # => /posts/1.json
#
# Comment.element_path(1, :post_id => 5)
# # => /posts/5/comments/1.xml
# # => /posts/5/comments/1.json
#
# Comment.element_path(1, :post_id => 5, :active => 1)
# # => /posts/5/comments/1.xml?active=1
# # => /posts/5/comments/1.json?active=1
#
# Comment.element_path(1, {:post_id => 5}, {:active => 1})
# # => /posts/5/comments/1.xml?active=1
# # => /posts/5/comments/1.json?active=1
#
def element_path(id, prefix_options = {}, query_options = nil)
check_prefix_options(prefix_options)
Expand All @@ -645,14 +645,14 @@ def element_path(id, prefix_options = {}, query_options = nil)
#
# ==== Options
# * +prefix_options+ - A hash to add a prefix to the request for nested URLs (e.g., <tt>:account_id => 19</tt>
# would yield a URL like <tt>/accounts/19/purchases/new.xml</tt>).
# would yield a URL like <tt>/accounts/19/purchases/new.json</tt>).
#
# ==== Examples
# Post.new_element_path
# # => /posts/new.xml
# # => /posts/new.json
#
# Comment.collection_path(:post_id => 5)
# # => /posts/5/comments/new.xml
# # => /posts/5/comments/new.json
def new_element_path(prefix_options = {})
"#{prefix(prefix_options)}#{collection_name}/new.#{format.extension}"
end
Expand All @@ -662,21 +662,21 @@ def new_element_path(prefix_options = {})
#
# ==== Options
# * +prefix_options+ - A hash to add a prefix to the request for nested URLs (e.g., <tt>:account_id => 19</tt>
# would yield a URL like <tt>/accounts/19/purchases.xml</tt>).
# would yield a URL like <tt>/accounts/19/purchases.json</tt>).
# * +query_options+ - A hash to add items to the query string for the request.
#
# ==== Examples
# Post.collection_path
# # => /posts.xml
# # => /posts.json
#
# Comment.collection_path(:post_id => 5)
# # => /posts/5/comments.xml
# # => /posts/5/comments.json
#
# Comment.collection_path(:post_id => 5, :active => 1)
# # => /posts/5/comments.xml?active=1
# # => /posts/5/comments.json?active=1
#
# Comment.collection_path({:post_id => 5}, {:active => 1})
# # => /posts/5/comments.xml?active=1
# # => /posts/5/comments.json?active=1
#
def collection_path(prefix_options = {}, query_options = nil)
check_prefix_options(prefix_options)
Expand Down Expand Up @@ -745,34 +745,34 @@ def create(attributes = {})
#
# ==== Examples
# Person.find(1)
# # => GET /people/1.xml
# # => GET /people/1.json
#
# Person.find(:all)
# # => GET /people.xml
# # => GET /people.json
#
# Person.find(:all, :params => { :title => "CEO" })
# # => GET /people.xml?title=CEO
# # => GET /people.json?title=CEO
#
# Person.find(:first, :from => :managers)
# # => GET /people/managers.xml
# # => GET /people/managers.json
#
# Person.find(:last, :from => :managers)
# # => GET /people/managers.xml
# # => GET /people/managers.json
#
# Person.find(:all, :from => "/companies/1/people.xml")
# # => GET /companies/1/people.xml
# Person.find(:all, :from => "/companies/1/people.json")
# # => GET /companies/1/people.json
#
# Person.find(:one, :from => :leader)
# # => GET /people/leader.xml
# # => GET /people/leader.json
#
# Person.find(:all, :from => :developers, :params => { :language => 'ruby' })
# # => GET /people/developers.xml?language=ruby
# # => GET /people/developers.json?language=ruby
#
# Person.find(:one, :from => "/companies/1/manager.xml")
# # => GET /companies/1/manager.xml
# Person.find(:one, :from => "/companies/1/manager.json")
# # => GET /companies/1/manager.json
#
# StreetAddress.find(1, :params => { :person_id => 1 })
# # => GET /people/1/street_addresses/1.xml
# # => GET /people/1/street_addresses/1.json
#
# == Failure or missing data
# A failure to find the requested object raises a ResourceNotFound
Expand Down Expand Up @@ -833,7 +833,7 @@ def all(*args)
# my_event = Event.find(:first) # let's assume this is event with ID 7
# Event.delete(my_event.id) # sends DELETE /events/7
#
# # Let's assume a request to events/5/cancel.xml
# # Let's assume a request to events/5/cancel.json
# Event.delete(params[:id]) # sends DELETE /events/5
def delete(id, options = {})
connection.delete(element_path(id, options))
Expand Down Expand Up @@ -1121,7 +1121,7 @@ def dup

# Saves (+POST+) or \updates (+PUT+) a resource. Delegates to +create+ if the object is \new,
# +update+ if it exists. If the response to the \save includes a body, it will be assumed that this body
# is XML for the final object as it looked after the \save (which would include attributes like +created_at+
# is Json for the final object as it looked after the \save (which would include attributes like +created_at+
# that weren't part of the original submit).
#
# ==== Examples
Expand Down
20 changes: 10 additions & 10 deletions activeresource/lib/active_resource/custom_methods.rb
Expand Up @@ -11,10 +11,10 @@ module ActiveResource
#
# This route set creates routes for the following HTTP requests:
#
# POST /people/new/register.xml # PeopleController.register
# PUT /people/1/promote.xml # PeopleController.promote with :id => 1
# DELETE /people/1/deactivate.xml # PeopleController.deactivate with :id => 1
# GET /people/active.xml # PeopleController.active
# POST /people/new/register.json # PeopleController.register
# PUT /people/1/promote.json # PeopleController.promote with :id => 1
# DELETE /people/1/deactivate.json # PeopleController.deactivate with :id => 1
# GET /people/active.json # PeopleController.active
#
# Using this module, Active Resource can use these custom REST methods just like the
# standard methods.
Expand All @@ -23,13 +23,13 @@ module ActiveResource
# self.site = "http://37s.sunrise.i:3000"
# end
#
# Person.new(:name => 'Ryan).post(:register) # POST /people/new/register.xml
# Person.new(:name => 'Ryan).post(:register) # POST /people/new/register.json
# # => { :id => 1, :name => 'Ryan' }
#
# Person.find(1).put(:promote, :position => 'Manager') # PUT /people/1/promote.xml
# Person.find(1).delete(:deactivate) # DELETE /people/1/deactivate.xml
# Person.find(1).put(:promote, :position => 'Manager') # PUT /people/1/promote.json
# Person.find(1).delete(:deactivate) # DELETE /people/1/deactivate.json
#
# Person.get(:active) # GET /people/active.xml
# Person.get(:active) # GET /people/active.json
# # => [{:id => 1, :name => 'Ryan'}, {:id => 2, :name => 'Joe'}]
#
module CustomMethods
Expand All @@ -41,10 +41,10 @@ class << self

# Invokes a GET to a given custom REST method. For example:
#
# Person.get(:active) # GET /people/active.xml
# Person.get(:active) # GET /people/active.json
# # => [{:id => 1, :name => 'Ryan'}, {:id => 2, :name => 'Joe'}]
#
# Person.get(:active, :awesome => true) # GET /people/active.xml?awesome=true
# Person.get(:active, :awesome => true) # GET /people/active.json?awesome=true
# # => [{:id => 1, :name => 'Ryan'}]
#
# Note: the objects returned from this method are not automatically converted
Expand Down
46 changes: 23 additions & 23 deletions activeresource/lib/active_resource/http_mock.rb
Expand Up @@ -20,10 +20,10 @@ class InvalidRequestError < StandardError; end #:nodoc:
# * <tt>path</tt> - A string, starting with a "/", defining the URI that is expected to be
# called.
# * <tt>request_headers</tt> - Headers that are expected along with the request. This argument uses a
# hash format, such as <tt>{ "Content-Type" => "application/xml" }</tt>. This mock will only trigger
# hash format, such as <tt>{ "Content-Type" => "application/json" }</tt>. This mock will only trigger
# if your tests sends a request with identical headers.
# * <tt>body</tt> - The data to be returned. This should be a string of Active Resource parseable content,
# such as XML.
# such as Json.
# * <tt>status</tt> - The HTTP response code, as an integer, to return with the response.
# * <tt>response_headers</tt> - Headers to be returned with the response. Uses the same hash format as
# <tt>request_headers</tt> listed above.
Expand All @@ -35,12 +35,12 @@ class InvalidRequestError < StandardError; end #:nodoc:
#
# ==== Example
# def setup
# @matz = { :id => 1, :name => "Matz" }.to_xml(:root => "person")
# @matz = { :person => { :id => 1, :name => "Matz" } }.to_json
# ActiveResource::HttpMock.respond_to do |mock|
# mock.post "/people.xml", {}, @matz, 201, "Location" => "/people/1.xml"
# mock.get "/people/1.xml", {}, @matz
# mock.put "/people/1.xml", {}, nil, 204
# mock.delete "/people/1.xml", {}, nil, 200
# mock.post "/people.json", {}, @matz, 201, "Location" => "/people/1.json"
# mock.get "/people/1.json", {}, @matz
# mock.put "/people/1.json", {}, nil, 204
# mock.delete "/people/1.json", {}, nil, 200
# end
# end
#
Expand Down Expand Up @@ -85,17 +85,17 @@ class << self
#
# ==== Example
# def setup
# @matz = { :id => 1, :name => "Matz" }.to_xml(:root => "person")
# @matz = { :person => { :id => 1, :name => "Matz" } }.to_json
# ActiveResource::HttpMock.respond_to do |mock|
# mock.get "/people/1.xml", {}, @matz
# mock.get "/people/1.json", {}, @matz
# end
# end
#
# def test_should_request_remote_service
# person = Person.find(1) # Call the remote service
#
# # This request object has the same HTTP method and path as declared by the mock
# expected_request = ActiveResource::Request.new(:get, "/people/1.xml")
# expected_request = ActiveResource::Request.new(:get, "/people/1.json")
#
# # Assert that the mock received, and responded to, the expected request from the model
# assert ActiveResource::HttpMock.requests.include?(expected_request)
Expand All @@ -117,12 +117,12 @@ def responses
#
# === Example
#
# @matz = { :id => 1, :name => "Matz" }.to_xml(:root => "person")
# @matz = { :person => { :id => 1, :name => "Matz" } }.to_json
# ActiveResource::HttpMock.respond_to do |mock|
# mock.post "/people.xml", {}, @matz, 201, "Location" => "/people/1.xml"
# mock.get "/people/1.xml", {}, @matz
# mock.put "/people/1.xml", {}, nil, 204
# mock.delete "/people/1.xml", {}, nil, 200
# mock.post "/people.json", {}, @matz, 201, "Location" => "/people/1.json"
# mock.get "/people/1.json", {}, @matz
# mock.put "/people/1.json", {}, nil, 204
# mock.delete "/people/1.json", {}, nil, 200
# end
#
# Alternatively, accepts a hash of <tt>{Request => Response}</tt> pairs allowing you to generate
Expand All @@ -135,11 +135,11 @@ def responses
#
# Request.new(:#{method}, path, nil, request_headers)
#
# @matz = { :id => 1, :name => "Matz" }.to_xml(:root => "person")
# @matz = { :person => { :id => 1, :name => "Matz" } }.to_json
#
# create_matz = ActiveResource::Request.new(:post, '/people.xml', @matz, {})
# created_response = ActiveResource::Response.new("", 201, {"Location" => "/people/1.xml"})
# get_matz = ActiveResource::Request.new(:get, '/people/1.xml', nil)
# create_matz = ActiveResource::Request.new(:post, '/people.json', @matz, {})
# created_response = ActiveResource::Response.new("", 201, {"Location" => "/people/1.json"})
# get_matz = ActiveResource::Request.new(:get, '/people/1.json', nil)
# ok_response = ActiveResource::Response.new("", 200, {})
#
# pairs = {create_matz => created_response, get_matz => ok_response}
Expand All @@ -154,12 +154,12 @@ def responses
# === Example
#
# ActiveResource::HttpMock.respond_to do |mock|
# mock.send(:get, "/people/1", {}, "XML1")
# mock.send(:get, "/people/1", {}, "JSON1")
# end
# ActiveResource::HttpMock.responses.length #=> 1
#
# ActiveResource::HttpMock.respond_to(false) do |mock|
# mock.send(:get, "/people/2", {}, "XML2")
# mock.send(:get, "/people/2", {}, "JSON2")
# end
# ActiveResource::HttpMock.responses.length #=> 2
#
Expand All @@ -169,11 +169,11 @@ def responses
# === Example
#
# ActiveResource::HttpMock.respond_to do |mock|
# mock.send(:get, "/people/1", {}, "XML1")
# mock.send(:get, "/people/1", {}, "JSON1")
# end
# ActiveResource::HttpMock.responses.length #=> 1
#
# get_matz = ActiveResource::Request.new(:get, '/people/1.xml', nil)
# get_matz = ActiveResource::Request.new(:get, '/people/1.json', nil)
# ok_response = ActiveResource::Response.new("", 200, {})
#
# pairs = {get_matz => ok_response}
Expand Down

0 comments on commit 0095869

Please sign in to comment.