class RedditGet::Subreddit

Grab subreddit top page as json

Constants

BASE_URL

Public Class Methods

collect(subreddit, with_comments: false) click to toggle source
# File lib/reddit_get.rb, line 69
def self.collect(subreddit, with_comments: false)
  collect_all([subreddit], with_comments: with_comments)
end
collect_all(subreddits, with_comments: false) click to toggle source
# File lib/reddit_get.rb, line 58
def self.collect_all(subreddits, with_comments: false)
  raise TypeError, 'Must pass an array of subreddits' unless subreddits.is_a?(Array)

  results = subreddits.zip([]).to_h
  subreddits.uniq.each do |subreddit|
    grab_posts(results, subreddit, with_comments: with_comments)
  end
  scheduler_run
  Data.new(results)
end

Private Class Methods

get_json(uri) click to toggle source
# File lib/reddit_get.rb, line 108
def get_json(uri)
  req = Net::HTTP::Get.new(
    uri,
    { 'User-Agent':
        'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0' }
  )
  body = Net::HTTP.start('old.reddit.com', 443, use_ssl: true) do |http|
    http.request(req)
  end.body

  JSON.parse(body)
end
get_reddit_comments(url) click to toggle source
# File lib/reddit_get.rb, line 104
def get_reddit_comments(url)
  get_json("#{BASE_URL}#{url}.json")[1].dig('data', 'children')
end
get_reddit_posts(subreddit) click to toggle source
# File lib/reddit_get.rb, line 100
def get_reddit_posts(subreddit)
  get_json(URI("#{BASE_URL}/r/#{subreddit}.json")).dig('data', 'children')
end
grab_comments(post) click to toggle source
# File lib/reddit_get.rb, line 91
def grab_comments(post)
  url = post['data']['permalink']
  Fiber.new do
    post['data']['comments'] = get_reddit_comments(url).map! do |comment|
      comment['data']
    end
  end.resume
end
grab_posts(results, subreddit, with_comments:) click to toggle source
# File lib/reddit_get.rb, line 82
def grab_posts(results, subreddit, with_comments:)
  Fiber.new do
    results[subreddit] = get_reddit_posts(subreddit).map! do |post|
      grab_comments(post) if with_comments
      post['data']
    end
  end.resume
end
scheduler_run() click to toggle source
# File lib/reddit_get.rb, line 76
def scheduler_run
  scheduler = Scheduler.new
  Fiber.set_scheduler scheduler
  scheduler.run
end