atom 0.2 → 0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/atom.rb +88 -27
  2. data/lib/xmlmapping.rb +20 -13
  3. metadata +2 -2
@@ -26,6 +26,74 @@ require 'time'
26
26
 
27
27
  module Atom
28
28
  NAMESPACE = 'http://www.w3.org/2005/Atom'
29
+ XHTML_NAMESPACE = 'http://www.w3.org/1999/xhtml'
30
+
31
+ class Text < String
32
+ attr :mime_type
33
+
34
+ def initialize(element)
35
+ type = element.attribute('type', NAMESPACE)
36
+
37
+ @mime_type = if type.nil?
38
+ 'text/plain'
39
+ else
40
+ case type.value
41
+ when 'text': 'text/plain'
42
+ when 'html': 'text/html'
43
+ when 'xhtml': 'text/xhtml'
44
+ else raise "Unknown type: #{type.value}"
45
+ end
46
+ end
47
+
48
+ value = case @mime_type
49
+ when 'text/plain', 'text/html': element.texts.map {|t| t.value }.join
50
+ when 'text/xhtml' :
51
+ REXML::XPath.first(element, 'xhtml:div', 'xhtml' => XHTML_NAMESPACE).children.to_s
52
+ # TODO: resolve relative uris
53
+ end
54
+
55
+ super value
56
+ end
57
+ end
58
+
59
+ class Content
60
+ attr :mime_type
61
+ attr :src
62
+ attr :value
63
+
64
+ def initialize(element)
65
+ type = element.attribute('type', NAMESPACE)
66
+ src = element.attribute('src', NAMESPACE)
67
+
68
+ if src.nil?
69
+ @mime_type = if type.nil?
70
+ 'text/plain'
71
+ else
72
+ case type.value
73
+ when 'text': 'text/plain'
74
+ when 'html': 'text/html'
75
+ when 'xhtml': 'text/xhtml'
76
+ else type.value
77
+ end
78
+ end
79
+
80
+ @value = case @mime_type
81
+ when 'text/plain', 'text/html': element.texts.map { |t| t.value }.join
82
+ when 'text/xhtml':
83
+ REXML::XPath.first(element, 'xhtml:div', 'xhtml' => XHTML_NAMESPACE).children.to_s
84
+ when /\+xml$|\/xml$/: REXML::XPath.first(element).children.to_s
85
+ else element.texts.join.strip.unpack("m")[0]
86
+ end
87
+ else
88
+ @src = src.value
89
+ if !type.nil?
90
+ @mime_type = type.value
91
+ end
92
+
93
+ @value = nil
94
+ end
95
+ end
96
+ end
29
97
 
30
98
  class Person
31
99
  include XMLMapping
@@ -37,7 +105,11 @@ module Atom
37
105
  has_one :uri
38
106
 
39
107
  def to_s
40
- name
108
+ if !email.nil?
109
+ "#{name} (#{email})"
110
+ else
111
+ name
112
+ end
41
113
  end
42
114
  end
43
115
 
@@ -48,10 +120,10 @@ module Atom
48
120
 
49
121
  has_attribute :uri
50
122
  has_attribute :version
51
- text :value
123
+ text :name
52
124
 
53
125
  def to_s
54
- value
126
+ name
55
127
  end
56
128
  end
57
129
 
@@ -61,7 +133,7 @@ module Atom
61
133
  namespace NAMESPACE
62
134
 
63
135
  has_attribute :href
64
- has_attribute :rel
136
+ has_attribute :rel, :default => 'alternate'
65
137
  has_attribute :type
66
138
  has_attribute :hreflang
67
139
  has_attribute :title
@@ -86,35 +158,23 @@ module Atom
86
158
  end
87
159
  end
88
160
 
89
- class Content
90
- include XMLMapping
91
-
92
- namespace NAMESPACE
93
-
94
- has_attribute :type
95
- has_attribute :src
96
- text :value
97
-
98
- def to_s
99
- value || src
100
- end
101
- end
102
-
103
161
  class Source
104
162
  include XMLMapping
105
163
 
106
164
  namespace NAMESPACE
107
165
 
166
+ has_one :id
108
167
  has_many :authors, :name => 'author', :type => Person
109
168
  has_many :categories, :name => 'category', :type => Category
110
169
  has_one :generator, :type => Generator
111
170
  has_one :icon
112
171
  has_many :links, :name => 'link', :type => Link
113
172
  has_one :logo
114
- has_one :rights
115
- has_one :subtitle
116
- has_one :title
173
+ has_one :rights, :type => Text
174
+ has_one :subtitle, :type => Text
175
+ has_one :title, :type => Text
117
176
  has_one :updated, :transform => lambda { |t| Time.iso8601(t) }
177
+ has_many :contributors, :name => 'contributor', :type => Person
118
178
  end
119
179
 
120
180
  class Entry
@@ -123,16 +183,17 @@ module Atom
123
183
  namespace NAMESPACE
124
184
 
125
185
  has_one :id
126
- has_one :title
127
- has_one :summary
186
+ has_one :title, :type => Text
187
+ has_one :summary, :type => Text
128
188
  has_many :authors, :name => 'author', :type => Person
129
189
  has_many :contributors, :name => 'contributor', :type => Person
130
190
  has_one :published, :transform => lambda { |t| Time.iso8601(t) }
131
191
  has_one :updated, :transform => lambda { |t| Time.iso8601(t) }
132
192
  has_many :links, :name => 'link', :type => Link
133
- has_many :category, :name => 'category', :type => Category
193
+ has_many :categories, :name => 'category', :type => Category
134
194
  has_one :content, :type => Content
135
195
  has_one :source, :type => Source
196
+ has_one :rights, :type => Text
136
197
 
137
198
  has_many :extended_elements, :name => :any, :namespace => :any, :type => :raw
138
199
  end
@@ -143,8 +204,8 @@ module Atom
143
204
  namespace NAMESPACE
144
205
 
145
206
  has_one :id
146
- has_one :title
147
- has_one :subtitle
207
+ has_one :title, :type => Text
208
+ has_one :subtitle, :type => Text
148
209
  has_many :authors, :name => 'author', :type => Person
149
210
  has_many :contributors, :name => 'contributor', :type => Person
150
211
  has_many :entries, :name => 'entry', :type => Entry
@@ -152,7 +213,7 @@ module Atom
152
213
  has_many :links, :name => 'link', :type => Link
153
214
  has_one :updated, :transform => lambda { |t| Time.iso8601(t) }
154
215
 
155
- has_one :rights
216
+ has_one :rights, :type => Text
156
217
  has_one :icon
157
218
  has_one :logo
158
219
  has_many :categories, :name => 'category', :type => Category
@@ -42,6 +42,11 @@ module XMLMapping
42
42
  raw_mappings.values.select { |mapping| mapping[:cardinality] == :many }.each { |m|
43
43
  instance_variable_set("@#{m[:attribute]}", [])
44
44
  }
45
+
46
+ # initialize defaults
47
+ raw_mappings.values.select { |mapping| mapping.has_key? :default }.each { |m|
48
+ instance_variable_set("@#{m[:attribute]}", m[:default])
49
+ }
45
50
 
46
51
  root.each_element { |e|
47
52
  process(e, mappings[:element])
@@ -64,19 +69,18 @@ module XMLMapping
64
69
  def process(e, mappings)
65
70
  mapping = find_mapping(mappings, e.namespace, e.name)
66
71
 
67
- if !mapping.nil?
68
- value = extract_value(e, mapping)
69
-
70
- attribute = mapping[:attribute]
71
- previous = instance_variable_get("@#{attribute}")
72
- case mapping[:cardinality]
73
- when :one
74
- raise "Found more than one #{e.name}" if !previous.nil?
75
- instance_variable_set("@#{attribute}", value)
76
- when :many
77
- previous << value
78
- end
79
- end
72
+ if !mapping.nil?
73
+ value = extract_value(e, mapping)
74
+
75
+ attribute = mapping[:attribute]
76
+ previous = instance_variable_get("@#{attribute}")
77
+ case mapping[:cardinality]
78
+ when :one
79
+ instance_variable_set("@#{attribute}", value)
80
+ when :many
81
+ previous << value
82
+ end
83
+ end
80
84
  end
81
85
 
82
86
  def find_mapping(mappings, namespace, name)
@@ -153,6 +157,9 @@ module XMLMapping
153
157
  add(attribute, :text, options)
154
158
  end
155
159
 
160
+ def ensure(message, &block)
161
+ end
162
+
156
163
  private
157
164
  def add(attribute, xml_type, mapping)
158
165
  attr attribute
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.11
3
3
  specification_version: 1
4
4
  name: atom
5
5
  version: !ruby/object:Gem::Version
6
- version: "0.2"
7
- date: 2006-04-08 00:00:00 -07:00
6
+ version: "0.3"
7
+ date: 2006-04-11 00:00:00 -07:00
8
8
  summary: Ruby library for working with the Atom syndication format
9
9
  require_paths:
10
10
  - lib