roar 0.8.0 → 0.9.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.
- data/CHANGES.markdown +18 -0
- data/Gemfile +1 -7
- data/README.textile +56 -65
- data/Rakefile +1 -1
- data/TODO.markdown +1 -0
- data/lib/roar/rails/representer_methods.rb +3 -2
- data/lib/roar/representer/feature/http_verbs.rb +29 -37
- data/lib/roar/representer/feature/hypermedia.rb +78 -13
- data/lib/roar/representer/feature/model_representing.rb +19 -27
- data/lib/roar/representer/feature/transport.rb +45 -0
- data/lib/roar/representer/json.rb +43 -14
- data/lib/roar/representer/xml.rb +44 -24
- data/lib/roar/representer.rb +31 -57
- data/lib/roar/version.rb +1 -1
- data/roar.gemspec +4 -6
- data/test/Gemfile +1 -0
- data/test/dummy/config/routes.rb +1 -1
- data/test/fake_server.rb +8 -11
- data/test/http_verbs_feature_test.rb +70 -0
- data/test/hypermedia_feature_test.rb +173 -0
- data/test/integration_test.rb +63 -100
- data/test/json_representer_test.rb +41 -35
- data/test/model_representing_test.rb +24 -26
- data/test/order_representers.rb +6 -3
- data/test/representer_test.rb +38 -4
- data/test/test_helper.rb +26 -8
- data/test/transport_test.rb +10 -13
- data/test/xml_representer_test.rb +81 -101
- metadata +20 -57
- data/lib/roar/client/entity_proxy.rb +0 -58
- data/lib/roar/client/proxy.rb +0 -14
- data/lib/roar/client/transport.rb +0 -29
- data/lib/roar/model/representable.rb +0 -31
- data/lib/roar/model.rb +0 -36
- data/test/http_verbs_test.rb +0 -46
- data/test/hypermedia_test.rb +0 -35
- data/test/model_test.rb +0 -50
- data/test/proxy_test.rb +0 -89
- data/test/representable_test.rb +0 -49
- data/test/ruby_representation_test.rb +0 -144
- data/test/test_helper_test.rb +0 -59
- data/test/xml_hypermedia_test.rb +0 -47
data/CHANGES.markdown
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
h2. 0.9.0
|
|
2
|
+
|
|
3
|
+
* Using representable-0.12.x.
|
|
4
|
+
* `Representer::Base` is now simply `Representer`.
|
|
5
|
+
* Removed all the class methods from `HttpVerbs` except for `get`.
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
h2. 0.8.3
|
|
9
|
+
|
|
10
|
+
* Maintenance release for representable compat.
|
|
11
|
+
|
|
12
|
+
h2. 0.8.2
|
|
13
|
+
|
|
14
|
+
* Removing `restfulie` dependency - we now use `Net::HTTP`.
|
|
15
|
+
|
|
16
|
+
h2. 0.8.1
|
|
17
|
+
|
|
18
|
+
* Added the :except and :include options to `#from_*`.
|
data/Gemfile
CHANGED
|
@@ -3,10 +3,4 @@ source "http://rubygems.org"
|
|
|
3
3
|
# Specify your gem's dependencies in roar.gemspec
|
|
4
4
|
gemspec
|
|
5
5
|
|
|
6
|
-
#gem "representable", :path => "
|
|
7
|
-
#gem "test_xml", :path => "/home/nick/projects/test_xml" #"~> 0.1.0"
|
|
8
|
-
|
|
9
|
-
group :test do
|
|
10
|
-
gem "rails", "~> 3.0.0"
|
|
11
|
-
gem "sqlite3"
|
|
12
|
-
end
|
|
6
|
+
#gem "representable", :path => "../representable"
|
data/README.textile
CHANGED
|
@@ -1,26 +1,27 @@
|
|
|
1
1
|
h1. ROAR
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
_Resource-Oriented Architectures in Ruby._
|
|
4
|
+
|
|
5
|
+
"Lets make documents suit our models and not models fit to documents."
|
|
4
6
|
|
|
5
7
|
|
|
6
8
|
h2. Introduction
|
|
7
9
|
|
|
8
|
-
|
|
10
|
+
REST is about representations. Representations are documents. Documents are pushed back and forth between clients and REST services. Roar focuses on providing object-oriented representations.
|
|
9
11
|
|
|
10
|
-
REST
|
|
12
|
+
Most REST gems provide a neat HTTP interface, automatic parsing of documents and a more or less convenient way for rendering representations. Roar is different. The central concept of Roar are *representers* - object-oriented documents suitable for parsing _and_ rendering, extendable at runtime and with hypermedia support. The Representer concept is the answer to the missing REST abstraction layer in most frameworks.
|
|
11
13
|
|
|
12
14
|
|
|
13
15
|
h2. Features
|
|
14
16
|
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
|
|
23
|
-
* Makes *testing distributed REST systems* as easy as possible.
|
|
17
|
+
* OOP access to documents.
|
|
18
|
+
* Parsing _and_ rendering of representations in one place.
|
|
19
|
+
* Declaratively define document syntax and semantics.
|
|
20
|
+
* Hypermedia support.
|
|
21
|
+
* ActiveResource-like client support.
|
|
22
|
+
* Useable in both client _and_ server.
|
|
23
|
+
* Framework agnostic, runs with sinatra, Rails, webmachine and friends.
|
|
24
|
+
|
|
24
25
|
|
|
25
26
|
h2. Example
|
|
26
27
|
|
|
@@ -62,15 +63,20 @@ h2. Representers
|
|
|
62
63
|
|
|
63
64
|
To render a representational document, the backend service has to define a representer.
|
|
64
65
|
|
|
65
|
-
<pre>
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
66
|
+
<pre>
|
|
67
|
+
require 'roar/representer/json'
|
|
68
|
+
require 'roar/representer/feature/hypermedia'
|
|
69
|
+
|
|
70
|
+
class Article
|
|
71
|
+
include Roar::Representer::JSON
|
|
72
|
+
include Roar::Representer::Feature::Hypermedia
|
|
73
|
+
|
|
74
|
+
property :title
|
|
75
|
+
property :id
|
|
76
|
+
|
|
77
|
+
link :self do
|
|
78
|
+
article_url(represented)
|
|
79
|
+
end
|
|
74
80
|
end
|
|
75
81
|
</pre>
|
|
76
82
|
|
|
@@ -81,18 +87,10 @@ h3. Rendering Representations in the Service
|
|
|
81
87
|
|
|
82
88
|
In order to *render* an actual document, the backend service would have to do a few steps: creating a representer, filling in data, and then serialize it.
|
|
83
89
|
|
|
84
|
-
<pre>
|
|
85
|
-
:
|
|
86
|
-
:
|
|
87
|
-
|
|
88
|
-
</pre>
|
|
89
|
-
|
|
90
|
-
Using the @ModelRepresenting@ feature module we can take a shortcut.
|
|
91
|
-
|
|
92
|
-
<pre>@beer = Article.find_by_title("Lonestar")
|
|
93
|
-
|
|
94
|
-
JSON::Article.from_model(@beer).
|
|
95
|
-
serialize # => "{\"article\":{\"id\":666, ...
|
|
90
|
+
<pre>Article.new(
|
|
91
|
+
title: "Lonestar",
|
|
92
|
+
id: 666).
|
|
93
|
+
to_json # => "{\"article\":{\"id\":666, ...
|
|
96
94
|
</pre>
|
|
97
95
|
|
|
98
96
|
Articles itself are useless, so they may be placed into orders. This is the next example.
|
|
@@ -124,21 +122,23 @@ What if we wanted to check an existing order? We'd @GET http://orders/1@, right?
|
|
|
124
122
|
|
|
125
123
|
Since orders may contain a composition of articles, how would the order service define its representer?
|
|
126
124
|
|
|
127
|
-
<pre>
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
items_url
|
|
140
|
-
end
|
|
125
|
+
<pre>
|
|
126
|
+
class Order
|
|
127
|
+
include Roar::Representer::JSON
|
|
128
|
+
include Roar::Representer::Feature::Hypermedia
|
|
129
|
+
|
|
130
|
+
property :id
|
|
131
|
+
property :client_id
|
|
132
|
+
|
|
133
|
+
collection :articles, :as => Article
|
|
134
|
+
|
|
135
|
+
link :self do
|
|
136
|
+
order_url(represented)
|
|
141
137
|
end
|
|
138
|
+
|
|
139
|
+
link :items do
|
|
140
|
+
items_url
|
|
141
|
+
end
|
|
142
142
|
end
|
|
143
143
|
</pre>
|
|
144
144
|
|
|
@@ -153,21 +153,12 @@ If we were to implement an endpoint for creating new orders, we'd allow POST to
|
|
|
153
153
|
|
|
154
154
|
<pre>
|
|
155
155
|
post "/orders" do
|
|
156
|
-
incoming =
|
|
156
|
+
incoming = Order.deserialize(request.body.string)
|
|
157
157
|
puts incoming.to_attributes #=> {:client_id => 815}
|
|
158
|
+
end
|
|
158
159
|
</pre>
|
|
159
160
|
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
<pre>
|
|
163
|
-
post "/orders" do
|
|
164
|
-
# ...
|
|
165
|
-
@order = Order.create(incoming.to_nested_attributes)
|
|
166
|
-
|
|
167
|
-
JSON::Order.for_model(@order).serialize
|
|
168
|
-
</pre>
|
|
169
|
-
|
|
170
|
-
Look how the @#to_nested_attributes@ method helps extracting data from the incoming document and, again, @#serialize@ returns the freshly created order's representation. Roar's representers are truely working in both directions, rendering and parsing and thus prevent you from redundant knowledge sharing.
|
|
161
|
+
Look how the @#to_attributes@ method helps extracting data from the incoming document and, again, @#to_json@ returns the freshly created order's representation. Roar's representers are truely working in both directions, rendering and parsing and thus prevent you from redundant knowledge sharing.
|
|
171
162
|
|
|
172
163
|
|
|
173
164
|
h2. Representers in the Client
|
|
@@ -183,8 +174,8 @@ To create a new order, the frontend needs to POST to the REST backend. Here's ho
|
|
|
183
174
|
|
|
184
175
|
|
|
185
176
|
<pre>
|
|
186
|
-
order =
|
|
187
|
-
order.post
|
|
177
|
+
order = Order.new(:client_id => current_user.id)
|
|
178
|
+
order.post("http://orders/")
|
|
188
179
|
</pre>
|
|
189
180
|
|
|
190
181
|
A couple of noteworthy steps happen here.
|
|
@@ -193,7 +184,7 @@ A couple of noteworthy steps happen here.
|
|
|
193
184
|
# Initial values like the client's id are passed as arguments and placed in the document.
|
|
194
185
|
# The filled-out document is POSTed to the given URL.
|
|
195
186
|
# The backend service creates an actual order record and sends back the representation.
|
|
196
|
-
# In the @#post
|
|
187
|
+
# In the @#post@ call, the returned document is parsed and attributes in the representer instance are updated accordingly,
|
|
197
188
|
|
|
198
189
|
After the HTTP roundtrip, the order instance keeps all the information we need for proceeding the ordering workflow.
|
|
199
190
|
|
|
@@ -233,7 +224,7 @@ h3. Using Hypermedia
|
|
|
233
224
|
Let the frontend add the delicious "Lonestar" beer to our order, now!
|
|
234
225
|
|
|
235
226
|
<pre>
|
|
236
|
-
beer =
|
|
227
|
+
beer = Article.new(:title => "Lonestar Beer")
|
|
237
228
|
beer.post(order.links[:items])
|
|
238
229
|
</pre>
|
|
239
230
|
|
|
@@ -274,9 +265,9 @@ What if the ordering API is going a different way? What if we had to place artic
|
|
|
274
265
|
Here's what could happen in the frontend.
|
|
275
266
|
|
|
276
267
|
<pre>
|
|
277
|
-
beer =
|
|
268
|
+
beer = Article.new(:title => "Lonestar Beer")
|
|
278
269
|
order.items << beer
|
|
279
|
-
order.post
|
|
270
|
+
order.post(order.links[:self])
|
|
280
271
|
</pre>
|
|
281
272
|
|
|
282
273
|
This was dead simple since representations can be composed of different documents in Roar.
|
data/Rakefile
CHANGED
|
@@ -7,7 +7,7 @@ task :default => [:test, :testrails]
|
|
|
7
7
|
|
|
8
8
|
Rake::TestTask.new(:test) do |test|
|
|
9
9
|
test.libs << 'test'
|
|
10
|
-
test.test_files = FileList['test
|
|
10
|
+
test.test_files = FileList['test/*_test.rb'] - ['test/integration_test.rb', 'test/active_record_integration_test.rb']
|
|
11
11
|
test.verbose = true
|
|
12
12
|
end
|
|
13
13
|
|
data/TODO.markdown
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
* Add proxies, so nested models can be lazy-loaded.
|
|
@@ -43,8 +43,9 @@ module Roar
|
|
|
43
43
|
singular_name = name.to_s.singularize
|
|
44
44
|
|
|
45
45
|
super name, options.reverse_merge(
|
|
46
|
-
:
|
|
47
|
-
|
|
46
|
+
:class => "representer/#{namespace}/#{singular_name}".classify.constantize,
|
|
47
|
+
#:tag => singular_name # FIXME: how/where to decide if singular TAG or not?
|
|
48
|
+
)
|
|
48
49
|
end
|
|
49
50
|
end
|
|
50
51
|
end
|
|
@@ -1,62 +1,54 @@
|
|
|
1
|
-
require 'roar/
|
|
1
|
+
require 'roar/representer/feature/transport'
|
|
2
2
|
|
|
3
3
|
module Roar
|
|
4
|
-
#
|
|
4
|
+
# Gives HTTP-power to representers. They can serialize, send, process and deserialize HTTP-requests.
|
|
5
5
|
module Representer
|
|
6
6
|
module Feature
|
|
7
7
|
module HttpVerbs
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
included do |base|
|
|
11
|
-
base.class_attribute :resource_base
|
|
8
|
+
def self.included(base)
|
|
9
|
+
base.extend ClassMethods
|
|
12
10
|
end
|
|
13
11
|
|
|
12
|
+
|
|
14
13
|
module ClassMethods
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
representation = get_uri(url, format).body
|
|
20
|
-
deserialize(representation)
|
|
14
|
+
# GETs +url+ with +format+ and returns deserialized representer.
|
|
15
|
+
def get(url, format)
|
|
16
|
+
document = http.get_uri(url, format).body
|
|
17
|
+
deserialize(document)
|
|
21
18
|
end
|
|
22
19
|
|
|
23
|
-
def
|
|
24
|
-
|
|
25
|
-
deserialize(representation)
|
|
26
|
-
end
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
def put(url, body, format)
|
|
30
|
-
representation = put_uri(url, body, format).body
|
|
31
|
-
deserialize(representation)
|
|
20
|
+
def http
|
|
21
|
+
Transport
|
|
32
22
|
end
|
|
33
23
|
end
|
|
34
24
|
|
|
25
|
+
|
|
26
|
+
# Serializes the object, POSTs it to +url+ with +format+, deserializes the returned document
|
|
27
|
+
# and updates properties accordingly.
|
|
35
28
|
def post(url, format)
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
rep = post(*args) # TODO: make this better.
|
|
40
|
-
|
|
41
|
-
self.class.representable_attrs.each do |definition|
|
|
42
|
-
|
|
43
|
-
send(definition.setter, rep.public_send(definition.accessor))
|
|
44
|
-
end # TODO: this sucks. do this with #properties and #replace_properties.
|
|
29
|
+
# DISCUSS: what if a redirect happens here?
|
|
30
|
+
document = http.post_uri(url, serialize, format).body
|
|
31
|
+
deserialize(document)
|
|
45
32
|
end
|
|
46
33
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
send(definition.setter, rep.public_send(definition.accessor))
|
|
52
|
-
end # TODO: this sucks. do this with #properties and #replace_properties.
|
|
34
|
+
# GETs +url+ with +format+, deserializes the returned document and updates properties accordingly.
|
|
35
|
+
def get(url, format)
|
|
36
|
+
document = http.get_uri(url, format).body
|
|
37
|
+
deserialize(document)
|
|
53
38
|
end
|
|
54
39
|
|
|
40
|
+
# Serializes the object, PUTs it to +url+ with +format+, deserializes the returned document
|
|
41
|
+
# and updates properties accordingly.
|
|
55
42
|
def put(url, format)
|
|
56
|
-
|
|
43
|
+
document = http.put_uri(url, serialize, format).body
|
|
44
|
+
deserialize(document)
|
|
57
45
|
end
|
|
58
46
|
|
|
59
47
|
# TODO: implement delete, patch.
|
|
48
|
+
private
|
|
49
|
+
def http
|
|
50
|
+
Transport # DISCUSS: might be refering to separate http object soon.
|
|
51
|
+
end
|
|
60
52
|
end
|
|
61
53
|
end
|
|
62
54
|
end
|
|
@@ -1,42 +1,107 @@
|
|
|
1
|
-
require "roar/model"
|
|
2
|
-
|
|
3
1
|
module Roar
|
|
4
2
|
module Representer
|
|
5
3
|
module Feature
|
|
6
|
-
# Adds
|
|
7
|
-
#
|
|
8
|
-
|
|
9
|
-
|
|
4
|
+
# Adds #link to the representer to define hypermedia links in the representation.
|
|
5
|
+
#
|
|
6
|
+
# Example:
|
|
7
|
+
#
|
|
8
|
+
# class Order
|
|
9
|
+
# include Roar::Representer::JSON
|
|
10
|
+
#
|
|
11
|
+
# property :id
|
|
12
|
+
#
|
|
13
|
+
# link :self do
|
|
14
|
+
# "http://orders/#{id}"
|
|
15
|
+
# end
|
|
16
|
+
module Hypermedia
|
|
17
|
+
def self.included(base)
|
|
18
|
+
base.extend ClassMethods
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def before_serialize(options={})
|
|
22
|
+
prepare_links! unless options[:links] == false # DISCUSS: doesn't work when links are already setup (e.g. from #deserialize).
|
|
23
|
+
super # Representer::Base
|
|
24
|
+
end
|
|
10
25
|
|
|
11
|
-
def links=(
|
|
12
|
-
|
|
26
|
+
def links=(link_list)
|
|
27
|
+
links.replace(link_list)
|
|
13
28
|
end
|
|
14
29
|
|
|
15
30
|
def links
|
|
16
|
-
@links
|
|
31
|
+
@links ||= LinkCollection.new
|
|
17
32
|
end
|
|
18
33
|
|
|
34
|
+
protected
|
|
35
|
+
# Setup hypermedia links by invoking their blocks. Usually called by #serialize.
|
|
36
|
+
def prepare_links!
|
|
37
|
+
links_def = find_links_definition or return
|
|
38
|
+
hyperlink_class = links_def.sought_type
|
|
39
|
+
|
|
40
|
+
links_def.rel2block.each do |link|
|
|
41
|
+
links.update_link(hyperlink_class.from_attributes( # create Hyperlink representer.
|
|
42
|
+
:rel => link[:rel],
|
|
43
|
+
:href => run_link_block(link[:block]))
|
|
44
|
+
)
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def run_link_block(block)
|
|
49
|
+
instance_exec(&block)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def find_links_definition
|
|
53
|
+
representable_attrs.find { |d| d.is_a?(LinksDefinition) }
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
|
|
19
57
|
class LinkCollection < Array
|
|
20
58
|
def [](rel)
|
|
21
59
|
link = find { |l| l.rel.to_s == rel.to_s } and return link.href
|
|
22
60
|
end
|
|
61
|
+
|
|
62
|
+
# Checks if the link is already contained by querying for its +rel+.
|
|
63
|
+
# If so, it gets replaced. Otherwise, the new link gets appended.
|
|
64
|
+
def update_link(link)
|
|
65
|
+
if i = find_index { |l| l.rel.to_s == link.rel.to_s }
|
|
66
|
+
return self[i] = link
|
|
67
|
+
end
|
|
68
|
+
self << link
|
|
69
|
+
end
|
|
23
70
|
end
|
|
24
71
|
|
|
25
72
|
|
|
26
73
|
module ClassMethods
|
|
27
|
-
#
|
|
74
|
+
# Declares a hypermedia link in the document.
|
|
75
|
+
#
|
|
76
|
+
# Example:
|
|
77
|
+
#
|
|
78
|
+
# link :self do
|
|
79
|
+
# "http://orders/#{id}"
|
|
80
|
+
# end
|
|
81
|
+
#
|
|
82
|
+
# The block is executed in instance context, so you may call properties or other accessors.
|
|
83
|
+
# Note that you're free to put decider logic into #link blocks, too.
|
|
28
84
|
def link(rel, &block)
|
|
29
|
-
unless links =
|
|
85
|
+
unless links = find_links_definition
|
|
30
86
|
links = LinksDefinition.new(:links, links_definition_options)
|
|
31
87
|
representable_attrs << links
|
|
32
|
-
#add_reader(links) # TODO: refactor in Roxml.
|
|
33
|
-
# attr_writer(links.accessor)
|
|
34
88
|
end
|
|
35
89
|
|
|
36
90
|
links.rel2block << {:rel => rel, :block => block}
|
|
37
91
|
end
|
|
92
|
+
|
|
93
|
+
def find_links_definition
|
|
94
|
+
representable_attrs.find { |d| d.is_a?(LinksDefinition) }
|
|
95
|
+
end
|
|
38
96
|
end
|
|
39
97
|
|
|
98
|
+
|
|
99
|
+
class LinksDefinition < Representable::Definition
|
|
100
|
+
# TODO: hide rel2block in interface.
|
|
101
|
+
def rel2block
|
|
102
|
+
@rel2block ||= []
|
|
103
|
+
end
|
|
104
|
+
end
|
|
40
105
|
end
|
|
41
106
|
end
|
|
42
107
|
end
|
|
@@ -39,7 +39,7 @@ module Roar
|
|
|
39
39
|
# Properties that are mapped to a model attribute.
|
|
40
40
|
class ModelDefinition < ::Representable::Definition
|
|
41
41
|
def compute_attribute_for(represented, attributes)
|
|
42
|
-
value = represented.send(
|
|
42
|
+
value = represented.send(getter)
|
|
43
43
|
|
|
44
44
|
if typed?
|
|
45
45
|
value = apply(value) do |v|
|
|
@@ -47,40 +47,32 @@ module Roar
|
|
|
47
47
|
end
|
|
48
48
|
end
|
|
49
49
|
|
|
50
|
-
attributes[
|
|
50
|
+
attributes[name] = value
|
|
51
51
|
end
|
|
52
52
|
end
|
|
53
53
|
end
|
|
54
54
|
|
|
55
55
|
|
|
56
56
|
module ActiveRecordMethods
|
|
57
|
-
def to_nested_attributes # FIXME:
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
57
|
+
def to_nested_attributes # FIXME: extract iterating with #to_attributes.
|
|
58
|
+
{}.tap do |attributes|
|
|
59
|
+
self.class.representable_attrs.each do |definition|
|
|
60
|
+
next unless definition.kind_of?(ModelRepresenting::ModelDefinition)
|
|
61
|
+
|
|
62
|
+
value = public_send(definition.getter)
|
|
63
|
+
|
|
64
|
+
if definition.typed?
|
|
65
|
+
value = definition.apply(value) do |v|
|
|
66
|
+
v.to_nested_attributes # applied to each typed attribute (even in collections).
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
key = definition.name
|
|
71
|
+
key = "#{key}_attributes" if definition.typed?
|
|
72
|
+
|
|
73
|
+
attributes[key] = value
|
|
70
74
|
end
|
|
71
75
|
end
|
|
72
|
-
|
|
73
|
-
attrs
|
|
74
|
-
end
|
|
75
|
-
|
|
76
|
-
private
|
|
77
|
-
def clear_attributes(attrs)
|
|
78
|
-
puts "clearing #{attrs.inspect}"
|
|
79
|
-
attrs.each do |k,v|
|
|
80
|
-
attrs.delete(k) if k == "links"
|
|
81
|
-
|
|
82
|
-
clear_attributes(v) if v.is_a?(Hash)
|
|
83
|
-
end
|
|
84
76
|
end
|
|
85
77
|
end
|
|
86
78
|
end
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
require "net/http"
|
|
2
|
+
require "uri"
|
|
3
|
+
|
|
4
|
+
module Roar
|
|
5
|
+
module Representer
|
|
6
|
+
module Feature
|
|
7
|
+
# Implements the HTTP verbs with Net::HTTP.
|
|
8
|
+
module Transport
|
|
9
|
+
class << self
|
|
10
|
+
# TODO: generically handle return codes/let Restfulie do it.
|
|
11
|
+
def get_uri(uri, as)
|
|
12
|
+
do_request(Net::HTTP::Get, uri, as)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def post_uri(uri, body, as)
|
|
16
|
+
do_request(Net::HTTP::Post, uri, as)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def put_uri(uri, body, as)
|
|
20
|
+
do_request(Net::HTTP::Put, uri, as)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def patch_uri(uri, body, as)
|
|
24
|
+
do_request(Net::HTTP::Patch, uri, as)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def delete_uri(uri, as)
|
|
28
|
+
do_request(Net::HTTP::Delete, uri, as)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
private
|
|
32
|
+
def do_request(what, uri, as, body="")
|
|
33
|
+
# DISCUSS: can this be made easier?
|
|
34
|
+
uri = URI.parse(uri)
|
|
35
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
|
36
|
+
req = what.new(uri.request_uri)
|
|
37
|
+
req.content_type = as
|
|
38
|
+
req.body = body if body
|
|
39
|
+
http.request(req)
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
@@ -1,32 +1,61 @@
|
|
|
1
1
|
require 'roar/representer'
|
|
2
2
|
require 'representable/json'
|
|
3
3
|
|
|
4
|
-
|
|
5
4
|
module Roar
|
|
6
5
|
module Representer
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
module JSON
|
|
7
|
+
def self.included(base)
|
|
8
|
+
base.class_eval do
|
|
9
|
+
include Representer
|
|
10
|
+
include Representable::JSON
|
|
11
|
+
|
|
12
|
+
extend ClassMethods
|
|
13
|
+
include InstanceMethods # otherwise Representable overrides our #to_json.
|
|
14
|
+
end
|
|
15
|
+
end
|
|
9
16
|
|
|
10
|
-
|
|
11
|
-
|
|
17
|
+
module InstanceMethods
|
|
18
|
+
def to_hash(*args)
|
|
19
|
+
before_serialize(*args)
|
|
20
|
+
super
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def from_json(document, options={})
|
|
24
|
+
document ||= "{}" # DISCUSS: provide this for convenience, or better not?
|
|
25
|
+
|
|
26
|
+
super
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# Generic entry-point for rendering.
|
|
30
|
+
def serialize(*args)
|
|
31
|
+
to_json(*args)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def deserialize(*args)
|
|
35
|
+
from_json(*args)
|
|
36
|
+
end
|
|
12
37
|
end
|
|
13
38
|
|
|
14
|
-
|
|
15
|
-
|
|
39
|
+
|
|
40
|
+
module ClassMethods
|
|
41
|
+
def deserialize(json)
|
|
42
|
+
from_json(json)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# TODO: move to instance method, or remove?
|
|
46
|
+
def links_definition_options
|
|
47
|
+
{:class => Hyperlink , :collection => true}
|
|
48
|
+
end
|
|
16
49
|
end
|
|
17
50
|
|
|
51
|
+
|
|
18
52
|
# Encapsulates a hypermedia link.
|
|
19
|
-
class Hyperlink
|
|
20
|
-
|
|
53
|
+
class Hyperlink
|
|
54
|
+
include JSON
|
|
21
55
|
|
|
22
56
|
property :rel
|
|
23
57
|
property :href
|
|
24
58
|
end
|
|
25
|
-
|
|
26
|
-
def self.links_definition_options
|
|
27
|
-
{:as => [Hyperlink]}
|
|
28
|
-
end
|
|
29
59
|
end
|
|
30
|
-
|
|
31
60
|
end
|
|
32
61
|
end
|