Nikkou

description

Description

Nikkou...

time

Returns a Time object (in UTC) by automatically parsing the text or specified attribute of the node.

# <a href="/p/1">3 hours ago</a>
doc.search('a').first.time # 2013-04-16 02:42:34 UTC
Options

attribute - The attribute to parse:

# <a href="/p/1" data-published-at="2013-04-16 02:42:34">My link</a>
doc.search('a').first.time(attribute: 'data-published-at') # 2013-04-16 02:42:34 UTC

time_zone - The document's time zone (the time will be converted from that to UTC):

# <a href="/p/1">3 hours ago</a>
doc.search('a').first.time(time_zone: 'America/New_York') # 2013-04-16 06:42:34 UTC

url

Returns an absolute URL; useful for parsing relative hrefs. The document's uri needs to be set for Nikkou to know what domain to add to relative hrefs.

# <a href="/p/1">My link</a>
doc.uri = 'http://mysite.com/mypage'
doc.search('a').first.url # http://mysite.com/p/1
Options

attribute - The attribute to parse:

# <a href="/p/1" data-comments-url="/p/1#comments">My Link</a>
doc.uri = 'http://mysite.com/mypage'
doc.search('a').first.url('data-comments-url') # http://mysite.com/p/1#comments

attr_matches(attribute, pattern)

Selects nodes with an attribute matching a pattern. The pattern's matches are stored in Node#matches.

# <span data-tooltip="3 Comments">My Text</span>
doc.search('span').attr_matches('data-tooltip', /(\d+) comments/i).first.text # My Text
doc.search('span').attr_matches('data-tooltip', /(\d+) comments/i).first.matches # ["3 Comments", "3"]

text_matches(attribute, pattern)

Selects nodes with text matching a pattern. The pattern's matches are stored in Node#matches.

# <a href="/p/1">3 Comments</a>
doc.search('span').text_matches(/^(\d+) comments$/i).first.attr('href') # "/p/1"
doc.search('span').text_matches(/^(\d+) comments$/i).first.matches # ["3 Comments", "3"]

License

Nikkou is released under the MIT License. Please see the MIT-LICENSE file for details.