Permalink
Browse files

Remove cruft. Fix friending/unfriending.

  • Loading branch information...
1 parent 07adf11 commit 1ab822d865a70c89f9290afba180706c5b8cc51c james cook committed Oct 6, 2010
Showing with 121 additions and 51 deletions.
  1. +2 −0 Rakefile
  2. +1 −0 VERSION
  3. +25 −0 features/step_definitions/user_steps.rb
  4. +11 −0 features/user.feature
  5. +2 −0 lib/reddit.rb
  6. +7 −4 lib/reddit/api.rb
  7. +19 −22 lib/reddit/base.rb
  8. +1 −0 lib/reddit/comment.rb
  9. +5 −23 lib/reddit/submission.rb
  10. +47 −0 lib/reddit/user.rb
  11. +1 −2 lib/reddit/vote.rb
View
@@ -4,3 +4,5 @@ require 'cucumber/rake/task'
Cucumber::Rake::Task.new do |t|
t.cucumber_opts = %w{--format pretty}
end
+
+task :default => [:cucumber]
View
@@ -0,0 +1 @@
+0.1.4
@@ -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
+
+
+
View
@@ -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
+
View
@@ -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
View
@@ -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
@@ -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")
View
@@ -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
@@ -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
@@ -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
@@ -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?
@@ -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
@@ -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
@@ -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)
@@ -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 = []
View
@@ -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
View
@@ -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

0 comments on commit 1ab822d

Please sign in to comment.