Class: VastApi::Listings::Entry

Inherits:
Object
  • Object
show all
Defined in:
lib/vast_api/listings/entry.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(doc) ⇒ Entry

Returns a new instance of Entry.



5
6
7
8
# File 'lib/vast_api/listings/entry.rb', line 5

def initialize(doc)
  @doc = doc
  @attributes = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object (private)



85
86
87
# File 'lib/vast_api/listings/entry.rb', line 85

def method_missing(sym, *args, &block)
  self[sym.to_s]  
end

Instance Attribute Details

#docObject (readonly)

Returns the value of attribute doc.



4
5
6
# File 'lib/vast_api/listings/entry.rb', line 4

def doc
  @doc
end

Instance Method Details

#[](var_name) ⇒ Object



10
11
12
13
14
15
16
17
18
19
# File 'lib/vast_api/listings/entry.rb', line 10

def [](var_name)
  @attributes[var_name] ||=
    if %W{id author link feeds updated published}.index(var_name)
      self.send(var_name)
    elsif %W{ title }.index(var_name)
      get_var('xmlns:'+var_name)
    else
      get_var("v:#{var_name}")
    end
end

#attributesObject



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/vast_api/listings/entry.rb', line 21

def attributes
  vast_links
  @doc.children.each do |item|
    next if item.name == 'links'
    if %W{id author link feeds updated published}.index(var_name)
      self.send(var_name)
    else
      @attributes[item.name] ||= item.content
    end
  end
  @attributes
end

#authorObject



42
43
44
# File 'lib/vast_api/listings/entry.rb', line 42

def author
  @attributes['author'] ||= get_children(@doc.at_xpath('xmlns:author'))
end

#idObject



34
35
36
# File 'lib/vast_api/listings/entry.rb', line 34

def id
  @attributes['id'] ||= get_var('v:item_id')
end


38
39
40
# File 'lib/vast_api/listings/entry.rb', line 38

def link
  @attributes['link'] ||= get_var('xmlns:id')
end

#publishedObject



50
51
52
# File 'lib/vast_api/listings/entry.rb', line 50

def published
  @attributes["published"] ||= DateTime.strptime(@doc.at_xpath('xmlns:published').content)
end

#updatedObject



46
47
48
# File 'lib/vast_api/listings/entry.rb', line 46

def updated
  @attributes["updated"] ||= DateTime.strptime(@doc.at_xpath('xmlns:updated').content)
end


54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/vast_api/listings/entry.rb', line 54

def vast_links
  if @attributes["vast_links"].nil?
    @attributes["vast_links"] = {}
    @doc.xpath('xmlns:link').each do |ln|
      attributes = ln.attributes
      @attributes["vast_links"][attributes['rel'].content] = {}
      attributes.each do |key, node|
        @attributes["vast_links"][attributes['rel'].content][key] = node.content
      end
    end
  end
  @attributes["vast_links"]
end