Class: GoogleApps::Atom::Feed

Inherits:
Document
  • Object
show all
Defined in:
lib/google_apps/atom/feed.rb

Constant Summary collapse

TYPE_MATCH =

FIXED: If we grab the first id element in the feed we can simply scan it.

/\/(user|nickname|group|member)/

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Document

add_type, #attrs_from_props, #build_root, #delete_node, #determine_namespaces, #find_and_update, #find_values, inherited, #make_document, #new_empty_doc, #parse, #set_instances, sub_to_meth, #to_s, #type_to_s, #type_to_sym, types

Methods included from Node

#add_attributes, #add_namespaces, #add_prop_node, #check_value, #create_node, #get_content, #get_values, #node_match?

Constructor Details

#initialize(xml) ⇒ Feed

Returns a new instance of Feed.



11
12
13
14
15
16
17
# File 'lib/google_apps/atom/feed.rb', line 11

def initialize(xml)
  type = determine_type xml # This only works when xml is a string.

  super(xml)

  @items = entries_from document: @doc, type: type, entry_tag: 'entry'
end

Instance Attribute Details

#docObject (readonly)

TODO: Google’s feed responses are inconsistent. Will need special fun time, assholes.



5
6
7
# File 'lib/google_apps/atom/feed.rb', line 5

def doc
  @doc
end

#itemsObject (readonly)

TODO: Google’s feed responses are inconsistent. Will need special fun time, assholes.



5
6
7
# File 'lib/google_apps/atom/feed.rb', line 5

def items
  @items
end

#next_pageObject (readonly)

TODO: Google’s feed responses are inconsistent. Will need special fun time, assholes.



5
6
7
# File 'lib/google_apps/atom/feed.rb', line 5

def next_page
  @next_page
end

Instance Method Details

#add_category(content_array, type) ⇒ Object

add_category adds the proper atom:category node to the content_array

add_category content_array, ‘user’

add_category returns the modified content_array



78
79
80
# File 'lib/google_apps/atom/feed.rb', line 78

def add_category(content_array, type)
  content_array.unshift(create_node(type: 'atom:category', attrs: Atom::CATEGORY[type.to_sym]).to_s) if Atom::CATEGORY[type.to_sym]
end

#determine_type(xml) ⇒ Object

Determine the feed type from the feed id element.

Parameters:

Returns:

  • snake cased doc type



117
118
119
120
121
122
# File 'lib/google_apps/atom/feed.rb', line 117

def determine_type(xml)
  id_element = xml.scan(/<id.*?\/id/).first
  matches = id_element.scan(TYPE_MATCH).flatten

  matches.join '_'
end

#entries_from(properties) ⇒ Object

TODO: There is no reason not to document. Especially with complicated methods.



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/google_apps/atom/feed.rb', line 21

def entries_from(properties)
  type = properties[:type].to_sym

  properties[:document].root.inject([]) do |results, entry|
    if entry.name == properties[:entry_tag]
      results << new_doc(type, node_to_ary(entry), ['apps:', 'atom:', 'gd:'])
    end
    set_next_page(entry) if entry.name == 'link' and entry.attributes[:rel] == 'next'
    results
  end
end

#entry_wrap(content_array) ⇒ Object

entry_wrap adds atom:entry opening and closing tags to the provided content_array and the beginning and end.

entry_wrap content_array

entry_wrap returns an array with an opening atom:entry element prepended to the front and a closing atom:entry tag appended to the end.



105
106
107
# File 'lib/google_apps/atom/feed.rb', line 105

def entry_wrap(content_array)
  content_array.unshift(Atom::ENTRY_TAG[0]).push(Atom::ENTRY_TAG[1])
end

#grab_elements(content_array, filter) ⇒ Object

grab_elements applies the specified filter to the provided array. Google’s feed provides a lot of data that we don’t need in an entry document.

grab_elements content_array, ‘apps:’

grab_elements returns an array of items from content_array that match the given filter.



91
92
93
# File 'lib/google_apps/atom/feed.rb', line 91

def grab_elements(content_array, filter)
  content_array.grep(Regexp.new filter)
end

#new_doc(type, content_array, filters) ⇒ Object

new_doc creates a new Atom document from the data provided in the feed. new_doc takes a type, an array of content to be placed into the document as well as an array of filters.

new_doc ‘user’, content_array, [‘apps:’]

new_doc returns an GoogleApps::Atom document of the specified type.



60
61
62
63
64
65
66
67
68
69
# File 'lib/google_apps/atom/feed.rb', line 60

def new_doc(type, content_array, filters)
  content_array = filters.inject([]) do |content, filter|
    content << grab_elements(content_array, filter)
    content
  end

  add_category content_array, type

  Atom.send type, entry_wrap(content_array.flatten).join("\n")
end

#node_to_ary(node) ⇒ Object

node_to_ary converts a Atom::XML::Node to an array.

node_to_ary node

node_to_ary returns the string representation of the given node split on n.



46
47
48
# File 'lib/google_apps/atom/feed.rb', line 46

def node_to_ary(node)
  node.to_s.split("\n")
end

#set_next_page(node) ⇒ Object

TODO: Obvious but still needs documentation.



35
36
37
# File 'lib/google_apps/atom/feed.rb', line 35

def set_next_page(node)
  @next_page = node.attributes[:href]
end