Module: GoogleApps::Atom::Node

Included in:
Document
Defined in:
lib/google_apps/atom/node.rb

Instance Method Summary collapse

Instance Method Details

#add_attributes(node, attributes) ⇒ Object

add_attributes adds the specified attributes to the given node. It takes a LibXML::XML::Node and an array of name, value attribute pairs.

add_attribute node, [[‘title’, ‘emperor’], [‘name’, ‘Napoleon’]]

add_attribute returns the modified node.



59
60
61
62
63
64
65
# File 'lib/google_apps/atom/node.rb', line 59

def add_attributes(node, attributes)
  attributes.each do |attribute|
    node.attributes[attribute[0]] = attribute[1]
  end

  node
end

#add_namespaces(node, namespaces) ⇒ Object

add_namespaces adds the specified namespaces to the specified node. namespaces should be a hash of name, value pairs.

add_namespaces node, atom: ‘www.w3.org/2005/Atom’, apps: ‘schemas.google.com/apps/2006

add_namespaces returns the node with namespaces



43
44
45
46
47
48
49
# File 'lib/google_apps/atom/node.rb', line 43

def add_namespaces(node, namespaces)
  namespaces.each_pair do |name, value|
    Atom::XML::Namespace.new node, name.to_s, value
  end

  node
end

#add_prop_node(name, value, node = nil) ⇒ Object

Parameters:

  • name (String)

    the value for the name attribute

  • value (String)

    the value for the value attribute

Returns:



29
30
31
32
33
# File 'lib/google_apps/atom/node.rb', line 29

def add_prop_node(name, value, node = nil)
  node ||= @doc.root

  node << create_node({type: 'apps:property', attrs: [['name', name], ['value', value]]})
end

#check_value(value) ⇒ Object

Parameters:

Returns:



107
108
109
110
111
112
113
114
115
116
# File 'lib/google_apps/atom/node.rb', line 107

def check_value(value)
  case value
    when 'true'
      true
    when 'false'
      false
    else
      value
  end
end

#create_node(properties) ⇒ Object

create_node takes a hash of properties from which to build the XML node. The properties hash must have a :type key, it is also possible to pass an :attrs key with an array of attribute name, value pairs.

create_node type: ‘apps:property’, attrs: [[‘name’, ‘Tim’], [‘userName’, ‘[email protected]’]]

create_node returns an Atom::XML::Node with the specified properties.



14
15
16
17
18
19
20
# File 'lib/google_apps/atom/node.rb', line 14

def create_node(properties)
  if properties[:attrs]
    add_attributes Atom::XML::Node.new(properties[:type]), properties[:attrs]
  else
    Atom::XML::Node.new properties[:type]
  end
end

#get_content(document, xpath) ⇒ Object

get_content returns the content of the specified node. If multiple nodes match the xpath value get_content will return the content of the first occurance.

get_content document, ‘//title’

get_content returns the content of the node as a string.



84
85
86
# File 'lib/google_apps/atom/node.rb', line 84

def get_content(document, xpath)
  document.find(xpath).first.content
end

#get_values(element, key_attrib, value = 'value') ⇒ Object

get_values returns an array of all the value attributes on elements matching the given key_attrib pair on the specified element type.



92
93
94
95
96
97
# File 'lib/google_apps/atom/node.rb', line 92

def get_values(element, key_attrib, value = 'value')
  self.find('//' + element).inject([]) do |values, element|
    values << element.attributes[value] if element.attributes[key_attrib[0]].match key_attrib[1]
    values
  end
end

#node_match?(node, attributes) ⇒ Boolean

node_match? checks that each value for each specified attribute matches the specified value.

Returns:

  • (Boolean)


70
71
72
73
74
# File 'lib/google_apps/atom/node.rb', line 70

def node_match?(node, attributes)
  attributes.keys.inject(true) do |result, key|
    result and node.attributes[key].to_s == attributes[key][0]
  end
end