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.
Files changed (91) hide show
  1. data/LICENSE +14 -0
  2. data/README.md +159 -0
  3. data/lib/tokamak/builder/base.rb +74 -0
  4. data/lib/tokamak/{json/builder.rb → builder/json.rb} +38 -29
  5. data/lib/tokamak/builder/values.rb +33 -0
  6. data/lib/tokamak/{atom/builder.rb → builder/xml.rb} +46 -41
  7. data/lib/tokamak/builder.rb +22 -0
  8. data/lib/tokamak/errors.rb +3 -0
  9. data/lib/tokamak/hook/rails.rb +78 -0
  10. data/lib/tokamak/hook/sinatra.rb +18 -0
  11. data/lib/tokamak/hook/tilt.rb +42 -0
  12. data/lib/tokamak/hook.rb +6 -0
  13. data/lib/tokamak/recipes.rb +26 -0
  14. data/lib/tokamak/version.rb +13 -0
  15. data/lib/tokamak.rb +14 -18
  16. data/script/console +7 -0
  17. data/test/rails2_skel/Rakefile +16 -0
  18. data/test/rails2_skel/app/controllers/application_controller.rb +1 -0
  19. data/test/rails2_skel/app/controllers/test_controller.rb +18 -0
  20. data/test/rails2_skel/app/views/test/_feed_member.tokamak +9 -0
  21. data/test/rails2_skel/app/views/test/feed.tokamak +24 -0
  22. data/test/rails2_skel/app/views/test/show.tokamak +31 -0
  23. data/test/rails2_skel/config/boot.rb +110 -0
  24. data/test/rails2_skel/config/environment.rb +20 -0
  25. data/test/rails2_skel/config/environments/development.rb +17 -0
  26. data/test/rails2_skel/config/environments/production.rb +28 -0
  27. data/test/rails2_skel/config/environments/test.rb +28 -0
  28. data/test/rails2_skel/config/initializers/cookie_verification_secret.rb +2 -0
  29. data/test/rails2_skel/config/initializers/mime_types.rb +3 -0
  30. data/test/rails2_skel/config/initializers/new_rails_defaults.rb +10 -0
  31. data/test/rails2_skel/config/initializers/session_store.rb +5 -0
  32. data/test/rails2_skel/config/routes.rb +43 -0
  33. data/test/rails2_skel/script/console +3 -0
  34. data/test/test_helper.rb +7 -0
  35. data/test/tokamak/builder/base_test.rb +28 -0
  36. data/test/tokamak/builder/json_test.rb +227 -0
  37. data/test/tokamak/builder/xml_test.rb +254 -0
  38. data/test/tokamak/helper_test.rb +106 -0
  39. data/test/tokamak/hook/rails_test.rb +74 -0
  40. data/test/tokamak/hook/sinatra_test.rb +85 -0
  41. data/test/tokamak/hook/tilt_test.rb +35 -0
  42. data/test/tokamak/recipes_test.rb +90 -0
  43. metadata +106 -113
  44. data/.document +0 -5
  45. data/.rspec +0 -1
  46. data/Gemfile +0 -27
  47. data/Gemfile.lock +0 -77
  48. data/LICENSE.txt +0 -20
  49. data/README.rdoc +0 -69
  50. data/Rakefile +0 -50
  51. data/VERSION +0 -1
  52. data/lib/tokamak/atom/base.rb +0 -87
  53. data/lib/tokamak/atom/helpers.rb +0 -13
  54. data/lib/tokamak/atom.rb +0 -8
  55. data/lib/tokamak/error.rb +0 -6
  56. data/lib/tokamak/json/base.rb +0 -83
  57. data/lib/tokamak/json/helpers.rb +0 -13
  58. data/lib/tokamak/json.rb +0 -10
  59. data/lib/tokamak/representation/atom/atom.rng +0 -597
  60. data/lib/tokamak/representation/atom/base.rb +0 -140
  61. data/lib/tokamak/representation/atom/category.rb +0 -39
  62. data/lib/tokamak/representation/atom/entry.rb +0 -56
  63. data/lib/tokamak/representation/atom/factory.rb +0 -48
  64. data/lib/tokamak/representation/atom/feed.rb +0 -108
  65. data/lib/tokamak/representation/atom/link.rb +0 -66
  66. data/lib/tokamak/representation/atom/person.rb +0 -46
  67. data/lib/tokamak/representation/atom/source.rb +0 -57
  68. data/lib/tokamak/representation/atom/tag_collection.rb +0 -36
  69. data/lib/tokamak/representation/atom/xml.rb +0 -94
  70. data/lib/tokamak/representation/atom.rb +0 -18
  71. data/lib/tokamak/representation/generic.rb +0 -20
  72. data/lib/tokamak/representation/json/base.rb +0 -25
  73. data/lib/tokamak/representation/json/keys_as_methods.rb +0 -72
  74. data/lib/tokamak/representation/json/link.rb +0 -27
  75. data/lib/tokamak/representation/json/link_collection.rb +0 -21
  76. data/lib/tokamak/representation/json.rb +0 -11
  77. data/lib/tokamak/representation/links.rb +0 -9
  78. data/lib/tokamak/representation.rb +0 -3
  79. data/lib/tokamak/values.rb +0 -29
  80. data/lib/tokamak/xml/base.rb +0 -60
  81. data/lib/tokamak/xml/builder.rb +0 -115
  82. data/lib/tokamak/xml/helpers.rb +0 -13
  83. data/lib/tokamak/xml/link.rb +0 -31
  84. data/lib/tokamak/xml/links.rb +0 -35
  85. data/lib/tokamak/xml.rb +0 -12
  86. data/spec/integration/atom/atom_spec.rb +0 -191
  87. data/spec/integration/full_atom.xml +0 -92
  88. data/spec/integration/full_json.js +0 -46
  89. data/spec/integration/json/json_spec.rb +0 -172
  90. data/spec/integration/xml/xml_spec.rb +0 -203
  91. data/spec/spec_helper.rb +0 -12
@@ -1,140 +0,0 @@
1
- module Tokamak
2
- module Representation
3
- module Atom
4
- class Base < XML
5
- ATOM_ATTRIBUTES = {
6
- :feed => {
7
- :required => [:id, :title, :updated],
8
- :recommended => [:author, :link],
9
- :optional => [:category, :contributor, :rights, :generator, :icon, :logo, :subtitle]
10
- },
11
-
12
- :entry => {
13
- :required => [:id, :title, :updated],
14
- :recommended => [:author, :link, :content, :summary],
15
- :optional => [:category, :contributor, :rights, :published, :source]
16
- }
17
- }
18
-
19
- def initialize(xml_obj = nil)
20
- @authors = nil
21
- @contributors = nil
22
- @links = nil
23
- @categories = nil
24
- @reserved = []
25
- super(xml_obj)
26
- end
27
-
28
- # lists all extension points available in this entry
29
- def extensions
30
- result = []
31
- @doc.children.each do |e|
32
- if e.element? && !@reserved.map(&:to_s).include?(e.node_name)
33
- result << e
34
- end
35
- end
36
- return result
37
- end
38
-
39
- # simpletext
40
- def id
41
- text("id")
42
- end
43
-
44
- def id=(value)
45
- set_text("id", value)
46
- end
47
-
48
- # text
49
- def title
50
- text("title")
51
- end
52
-
53
- def title=(value)
54
- set_text("title", value)
55
- end
56
-
57
- def updated
58
- value = text("updated")
59
- Time.parse(value) unless value.nil?
60
- end
61
-
62
- def updated=(value)
63
- set_text("updated", value)
64
- end
65
-
66
- def published
67
- value = text("published")
68
- Time.parse(value) unless value.nil?
69
- end
70
-
71
- def published=(value)
72
- set_text("published", value)
73
- end
74
-
75
- # text
76
- def rights
77
- text("rights")
78
- end
79
-
80
- def rights=(value)
81
- set_text("rights", value)
82
- end
83
-
84
- # Author have name, and optional uri and email, this describes a person
85
- def authors
86
- return @authors if @authors
87
-
88
- @authors = TagCollection.new(@doc)
89
- @doc.xpath("xmlns:author").each do |author|
90
- @authors << Person.new("author", author)
91
- end
92
- @authors
93
-
94
- end
95
-
96
- def contributors
97
- return @contributors if @contributors
98
-
99
- @contributors = TagCollection.new(@doc)
100
- @doc.xpath("xmlns:author").each do |contributor|
101
- @contributors << Person.new("contributor", contributor)
102
- end
103
- @contributors
104
- end
105
-
106
- include Tokamak::Representation::Links
107
-
108
- # It has one required attribute, href, and five optional attributes: rel, type, hreflang, title, and length
109
- def links
110
- return @links if @links
111
-
112
- @links = TagCollection.new(@doc) do |array, symbol, *args|
113
- linkset = array.select {|link| link.rel == symbol.to_s }
114
- unless linkset.empty?
115
- linkset.size == 1 ? linkset.first : linkset
116
- else
117
- nil
118
- end
119
- end
120
- @doc.xpath("xmlns:link").each do |link|
121
- @links << Link.new(link)
122
- end
123
- @links
124
-
125
- end
126
-
127
- def categories
128
- return @categories if @categories
129
-
130
- @categories = TagCollection.new(@doc)
131
- @doc.xpath("xmlns:category").each do |category|
132
- @categories << Link.new(category)
133
- end
134
- @categories
135
-
136
- end
137
- end
138
- end
139
- end
140
- end
@@ -1,39 +0,0 @@
1
- module Tokamak
2
- module Representation
3
- module Atom
4
- class Category < XML
5
- def initialize(options_or_obj)
6
- if options_or_obj.kind_of?(Hash)
7
- @doc = Nokogiri::XML::Document.new()
8
- options_or_obj = create_element("category", options_or_obj)
9
- end
10
- super(options_or_obj)
11
- end
12
-
13
- def term
14
- @doc["term"]
15
- end
16
-
17
- def term=(value)
18
- @doc["term"] = value
19
- end
20
-
21
- def scheme
22
- @doc["scheme"]
23
- end
24
-
25
- def scheme=(value)
26
- @doc["scheme"] = value
27
- end
28
-
29
- def label
30
- @doc["label"]
31
- end
32
-
33
- def label=(value)
34
- @doc["label"] = value
35
- end
36
- end
37
- end
38
- end
39
- end
@@ -1,56 +0,0 @@
1
- module Tokamak
2
- module Representation
3
- module Atom
4
- class Entry < Base
5
- def initialize(xml_obj = nil)
6
- @source = nil
7
- super(xml_obj)
8
- @reserved = Base::ATOM_ATTRIBUTES[:entry][:required] + Base::ATOM_ATTRIBUTES[:entry][:recommended] + Base::ATOM_ATTRIBUTES[:entry][:optional]
9
- end
10
-
11
- # text
12
- def content
13
- text("content")
14
- end
15
-
16
- def content=(value)
17
- set_text("content", value)
18
- end
19
-
20
- # text
21
- def summary
22
- text("summary")
23
- end
24
-
25
- def summary=(value)
26
- set_text("summary", value)
27
- end
28
-
29
- # rfc 3339
30
- def published
31
- value = text("published")
32
- Time.parse(value) unless value.nil?
33
- end
34
-
35
- def published=(value)
36
- set_text("published", value)
37
- end
38
-
39
- # comp: id, title, udpated, rights (optional)
40
- def source
41
- unless @source
42
- @doc.xpath("xmlns:source").each do |source|
43
- @source = Generator.new(source)
44
- end
45
- end
46
-
47
- return source
48
- end
49
-
50
- def source=(obj)
51
- @source = obj
52
- end
53
- end
54
- end
55
- end
56
- end
@@ -1,48 +0,0 @@
1
- module Tokamak
2
- module Representation
3
- module Atom
4
- # Create a new Representation::Atom object using a +string_or_io+
5
- # object.
6
- #
7
- # Examples
8
- # xml = IO.read("spec/units/lib/atoms/full_atom.xml")
9
- # atom = Tokamak::Representation::Atom.new(xml)
10
- class Factory
11
- # RelaxNG file to validate atom
12
- RELAXNG_ATOM = File.join(File.dirname(__FILE__), 'atom.rng')
13
-
14
- if Nokogiri::VERSION_INFO["libxml"]["loaded"] < "2.7.7"
15
- puts "WARNING! In order to use schema validation on atom representations you need libxml version 2.7.7 or superior loaded in your system."
16
- SCHEMA = nil
17
- else
18
- SCHEMA = ::Nokogiri::XML::RelaxNG(File.open(RELAXNG_ATOM))
19
- end
20
-
21
- def self.create(string_or_io)
22
- doc = string_or_io.kind_of?(Nokogiri::XML::Document) ? string_or_io : Nokogiri::XML(string_or_io)
23
-
24
- if SCHEMA && !(errors = SCHEMA.validate(doc)).empty?
25
- raise AtomInvalid.new("Invalid Atom: "+ errors.join(", "))
26
- end
27
-
28
- representation_for doc
29
-
30
- end
31
-
32
- private
33
- def self.representation_for(doc)
34
- case doc.root.name
35
- when "feed"
36
- Tokamak::Representation::Atom::Feed.new(doc)
37
- when "entry"
38
- Tokamak::Representation::Atom::Entry.new(doc)
39
- end
40
- end
41
-
42
- end
43
-
44
- class AtomInvalid < StandardError
45
- end
46
- end
47
- end
48
- end
@@ -1,108 +0,0 @@
1
- module Tokamak
2
- module Representation
3
- module Atom
4
- class Feed < Base
5
- def initialize(xml_obj = nil)
6
- @generator = nil
7
- @entries = nil
8
- super(xml_obj)
9
- @reserved = Base::ATOM_ATTRIBUTES[:feed][:required] + Base::ATOM_ATTRIBUTES[:feed][:recommended] + Base::ATOM_ATTRIBUTES[:feed][:optional] + [:entry]
10
- end
11
-
12
- def entries
13
- unless @entries
14
- @entries = TagCollection.new(@doc)
15
- @doc.xpath("xmlns:entry").each do |entry|
16
- @entries << Entry.new(entry)
17
- end
18
- end
19
-
20
- return @entries
21
- end
22
-
23
- # comp: attr: uri, version (optionals); content (mandatory)
24
- def generator
25
- unless @generator
26
- @doc.xpath("xmlns:generator").each do |generator|
27
- @generator = Generator.new(generator)
28
- end
29
- end
30
-
31
- return generator
32
- end
33
-
34
- def generator=(obj)
35
- @generator = obj
36
- end
37
-
38
- # simple text
39
- def icon
40
- text("icon")
41
- end
42
-
43
- def icon=(value)
44
- set_text("icon", value)
45
- end
46
-
47
- # simple text
48
- def logo
49
- text("logo")
50
- end
51
-
52
- def logo=(value)
53
- set_text("logo", value)
54
- end
55
-
56
- # text
57
- def subtitle
58
- text("subtitle")
59
- end
60
-
61
- def subtitle=(value)
62
- set_text("subtitle", value)
63
- end
64
- end
65
-
66
- class Generator < XML
67
- def initialize(options_or_obj)
68
- if options_or_obj.kind_of?(Hash)
69
- @doc = Nokogiri::XML::Document.new()
70
- node = @doc.create_element("generator")
71
- node.add_namespace_definition(nil, "http://www.w3.org/2005/Atom")
72
- node.parent = @doc
73
- super(node)
74
- options_or_obj.each do |key,value|
75
- self.send("#{key}=".to_sym, value)
76
- end
77
- else
78
- super(options_or_obj)
79
- end
80
- end
81
-
82
- def content
83
- @doc.content
84
- end
85
-
86
- def content=(value)
87
- @doc.content = value
88
- end
89
-
90
- def uri
91
- @doc["uri"]
92
- end
93
-
94
- def uri=(value)
95
- @doc["uri"] = value
96
- end
97
-
98
- def version
99
- @doc["version"]
100
- end
101
-
102
- def version=(value)
103
- @doc["version"] = value
104
- end
105
- end
106
- end
107
- end
108
- end
@@ -1,66 +0,0 @@
1
- module Tokamak
2
- module Representation
3
- module Atom
4
- class Link < XML
5
- def initialize(options_or_obj)
6
- if options_or_obj.kind_of?(Hash)
7
- @doc = Nokogiri::XML::Document.new()
8
- options_or_obj = create_element("link", options_or_obj)
9
- end
10
- super(options_or_obj)
11
- end
12
-
13
- def href
14
- @doc["href"]
15
- end
16
-
17
- def href=(value)
18
- @doc["href"] = value
19
- end
20
-
21
- def rel
22
- @doc["rel"]
23
- end
24
-
25
- def rel=(value)
26
- @doc["rel"] = value
27
- end
28
-
29
- if method_defined?(:type)
30
- alias_method :__type__, :type
31
- end
32
- def type
33
- @doc["type"]
34
- end
35
-
36
- def type=(value)
37
- @doc["type"] = value
38
- end
39
-
40
- def hreflang
41
- @doc["hreflang"]
42
- end
43
-
44
- def hreflang=(value)
45
- @doc["hreflang"] = value
46
- end
47
-
48
- def title
49
- @doc["title"]
50
- end
51
-
52
- def title=(value)
53
- @doc["title"] = value
54
- end
55
-
56
- def length
57
- @doc["length"]
58
- end
59
-
60
- def length=(value)
61
- @doc["length"] = value
62
- end
63
- end
64
- end
65
- end
66
- end
@@ -1,46 +0,0 @@
1
- module Tokamak
2
- module Representation
3
- module Atom
4
- class Person < XML
5
- def initialize(node_type, options_or_obj)
6
- if options_or_obj.kind_of?(Hash)
7
- @doc = Nokogiri::XML::Document.new()
8
- node = @doc.create_element(node_type)
9
- node.add_namespace_definition(nil, "http://www.w3.org/2005/Atom")
10
- node.parent = @doc
11
- super(node)
12
- options_or_obj.each do |key,value|
13
- self.send("#{key}=".to_sym, value)
14
- end
15
- else
16
- super(options_or_obj)
17
- end
18
- end
19
-
20
- def name
21
- text("name")
22
- end
23
-
24
- def name=(value)
25
- set_text("name", value)
26
- end
27
-
28
- def uri
29
- text("uri")
30
- end
31
-
32
- def uri=(value)
33
- set_text("uri", value)
34
- end
35
-
36
- def email
37
- text("email")
38
- end
39
-
40
- def email=(value)
41
- set_text("email", value)
42
- end
43
- end
44
- end
45
- end
46
- end
@@ -1,57 +0,0 @@
1
- module Tokamak
2
- module Representation
3
- module Atom
4
- class Source < XML
5
- def initialize(options_or_obj)
6
- if options_or_obj.kind_of?(Hash)
7
- @doc = Nokogiri::XML::Document.new()
8
- node = @doc.create_element("source")
9
- node.add_namespace_definition(nil, "http://www.w3.org/2005/Atom")
10
- node.parent = @doc
11
- super(node)
12
- options_or_obj.each do |key,value|
13
- self.send("#{key}=".to_sym, value)
14
- end
15
- else
16
- super(options_or_obj)
17
- end
18
- end
19
-
20
- def id
21
- text("id")
22
- end
23
-
24
- def id=(value)
25
- set_text("id", value)
26
- end
27
-
28
- # text
29
- def title
30
- text("title")
31
- end
32
-
33
- def title=(value)
34
- set_text("title", value)
35
- end
36
-
37
- def updated
38
- value = text("updated")
39
- Time.parse(value) unless value.nil?
40
- end
41
-
42
- def updated=(value)
43
- set_text("updated", value)
44
- end
45
-
46
- # text
47
- def rights
48
- text("rights")
49
- end
50
-
51
- def rights=(value)
52
- set_text("rights", value)
53
- end
54
- end
55
- end
56
- end
57
- end
@@ -1,36 +0,0 @@
1
- module Tokamak
2
- module Representation
3
- module Atom
4
- class TagCollection < ::Array
5
- def initialize(parent_node, &block)
6
- @node = parent_node
7
- @method_missing_block = block_given? ? block : nil
8
- super(0)
9
- end
10
-
11
- def <<(obj)
12
- obj = [obj] unless obj.kind_of?(Array)
13
- obj.each do |o|
14
- o.doc.parent = @node
15
- super(o)
16
- end
17
- end
18
-
19
- def delete(obj)
20
- if super(obj)
21
- obj.doc.unlink
22
- obj = nil
23
- end
24
- end
25
-
26
- def method_missing(symbol, *args, &block)
27
- if @method_missing_block
28
- @method_missing_block.call(self, symbol, *args)
29
- else
30
- super
31
- end
32
- end
33
- end
34
- end
35
- end
36
- end
@@ -1,94 +0,0 @@
1
- module Tokamak
2
- module Representation
3
- module Atom
4
- class XML
5
- attr_accessor :doc
6
-
7
- def initialize(xml_obj = nil)
8
- if xml_obj
9
- xml_obj.kind_of?(Nokogiri::XML::Document) ? @doc = xml_obj.root : @doc = xml_obj
10
- else
11
- @doc = Nokogiri::XML::Document.new
12
- root_node = @doc.create_element(atom_type.to_s)
13
- root_node.add_namespace_definition(nil, "http://www.w3.org/2005/Atom")
14
- root_node.parent = @doc
15
- @doc = @doc.root
16
- end
17
- end
18
-
19
- # Tools
20
- def css(*args)
21
- @doc.css(*args)
22
- end
23
-
24
- def xpath(*args)
25
- @doc.xpath(*args)
26
- end
27
-
28
- def atom_type
29
- self.class.name.demodulize.downcase
30
- end
31
-
32
- def to_xml
33
- @doc.to_xml
34
- end
35
-
36
- def to_s
37
- to_xml
38
- end
39
-
40
- def method_missing(method, *args, &block)
41
- text(method) || super
42
- end
43
-
44
- protected
45
-
46
- def text(node_name)
47
- (node = @doc.at_xpath("xmlns:#{node_name}")) ? node.content : nil
48
- end
49
-
50
- def set_text(node_name, value)
51
- if (node = @doc.at_xpath("xmlns:#{node_name}"))
52
- value = value.xmlschema if value.kind_of?(Time) || value.kind_of?(DateTime)
53
- node.content = value
54
- else
55
- node = create_element(node_name, value)
56
- node.parent = @doc
57
- end
58
- end
59
-
60
- private
61
-
62
- def create_element(node, *args)
63
- node = @doc.document.create_element(node) do |n|
64
- args.each do |arg|
65
- include_arg n, arg
66
- end
67
- end
68
- end
69
-
70
- def include_arg(n, arg)
71
- case arg
72
- when Hash
73
- # Adding XML attributes
74
- arg.each { |k,v|
75
- key = k.to_s
76
- if key =~ /^xmlns(:\w+)?$/
77
- ns_name = key.split(":", 2)[1]
78
- n.add_namespace_definition(ns_name, v)
79
- return
80
- end
81
- n[k.to_s] = v.to_s
82
- }
83
- else
84
- # Adding XML node content
85
- arg.kind_of?(Time) || arg.kind_of?(DateTime) ? content = arg.xmlschema : content = arg
86
- n.content = content
87
- end
88
- end
89
-
90
-
91
- end
92
- end
93
- end
94
- end
@@ -1,18 +0,0 @@
1
- require 'nokogiri'
2
-
3
- module Tokamak
4
- module Representation
5
- module Atom
6
- autoload :Factory, 'tokamak/representation/atom/factory'
7
- autoload :XML, 'tokamak/representation/atom/xml'
8
- autoload :Base, 'tokamak/representation/atom/base'
9
- autoload :TagCollection, 'tokamak/representation/atom/tag_collection'
10
- autoload :Link, 'tokamak/representation/atom/link'
11
- autoload :Person, 'tokamak/representation/atom/person'
12
- autoload :Category, 'tokamak/representation/atom/category'
13
- autoload :Feed, 'tokamak/representation/atom/feed'
14
- autoload :Entry, 'tokamak/representation/atom/entry'
15
- autoload :Source, 'tokamak/representation/atom/source'
16
- end
17
- end
18
- end