Class: FeedMe::AbstractParser

Inherits:
Object
  • Object
show all
Defined in:
lib/feed_me/abstract_parser.rb

Direct Known Subclasses

FeedParser, FeedStruct, ItemParser

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xml, format) ⇒ AbstractParser

Returns a new instance of AbstractParser.



28
29
30
31
32
33
34
# File 'lib/feed_me/abstract_parser.rb', line 28

def initialize(xml, format)
  self.xml = xml
  self.format = format
  self.properties = self.class.properties[self.format]

  append_methods
end

Class Attribute Details

.propertiesObject

Returns the value of attribute properties.



5
6
7
# File 'lib/feed_me/abstract_parser.rb', line 5

def properties
  @properties
end

.root_nodesObject

Returns the value of attribute root_nodes.



5
6
7
# File 'lib/feed_me/abstract_parser.rb', line 5

def root_nodes
  @root_nodes
end

Instance Attribute Details

#formatObject

Returns the value of attribute format.



44
45
46
# File 'lib/feed_me/abstract_parser.rb', line 44

def format
  @format
end

#propertiesObject

Returns the value of attribute properties.



44
45
46
# File 'lib/feed_me/abstract_parser.rb', line 44

def properties
  @properties
end

#xmlObject Also known as: root_node

Returns the value of attribute xml.



44
45
46
# File 'lib/feed_me/abstract_parser.rb', line 44

def xml
  @xml
end

Class Method Details

.build(xml, format, *args) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/feed_me/abstract_parser.rb', line 7

def build(xml, format, *args)
  # in a world with activesupport this would have been written as
  #   format_parser = (format.to_s.camelize + self.to_s).constantize
  camelized_format = format.to_s.split('_').map{ |w| w.capitalize }.join('')
  bare_class = self.to_s.split('::').last

  begin
    format_parser = FeedMe.const_get(camelized_format + bare_class)
  rescue NameError
  end

  if format_parser.is_a?(Class) and format_parser.ancestors.include?(self)
    return format_parser.new(xml, format, *args)
  else
    return self.new(xml, format, *args)
  end

end

Instance Method Details

#to_hashObject



36
37
38
39
40
41
42
# File 'lib/feed_me/abstract_parser.rb', line 36

def to_hash
  hash = {}
  self.properties.each do |method, p|
    hash[method] = self.send(method)
  end
  return hash
end