Module: Reddit::JsonListing::ClassMethods

Defined in:
lib/ruby_reddit_api/json_listing.rb

Instance Method Summary collapse

Instance Method Details

#parse(json) ⇒ Array<Reddit::Submission, Reddit::User, Reddit::Comment, Reddit::Message>

Parameters:

  • JSON (Hash)

    received from Reddit

Returns:



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ruby_reddit_api/json_listing.rb', line 13

def parse(json)
  results = []
  if json.is_a?(Array)
    json.each do |j|
      results << parse(j)
    end
  else
    data     = json["data"]
    Reddit::Base.instance_variable_set("@modhash", data["modhash"]) # Needed for api calls

    children = data["children"] || [{"data" => data, "kind" => json["kind"] }]
    children.each do |message|
      kind     = message["kind"]
      message["data"]["kind"] = kind
      results << self.new(message["data"])
    end
  end

  return results.flatten
end