Class: Gistgen::HackerNews

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

Class Method Summary collapse

Class Method Details

.frontpageObject



9
10
11
12
13
14
15
16
# File 'lib/gistgen/hacker_news.rb', line 9

def self.frontpage
  begin
    res = Gistgen::Page.get_page("http://api.ihackernews.com/page")
    Gistgen::HackerNews.get_hash(res)
  rescue
    nil
  end
end

.get_hash(res) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/gistgen/hacker_news.rb', line 39

def self.get_hash(res)
  json = JSON.parse(res)
  json['items'].map do |i| 
    {"title" => i['title'], 
      "url" => Gistgen::URL.standardize(i['url']), 
      "score" => i['points'], 
      "time" => Gistgen::HackerNews.parse_time(i['postedAgo']),
      "discussion_url" => "http://news.ycombinator.com/item?id=#{i['id']}"
    }
  end
end

.get_score(hn_url) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/gistgen/hacker_news.rb', line 28

def self.get_score(hn_url)
  id = hn_url.match(/\d+$/)[0]
  begin
    res = Gistgen::Page.get_page("http://api.ihackernews.com/post/#{id}")
    json = JSON.parse(res)
    json['points']
  rescue
    nil
  end
end

.new_postsObject

don’t use this too often (low score shouldn’t be added)



19
20
21
22
23
24
25
26
# File 'lib/gistgen/hacker_news.rb', line 19

def self.new_posts
  begin
    res = Gistgen::Page.get_page("http://api.ihackernews.com/new")
    Gistgen::HackerNews.get_hash(res)
  rescue
    nil
  end
end

.parse_time(time_ago) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/gistgen/hacker_news.rb', line 51

def self.parse_time(time_ago)
  begin
    tmp = time_ago.split(' ')
    time = tmp[0].to_i.send(tmp[1]).ago
  rescue
    Time.now.utc
  end
end