Class: HackerNews::Item

Inherits:
Object
  • Object
show all
Defined in:
lib/hacker_news/item.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node, position) ⇒ Item

Returns a new instance of Item.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/hacker_news/item.rb', line 7

def initialize(node, position)
  tr = node.parent.parent
  points_comments_tr = tr.next
  points_comments_td = points_comments_tr.css("td.subtext")[0]
  
  #@title = Iconv.iconv('iso-8859-1//translit', 'utf-8', node.text)[0].to_s.strip
  @title = Iconv.iconv('iso-8859-1//IGNORE', 'US-ASCII', node.text)[0].to_s.strip
  @url = node[:href]
  @position = position
  @points = points_comments_td.css("span")[0].text.to_i rescue nil
  @item_id = points_comments_td.css("span")[0][:id].split("_")[1].to_i rescue nil
  @user = points_comments_td.css("a")[0].text.strip rescue nil
  @comments_count = points_comments_td.css("a")[1].text.to_i rescue 0
  @created_at = points_comments_tr.text.match(/by [-\w]+ (.*) \|/)[1].strip rescue nil
  @created_at ||= points_comments_tr.text
end

Instance Attribute Details

#comments_countObject

Returns the value of attribute comments_count.



5
6
7
# File 'lib/hacker_news/item.rb', line 5

def comments_count
  @comments_count
end

#created_atObject

Returns the value of attribute created_at.



5
6
7
# File 'lib/hacker_news/item.rb', line 5

def created_at
  @created_at
end

#item_idObject

Returns the value of attribute item_id.



5
6
7
# File 'lib/hacker_news/item.rb', line 5

def item_id
  @item_id
end

#pointsObject

Returns the value of attribute points.



5
6
7
# File 'lib/hacker_news/item.rb', line 5

def points
  @points
end

#positionObject

Returns the value of attribute position.



5
6
7
# File 'lib/hacker_news/item.rb', line 5

def position
  @position
end

#titleObject

Returns the value of attribute title.



5
6
7
# File 'lib/hacker_news/item.rb', line 5

def title
  @title
end

#urlObject

Returns the value of attribute url.



5
6
7
# File 'lib/hacker_news/item.rb', line 5

def url
  @url
end

#userObject

Returns the value of attribute user.



5
6
7
# File 'lib/hacker_news/item.rb', line 5

def user
  @user
end

Instance Method Details

#commentsObject



24
25
26
# File 'lib/hacker_news/item.rb', line 24

def comments
  @comments ||= Scraper.comments(self.item_id)
end