class Wreddit

Constants

VERSION

Public Class Methods

new() click to toggle source
# File lib/wreddit.rb, line 9
def initialize
  @client = Redis.new
  @uri = "https://www.reddit.com"
end

Public Instance Methods

comment(comment_id) click to toggle source
# File lib/wreddit.rb, line 39
def comment(comment_id)
  @uri += "/#{comment_id}"
  self
end
comments(article_id = nil) click to toggle source
# File lib/wreddit.rb, line 24
def comments(article_id = nil)
  # gets all comments if article_id is unspecified
  @uri += "/comments"
  # add article_id if article is specified
  if article_id
    @uri += "/#{article_id}"
  end
  self
end
descriptions() click to toggle source
# File lib/wreddit.rb, line 81
def descriptions
  descriptions = []
  begin
    response = JSON.parse(self.json)
  rescue JSON::ParserError, TypeError => e
    puts e
    response = self.json
  end
  unless response['data']['children'].nil?
    response['data']['children'].each do |child|
      descriptions.push(child['data']['selftext'])
    end
    return descriptions
  else
    return response
  end
end
html() click to toggle source
# File lib/wreddit.rb, line 130
def html
  res = @client.get(@uri)
  unless res
    # if res doesn't exist get it from HTTParty
    res = HTTParty.get(URI(@uri))
    if res.code == 200
      # if res is an OK (we are getting data)
      @client.set(@uri, res, ex: 60)
    end
  end
  return res
end
json() click to toggle source

parse helpers

# File lib/wreddit.rb, line 100
def json
  @uri+='.json'
  res = @client.get(@uri)
  unless res
    # if res doesn't exist get it from HTTParty
    res = HTTParty.get(URI(@uri))
    if res.code == 200
      # if res is an OK (we are getting data)
      @client.set(@uri, res , ex: 60)
      res = @client.get(@uri)
      return res
    end
  end
  return res
end
parse(method = nil) click to toggle source

general parsing

# File lib/wreddit.rb, line 144
def parse(method = nil)
  # method of parsing your request
  case method
  when "json"
    self.json
  when "xml"
    self.xml
  when "html"
    self.html
  else
    # assume the user wants it in JSON format
    self.json
  end
end
subreddit(subreddit_name = "all") click to toggle source

standard requests

# File lib/wreddit.rb, line 14
def subreddit(subreddit_name = "all")
  @uri += "/r/#{subreddit_name}"
  self
end
title(title_name) click to toggle source
# File lib/wreddit.rb, line 34
def title(title_name)
  @uri += "/#{title_name}"
  self
end
titles() click to toggle source
# File lib/wreddit.rb, line 63
def titles
  titles = []
  begin
    response = JSON.parse(self.json)
  rescue JSON::ParserError, TypeError => e
    puts e
    response = self.json
  end
  unless response['data']['children'].nil?
    response['data']['children'].each do |child|
      titles.push(child['data']['title'])
    end
    return titles
  else
    return response
  end
end
user(user_name) click to toggle source
# File lib/wreddit.rb, line 19
def user(user_name)
  @uri += "/u/#{user_name}"
  self
end
xml() click to toggle source
# File lib/wreddit.rb, line 116
def xml
  @uri+='.xml'
  res = @client.get(@uri)
  unless res
    # if res doesn't exist get it from HTTParty
    res = HTTParty.get(URI(@uri))
    if res.code == 200
      # if res is an OK (we are getting data)
      @client.set(@uri, res, ex: 60)
    end
  end
  return res
end