Class: Atomic::Atom::Entry

Inherits:
Object
  • Object
show all
Includes:
Parser
Defined in:
lib/atomic/atom.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Parser

#deserialize, included

Constructor Details

#initialize(params = {}) ⇒ Entry

Returns a new instance of Entry.



95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/atomic/atom.rb', line 95

def initialize(params = {})
  params.symbolize_keys!
  params.assert_valid_keys(:id, :title, :categories, :created_at, :updated_at, :content, :links, :author, :contributors)
  self.title = params[:title]
  self.id = params[:id]
  self.categories = params[:categories] || []
  self.created_at = params[:created_at].nil? ? Time.now : Time.parse(params[:created_at])
  self.updated_at = params[:updated_at].nil? ? self.created_at : Time.parse(params[:updated_at])
  self.content = params[:content] || {}
  self.links = params[:links] || []
  self.author = params[:author]
  self.contributors = params[:contributors] || []
end

Instance Attribute Details

#authorObject

Returns the value of attribute author.



93
94
95
# File 'lib/atomic/atom.rb', line 93

def author
  @author
end

#categoriesObject

Returns the value of attribute categories.



93
94
95
# File 'lib/atomic/atom.rb', line 93

def categories
  @categories
end

#contentObject

Returns the value of attribute content.



93
94
95
# File 'lib/atomic/atom.rb', line 93

def content
  @content
end

#contributorsObject

Returns the value of attribute contributors.



93
94
95
# File 'lib/atomic/atom.rb', line 93

def contributors
  @contributors
end

#created_atObject

Returns the value of attribute created_at.



93
94
95
# File 'lib/atomic/atom.rb', line 93

def created_at
  @created_at
end

#idObject

Returns the value of attribute id.



93
94
95
# File 'lib/atomic/atom.rb', line 93

def id
  @id
end

Returns the value of attribute links.



93
94
95
# File 'lib/atomic/atom.rb', line 93

def links
  @links
end

#titleObject

Returns the value of attribute title.



93
94
95
# File 'lib/atomic/atom.rb', line 93

def title
  @title
end

#updated_atObject

Returns the value of attribute updated_at.



93
94
95
# File 'lib/atomic/atom.rb', line 93

def updated_at
  @updated_at
end

Instance Method Details

#handle_close_element(node) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/atomic/atom.rb', line 143

def handle_close_element(node)
  case [node.depth, node.uri, node.name]
  when [0, NS_ATOM, 'entry']
  when [1, NS_ATOM, 'title']
    self.title = node.text
  when [1, NS_ATOM, 'id']
    self.id = node.text
  when [1, NS_ATOM, 'published']
    self.created_at = Time.parse(node.text)
  when [1, NS_ATOM, 'updated']
    self.updated_at = Time.parse(node.text)
  when [1, NS_ATOM, 'category']
    self.categories << {:scheme => node.attributes['scheme'], :term => node.attributes['term']}
  when [1, NS_ATOM, 'author']
  when [1, NS_ATOM, 'content']
    @processing_xml_content = false
  when [1, NS_ATOM, 'link']
    self.links << {:href => node.attributes['href'], :rel => node.attributes['rel'], :type => node.attributes['type']}
  else
    puts("Entry ==> Unexpected Close Element - [#{node.depth}] #{node.name} #{node.uri} #{node.attributes.inspect} #{node.text}") unless @processing_xml_content
  end
end

#handle_open_element(node, reader) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/atomic/atom.rb', line 109

def handle_open_element(node, reader)
  progressed = false
  case [node.depth, node.uri, node.name]
  when [0, NS_ATOM, 'entry']
  when [1, NS_ATOM, 'title']
  when [1, NS_ATOM, 'id']
  when [1, NS_ATOM, 'published']
  when [1, NS_ATOM, 'updated']
  when [1, NS_ATOM, 'category']
  when [1, NS_ATOM, 'author']
    self.author = Person.new.deserialize(reader)
    progressed = true
  when [1, NS_ATOM, 'contributor']
    self.contributors << Person.new.deserialize(reader)
    progressed = true
  when [1, NS_ATOM, 'content']
    if node.attributes['type'] == 'application/xml'
      @processing_xml_content = true
    end
  when [1, NS_ATOM, 'link']
  else
    if @processing_xml_content
      extension_class = Extensions::MAP[[node.uri, node.name]]
      unless extension_class.nil?
        extension_class.new(self).deserialize(reader)
        progressed = true
      end
    else
      puts("Entry ==> Unexpected Open Element - [#{node.depth}] #{node.name} #{node.uri} #{node.attributes.inspect}")
    end
  end
  return progressed
end

#to_hashObject



166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/atomic/atom.rb', line 166

def to_hash
  {
    :id => self.id,
    :title => self.title,
    :categories => self.categories,
    :created_at => self.created_at,
    :updated_at => self.updated_at,
    :content => self.content,
    :links => self.links,
    :author => self.author,
    :contributors => self.contributors.collect { |contributor| contributor.to_hash }
  }
end

#to_xml(as_document = true, target = nil) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/atomic/atom.rb', line 180

def to_xml(as_document=true, target=nil)
  nsdec = as_document ? {:"xmlns:atom" => NS_ATOM} : {}
  xml = Builder::XmlMarkup.new(:target => target)
  xml.instruct! if as_document
  xml.atom(:entry, nsdec) do
    xml.atom(:id, self.id)
    xml.atom(:title, self.title)
    xml.atom(:categories) do
      self.categories.each do |category|
        xml.atom(:category, category)
      end
    end
    xml.atom(:published, self.created_at.iso8601)
    xml.atom(:updated, self.updated_at.iso8601)
    xml.atom(:content, self.content) if self.content
    self.links.each do |link|
      xml.atom(:link, link)
    end
    self.author.to_xml(false, xml) if self.author
    self.contributors.each do |contributor|
      contributor.to_xml(false, xml)
    end
  end
  xml.target!
end