tokamak 1.0.0.beta2 → 1.0.0.beta4
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +14 -0
- data/README.md +159 -0
- data/lib/tokamak/builder/base.rb +74 -0
- data/lib/tokamak/{json/builder.rb → builder/json.rb} +38 -29
- data/lib/tokamak/builder/values.rb +33 -0
- data/lib/tokamak/{atom/builder.rb → builder/xml.rb} +46 -41
- data/lib/tokamak/builder.rb +22 -0
- data/lib/tokamak/errors.rb +3 -0
- data/lib/tokamak/hook/rails.rb +78 -0
- data/lib/tokamak/hook/sinatra.rb +18 -0
- data/lib/tokamak/hook/tilt.rb +42 -0
- data/lib/tokamak/hook.rb +6 -0
- data/lib/tokamak/recipes.rb +26 -0
- data/lib/tokamak/version.rb +13 -0
- data/lib/tokamak.rb +14 -18
- data/script/console +7 -0
- data/test/rails2_skel/Rakefile +16 -0
- data/test/rails2_skel/app/controllers/application_controller.rb +1 -0
- data/test/rails2_skel/app/controllers/test_controller.rb +18 -0
- data/test/rails2_skel/app/views/test/_feed_member.tokamak +9 -0
- data/test/rails2_skel/app/views/test/feed.tokamak +24 -0
- data/test/rails2_skel/app/views/test/show.tokamak +31 -0
- data/test/rails2_skel/config/boot.rb +110 -0
- data/test/rails2_skel/config/environment.rb +20 -0
- data/test/rails2_skel/config/environments/development.rb +17 -0
- data/test/rails2_skel/config/environments/production.rb +28 -0
- data/test/rails2_skel/config/environments/test.rb +28 -0
- data/test/rails2_skel/config/initializers/cookie_verification_secret.rb +2 -0
- data/test/rails2_skel/config/initializers/mime_types.rb +3 -0
- data/test/rails2_skel/config/initializers/new_rails_defaults.rb +10 -0
- data/test/rails2_skel/config/initializers/session_store.rb +5 -0
- data/test/rails2_skel/config/routes.rb +43 -0
- data/test/rails2_skel/script/console +3 -0
- data/test/test_helper.rb +7 -0
- data/test/tokamak/builder/base_test.rb +28 -0
- data/test/tokamak/builder/json_test.rb +227 -0
- data/test/tokamak/builder/xml_test.rb +254 -0
- data/test/tokamak/helper_test.rb +106 -0
- data/test/tokamak/hook/rails_test.rb +74 -0
- data/test/tokamak/hook/sinatra_test.rb +85 -0
- data/test/tokamak/hook/tilt_test.rb +35 -0
- data/test/tokamak/recipes_test.rb +90 -0
- metadata +106 -113
- data/.document +0 -5
- data/.rspec +0 -1
- data/Gemfile +0 -27
- data/Gemfile.lock +0 -77
- data/LICENSE.txt +0 -20
- data/README.rdoc +0 -69
- data/Rakefile +0 -50
- data/VERSION +0 -1
- data/lib/tokamak/atom/base.rb +0 -87
- data/lib/tokamak/atom/helpers.rb +0 -13
- data/lib/tokamak/atom.rb +0 -8
- data/lib/tokamak/error.rb +0 -6
- data/lib/tokamak/json/base.rb +0 -83
- data/lib/tokamak/json/helpers.rb +0 -13
- data/lib/tokamak/json.rb +0 -10
- data/lib/tokamak/representation/atom/atom.rng +0 -597
- data/lib/tokamak/representation/atom/base.rb +0 -140
- data/lib/tokamak/representation/atom/category.rb +0 -39
- data/lib/tokamak/representation/atom/entry.rb +0 -56
- data/lib/tokamak/representation/atom/factory.rb +0 -48
- data/lib/tokamak/representation/atom/feed.rb +0 -108
- data/lib/tokamak/representation/atom/link.rb +0 -66
- data/lib/tokamak/representation/atom/person.rb +0 -46
- data/lib/tokamak/representation/atom/source.rb +0 -57
- data/lib/tokamak/representation/atom/tag_collection.rb +0 -36
- data/lib/tokamak/representation/atom/xml.rb +0 -94
- data/lib/tokamak/representation/atom.rb +0 -18
- data/lib/tokamak/representation/generic.rb +0 -20
- data/lib/tokamak/representation/json/base.rb +0 -25
- data/lib/tokamak/representation/json/keys_as_methods.rb +0 -72
- data/lib/tokamak/representation/json/link.rb +0 -27
- data/lib/tokamak/representation/json/link_collection.rb +0 -21
- data/lib/tokamak/representation/json.rb +0 -11
- data/lib/tokamak/representation/links.rb +0 -9
- data/lib/tokamak/representation.rb +0 -3
- data/lib/tokamak/values.rb +0 -29
- data/lib/tokamak/xml/base.rb +0 -60
- data/lib/tokamak/xml/builder.rb +0 -115
- data/lib/tokamak/xml/helpers.rb +0 -13
- data/lib/tokamak/xml/link.rb +0 -31
- data/lib/tokamak/xml/links.rb +0 -35
- data/lib/tokamak/xml.rb +0 -12
- data/spec/integration/atom/atom_spec.rb +0 -191
- data/spec/integration/full_atom.xml +0 -92
- data/spec/integration/full_json.js +0 -46
- data/spec/integration/json/json_spec.rb +0 -172
- data/spec/integration/xml/xml_spec.rb +0 -203
- data/spec/spec_helper.rb +0 -12
@@ -1,20 +0,0 @@
|
|
1
|
-
module Tokamak
|
2
|
-
# Unknown representation's unmarshalling on the client side
|
3
|
-
module Representation
|
4
|
-
class Generic
|
5
|
-
|
6
|
-
# Because there is no media type registered, return the content itself
|
7
|
-
def unmarshal(content)
|
8
|
-
def content.links
|
9
|
-
[]
|
10
|
-
end
|
11
|
-
content
|
12
|
-
end
|
13
|
-
|
14
|
-
def marshal(string, rel)
|
15
|
-
string
|
16
|
-
end
|
17
|
-
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
@@ -1,25 +0,0 @@
|
|
1
|
-
module Tokamak
|
2
|
-
module Representation
|
3
|
-
class Json
|
4
|
-
module Base
|
5
|
-
module ClassMethods
|
6
|
-
|
7
|
-
# creates a json unmarshalled version of this object
|
8
|
-
def create(obj = nil)
|
9
|
-
@json = {}
|
10
|
-
return @json.extend(KeysAsMethods) unless obj
|
11
|
-
|
12
|
-
if obj.kind_of?(Hash) || obj.kind_of?(Array)
|
13
|
-
@json = obj
|
14
|
-
else
|
15
|
-
@json = ::JSON.parse(obj)
|
16
|
-
end
|
17
|
-
|
18
|
-
@json.extend(KeysAsMethods)
|
19
|
-
end
|
20
|
-
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
@@ -1,72 +0,0 @@
|
|
1
|
-
module Tokamak
|
2
|
-
module Representation
|
3
|
-
class Json
|
4
|
-
module KeysAsMethods
|
5
|
-
def self.extended(base)
|
6
|
-
[:type, :id].each { |m| base.__free_method__(m) }
|
7
|
-
end
|
8
|
-
|
9
|
-
def [](key)
|
10
|
-
__normalize__(super(key))
|
11
|
-
end
|
12
|
-
|
13
|
-
def []=(key, value)
|
14
|
-
super(key,value)
|
15
|
-
end
|
16
|
-
|
17
|
-
def to_s
|
18
|
-
super.to_json
|
19
|
-
end
|
20
|
-
|
21
|
-
def method_missing(name, *args)
|
22
|
-
method_name = name.to_s
|
23
|
-
if method_name[-1] == "="[-1] #1.8 and 1.9 compatible
|
24
|
-
self[method_name.chop] = args[0]
|
25
|
-
else
|
26
|
-
self[method_name]
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
# if you have a key that is also a method (such as Array#size)
|
31
|
-
# you can use this to free the method and use the method obj.size
|
32
|
-
# to access the value of key "size".
|
33
|
-
# you still can access the old method with __[method_name]__
|
34
|
-
def __free_method__(sym)
|
35
|
-
if self.__metaclass__.method_defined?(sym) && !respond_to?("__#{sym}__")
|
36
|
-
self.__metaclass__.send(:alias_method, "__#{sym}__", sym)
|
37
|
-
end
|
38
|
-
self.__metaclass__.send(:define_method, sym) { method_missing(sym.to_s) }
|
39
|
-
self
|
40
|
-
end
|
41
|
-
|
42
|
-
def __metaclass__
|
43
|
-
class << self; self; end
|
44
|
-
end
|
45
|
-
|
46
|
-
include Tokamak::Representation::Links
|
47
|
-
|
48
|
-
# easy accessors to links
|
49
|
-
def links
|
50
|
-
some_links = self["link"]
|
51
|
-
return nil unless some_links
|
52
|
-
some_links = [some_links] unless some_links.kind_of? Array
|
53
|
-
LinkCollection.new(some_links)
|
54
|
-
end
|
55
|
-
|
56
|
-
private
|
57
|
-
|
58
|
-
def __normalize__(value)
|
59
|
-
case value
|
60
|
-
when Hash
|
61
|
-
value.extend(KeysAsMethods)
|
62
|
-
when Array
|
63
|
-
value.map { |v| __normalize__(v) }
|
64
|
-
else
|
65
|
-
value
|
66
|
-
end
|
67
|
-
value
|
68
|
-
end
|
69
|
-
end
|
70
|
-
end
|
71
|
-
end
|
72
|
-
end
|
@@ -1,27 +0,0 @@
|
|
1
|
-
module Tokamak
|
2
|
-
module Representation
|
3
|
-
class Json
|
4
|
-
class Link
|
5
|
-
def initialize(obj)
|
6
|
-
@obj = obj
|
7
|
-
end
|
8
|
-
|
9
|
-
def type
|
10
|
-
@obj.type
|
11
|
-
end
|
12
|
-
|
13
|
-
def href
|
14
|
-
@obj.href
|
15
|
-
end
|
16
|
-
|
17
|
-
def rel
|
18
|
-
@obj.rel
|
19
|
-
end
|
20
|
-
|
21
|
-
def method_missing(symbol, *args, &block)
|
22
|
-
@obj.send(symbol, *args, &block)
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
@@ -1,21 +0,0 @@
|
|
1
|
-
module Tokamak
|
2
|
-
module Representation
|
3
|
-
class Json
|
4
|
-
class LinkCollection
|
5
|
-
def initialize(parent_node)
|
6
|
-
@node = parent_node
|
7
|
-
end
|
8
|
-
|
9
|
-
def method_missing(symbol, *args, &block)
|
10
|
-
linkset = @node.select {|link| link.rel == symbol.to_s }
|
11
|
-
linkset.map! { |link| Link.new(link) }
|
12
|
-
unless linkset.empty?
|
13
|
-
linkset.size == 1 ? linkset.first : linkset
|
14
|
-
else
|
15
|
-
nil
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
@@ -1,11 +0,0 @@
|
|
1
|
-
module Tokamak
|
2
|
-
module Representation
|
3
|
-
class Json
|
4
|
-
autoload :Base, 'tokamak/representation/json/base'
|
5
|
-
autoload :KeysAsMethods, 'tokamak/representation/json/keys_as_methods'
|
6
|
-
autoload :Link, 'tokamak/representation/json/link'
|
7
|
-
autoload :LinkCollection, 'tokamak/representation/json/link_collection'
|
8
|
-
extend Base::ClassMethods
|
9
|
-
end
|
10
|
-
end
|
11
|
-
end
|
data/lib/tokamak/values.rb
DELETED
@@ -1,29 +0,0 @@
|
|
1
|
-
module Tokamak
|
2
|
-
# This is a Blank Slate class to support the renderization of the values block for Resource Representation Interfaces
|
3
|
-
# Every Media type should implement a Builder with a insert_value method that renders the values block to a specific format
|
4
|
-
class Values
|
5
|
-
attr_accessor :builder
|
6
|
-
|
7
|
-
# remove all methods from this class
|
8
|
-
instance_methods.each do |m|
|
9
|
-
undef_method m unless m.to_s =~ /\[\]|method_missing|send|respond_to\?|^__/
|
10
|
-
end
|
11
|
-
|
12
|
-
def initialize(builder)
|
13
|
-
@builder = builder
|
14
|
-
@current_prefix = nil
|
15
|
-
end
|
16
|
-
|
17
|
-
def [](prefix)
|
18
|
-
@current_prefix = prefix
|
19
|
-
self
|
20
|
-
end
|
21
|
-
|
22
|
-
def method_missing(symbol, *args, &block)
|
23
|
-
name = symbol.to_s
|
24
|
-
prefix = @current_prefix
|
25
|
-
@current_prefix = nil
|
26
|
-
@builder.insert_value(name, prefix, *args, &block)
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
data/lib/tokamak/xml/base.rb
DELETED
@@ -1,60 +0,0 @@
|
|
1
|
-
require 'active_support/core_ext/hash/conversions'
|
2
|
-
|
3
|
-
module Tokamak
|
4
|
-
module Xml
|
5
|
-
module Base
|
6
|
-
module ClassMethods
|
7
|
-
mattr_reader :media_type_name
|
8
|
-
@@media_type_name = 'application/xml'
|
9
|
-
|
10
|
-
mattr_reader :headers
|
11
|
-
@@headers = {
|
12
|
-
:post => { 'Content-Type' => media_type_name }
|
13
|
-
}
|
14
|
-
|
15
|
-
def marshal(entity, options = {})
|
16
|
-
to_xml(entity, options)
|
17
|
-
end
|
18
|
-
|
19
|
-
def unmarshal(string)
|
20
|
-
Hash.from_xml string
|
21
|
-
end
|
22
|
-
|
23
|
-
mattr_reader :recipes
|
24
|
-
@@recipes = {}
|
25
|
-
|
26
|
-
def describe_recipe(recipe_name, options={}, &block)
|
27
|
-
raise 'Undefined recipe' unless block_given?
|
28
|
-
raise 'Undefined recipe_name' unless recipe_name
|
29
|
-
@@recipes[recipe_name] = block
|
30
|
-
end
|
31
|
-
|
32
|
-
def to_xml(obj, options = {}, &block)
|
33
|
-
return obj if obj.kind_of?(String)
|
34
|
-
|
35
|
-
if block_given?
|
36
|
-
recipe = block
|
37
|
-
elsif options[:recipe]
|
38
|
-
recipe = @@recipes[options[:recipe]]
|
39
|
-
elsif obj.kind_of?(Hash) && obj.size==1
|
40
|
-
root = obj.values.first
|
41
|
-
return root.to_xml(:root => obj.keys.first)
|
42
|
-
else
|
43
|
-
return obj.to_xml
|
44
|
-
end
|
45
|
-
|
46
|
-
# Create representation and proxy
|
47
|
-
builder = Builder.new(obj, options)
|
48
|
-
|
49
|
-
# Check recipe arity size before calling it
|
50
|
-
recipe.call(*[builder, obj, options][0,recipe.arity])
|
51
|
-
builder.doc.to_xml
|
52
|
-
end
|
53
|
-
|
54
|
-
def helper
|
55
|
-
Helpers
|
56
|
-
end
|
57
|
-
end
|
58
|
-
end
|
59
|
-
end
|
60
|
-
end
|
data/lib/tokamak/xml/builder.rb
DELETED
@@ -1,115 +0,0 @@
|
|
1
|
-
module Tokamak
|
2
|
-
module Xml
|
3
|
-
# Implements the interface for marshal Xml media type requests (application/xml)
|
4
|
-
class Builder
|
5
|
-
attr_reader :doc
|
6
|
-
def initialize(obj, options = {})
|
7
|
-
@doc = Nokogiri::XML::Document.new
|
8
|
-
@obj = obj
|
9
|
-
root = options[:root] || Tokamak.root_element_for(obj)
|
10
|
-
@parent = @doc.create_element(root)
|
11
|
-
@parent.parent = @doc
|
12
|
-
end
|
13
|
-
|
14
|
-
def method_missing(sym, *args, &block)
|
15
|
-
values do |v|
|
16
|
-
v.send sym, *args, &block
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
def values(options = {}, &block)
|
21
|
-
options.each do |key,value|
|
22
|
-
attr = key.to_s
|
23
|
-
if attr =~ /^xmlns(:\w+)?$/
|
24
|
-
ns = attr.split(":", 2)[1]
|
25
|
-
@parent.add_namespace_definition(ns, value)
|
26
|
-
end
|
27
|
-
end
|
28
|
-
yield Values.new(self)
|
29
|
-
end
|
30
|
-
|
31
|
-
def insert_value(name, prefix, *args, &block)
|
32
|
-
node = create_element(name.to_s, prefix, *args)
|
33
|
-
node.parent = @parent
|
34
|
-
|
35
|
-
if block_given?
|
36
|
-
@parent = node
|
37
|
-
block.call
|
38
|
-
@parent = node.parent
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
def link(relationship, uri, options = {})
|
43
|
-
options["rel"] = relationship.to_s
|
44
|
-
options["href"] = uri
|
45
|
-
options["type"] ||= "application/xml"
|
46
|
-
insert_value("link", nil, options)
|
47
|
-
end
|
48
|
-
|
49
|
-
def members(a_collection = nil, options = {}, &block)
|
50
|
-
collection = a_collection || @obj
|
51
|
-
raise Error::BuilderError("Members method require a collection to execute") unless collection.respond_to?(:each)
|
52
|
-
collection.each do |member|
|
53
|
-
root = options[:root] || Tokamak.root_element_for(member)
|
54
|
-
entry = @doc.create_element(root)
|
55
|
-
entry.parent = @parent
|
56
|
-
@parent = entry
|
57
|
-
block.call(self, member)
|
58
|
-
@parent = entry.parent
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
private
|
63
|
-
|
64
|
-
def create_element(node, prefix, *args)
|
65
|
-
node = @doc.create_element(node) do |n|
|
66
|
-
if prefix
|
67
|
-
if namespace = prefix_valid?(prefix)
|
68
|
-
# Adding namespace prefix
|
69
|
-
n.namespace = namespace
|
70
|
-
namespace = nil
|
71
|
-
end
|
72
|
-
end
|
73
|
-
|
74
|
-
args.each do |arg|
|
75
|
-
case arg
|
76
|
-
# Adding XML attributes
|
77
|
-
when Hash
|
78
|
-
arg.each { |k,v|
|
79
|
-
key = k.to_s
|
80
|
-
if key =~ /^xmlns(:\w+)?$/
|
81
|
-
ns_name = key.split(":", 2)[1]
|
82
|
-
n.add_namespace_definition(ns_name, v)
|
83
|
-
next
|
84
|
-
end
|
85
|
-
n[k.to_s] = v.to_s
|
86
|
-
}
|
87
|
-
# Adding XML node content
|
88
|
-
else
|
89
|
-
content = if arg.kind_of?(Time) || arg.kind_of?(DateTime)
|
90
|
-
arg.xmlschema
|
91
|
-
else
|
92
|
-
arg
|
93
|
-
end
|
94
|
-
n.content = content
|
95
|
-
end
|
96
|
-
end
|
97
|
-
end
|
98
|
-
end
|
99
|
-
|
100
|
-
def prefix_valid?(prefix)
|
101
|
-
ns = @parent.namespace_definitions.find { |x| x.prefix == prefix.to_s }
|
102
|
-
|
103
|
-
unless ns
|
104
|
-
@parent.ancestors.each do |a|
|
105
|
-
next if a == @doc
|
106
|
-
ns = a.namespace_definitions.find { |x| x.prefix == prefix.to_s }
|
107
|
-
break if ns
|
108
|
-
end
|
109
|
-
end
|
110
|
-
|
111
|
-
return ns
|
112
|
-
end
|
113
|
-
end
|
114
|
-
end
|
115
|
-
end
|
data/lib/tokamak/xml/helpers.rb
DELETED
data/lib/tokamak/xml/link.rb
DELETED
@@ -1,31 +0,0 @@
|
|
1
|
-
module Tokamak
|
2
|
-
module Xml
|
3
|
-
class Link
|
4
|
-
def initialize(options = {})
|
5
|
-
@options = options
|
6
|
-
end
|
7
|
-
def href
|
8
|
-
@options["href"]
|
9
|
-
end
|
10
|
-
def rel
|
11
|
-
@options["rel"]
|
12
|
-
end
|
13
|
-
def content_type
|
14
|
-
@options["type"]
|
15
|
-
end
|
16
|
-
def type
|
17
|
-
content_type
|
18
|
-
end
|
19
|
-
def follow
|
20
|
-
r = Restfulie.at(href)
|
21
|
-
r = r.as(content_type) if content_type
|
22
|
-
r
|
23
|
-
end
|
24
|
-
|
25
|
-
def to_s
|
26
|
-
"<link to #{@options}>"
|
27
|
-
end
|
28
|
-
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
data/lib/tokamak/xml/links.rb
DELETED
@@ -1,35 +0,0 @@
|
|
1
|
-
module Tokamak
|
2
|
-
module Xml
|
3
|
-
|
4
|
-
# an object to represent a list of links that can be invoked
|
5
|
-
class Links
|
6
|
-
|
7
|
-
def initialize(links)
|
8
|
-
@hash = {}
|
9
|
-
links = [links] unless links.kind_of? Array
|
10
|
-
links = [] unless links
|
11
|
-
links.each { |l|
|
12
|
-
link = Tokamak::Xml::Link.new(l)
|
13
|
-
@hash[link.rel.to_s] = link
|
14
|
-
}
|
15
|
-
end
|
16
|
-
|
17
|
-
def [](name)
|
18
|
-
@hash[name]
|
19
|
-
end
|
20
|
-
|
21
|
-
def size
|
22
|
-
@hash.size
|
23
|
-
end
|
24
|
-
|
25
|
-
def keys
|
26
|
-
@hash.keys
|
27
|
-
end
|
28
|
-
|
29
|
-
def method_missing(sym, *args)
|
30
|
-
raise "Links can not receive arguments" unless args.empty?
|
31
|
-
self[sym.to_s]
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
data/lib/tokamak/xml.rb
DELETED
@@ -1,12 +0,0 @@
|
|
1
|
-
require 'nokogiri'
|
2
|
-
|
3
|
-
module Tokamak
|
4
|
-
module Xml
|
5
|
-
autoload :Base, 'tokamak/xml/base'
|
6
|
-
autoload :Builder, 'tokamak/xml/builder'
|
7
|
-
autoload :Helpers, 'tokamak/xml/helpers'
|
8
|
-
autoload :Links, 'tokamak/xml/links'
|
9
|
-
autoload :Link, 'tokamak/xml/link'
|
10
|
-
extend Base::ClassMethods
|
11
|
-
end
|
12
|
-
end
|
@@ -1,191 +0,0 @@
|
|
1
|
-
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
-
|
3
|
-
module Tokamak::Test
|
4
|
-
class SimpleClass
|
5
|
-
attr_accessor :id, :title, :updated
|
6
|
-
def initialize(id,title,updated)
|
7
|
-
@id, @title, @updated = id, title, updated
|
8
|
-
end
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
describe Tokamak do
|
13
|
-
describe 'Atom' do
|
14
|
-
|
15
|
-
describe "Feed" do
|
16
|
-
it "should create a feed from builder DSL" do
|
17
|
-
time = Time.now
|
18
|
-
some_articles = [
|
19
|
-
{:id => 1, :title => "a great article", :updated => time},
|
20
|
-
{:id => 2, :title => "another great article", :updated => time}
|
21
|
-
]
|
22
|
-
|
23
|
-
feed = to_atom(some_articles) do |collection|
|
24
|
-
collection.values do |values|
|
25
|
-
values.id "http://example.com/feed"
|
26
|
-
values.title "Feed"
|
27
|
-
values.updated time
|
28
|
-
|
29
|
-
values.author {
|
30
|
-
values.name "John Doe"
|
31
|
-
values.email "joedoe@example.com"
|
32
|
-
}
|
33
|
-
|
34
|
-
values.author {
|
35
|
-
values.name "Foo Bar"
|
36
|
-
values.email "foobar@example.com"
|
37
|
-
}
|
38
|
-
end
|
39
|
-
|
40
|
-
collection.link("next", "http://a.link.com/next")
|
41
|
-
collection.link("previous", "http://a.link.com/previous")
|
42
|
-
|
43
|
-
collection.members do |member, article|
|
44
|
-
member.values do |values|
|
45
|
-
values.id "uri:#{article[:id]}"
|
46
|
-
values.title article[:title]
|
47
|
-
values.updated article[:updated]
|
48
|
-
end
|
49
|
-
|
50
|
-
member.link("image", "http://example.com/image/1")
|
51
|
-
member.link("image", "http://example.com/image/2", :type => "application/atom+xml")
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
feed.atom_type.should == "feed"
|
56
|
-
feed.id.should == "http://example.com/feed"
|
57
|
-
feed.title.should == "Feed"
|
58
|
-
feed.updated.should == DateTime.parse(time.xmlschema)
|
59
|
-
feed.authors.first.name.should == "John Doe"
|
60
|
-
feed.authors.last.email.should == "foobar@example.com"
|
61
|
-
|
62
|
-
feed.entries.first.id.should == "uri:1"
|
63
|
-
feed.entries.first.title.should == "a great article"
|
64
|
-
end
|
65
|
-
|
66
|
-
it "should create a feed from a string input" do
|
67
|
-
full_atom = IO.read(File.dirname(__FILE__) + '/../full_atom.xml')
|
68
|
-
feed = to_atom(full_atom)
|
69
|
-
|
70
|
-
feed.id.should == "http://example.com/albums/1"
|
71
|
-
feed.title.should == "Albums feed"
|
72
|
-
feed.updated.should be_kind_of(Time)
|
73
|
-
feed.updated.should == Time.parse("2010-05-03T16:29:26-03:00")
|
74
|
-
end
|
75
|
-
|
76
|
-
end
|
77
|
-
|
78
|
-
describe "Entry" do
|
79
|
-
it "should create an entry from builder DSL" do
|
80
|
-
time = Time.now
|
81
|
-
an_article = {:id => 1, :title => "a great article", :updated => time}
|
82
|
-
|
83
|
-
entry = to_atom(an_article, :atom_type => :entry) do |member, article|
|
84
|
-
member.values do |values|
|
85
|
-
values.id "uri:#{article[:id]}"
|
86
|
-
values.title article[:title]
|
87
|
-
values.updated article[:updated]
|
88
|
-
end
|
89
|
-
|
90
|
-
member.link("image", "http://example.com/image/1")
|
91
|
-
member.link("image", "http://example.com/image/2", :type => "application/atom+xml")
|
92
|
-
end
|
93
|
-
|
94
|
-
entry.atom_type.should == "entry"
|
95
|
-
entry.id.should == "uri:1"
|
96
|
-
entry.title.should == "a great article"
|
97
|
-
entry.updated.should == DateTime.parse(time.xmlschema)
|
98
|
-
end
|
99
|
-
|
100
|
-
it "should be able to declare links inside values block" do
|
101
|
-
time = Time.now
|
102
|
-
an_article = {:id => 1, :title => "a great article", :updated => time}
|
103
|
-
|
104
|
-
entry = to_atom(an_article, :atom_type => :entry) do |member, article|
|
105
|
-
member.values do |values|
|
106
|
-
values.id "uri:#{article[:id]}"
|
107
|
-
values.title article[:title]
|
108
|
-
values.updated article[:updated]
|
109
|
-
|
110
|
-
values.domain("xmlns" => "http://a.namespace.com") {
|
111
|
-
member.link("image", "http://example.com/image/1")
|
112
|
-
member.link("image", "http://example.com/image/2", :type => "application/atom+xml")
|
113
|
-
}
|
114
|
-
end
|
115
|
-
end
|
116
|
-
|
117
|
-
entry.atom_type.should == "entry"
|
118
|
-
entry.id.should == "uri:1"
|
119
|
-
entry.title.should == "a great article"
|
120
|
-
entry.updated.should == DateTime.parse(time.xmlschema)
|
121
|
-
|
122
|
-
entry.doc.xpath("xmlns:domain", "xmlns" => "http://a.namespace.com").children.first.node_name.should == "link"
|
123
|
-
end
|
124
|
-
|
125
|
-
it "should create an entry from an already declared recipe" do
|
126
|
-
|
127
|
-
describe_recipe(:simple_entry) do |member, article|
|
128
|
-
member.values do |values|
|
129
|
-
values.id "uri:#{article[:id]}"
|
130
|
-
values.title article[:title]
|
131
|
-
values.updated article[:updated]
|
132
|
-
end
|
133
|
-
|
134
|
-
member.link("image", "http://example.com/image/1")
|
135
|
-
member.link("image", "http://example.com/image/2", :type => "application/atom+xml")
|
136
|
-
end
|
137
|
-
|
138
|
-
time = Time.now
|
139
|
-
an_article = {:id => 1, :title => "a great article", :updated => time}
|
140
|
-
|
141
|
-
entry = to_atom(an_article, :atom_type => :entry, :recipe => :simple_entry)
|
142
|
-
|
143
|
-
entry.atom_type.should == "entry"
|
144
|
-
entry.id.should == "uri:1"
|
145
|
-
entry.title.should == "a great article"
|
146
|
-
entry.updated.should == DateTime.parse(time.xmlschema)
|
147
|
-
end
|
148
|
-
|
149
|
-
end
|
150
|
-
|
151
|
-
describe "Errors" do
|
152
|
-
it "should raise error for converter without recipe" do
|
153
|
-
lambda {
|
154
|
-
to_atom
|
155
|
-
}.should raise_error(Tokamak::ConverterError, "Recipe required")
|
156
|
-
end
|
157
|
-
|
158
|
-
it "raise error to invalid atom type" do
|
159
|
-
lambda {
|
160
|
-
obj = Object.new
|
161
|
-
describe_recipe(:simple_entry) do |member, article|
|
162
|
-
member.values do |values|
|
163
|
-
values.id "uri:#{article[:id]}"
|
164
|
-
values.title article[:title]
|
165
|
-
values.updated article[:updated]
|
166
|
-
end
|
167
|
-
|
168
|
-
member.link("image", "http://example.com/image/1")
|
169
|
-
member.link("image", "http://example.com/image/2", :type => "application/atom+xml")
|
170
|
-
end
|
171
|
-
|
172
|
-
Tokamak::Atom.to_atom(obj, :recipe => :simple_entry, :atom_type => :foo)
|
173
|
-
}.should raise_error(Tokamak::ConverterError, "Undefined atom type foo")
|
174
|
-
end
|
175
|
-
end
|
176
|
-
|
177
|
-
end
|
178
|
-
|
179
|
-
def to_atom(*args, &recipe)
|
180
|
-
Tokamak::Atom.to_atom(*args, &recipe)
|
181
|
-
end
|
182
|
-
|
183
|
-
def describe_recipe(*args, &recipe)
|
184
|
-
Tokamak::Atom.describe_recipe(*args, &recipe)
|
185
|
-
end
|
186
|
-
|
187
|
-
def simple_object(*args)
|
188
|
-
Tokamak::Test::SimpleClass.new(*args)
|
189
|
-
end
|
190
|
-
|
191
|
-
end
|