Module: Nikkou::Nokogiri::XML::Node

Includes:
Drillable, Findable
Defined in:
lib/nikkou/nokogiri/xml/node.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Findable

#find

Methods included from Drillable

#drill

Instance Attribute Details

#matchesObject

Returns the value of attribute matches.



8
9
10
# File 'lib/nikkou/nokogiri/xml/node.rb', line 8

def matches
  @matches
end

Instance Method Details

#attr_equals(attribute, string) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/nikkou/nokogiri/xml/node.rb', line 10

def attr_equals(attribute, string)
  list = []
  traverse do |node|
    list << node if node.attr(attribute) == string
  end
  ::Nokogiri::XML::NodeSet.new(document, list)
end

#attr_includes(attribute, string) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/nikkou/nokogiri/xml/node.rb', line 18

def attr_includes(attribute, string)
  list = []
  traverse do |node|
    next if node.attr(attribute).nil?
    list << node if node.attr(attribute).include?(string)
  end
  ::Nokogiri::XML::NodeSet.new(document, list)
end

#attr_matches(attribute, pattern) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/nikkou/nokogiri/xml/node.rb', line 27

def attr_matches(attribute, pattern)
  list = []
  traverse do |node|
    next if node.attr(attribute).nil?
    if node.attr(attribute).match(pattern)
      node.matches = $~.to_a
      list << node
    end
  end
  ::Nokogiri::XML::NodeSet.new(document, list)
end

#parse_textObject



39
40
41
# File 'lib/nikkou/nokogiri/xml/node.rb', line 39

def parse_text
  parse(text)
end

#text_equals(string) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/nikkou/nokogiri/xml/node.rb', line 43

def text_equals(string)
  list = []
  traverse do |node|
    next if node.is_a?(::Nokogiri::XML::Text)
    list << node if node.text == string
  end
  ::Nokogiri::XML::NodeSet.new(document, list)
end

#text_includes(string) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/nikkou/nokogiri/xml/node.rb', line 52

def text_includes(string)
  list = []
  traverse do |node|
    next if node.is_a?(::Nokogiri::XML::Text)
    list << node if node.text.include?(string)
  end
  ::Nokogiri::XML::NodeSet.new(document, list)
end

#text_matches(pattern) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/nikkou/nokogiri/xml/node.rb', line 61

def text_matches(pattern)
  list = []
  traverse do |node|
    next if node.is_a?(::Nokogiri::XML::Text)
    if node.text.match(pattern)
      node.matches = $~.to_a
      list << node
    end
  end
  ::Nokogiri::XML::NodeSet.new(document, list)
end

#time(options = {}) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/nikkou/nokogiri/xml/node.rb', line 73

def time(options={})
  defaults = {
    attribute: nil,
    time_zone: 'UTC' # e.g. 'Eastern Time (US & Canada)'
  }
  options.reverse_merge!(defaults)
  time_zone = TZInfo::Timezone.get(options[:time_zone])
  string = options[:attribute] ? attr(options[:attribute]).to_s : text.to_s
  if string =~ /(\d+)\s+(seconds?|minutes?|hours?|days?|weeks?|months?)\s+ago/i
    number = $1.to_i
    units = $2
    time = (Time.now.utc - number.send(units))
  else
    time = Time.zone.parse(string)
  end
  time_zone.local_to_utc(time)
end

#url(attribute = 'href') ⇒ Object



91
92
93
94
95
96
97
98
99
# File 'lib/nikkou/nokogiri/xml/node.rb', line 91

def url(attribute='href')
  return nil if attr(attribute).nil?
  href = attr(attribute)
  return href if href =~ /^https?:\/\//
  return "http:#{href}" if href.start_with?('//')
  return nil if document.nil? || document.uri.nil?
  root_url = "#{document.uri.scheme}://#{document.uri.host}"
  URI.join(root_url, href).to_s
end