activeresource 2.0.1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of activeresource might be problematic. Click here for more details.

data/CHANGELOG ADDED
@@ -0,0 +1,216 @@
1
+ *2.0.1* (December 7th, 2007)
2
+
3
+ * Don't cache net/http object so that ActiveResource is more thread-safe. Closes #10142 [kou]
4
+
5
+ * Update XML documentation examples to include explicit type attributes. Closes #9754 [hasmanyjosh]
6
+
7
+ * Added one-off declarations of mock behavior [DHH]. Example:
8
+
9
+ Before:
10
+ ActiveResource::HttpMock.respond_to do |mock|
11
+ mock.get "/people/1.xml", {}, "<person><name>David</name></person>"
12
+ end
13
+
14
+ Now:
15
+ ActiveResource::HttpMock.respond_to.get "/people/1.xml", {}, "<person><name>David</name></person>"
16
+
17
+ * Added ActiveResource.format= which defaults to :xml but can also be set to :json [DHH]. Example:
18
+
19
+ class Person < ActiveResource::Base
20
+ self.site = "http://app/"
21
+ self.format = :json
22
+ end
23
+
24
+ person = Person.find(1) # => GET http://app/people/1.json
25
+ person.name = "David"
26
+ person.save # => PUT http://app/people/1.json {name: "David"}
27
+
28
+ Person.format = :xml
29
+ person.name = "Mary"
30
+ person.save # => PUT http://app/people/1.json <person><name>Mary</name></person>
31
+
32
+ * Fix reload error when path prefix is used. #8727 [Ian Warshak]
33
+
34
+ * Remove ActiveResource::Struct because it hasn't proven very useful. Creating a new ActiveResource::Base subclass is often less code and always clearer. #8612 [Josh Peek]
35
+
36
+ * Fix query methods on resources. [Cody Fauser]
37
+
38
+ * pass the prefix_options to the instantiated record when using find without a specific id. Closes #8544 [alloy]
39
+
40
+ * Recognize and raise an exception on 405 Method Not Allowed responses. #7692 [Josh Peek]
41
+
42
+ * Handle string and symbol param keys when splitting params into prefix params and query params.
43
+
44
+ Comment.find(:all, :params => { :article_id => 5, :page => 2 }) or Comment.find(:all, :params => { 'article_id' => 5, :page => 2 })
45
+
46
+ * Added find-one with symbol [DHH]. Example: Person.find(:one, :from => :leader) # => GET /people/leader.xml
47
+
48
+ * BACKWARDS INCOMPATIBLE: Changed the finder API to be more extensible with :params and more strict usage of scopes [DHH]. Changes:
49
+
50
+ Person.find(:all, :title => "CEO") ...becomes: Person.find(:all, :params => { :title => "CEO" })
51
+ Person.find(:managers) ...becomes: Person.find(:all, :from => :managers)
52
+ Person.find("/companies/1/manager.xml") ...becomes: Person.find(:one, :from => "/companies/1/manager.xml")
53
+
54
+ * Add support for setting custom headers per Active Resource model [Rick]
55
+
56
+ class Project
57
+ headers['X-Token'] = 'foo'
58
+ end
59
+
60
+ # makes the GET request with the custom X-Token header
61
+ Project.find(:all)
62
+
63
+ * Added find-by-path options to ActiveResource::Base.find [DHH]. Examples:
64
+
65
+ employees = Person.find(:all, :from => "/companies/1/people.xml") # => GET /companies/1/people.xml
66
+ manager = Person.find("/companies/1/manager.xml") # => GET /companies/1/manager.xml
67
+
68
+
69
+ * Added support for using classes from within a single nested module [DHH]. Example:
70
+
71
+ module Highrise
72
+ class Note < ActiveResource::Base
73
+ self.site = "http://37s.sunrise.i:3000"
74
+ end
75
+
76
+ class Comment < ActiveResource::Base
77
+ self.site = "http://37s.sunrise.i:3000"
78
+ end
79
+ end
80
+
81
+ assert_kind_of Highrise::Comment, Note.find(1).comments.first
82
+
83
+
84
+ * Added load_attributes_from_response as a way of loading attributes from other responses than just create [DHH]
85
+
86
+ class Highrise::Task < ActiveResource::Base
87
+ def complete
88
+ load_attributes_from_response(post(:complete))
89
+ end
90
+ end
91
+
92
+ ...will set "done_at" when complete is called.
93
+
94
+
95
+ * Added support for calling custom methods #6979 [rwdaigle]
96
+
97
+ Person.find(:managers) # => GET /people/managers.xml
98
+ Kase.find(1).post(:close) # => POST /kases/1/close.xml
99
+
100
+ * Remove explicit prefix_options parameter for ActiveResource::Base#initialize. [Rick]
101
+ ActiveResource splits the prefix_options from it automatically.
102
+
103
+ * Allow ActiveResource::Base.delete with custom prefix. [Rick]
104
+
105
+ * Add ActiveResource::Base#dup [Rick]
106
+
107
+ * Fixed constant warning when fetching the same object multiple times [DHH]
108
+
109
+ * Added that saves which get a body response (and not just a 201) will use that response to update themselves [DHH]
110
+
111
+ * Disregard namespaces from the default element name, so Highrise::Person will just try to fetch from "/people", not "/highrise/people" [DHH]
112
+
113
+ * Allow array and hash query parameters. #7756 [Greg Spurrier]
114
+
115
+ * Loading a resource preserves its prefix_options. #7353 [Ryan Daigle]
116
+
117
+ * Carry over the convenience of #create from ActiveRecord. Closes #7340. [Ryan Daigle]
118
+
119
+ * Increase ActiveResource::Base test coverage. Closes #7173, #7174 [Rich Collins]
120
+
121
+ * Interpret 422 Unprocessable Entity as ResourceInvalid. #7097 [dkubb]
122
+
123
+ * Mega documentation patches. #7025, #7069 [rwdaigle]
124
+
125
+ * Base.exists?(id, options) and Base#exists? check whether the resource is found. #6970 [rwdaigle]
126
+
127
+ * Query string support. [untext, Jeremy Kemper]
128
+ # GET /forums/1/topics.xml?sort=created_at
129
+ Topic.find(:all, :forum_id => 1, :sort => 'created_at')
130
+
131
+ * Base#==, eql?, and hash methods. == returns true if its argument is identical to self or if it's an instance of the same class, is not new?, and has the same id. eql? is an alias for ==. hash delegates to id. [Jeremy Kemper]
132
+
133
+ * Allow subclassed resources to share the site info [Rick, Jeremy Kemper]
134
+ d
135
+ class BeastResource < ActiveResource::Base
136
+ self.site = 'http://beast.caboo.se'
137
+ end
138
+
139
+ class Forum < BeastResource
140
+ # taken from BeastResource
141
+ # self.site = 'http://beast.caboo.se'
142
+ end
143
+
144
+ class Topic < BeastResource
145
+ self.site += '/forums/:forum_id'
146
+ end
147
+
148
+ * Fix issues with ActiveResource collection handling. Closes #6291. [bmilekic]
149
+
150
+ * Use attr_accessor_with_default to dry up attribute initialization. References #6538. [Stuart Halloway]
151
+
152
+ * Add basic logging support for logging outgoing requests. [Jamis Buck]
153
+
154
+ * Add Base.delete for deleting resources without having to instantiate them first. [Jamis Buck]
155
+
156
+ * Make #save behavior mimic AR::Base#save (true on success, false on failure). [Jamis Buck]
157
+
158
+ * Add Basic HTTP Authentication to ActiveResource (closes #6305). [jonathan]
159
+
160
+ * Extracted #id_from_response as an entry point for customizing how a created resource gets its own ID.
161
+ By default, it extracts from the Location response header.
162
+
163
+ * Optimistic locking: raise ActiveResource::ResourceConflict on 409 Conflict response. [Jeremy Kemper]
164
+
165
+ # Example controller action
166
+ def update
167
+ @person.save!
168
+ rescue ActiveRecord::StaleObjectError
169
+ render :xml => @person.reload.to_xml, :status => '409 Conflict'
170
+ end
171
+
172
+ * Basic validation support [Rick Olson]
173
+
174
+ Parses the xml response of ActiveRecord::Errors#to_xml with a similar interface to ActiveRecord::Errors.
175
+
176
+ render :xml => @person.errors.to_xml, :status => '400 Validation Error'
177
+
178
+ * Deep hashes are converted into collections of resources. [Jeremy Kemper]
179
+ Person.new :name => 'Bob',
180
+ :address => { :id => 1, :city => 'Portland' },
181
+ :contacts => [{ :id => 1 }, { :id => 2 }]
182
+ Looks for Address and Contact resources and creates them if unavailable.
183
+ So clients can fetch a complex resource in a single request if you e.g.
184
+ render :xml => @person.to_xml(:include => [:address, :contacts])
185
+ in your controller action.
186
+
187
+ * Major updates [Rick Olson]
188
+
189
+ * Add full support for find/create/update/destroy
190
+ * Add support for specifying prefixes.
191
+ * Allow overriding of element_name, collection_name, and primary key
192
+ * Provide simpler HTTP mock interface for testing
193
+
194
+ # rails routing code
195
+ map.resources :posts do |post|
196
+ post.resources :comments
197
+ end
198
+
199
+ # ActiveResources
200
+ class Post < ActiveResource::Base
201
+ self.site = "http://37s.sunrise.i:3000/"
202
+ end
203
+
204
+ class Comment < ActiveResource::Base
205
+ self.site = "http://37s.sunrise.i:3000/posts/:post_id/"
206
+ end
207
+
208
+ @post = Post.find 5
209
+ @comments = Comment.find :all, :post_id => @post.id
210
+
211
+ @comment = Comment.new({:body => 'hello world'}, {:post_id => @post.id})
212
+ @comment.save
213
+
214
+ * Base.site= accepts URIs. 200...400 are valid response codes. PUT and POST request bodies default to ''. [Jeremy Kemper]
215
+
216
+ * Initial checkin: object-oriented client for restful HTTP resources which follow the Rails convention. [DHH]
data/README ADDED
@@ -0,0 +1,165 @@
1
+ = Active Resource
2
+
3
+ Active Resource (ARes) connects business objects and Representational State Transfer (REST)
4
+ web services. It implements object-relational mapping for REST webservices to provide transparent
5
+ proxying capabilities between a client (ActiveResource) and a RESTful service (which is provided by Simply RESTful routing
6
+ in ActionController::Resources).
7
+
8
+ == Philosophy
9
+
10
+ Active Resource attempts to provide a coherent wrapper object-relational mapping for REST
11
+ web services. It follows the same philosophy as Active Record, in that one of its prime aims
12
+ is to reduce the amount of code needed to map to these resources. This is made possible
13
+ by relying on a number of code- and protocol-based conventions that make it easy for Active Resource
14
+ to infer complex relations and structures. These conventions are outlined in detail in the documentation
15
+ for ActiveResource::Base.
16
+
17
+ == Overview
18
+
19
+ Model classes are mapped to remote REST resources by Active Resource much the same way Active Record maps model classes to database
20
+ tables. When a request is made to a remote resource, a REST XML request is generated, transmitted, and the result
21
+ received and serialized into a usable Ruby object.
22
+
23
+ === Configuration and Usage
24
+
25
+ Putting ActiveResource to use is very similar to ActiveRecord. It's as simple as creating a model class
26
+ that inherits from ActiveResource::Base and providing a <tt>site</tt> class variable to it:
27
+
28
+ class Person < ActiveResource::Base
29
+ self.site = "http://api.people.com:3000/"
30
+ end
31
+
32
+ Now the Person class is REST enabled and can invoke REST services very similarly to how ActiveRecord invokes
33
+ lifecycle methods that operate against a persistent store.
34
+
35
+ # Find a person with id = 1
36
+ ryan = Person.find(1)
37
+ Person.exists?(1) #=> true
38
+
39
+ As you can see, the methods are quite similar to Active Record's methods for dealing with database
40
+ records. But rather than dealing with
41
+
42
+ ==== Protocol
43
+
44
+ Active Resource is built on a standard XML format for requesting and submitting resources over HTTP. It mirrors the RESTful routing
45
+ built into ActionController but will also work with any other REST service that properly implements the protocol.
46
+ REST uses HTTP, but unlike "typical" web applications, it makes use of all the verbs available in the HTTP specification:
47
+
48
+ * GET requests are used for finding and retrieving resources.
49
+ * POST requests are used to create new resources.
50
+ * PUT requests are used to update existing resources.
51
+ * DELETE requests are used to delete resources.
52
+
53
+ For more information on how this protocol works with Active Resource, see the ActiveResource::Base documentation;
54
+ for more general information on REST web services, see the article here[http://en.wikipedia.org/wiki/Representational_State_Transfer].
55
+
56
+ ==== Find
57
+
58
+ GET Http requests expect the XML form of whatever resource/resources is/are being requested. So,
59
+ for a request for a single element - the XML of that item is expected in response:
60
+
61
+ # Expects a response of
62
+ #
63
+ # <person><id type="integer">1</id><attribute1>value1</attribute1><attribute2>..</attribute2></person>
64
+ #
65
+ # for GET http://api.people.com:3000/people/1.xml
66
+ #
67
+ ryan = Person.find(1)
68
+
69
+ The XML document that is received is used to build a new object of type Person, with each
70
+ XML element becoming an attribute on the object.
71
+
72
+ ryan.is_a? Person #=> true
73
+ ryan.attribute1 #=> 'value1'
74
+
75
+ Any complex element (one that contains other elements) becomes its own object:
76
+
77
+ # With this response:
78
+ #
79
+ # <person><id>1</id><attribute1>value1</attribute1><complex><attribute2>value2</attribute2></complex></person>
80
+ #
81
+ # for GET http://api.people.com:3000/people/1.xml
82
+ #
83
+ ryan = Person.find(1)
84
+ ryan.complex #=> <Person::Complex::xxxxx>
85
+ ryan.complex.attribute2 #=> 'value2'
86
+
87
+ Collections can also be requested in a similar fashion
88
+
89
+ # Expects a response of
90
+ #
91
+ # <people type="array">
92
+ # <person><id type="integer">1</id><first>Ryan</first></person>
93
+ # <person><id type="integer">2</id><first>Jim</first></person>
94
+ # </people>
95
+ #
96
+ # for GET http://api.people.com:3000/people.xml
97
+ #
98
+ people = Person.find(:all)
99
+ people.first #=> <Person::xxx 'first' => 'Ryan' ...>
100
+ people.last #=> <Person::xxx 'first' => 'Jim' ...>
101
+
102
+ ==== Create
103
+
104
+ Creating a new resource submits the xml form of the resource as the body of the request and expects
105
+ a 'Location' header in the response with the RESTful URL location of the newly created resource. The
106
+ id of the newly created resource is parsed out of the Location response header and automatically set
107
+ as the id of the ARes object.
108
+
109
+ # <person><first>Ryan</first></person>
110
+ #
111
+ # is submitted as the body on
112
+ #
113
+ # POST http://api.people.com:3000/people.xml
114
+ #
115
+ # when save is called on a new Person object. An empty response is
116
+ # is expected with a 'Location' header value:
117
+ #
118
+ # Response (201): Location: http://api.people.com:3000/people/2
119
+ #
120
+ ryan = Person.new(:first => 'Ryan')
121
+ ryan.new? #=> true
122
+ ryan.save #=> true
123
+ ryan.new? #=> false
124
+ ryan.id #=> 2
125
+
126
+ ==== Update
127
+
128
+ 'save' is also used to update an existing resource - and follows the same protocol as creating a resource
129
+ with the exception that no response headers are needed - just an empty response when the update on the
130
+ server side was successful.
131
+
132
+ # <person><first>Ryan</first></person>
133
+ #
134
+ # is submitted as the body on
135
+ #
136
+ # PUT http://api.people.com:3000/people/1.xml
137
+ #
138
+ # when save is called on an existing Person object. An empty response is
139
+ # is expected with code (204)
140
+ #
141
+ ryan = Person.find(1)
142
+ ryan.first #=> 'Ryan'
143
+ ryan.first = 'Rizzle'
144
+ ryan.save #=> true
145
+
146
+ ==== Delete
147
+
148
+ Destruction of a resource can be invoked as a class and instance method of the resource.
149
+
150
+ # A request is made to
151
+ #
152
+ # DELETE http://api.people.com:3000/people/1.xml
153
+ #
154
+ # for both of these forms. An empty response with
155
+ # is expected with response code (200)
156
+ #
157
+ ryan = Person.find(1)
158
+ ryan.destroy #=> true
159
+ ryan.exists? #=> false
160
+ Person.delete(2) #=> true
161
+ Person.exists?(2) #=> false
162
+
163
+
164
+ You can find more usage information in the ActiveResource::Base documentation.
165
+
data/Rakefile ADDED
@@ -0,0 +1,133 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rake/testtask'
4
+ require 'rake/rdoctask'
5
+ require 'rake/packagetask'
6
+ require 'rake/gempackagetask'
7
+ require 'rake/contrib/rubyforgepublisher'
8
+ require File.join(File.dirname(__FILE__), 'lib', 'active_resource', 'version')
9
+
10
+ PKG_BUILD = ENV['PKG_BUILD'] ? '.' + ENV['PKG_BUILD'] : ''
11
+ PKG_NAME = 'activeresource'
12
+ PKG_VERSION = ActiveResource::VERSION::STRING + PKG_BUILD
13
+ PKG_FILE_NAME = "#{PKG_NAME}-#{PKG_VERSION}"
14
+
15
+ RELEASE_NAME = "REL #{PKG_VERSION}"
16
+
17
+ RUBY_FORGE_PROJECT = "activerecord"
18
+ RUBY_FORGE_USER = "webster132"
19
+
20
+ PKG_FILES = FileList[
21
+ "lib/**/*", "test/**/*", "[A-Z]*", "Rakefile"
22
+ ].exclude(/\bCVS\b|~$/)
23
+
24
+ desc "Default Task"
25
+ task :default => [ :test ]
26
+
27
+ # Run the unit tests
28
+
29
+ Rake::TestTask.new { |t|
30
+ t.libs << "test"
31
+ t.pattern = 'test/**/*_test.rb'
32
+ t.verbose = true
33
+ t.warning = true
34
+ }
35
+
36
+
37
+ # Generate the RDoc documentation
38
+
39
+ Rake::RDocTask.new { |rdoc|
40
+ rdoc.rdoc_dir = 'doc'
41
+ rdoc.title = "Active Resource -- Object-oriented REST services"
42
+ rdoc.options << '--line-numbers' << '--inline-source' << '-A cattr_accessor=object'
43
+ rdoc.options << '--charset' << 'utf-8'
44
+ rdoc.template = "#{ENV['template']}.rb" if ENV['template']
45
+ rdoc.rdoc_files.include('README', 'CHANGELOG')
46
+ rdoc.rdoc_files.include('lib/**/*.rb')
47
+ }
48
+
49
+
50
+ # Create compressed packages
51
+
52
+ dist_dirs = [ "lib", "test", "examples", "dev-utils" ]
53
+
54
+ spec = Gem::Specification.new do |s|
55
+ s.name = PKG_NAME
56
+ s.version = PKG_VERSION
57
+ s.summary = "Think Active Record for web resources."
58
+ s.description = %q{Wraps web resources in model classes that can be manipulated through XML over REST.}
59
+
60
+ s.files = [ "Rakefile", "README", "CHANGELOG" ]
61
+ dist_dirs.each do |dir|
62
+ s.files = s.files + Dir.glob( "#{dir}/**/*" ).delete_if { |item| item.include?( "\.svn" ) }
63
+ end
64
+
65
+ s.add_dependency('activesupport', '= 2.0.1' + PKG_BUILD)
66
+
67
+ s.require_path = 'lib'
68
+ s.autorequire = 'active_resource'
69
+
70
+ s.has_rdoc = true
71
+ s.extra_rdoc_files = %w( README )
72
+ s.rdoc_options.concat ['--main', 'README']
73
+
74
+ s.author = "David Heinemeier Hansson"
75
+ s.email = "david@loudthinking.com"
76
+ s.homepage = "http://www.rubyonrails.org"
77
+ s.rubyforge_project = "activeresource"
78
+ end
79
+
80
+ Rake::GemPackageTask.new(spec) do |p|
81
+ p.gem_spec = spec
82
+ p.need_tar = true
83
+ p.need_zip = true
84
+ end
85
+
86
+ task :lines do
87
+ lines, codelines, total_lines, total_codelines = 0, 0, 0, 0
88
+
89
+ for file_name in FileList["lib/active_resource/**/*.rb"]
90
+ next if file_name =~ /vendor/
91
+ f = File.open(file_name)
92
+
93
+ while line = f.gets
94
+ lines += 1
95
+ next if line =~ /^\s*$/
96
+ next if line =~ /^\s*#/
97
+ codelines += 1
98
+ end
99
+ puts "L: #{sprintf("%4d", lines)}, LOC #{sprintf("%4d", codelines)} | #{file_name}"
100
+
101
+ total_lines += lines
102
+ total_codelines += codelines
103
+
104
+ lines, codelines = 0, 0
105
+ end
106
+
107
+ puts "Total: Lines #{total_lines}, LOC #{total_codelines}"
108
+ end
109
+
110
+
111
+ # Publishing ------------------------------------------------------
112
+
113
+ desc "Publish the beta gem"
114
+ task :pgem => [:package] do
115
+ Rake::SshFilePublisher.new("davidhh@wrath.rubyonrails.org", "public_html/gems/gems", "pkg", "#{PKG_FILE_NAME}.gem").upload
116
+ `ssh davidhh@wrath.rubyonrails.org './gemupdate.sh'`
117
+ end
118
+
119
+ desc "Publish the API documentation"
120
+ task :pdoc => [:rdoc] do
121
+ Rake::SshDirPublisher.new("davidhh@wrath.rubyonrails.org", "public_html/ar", "doc").upload
122
+ end
123
+
124
+ desc "Publish the release files to RubyForge."
125
+ task :release => [ :package ] do
126
+ `rubyforge login`
127
+
128
+ for ext in %w( gem tgz zip )
129
+ release_command = "rubyforge add_release #{PKG_NAME} #{PKG_NAME} 'REL #{PKG_VERSION}' pkg/#{PKG_NAME}-#{PKG_VERSION}.#{ext}"
130
+ puts release_command
131
+ system(release_command)
132
+ end
133
+ end