Skip to content

Commit

Permalink
Remove cruft. Fix friending/unfriending.
Browse files Browse the repository at this point in the history
  • Loading branch information
james cook committed Oct 6, 2010
1 parent 07adf11 commit 1ab822d
Show file tree
Hide file tree
Showing 11 changed files with 121 additions and 51 deletions.
2 changes: 2 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ require 'cucumber/rake/task'
Cucumber::Rake::Task.new do |t|
t.cucumber_opts = %w{--format pretty}
end

task :default => [:cucumber]
1 change: 1 addition & 0 deletions VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.1.4
25 changes: 25 additions & 0 deletions features/step_definitions/user_steps.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Before do
load_server_config
Reddit::Api.base_uri @address
Reddit::Submission.base_uri @address
Reddit::Comment.base_uri @address
Reddit::User.base_uri @address
@api = Reddit::Api.new @user, @pass
@api.login
end

Given /^I select a redditor$/ do
@submission = @api.browse("reddit_test1")[0]
@user = @submission.author
end

Then /^I should be able to friend them$/ do
@user.friend.should be true
end

Then /^I should be able to unfriend them$/ do
@user.unfriend.should be true
end



11 changes: 11 additions & 0 deletions features/user.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@user
Feature: User
In order have friends
As a Redditor
I want to be able to add and remove them

Scenario: Valid user and password
Given I select a redditor
Then I should be able to friend them
And I should be able to unfriend them

2 changes: 2 additions & 0 deletions lib/reddit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
require "json"
require "reddit/base"
require "reddit/api"
require "reddit/user"
require "reddit/vote"
require "reddit/submission"
require "reddit/comment"

module Reddit
VERSION = File.exist?("VERSION") ? File.read("VERSION").chomp : ""
end
11 changes: 7 additions & 4 deletions lib/reddit/api.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module Reddit

class Api < Base
attr_reader :user, :password, :last_action, :debug
attr_reader :last_action, :debug

def initialize(user,password, options={})
def initialize(user=nil,password=nil, options={})
@user = user
@password = password
@debug = StringIO.new
Expand All @@ -16,15 +16,18 @@ def inspect
def browse(subreddit, options={})
subreddit = sanitize_subreddit(subreddit)
options.merge! :handler => "Submission"
if options[:limit]
options.merge!({:query => {:limit => options[:limit]}})
end
read("/r/#{subreddit}.json", options )
end

def read(url, options={})
unless throttled?
@debug.rewind
verb = (options[:verb] || "get")
verb = (options[:verb] || "get")
param_key = (verb == "get") ? :query : :body
resp = self.class.send( verb, url, {param_key => (options[param_key] || {}), :headers => base_headers, :debug_output => @debug})
resp = self.class.send( verb, url, {param_key => (options[param_key] || {}), :headers => base_headers, :debug_output => @debug})
if valid_response?(resp)
@last_action = Time.now
klass = Reddit.const_get(options[:handler] || "Submission")
Expand Down
41 changes: 19 additions & 22 deletions lib/reddit/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ class Base

attr_reader :last_action, :debug
base_uri "www.reddit.com"
class << self; attr_reader :cookie, :modhash; end
class << self; attr_reader :cookie, :modhash, :user_id, :user, end

def initialize(user,password, options={})
@user = user
@password = password
def initialize(options={})
@debug = StringIO.new
end

Expand All @@ -17,8 +15,7 @@ def inspect
end

def login
url = action_mapping["login"]["path"]
capture_session(self.class.post( url, {:body => {:user => user, :passwd => password}, :debug_output => @debug} ) )
capture_session(self.class.post( "/api/login", {:body => {:user => @user, :passwd => @password}, :debug_output => @debug} ) )
logged_in?
end

Expand All @@ -34,6 +31,14 @@ def modhash
Reddit::Base.modhash
end

def user_id
Reddit::Base.user_id
end

def user
Reddit::Base.user
end

def logged_in?
!!(cookie && (cookie =~ /reddit_session/) != nil)
end
Expand All @@ -54,6 +59,13 @@ def valid_response?(response)
def capture_session(response)
cookies = response.headers["set-cookie"]
Reddit::Base.instance_variable_set("@cookie", cookies)
Reddit::Base.instance_variable_set("@user", @user)
end

def capture_user_id
return true if user_id
this_user = read("/user/#{user}/about.json", :handler => "User")
Reddit::Base.instance_variable_set("@user_id", this_user.id)
end

def throttled?
Expand All @@ -66,29 +78,14 @@ def sanitize_subreddit(subreddit)
subreddit
end

def action_mapping
{
"login" => {"path" => "/api/login", "verb" => "POST"},
"vote" => {"path" => "/api/vote", "verb" => "POST"},
"save" => {"path" => "/api/save", "verb" => "POST"},
"unsave" => {"path" => "/api/unsave", "verb" => "POST"},
"comment" => {"path" => "/api/comment", "verb" => "POST"},
"subscribe" => {"path" => "/api/subscribe", "verb" => "POST" },
"comments" => {"path" => "/comments", "verb" => "GET", "handler" => "Comment" },
"my_reddits" => {"path" => "/reddits/mine", "verb" => "GET" },
"saved" => {"path" => "saved", "verb" => "GET", "handler" => "Submission"},
"" => {"path" => "", "verb" => "GET", "handler" => "Submission"}
}
end

class << self

def base_headers
{'Cookie' => Reddit::Base.cookie.to_s, 'user-agent' => user_agent}
end

def user_agent
"Ruby Reddit Client v0.0.1"
"Ruby Reddit Client v#{VERSION}"
end
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/reddit/comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ def parse(json)

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

Expand Down
28 changes: 5 additions & 23 deletions lib/reddit/submission.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
module Reddit
class Submission < Api
attr_reader :domain, :media_embed, :subreddit, :selftext_html, :selftext, :likes, :saved, :clicked, :author, :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
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)
json = data
parse(json)
parse(data)
@debug = StringIO.new
end

def inspect
"<Reddit::Submission id='#{id}' author='#{author}' title='#{title}'>"
"<Reddit::Submission id='#{id}' author='#{@author}' title='#{title}'>"
end

def id
"#{kind}_#{@id}"
end

def reload
#TODO
def author
@author_data ||= read("/user/#{@author}/about.json", :handler => "User")
end

def add_comment(text)
Expand Down Expand Up @@ -89,23 +88,6 @@ def parse(json)
end

class << self
=begin
# Damn you captcha!!
def post_link(options)
submit(options.merge(:kind => "url"))
end
def post_text(options)
submit(options.merge(:kind => "self"))
end
def submit(options={})
raise "You must send :subreddit" unless options.key? :subreddit
api_options = options.merge(:uh => modhash, :sr => options[:subreddit], :kind => options[:kind])
resp = self.post("/api/submit", {:body => options, :headers => base_headers})
resp.code == 200
end
=end

def parse(json)
submissions = []
Expand Down
47 changes: 47 additions & 0 deletions lib/reddit/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@

module Reddit
class User < Api
attr_reader :name, :debug, :created, :created_utc, :link_karma, :comment_karma, :is_mod, :has_mod_mail, :kind
def initialize(json)
@debug = StringIO.new
parse(json)
end

def inspect
"<Reddit::User name='#{name}'>"
end

def id
"#{kind}_#{@id}"
end

def to_s
name
end

def friend
capture_user_id
resp=self.class.post("/api/friend", {:body => {:name => name, :container => user_id, :type => "friend", :uh => modhash}, :headers => base_headers, :debug_output => @debug })
resp.code == 200
end

def unfriend
capture_user_id
resp=self.class.post("/api/unfriend", {:body => {:name => name, :container => user_id, :type => "friend", :uh => modhash}, :headers => base_headers, :debug_output => @debug })
resp.code == 200
end

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

def self.parse(json)
kind, data = json["kind"], json["data"]
data["kind"] = kind
return Reddit::User.new(data)
end
end
end
3 changes: 1 addition & 2 deletions lib/reddit/vote.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ def inspect
def vote(direction)
return false unless logged_in?
up_or_down = direction == :up ? 1 : -1
url = self.send(:action_mapping)["vote"]["path"]
resp = self.class.post( url, {:body => {:id => submission.id, :dir => up_or_down, :uh => modhash}, :headers => base_headers})
resp = self.class.post( "/api/vote", {:body => {:id => submission.id, :dir => up_or_down, :uh => modhash}, :headers => base_headers})
if resp.code == 200
return true
else
Expand Down

0 comments on commit 1ab822d

Please sign in to comment.