class RedditComments::GetComments

Attributes

comments[RW]
post[RW]
request[RW]
url[RW]

Public Class Methods

new(url) click to toggle source
# File lib/reddit_comments.rb, line 17
def initialize(url)
  @url = url
  @comments = []
end

Public Instance Methods

append_json() click to toggle source
# File lib/reddit_comments.rb, line 55
def append_json
  unless url.match(/\/.json\z/)
    if url[-1] == "/"
      @url = url + ".json"
    else
      @url = url + "/.json"
    end
  end
end
get_comments() click to toggle source
# File lib/reddit_comments.rb, line 39
def get_comments
  @post[1]["data"]["children"].each do |child|
    @comments << recursive_comment_digging(child["data"])
  end
  @comments = comments.flatten
end
parse_post() click to toggle source
# File lib/reddit_comments.rb, line 46
def parse_post
  uri = URI.parse(url.dup)
  http = Net::HTTP.new(uri.host)
  request = Net::HTTP::Get.new(uri.request_uri)
  request.initialize_http_header({"User-Agent" => "Reddit_Comments_Gem_0.1_mindplace"})
  body = http.request(request)
  @post = JSON.parse(body.body)
end
recursive_comment_digging(child, comments=[]) click to toggle source
# File lib/reddit_comments.rb, line 22
def recursive_comment_digging(child, comments=[])
  post = {}
  post["id"] = child["id"]
  post["parent_id"] = child["parent_id"]
  post["author"] = child["author"]
  post["body"] = child["body"]

  comments << post

  if child["replies"] != nil && child["replies"] != ""
    child["replies"]["data"]["children"].each do |comment|
      comments << self.recursive_comment_digging(comment["data"])
    end
  end
  comments
end
retrieve() click to toggle source
# File lib/reddit_comments.rb, line 71
def retrieve
  link_tested
  append_json
  parse_post
  get_comments
end