Class: Gistgen::Reddit

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

Class Method Summary collapse

Class Method Details

.fetch(url) ⇒ Object

code.reddit.com/wiki/API reddit api is so nice, you just pick a page and add .json to get just the data ex: www.reddit.com/.json



11
12
13
14
# File 'lib/gistgen/reddit.rb', line 11

def self.fetch(url)
  res = Gistgen::Page.get_page("#{url.gsub(/\/$/,'')}/.json")
  Gistgen::Reddit.get_hash(res)
end

.get_hash(res) ⇒ Object



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

def self.get_hash(res)
  json = JSON.parse(res)
  items = json['data']['children']
  items.map do |i|
    post = i['data']
    {"title" => post['title'], 
      "url" => Gistgen::URL.standardize(post['url']), 
      "score" => post['score'], 
      "time" => Time.at(post['created_utc']),
      "discussion_url" => "http://reddit.com#{post['permalink']}"
    }
  end
end

.get_score(reddit_url) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/gistgen/reddit.rb', line 16

def self.get_score(reddit_url)
  begin
    res = Gistgen::Page.get_page("#{reddit_url.gsub(/\/$/,'')}/.json")
    score = res.scan(/"score"\s*:\s*(\d+)/)[0].join('').to_i #reddit nested comments is too deep for json
  rescue
    nil
  end
end