activeresource 4.1.0 → 6.0.0
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 +5 -5
- data/MIT-LICENSE +20 -0
- data/README.md +324 -0
- data/lib/active_resource/active_job_serializer.rb +26 -0
- data/lib/active_resource/associations/builder/association.rb +6 -6
- data/lib/active_resource/associations/builder/belongs_to.rb +5 -3
- data/lib/active_resource/associations/builder/has_many.rb +4 -2
- data/lib/active_resource/associations/builder/has_one.rb +5 -3
- data/lib/active_resource/associations.rb +23 -20
- data/lib/active_resource/base.rb +233 -113
- data/lib/active_resource/callbacks.rb +3 -1
- data/lib/active_resource/collection.rb +21 -12
- data/lib/active_resource/connection.rb +78 -81
- data/lib/active_resource/custom_methods.rb +8 -6
- data/lib/active_resource/exceptions.rb +17 -5
- data/lib/active_resource/formats/json_format.rb +4 -1
- data/lib/active_resource/formats/xml_format.rb +4 -2
- data/lib/active_resource/formats.rb +5 -3
- data/lib/active_resource/http_mock.rb +23 -27
- data/lib/active_resource/inheriting_hash.rb +15 -0
- data/lib/active_resource/log_subscriber.rb +14 -3
- data/lib/active_resource/railtie.rb +10 -10
- data/lib/active_resource/reflection.rb +11 -10
- data/lib/active_resource/schema.rb +6 -3
- data/lib/active_resource/singleton.rb +25 -28
- data/lib/active_resource/threadsafe_attributes.rb +35 -31
- data/lib/active_resource/validations.rb +18 -15
- data/lib/active_resource/version.rb +6 -4
- data/lib/active_resource.rb +8 -7
- data/lib/activeresource.rb +3 -1
- metadata +41 -24
- data/README.rdoc +0 -231
- data/lib/active_resource/observing.rb +0 -31
data/README.rdoc
DELETED
@@ -1,231 +0,0 @@
|
|
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 web services 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 JSON request is generated, transmitted, and the result
|
21
|
-
received and serialized into a usable Ruby object.
|
22
|
-
|
23
|
-
== Download and installation
|
24
|
-
|
25
|
-
The latest version of Active Resource can be installed with RubyGems:
|
26
|
-
|
27
|
-
% [sudo] gem install activeresource
|
28
|
-
|
29
|
-
Or added to a Gemfile:
|
30
|
-
|
31
|
-
gem 'activeresource'
|
32
|
-
|
33
|
-
Source code can be downloaded on GitHub
|
34
|
-
|
35
|
-
* https://github.com/rails/activeresource/tree/master/activeresource
|
36
|
-
|
37
|
-
=== Configuration and Usage
|
38
|
-
|
39
|
-
Putting Active Resource to use is very similar to Active Record. It's as simple as creating a model class
|
40
|
-
that inherits from ActiveResource::Base and providing a <tt>site</tt> class variable to it:
|
41
|
-
|
42
|
-
class Person < ActiveResource::Base
|
43
|
-
self.site = "http://api.people.com:3000"
|
44
|
-
end
|
45
|
-
|
46
|
-
Now the Person class is REST enabled and can invoke REST services very similarly to how Active Record invokes
|
47
|
-
life cycle methods that operate against a persistent store.
|
48
|
-
|
49
|
-
# Find a person with id = 1
|
50
|
-
tyler = Person.find(1)
|
51
|
-
Person.exists?(1) # => true
|
52
|
-
|
53
|
-
As you can see, the methods are quite similar to Active Record's methods for dealing with database
|
54
|
-
records. But rather than dealing directly with a database record, you're dealing with HTTP resources (which may or may not be database records).
|
55
|
-
|
56
|
-
==== Protocol
|
57
|
-
|
58
|
-
Active Resource is built on a standard JSON or XML format for requesting and submitting resources over HTTP. It mirrors the RESTful routing
|
59
|
-
built into Action Controller but will also work with any other REST service that properly implements the protocol.
|
60
|
-
REST uses HTTP, but unlike "typical" web applications, it makes use of all the verbs available in the HTTP specification:
|
61
|
-
|
62
|
-
* GET requests are used for finding and retrieving resources.
|
63
|
-
* POST requests are used to create new resources.
|
64
|
-
* PUT requests are used to update existing resources.
|
65
|
-
* DELETE requests are used to delete resources.
|
66
|
-
|
67
|
-
For more information on how this protocol works with Active Resource, see the ActiveResource::Base documentation;
|
68
|
-
for more general information on REST web services, see the article here[http://en.wikipedia.org/wiki/Representational_State_Transfer].
|
69
|
-
|
70
|
-
==== Find
|
71
|
-
|
72
|
-
Find requests use the GET method and expect the JSON form of whatever resource/resources is/are being requested. So,
|
73
|
-
for a request for a single element, the JSON of that item is expected in response:
|
74
|
-
|
75
|
-
# Expects a response of
|
76
|
-
#
|
77
|
-
# {"id":1,"first":"Tyler","last":"Durden"}
|
78
|
-
#
|
79
|
-
# for GET http://api.people.com:3000/people/1.json
|
80
|
-
#
|
81
|
-
tyler = Person.find(1)
|
82
|
-
|
83
|
-
The JSON document that is received is used to build a new object of type Person, with each
|
84
|
-
JSON element becoming an attribute on the object.
|
85
|
-
|
86
|
-
tyler.is_a? Person # => true
|
87
|
-
tyler.last # => 'Durden'
|
88
|
-
|
89
|
-
Any complex element (one that contains other elements) becomes its own object:
|
90
|
-
|
91
|
-
# With this response:
|
92
|
-
# {"id":1,"first":"Tyler","address":{"street":"Paper St.","state":"CA"}}
|
93
|
-
#
|
94
|
-
# for GET http://api.people.com:3000/people/1.json
|
95
|
-
#
|
96
|
-
tyler = Person.find(1)
|
97
|
-
tyler.address # => <Person::Address::xxxxx>
|
98
|
-
tyler.address.street # => 'Paper St.'
|
99
|
-
|
100
|
-
Collections can also be requested in a similar fashion
|
101
|
-
|
102
|
-
# Expects a response of
|
103
|
-
#
|
104
|
-
# [
|
105
|
-
# {"id":1,"first":"Tyler","last":"Durden"},
|
106
|
-
# {"id":2,"first":"Tony","last":"Stark",}
|
107
|
-
# ]
|
108
|
-
#
|
109
|
-
# for GET http://api.people.com:3000/people.json
|
110
|
-
#
|
111
|
-
people = Person.all
|
112
|
-
people.first # => <Person::xxx 'first' => 'Tyler' ...>
|
113
|
-
people.last # => <Person::xxx 'first' => 'Tony' ...>
|
114
|
-
|
115
|
-
==== Create
|
116
|
-
|
117
|
-
Creating a new resource submits the JSON form of the resource as the body of the request and expects
|
118
|
-
a 'Location' header in the response with the RESTful URL location of the newly created resource. The
|
119
|
-
id of the newly created resource is parsed out of the Location response header and automatically set
|
120
|
-
as the id of the ARes object.
|
121
|
-
|
122
|
-
# {"first":"Tyler","last":"Durden"}
|
123
|
-
#
|
124
|
-
# is submitted as the body on
|
125
|
-
#
|
126
|
-
# if include_root_in_json is set to true => {"person":{"first":"Tyler"}}
|
127
|
-
#
|
128
|
-
# POST http://api.people.com:3000/people.json
|
129
|
-
#
|
130
|
-
# when save is called on a new Person object. An empty response is
|
131
|
-
# is expected with a 'Location' header value:
|
132
|
-
#
|
133
|
-
# Response (201): Location: http://api.people.com:3000/people/2
|
134
|
-
#
|
135
|
-
tyler = Person.new(:first => 'Tyler')
|
136
|
-
tyler.new? # => true
|
137
|
-
tyler.save # => true
|
138
|
-
tyler.new? # => false
|
139
|
-
tyler.id # => 2
|
140
|
-
|
141
|
-
==== Update
|
142
|
-
|
143
|
-
'save' is also used to update an existing resource and follows the same protocol as creating a resource
|
144
|
-
with the exception that no response headers are needed -- just an empty response when the update on the
|
145
|
-
server side was successful.
|
146
|
-
|
147
|
-
# {"first":"Tyler"}
|
148
|
-
#
|
149
|
-
# is submitted as the body on
|
150
|
-
#
|
151
|
-
# if include_root_in_json is set to true => {"person":{"first":"Tyler"}}
|
152
|
-
#
|
153
|
-
# PUT http://api.people.com:3000/people/1.json
|
154
|
-
#
|
155
|
-
# when save is called on an existing Person object. An empty response is
|
156
|
-
# is expected with code (204)
|
157
|
-
#
|
158
|
-
tyler = Person.find(1)
|
159
|
-
tyler.first # => 'Tyler'
|
160
|
-
tyler.first = 'Tyson'
|
161
|
-
tyler.save # => true
|
162
|
-
|
163
|
-
==== Delete
|
164
|
-
|
165
|
-
Destruction of a resource can be invoked as a class and instance method of the resource.
|
166
|
-
|
167
|
-
# A request is made to
|
168
|
-
#
|
169
|
-
# DELETE http://api.people.com:3000/people/1.json
|
170
|
-
#
|
171
|
-
# for both of these forms. An empty response with
|
172
|
-
# is expected with response code (200)
|
173
|
-
#
|
174
|
-
tyler = Person.find(1)
|
175
|
-
tyler.destroy # => true
|
176
|
-
tyler.exists? # => false
|
177
|
-
Person.delete(2) # => true
|
178
|
-
Person.exists?(2) # => false
|
179
|
-
|
180
|
-
==== Associations
|
181
|
-
|
182
|
-
Relationships between resources can be declared using the standard association syntax
|
183
|
-
that should be familiar to anyone who uses activerecord. For example, using the
|
184
|
-
class definition below:
|
185
|
-
|
186
|
-
class Post < ActiveResource::Base
|
187
|
-
self.site = "http://blog.io"
|
188
|
-
has_many :comments
|
189
|
-
end
|
190
|
-
|
191
|
-
post = Post.find(1) # issues GET http://blog.io/posts/1.json
|
192
|
-
comments = post.comments # issues GET http://blog.io/posts/1/comments.json
|
193
|
-
|
194
|
-
|
195
|
-
If you control the server, you may wish to include nested resources thus avoiding a
|
196
|
-
second network request. Given the resource above, if the response includes comments
|
197
|
-
in the response, they will be automatically loaded into the activeresource object.
|
198
|
-
The server-side model can be adjusted as follows to include comments in the response.
|
199
|
-
|
200
|
-
class Post < ActiveRecord::Base
|
201
|
-
has_many :comments
|
202
|
-
|
203
|
-
def as_json(options)
|
204
|
-
super.merge(:include=>[:comments])
|
205
|
-
end
|
206
|
-
end
|
207
|
-
|
208
|
-
== License
|
209
|
-
|
210
|
-
Active Resource is released under the MIT license:
|
211
|
-
|
212
|
-
* http://www.opensource.org/licenses/MIT
|
213
|
-
|
214
|
-
== Contributing to Active Resource
|
215
|
-
|
216
|
-
Active Resource is work of many contributors. You're encouraged to submit pull requests, propose
|
217
|
-
features and discuss issues.
|
218
|
-
|
219
|
-
See {CONTRIBUTING}[https://github.com/rails/activeresource/blob/master/CONTRIBUTING.md].
|
220
|
-
|
221
|
-
== Support
|
222
|
-
|
223
|
-
API documentation is at
|
224
|
-
|
225
|
-
* http://rubydoc.info/gems/activeresource/4.0.0/frames
|
226
|
-
|
227
|
-
Bug reports and feature requests can be filed with the rest for the Ruby on Rails project here:
|
228
|
-
|
229
|
-
* https://github.com/rails/activeresource/issues
|
230
|
-
|
231
|
-
You can find more usage information in the ActiveResource::Base documentation.
|
@@ -1,31 +0,0 @@
|
|
1
|
-
require 'rails/observers/active_model/observing'
|
2
|
-
|
3
|
-
module ActiveResource
|
4
|
-
module Observing
|
5
|
-
extend ActiveSupport::Concern
|
6
|
-
include ActiveModel::Observing
|
7
|
-
|
8
|
-
included do
|
9
|
-
%w( create save update destroy ).each do |method|
|
10
|
-
# def create_with_notifications(*args, &block)
|
11
|
-
# notify_observers(:before_create)
|
12
|
-
# if result = create_without_notifications(*args, &block)
|
13
|
-
# notify_observers(:after_create)
|
14
|
-
# end
|
15
|
-
# result
|
16
|
-
# end
|
17
|
-
# alias_method_chain(create, :notifications)
|
18
|
-
class_eval(<<-EOS, __FILE__, __LINE__ + 1)
|
19
|
-
def #{method}_with_notifications(*args, &block)
|
20
|
-
notify_observers(:before_#{method})
|
21
|
-
if result = #{method}_without_notifications(*args, &block)
|
22
|
-
notify_observers(:after_#{method})
|
23
|
-
end
|
24
|
-
result
|
25
|
-
end
|
26
|
-
EOS
|
27
|
-
alias_method_chain(method, :notifications)
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|