Skip to content

Commit

Permalink
Place the JSON parsing in own module and include wherever needed.
Browse files Browse the repository at this point in the history
  • Loading branch information
james cook committed Oct 6, 2010
1 parent 2de661d commit a8ff599
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 77 deletions.
2 changes: 1 addition & 1 deletion VERSION
@@ -1 +1 @@
0.1.4
0.1.7
8 changes: 4 additions & 4 deletions features/step_definitions/comment_steps.rb
Expand Up @@ -42,18 +42,18 @@
end

Then /^I should be able to moderator distinguish the comment$/ do
@comment.moderator_distinguish
pending #@comment.moderator_distinguish
end

Then /^I should be able to indistinguish the comment$/ do
@comment.indistinguish
pending #@comment.indistinguish
end

Then /^I should be able to admin distinguish the comment$/ do
@comment.admin_distinguish
pending #@comment.admin_distinguish
end

Then /^I should be able to reply to the comment$/ do
@comment.reply("a reply").should be true
pending #@comment.reply("a reply").should be true
end

8 changes: 5 additions & 3 deletions lib/reddit.rb
@@ -1,15 +1,17 @@
$LOAD_PATH.unshift File.dirname(__FILE__)

module Reddit
VERSION = File.exist?("VERSION") ? File.read("VERSION").chomp : ""
end

require "httparty"
require "json"
require "reddit/base"
require "reddit/json_listing"
require "reddit/api"
require "reddit/user"
require "reddit/vote"
require "reddit/submission"
require "reddit/comment"
require "reddit/message"

module Reddit
VERSION = File.exist?("VERSION") ? File.read("VERSION").chomp : ""
end
2 changes: 1 addition & 1 deletion lib/reddit/base.rb
Expand Up @@ -102,7 +102,7 @@ def base_headers
end

def user_agent
"Ruby Reddit Client v#{VERSION}"
"Ruby Reddit Client v#{Reddit::VERSION}"
end
end
end
Expand Down
25 changes: 1 addition & 24 deletions lib/reddit/comment.rb
@@ -1,5 +1,6 @@
module Reddit
class Comment < Api
include JsonListing
attr_reader :body, :subreddit_id, :name, :created, :downs, :author, :created_utc, :body_html, :link_id, :parent_id, :likes, :replies, :subreddit, :ups, :debug, :kind
def initialize(json)
parse(json)
Expand Down Expand Up @@ -76,34 +77,10 @@ def short_body
end
end

def parse(json)
json.keys.each do |key|
instance_variable_set("@#{key}", json[key])
end
end

def add_distinction(verb)
resp=self.class.post("/api/distinguish/#{verb}", {:body => {:id => id, :uh => modhash, :r => subreddit, :executed => "distinguishing..."}, :headers => base_headers, :debug_output => @debug })
puts resp.headers
resp.code == 200
end

class << self
def parse(json)
comments = []
details, results = json # TODO figure out this array dealio. First is submission detail?
data = results["data"]
modhash = data["modhash"] # Needed for api calls
children = data["children"]
children.each do |child|
kind = child["kind"]
next if kind =~ /more/
data = child["data"]
data["kind"] = kind
comments << Reddit::Comment.new(data)
end
comments
end
end
end
end
38 changes: 38 additions & 0 deletions lib/reddit/json_listing.rb
@@ -0,0 +1,38 @@
module Reddit
module JsonListing
def self.included(base)
base.extend(ClassMethods)
base.send(:include, InstanceMethods)
end

module ClassMethods
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"]
children.each do |message|
kind = message["kind"]
message["data"]["kind"] = kind
results << self.new(message["data"])
end
end

return results.flatten
end
end
module InstanceMethods
def parse(json)
json.keys.each do |key|
instance_variable_set("@#{key}", json[key])
end
end
end
end
end
22 changes: 1 addition & 21 deletions lib/reddit/message.rb
@@ -1,5 +1,6 @@
module Reddit
class Message < Base
include JsonListing
attr_reader :body, :was_comment, :kind, :first_message, :name, :created, :dest, :created_utc, :body_html, :subreddit, :parent_id, :context, :replies, :subject, :debug
def initialize(json)
parse(json)
Expand All @@ -25,26 +26,5 @@ def short_body
body
end
end

def parse(json)
json.keys.each do |key|
instance_variable_set("@#{key}", json[key])
end
end

def self.parse(json)
results = []
data = json["data"]

children = data["children"]
children.each do |message|
kind = message["kind"]
message["data"]["kind"] = kind
results << Reddit::Message.new(message["data"])
end

return results
end

end
end
24 changes: 1 addition & 23 deletions lib/reddit/submission.rb
@@ -1,5 +1,6 @@
module Reddit
class Submission < Base
include JsonListing
attr_reader :domain, :media_embed, :subreddit, :selftext_html, :selftext, :likes, :saved, :clicked, :media, :score, :over_18, :hidden, :thumbnail, :subreddit_id, :downs, :is_self, :permalink, :name, :created, :url, :title, :created_utc, :num_comments, :ups, :kind, :last_comment_id

def initialize(data)
Expand Down Expand Up @@ -81,29 +82,6 @@ def comments(more=false)
end
end

def parse(json)
json.keys.each do |key|
instance_variable_set("@#{key}", json[key])
end
end

class << self

def parse(json)
submissions = []
data = json["data"]
Reddit::Base.instance_variable_set("@modhash", data["modhash"]) # Needed for api calls
children = data["children"]
children.each do |child|
data = child["data"]
data["kind"] = child["kind"]
next if data["kind"] =~ /more/
submissions << Reddit::Submission.new(data)
end
submissions
end
end

protected
def add_distinction(verb)
resp=self.class.post("/api/distinguish/#{verb}", {:body => {:id => id, :uh => modhash, :r => subreddit, :executed => "distinguishing..."}, :headers => base_headers, :debug_output => @debug })
Expand Down

0 comments on commit a8ff599

Please sign in to comment.