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.
Files changed (42) hide show
  1. data/CHANGES.markdown +18 -0
  2. data/Gemfile +1 -7
  3. data/README.textile +56 -65
  4. data/Rakefile +1 -1
  5. data/TODO.markdown +1 -0
  6. data/lib/roar/rails/representer_methods.rb +3 -2
  7. data/lib/roar/representer/feature/http_verbs.rb +29 -37
  8. data/lib/roar/representer/feature/hypermedia.rb +78 -13
  9. data/lib/roar/representer/feature/model_representing.rb +19 -27
  10. data/lib/roar/representer/feature/transport.rb +45 -0
  11. data/lib/roar/representer/json.rb +43 -14
  12. data/lib/roar/representer/xml.rb +44 -24
  13. data/lib/roar/representer.rb +31 -57
  14. data/lib/roar/version.rb +1 -1
  15. data/roar.gemspec +4 -6
  16. data/test/Gemfile +1 -0
  17. data/test/dummy/config/routes.rb +1 -1
  18. data/test/fake_server.rb +8 -11
  19. data/test/http_verbs_feature_test.rb +70 -0
  20. data/test/hypermedia_feature_test.rb +173 -0
  21. data/test/integration_test.rb +63 -100
  22. data/test/json_representer_test.rb +41 -35
  23. data/test/model_representing_test.rb +24 -26
  24. data/test/order_representers.rb +6 -3
  25. data/test/representer_test.rb +38 -4
  26. data/test/test_helper.rb +26 -8
  27. data/test/transport_test.rb +10 -13
  28. data/test/xml_representer_test.rb +81 -101
  29. metadata +20 -57
  30. data/lib/roar/client/entity_proxy.rb +0 -58
  31. data/lib/roar/client/proxy.rb +0 -14
  32. data/lib/roar/client/transport.rb +0 -29
  33. data/lib/roar/model/representable.rb +0 -31
  34. data/lib/roar/model.rb +0 -36
  35. data/test/http_verbs_test.rb +0 -46
  36. data/test/hypermedia_test.rb +0 -35
  37. data/test/model_test.rb +0 -50
  38. data/test/proxy_test.rb +0 -89
  39. data/test/representable_test.rb +0 -49
  40. data/test/ruby_representation_test.rb +0 -144
  41. data/test/test_helper_test.rb +0 -59
  42. data/test/xml_hypermedia_test.rb +0 -47
@@ -1,43 +1,63 @@
1
1
  require 'roar/representer'
2
2
  require 'representable/xml'
3
3
 
4
-
5
4
  module Roar
6
- # Basic work-flow
7
- # in: * representer parses representation
8
- # * recognized elements are stored as representer attributes
9
- # out: * attributes in representer are assigned - either as hash in #to_xml, by calling #serialize(represented),
10
- # by calling representer's accessors (eg in client?) or whatever else
11
- # * representation is compiled from representer only
12
- # TODO: make XML a module to include in Hyperlink < Base.
5
+ # Includes #from_xml and #to_xml into your represented object.
6
+ # In addition to that, some more options are available when declaring properties.
13
7
  module Representer
14
- class XML < Base
15
- include Representable::XML
16
-
17
- def serialize
18
- to_xml.serialize
8
+ module XML
9
+ def self.included(base)
10
+ base.class_eval do
11
+ include Representer
12
+ include Representable::XML
13
+
14
+ extend ClassMethods
15
+ include InstanceMethods # otherwise Representable overrides our #to_xml.
16
+ end
19
17
  end
20
18
 
21
- def self.deserialize(xml)
22
- from_xml(xml)
19
+ module InstanceMethods
20
+ def to_xml(*args)
21
+ before_serialize(*args)
22
+ super
23
+ end
24
+
25
+ # Generic entry-point for rendering.
26
+ def serialize(*args)
27
+ to_xml(*args)
28
+ end
29
+
30
+ def deserialize(*args)
31
+ from_xml(*args)
32
+ end
23
33
  end
24
34
 
25
35
 
36
+ module ClassMethods
37
+ include Representable::XML::ClassMethods
38
+
39
+ def links_definition_options
40
+ {:from => :link, :class => Hyperlink, :collection => true}
41
+ end
42
+
43
+ # Generic entry-point for parsing.
44
+ def deserialize(*args)
45
+ from_xml(*args)
46
+ end
47
+ end
48
+
49
+
26
50
  # Encapsulates a hypermedia <link ...>.
27
- class Hyperlink < self
28
- self.representation_name = :link
51
+ class Hyperlink
52
+ # TODO: make XML a module to include in Hyperlink < Base.
53
+ include XML
54
+
55
+ self.representation_wrap = :link
29
56
 
30
57
  property :rel, :from => "@rel"
31
58
  property :href, :from => "@href"
32
59
  end
33
60
 
34
-
35
- def self.links_definition_options
36
- {:tag => :link, :as => [Hyperlink]}
37
- end
38
-
39
- require 'roar/representer/feature/hypermedia'
40
- include Feature::Hypermedia
41
61
  end
42
62
  end
43
63
  end
@@ -2,71 +2,45 @@ require 'representable'
2
2
 
3
3
  module Roar
4
4
  module Representer
5
- class Base
6
- include Representable
7
-
8
- class << self
9
- alias_method :property, :representable_property
10
- alias_method :collection, :representable_collection
11
-
12
- # Creates a representer instance and fills it with +attributes+.
13
- def from_attributes(attributes)
14
- new.tap do |representer|
15
- yield representer if block_given?
16
-
17
- representable_attrs.each do |definition|
18
- definition.populate(representer, attributes)
19
- end
20
- end
21
- end
22
- end
23
-
24
-
25
- def initialize(properties={})
26
- properties.each { |p,v| send("#{p}=", v) } # DISCUSS: check if valid property?
5
+ def self.included(base)
6
+ base.class_eval do
7
+ include Representable
8
+ extend ClassMethods
27
9
  end
10
+ end
11
+
28
12
 
29
- # Convert representer's attributes to a nested attributes hash.
30
- def to_attributes
31
- {}.tap do |attributes|
32
- self.class.representable_attrs.each do |definition|
33
- value = public_send(definition.accessor)
34
-
35
- if definition.typed?
36
- value = definition.apply(value) do |v|
37
- v.to_attributes # applied to each typed attribute (even in collections).
38
- end
39
- end
40
-
41
- attributes[definition.accessor] = value
42
- end
13
+ module ClassMethods
14
+ # Creates a representer instance and fills it with +attributes+.
15
+ # DISCUSS: remove.
16
+ def from_attributes(attributes) # DISCUSS: better move to #new? how do we handle the original #new then?
17
+ new.tap do |representer|
18
+ yield representer if block_given?
19
+ attributes.each { |p,v| representer.public_send("#{p}=", v) }
43
20
  end
44
21
  end
45
22
  end
46
23
 
47
- class LinksDefinition < Representable::Definition
48
- def rel2block
49
- @rel2block ||= []
50
- end
51
-
52
- def populate(representer, *args)
53
- representer.links ||= []
54
-
55
- rel2block.each do |link|
56
- representer.links << sought_type.from_attributes({ # create Hyperlink representer.
57
- "rel" => link[:rel],
58
- "href" => representer.instance_exec(&link[:block])}) # DISCUSS: run block in representer context?
24
+
25
+ # Convert representer's attributes to a nested attributes hash.
26
+ def to_attributes
27
+ {}.tap do |attributes|
28
+ self.class.representable_attrs.each do |definition|
29
+ value = public_send(definition.getter)
30
+
31
+ if definition.typed?
32
+ value = definition.apply(value) do |v|
33
+ v.to_attributes # applied to each typed attribute (even in collections).
34
+ end
35
+ end
36
+
37
+ attributes[definition.name] = value
59
38
  end
60
39
  end
61
40
  end
62
-
63
- end
64
- end
65
-
66
- # FIXME: move to some init asset.
67
- Representable::Definition.class_eval do
68
- # Populate the representer's attribute with the right value.
69
- def populate(representer, attributes)
70
- representer.public_send("#{accessor}=", attributes[accessor])
41
+
42
+ private
43
+ def before_serialize(*)
44
+ end
71
45
  end
72
46
  end
data/lib/roar/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Roar
2
- VERSION = "0.8.0"
2
+ VERSION = "0.9.0"
3
3
  end
data/roar.gemspec CHANGED
@@ -19,12 +19,10 @@ Gem::Specification.new do |s|
19
19
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
20
  s.require_paths = ["lib"]
21
21
 
22
- s.add_runtime_dependency "representable"
23
- s.add_runtime_dependency "restfulie", "~> 1.0.0"
24
- s.add_runtime_dependency "hooks", "~> 0.1.4"
25
- s.add_runtime_dependency "test_xml"
26
-
22
+ s.add_runtime_dependency "representable", "~> 0.12"
23
+ s.add_runtime_dependency "hooks", "~> 0.1.4"
24
+
25
+ s.add_development_dependency "test_xml"
27
26
  s.add_development_dependency "minitest", "~> 1.6.0"
28
27
  s.add_development_dependency "sinatra", "~> 1.2.6"
29
- s.add_development_dependency "sinatra-reloader", "~> 0.5.0"
30
28
  end
data/test/Gemfile CHANGED
@@ -4,3 +4,4 @@ gem "sinatra"
4
4
  gem "sinatra-contrib", :path => "./sinatra-contrib"
5
5
  gem "sinatra-activerecord"
6
6
  gem "roar", :path => ".."
7
+ gem "representable", :path => "../../representable"
@@ -3,5 +3,5 @@ Dummy::Application.routes.draw do
3
3
  root :to => 'musician#index'
4
4
  resources :albums
5
5
  resources :songs
6
- get "articles/starts_with/{query}", :to => "albums#search",:as => :album_search
6
+ get "articles/starts_with/{query}", :to => "albums#search",:class => :album_search
7
7
  end
data/test/fake_server.rb CHANGED
@@ -24,21 +24,18 @@ class FakeServer < Sinatra::Base
24
24
  # "<method>patch</method>"
25
25
  #end
26
26
 
27
- post "/band" do
28
- if request.content_type =~ /xml/
29
- %{<band><label>n/a</label><name>Strung Out</name>
30
- <link href="http://search" rel="search" />
31
- <link href="http://band/strungout" rel="self" />
32
- </band>}
33
- else
34
- '{"band": {"label": "n/a", "name": "Strung Out", "links": [{"href":"http://search", "rel": "search"}, {"href":"http://band/strungout", "rel": "self"}]}}'
35
- end
27
+ post "/bands" do
28
+ #if request.content_type =~ /xml/
29
+ '{"label": "n/a", "name": "Strung Out", "links": [{"href":"http://search", "rel": "search"}, {"href":"http://band/strungout", "rel": "self"}]}'
36
30
  end
37
31
 
38
- put "/band/strungout" do
39
- %{<band><label>Fat Wreck</label><name>Strung Out</name></band>}
32
+ put "/bands/strungout" do
33
+ {:name => "Strung Out", :label => "Fat Wreck"}.to_json
40
34
  end
41
35
 
36
+ get "/bands/slayer" do
37
+ {:name => "Slayer", :label => "Canadian Maple"}.to_json
38
+ end
42
39
 
43
40
 
44
41
  require Dir.pwd + '/order_representers'
@@ -0,0 +1,70 @@
1
+ require 'test_helper'
2
+ require 'roar/representer/feature/http_verbs'
3
+ require 'roar/representer/json'
4
+
5
+ class HttpVerbsTest < MiniTest::Spec
6
+ module BandRepresenter
7
+ include Roar::Representer::JSON
8
+
9
+ property :name
10
+ property :label
11
+ end
12
+
13
+ describe "HttpVerbs" do
14
+ before do
15
+ @band = Object.new
16
+ @band.extend(BandRepresenter)
17
+ @band.extend(Roar::Representer::Feature::HttpVerbs)
18
+ end
19
+
20
+ describe "HttpVerbs.get" do
21
+ it "returns instance from incoming representation" do
22
+ @Band = Class.new do
23
+ include Roar::Representer::JSON
24
+ include BandRepresenter
25
+ include Roar::Representer::Feature::HttpVerbs
26
+ end
27
+ @band = @Band.get("http://localhost:9999/bands/slayer", "application/json")
28
+ assert_equal "Slayer", @band.name
29
+ assert_equal "Canadian Maple", @band.label
30
+ end
31
+ end
32
+
33
+ describe "#get" do
34
+ it "updates instance with incoming representation" do
35
+ @band.get("http://localhost:9999/bands/slayer", "application/json")
36
+ assert_equal "Slayer", @band.name
37
+ assert_equal "Canadian Maple", @band.label
38
+ end
39
+ end
40
+
41
+ describe "#post" do
42
+ it "updates instance with incoming representation" do
43
+ @band.name = "Strung Out"
44
+ assert_equal nil, @band.label
45
+
46
+ @band.post("http://localhost:9999/bands", "application/xml")
47
+ assert_equal "Strung Out", @band.name
48
+ assert_equal "n/a", @band.label
49
+ end
50
+ end
51
+
52
+ describe "#put" do
53
+ it "updates instance with incoming representation" do
54
+ @band.name = "Strung Out"
55
+ @band.label = "Fat Wreck"
56
+ @band.put("http://localhost:9999/bands/strungout", "application/xml")
57
+ assert_equal "Strung Out", @band.name
58
+ assert_equal "Fat Wreck", @band.label
59
+ end
60
+ end
61
+
62
+ describe "#delete" do
63
+
64
+ end
65
+
66
+ describe "#patch" do
67
+
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,173 @@
1
+ require 'test_helper'
2
+ require 'roar/representer/feature/hypermedia'
3
+ require 'roar/representer/json'
4
+
5
+ class HypermediaTest
6
+ describe "Hypermedia Feature" do
7
+ before do
8
+ @bookmarks = Class.new do
9
+ include Roar::Representer::XML
10
+ include Roar::Representer::Feature::Hypermedia
11
+
12
+ self.representation_wrap = "bookmarks"
13
+ end
14
+
15
+ @bookmarks_with_links = Class.new(@bookmarks)
16
+ @bookmarks_with_links.class_eval do
17
+ self.representation_wrap = "bookmarks"
18
+
19
+ property :id
20
+ link :self do "http://bookmarks" end
21
+ link :all do "http://bookmarks/all" end
22
+ end
23
+ end
24
+
25
+
26
+ describe "#to_xml" do
27
+ it "sets up links and renders <link> correctly in XML" do
28
+ assert_xml_equal '<bookmarks>
29
+ <id>1</id>
30
+ <link rel="self" href="http://bookmarks"/>
31
+ <link rel="all" href="http://bookmarks/all"/>
32
+ </bookmarks>', @bookmarks_with_links.from_attributes(:id => 1).to_xml
33
+ end
34
+
35
+ it "still works even if there are no links defined" do
36
+ assert_xml_equal '<bookmarks/>', @bookmarks.new.to_xml
37
+ end
38
+
39
+ it "doesn't render links with :links => false" do
40
+ assert_xml_equal '<bookmarks>
41
+ <id>1</id>
42
+ </bookmarks>',
43
+ @bookmarks_with_links.from_attributes(:id => 1).to_xml(:links => false)
44
+ end
45
+ end
46
+
47
+ describe "#to_json" do
48
+ class Note
49
+ include Roar::Representer::JSON
50
+ include Roar::Representer::Feature::Hypermedia
51
+ link(:self) { "http://me" }
52
+ end
53
+
54
+ it "works twice" do
55
+ note = Note.new
56
+ assert_equal note.to_json, note.to_json
57
+ end
58
+
59
+ it "sets up links even when nested" do
60
+ class Page
61
+ include Roar::Representer::JSON
62
+ property :note, :class => Note
63
+ end
64
+
65
+ assert_equal "{\"note\":{\"links\":[{\"rel\":\"self\",\"href\":\"http://me\"}]}}", Page.from_attributes(note: Note.new).to_json
66
+ end
67
+ end
68
+
69
+
70
+
71
+ describe "#from_xml" do
72
+ it "extracts links from document" do
73
+ doc = @bookmarks_with_links.from_xml(%{
74
+ <bookmarks>
75
+ <link rel="self" href="http://bookmarks">
76
+ </bookmarks>
77
+ })
78
+
79
+ assert_kind_of Roar::Representer::Feature::Hypermedia::LinkCollection, doc.links
80
+ assert_equal 1, doc.links.size
81
+ assert_equal({"rel"=>"self", "href"=>"http://bookmarks"}, doc.links.first.to_attributes)
82
+ end
83
+
84
+ it "sets up an empty link list if no links found in the document" do
85
+ assert_equal [], @bookmarks_with_links.from_xml(%{<bookmarks/>}).links
86
+ end
87
+ end
88
+
89
+
90
+ describe "#links" do
91
+ before do
92
+ @set = @bookmarks.new
93
+ hyper = Roar::Representer::XML::Hyperlink
94
+
95
+ @set.links = [hyper.from_attributes({"rel" => "self", "href" => "http://self"}),
96
+ hyper.from_attributes({"rel" => "next", "href" => "http://next"})]
97
+ end
98
+
99
+ describe "#links=" do
100
+ it "wraps links in a LinkCollection" do
101
+ assert_kind_of Roar::Representer::Feature::Hypermedia::LinkCollection, @set.links
102
+ assert_equal 2, @set.links.size
103
+ end
104
+ end
105
+
106
+ describe "#link[]" do
107
+ it "provides shorthand accessor for rels" do
108
+ assert_equal "http://self", @set.links["self"]
109
+ assert_equal "http://self", @set.links[:self]
110
+ assert_equal "http://next", @set.links[:next]
111
+ assert_equal nil, @set.links[:prev]
112
+ end
113
+ end
114
+
115
+ it "returns an empty list when no links present" do
116
+ assert_equal Roar::Representer::Feature::Hypermedia::LinkCollection.new, @bookmarks.new.links
117
+ end
118
+ end
119
+
120
+
121
+ describe "#find_links_definition" do
122
+ it "returns Definition if links are present" do
123
+ @bookmarks.class_eval do
124
+ property :id
125
+ link :self
126
+ end
127
+
128
+ assert_equal "links", @bookmarks.find_links_definition.name
129
+ end
130
+
131
+ it "returns nil if no links defined" do
132
+ assert_equal nil, @bookmarks.find_links_definition
133
+ end
134
+ end
135
+ end
136
+ end
137
+
138
+ class LinksDefinitionTest < MiniTest::Spec
139
+ describe "LinksDefinition" do
140
+ before do
141
+ @d = Roar::Representer::Feature::Hypermedia::LinksDefinition.new(:links)
142
+ end
143
+
144
+ it "accepts options in constructor" do
145
+ assert_equal [], @d.rel2block
146
+ end
147
+
148
+ it "accepts configuration" do
149
+ @d.rel2block << {:rel => :self}
150
+ assert_equal [{:rel=>:self}], @d.rel2block
151
+ end
152
+
153
+ it "responds to #each to iterate rel2block" do
154
+ end
155
+
156
+ end
157
+ end
158
+
159
+ class LinkCollectionTest < MiniTest::Spec
160
+ describe "LinkCollection" do
161
+ it "provides #update_link" do
162
+ collection = Roar::Representer::Feature::Hypermedia::LinkCollection.new
163
+ link = Roar::Representer::XML::Hyperlink.from_attributes(rel: "self", href: "http://self")
164
+
165
+ collection.update_link(link)
166
+ assert_equal 1, collection.size
167
+
168
+ collection.update_link(link)
169
+ assert_equal 1, collection.size
170
+ end
171
+ end
172
+ end
173
+