Skip to content
Browse files

Implemented ability to login against the API. How you maintain this s…

…tate I have no idea
  • Loading branch information...
1 parent a71e0d1 commit 3f0ad10f55e4c9e4a4acee13b501986fa0a9126a Blake Watters committed
Showing with 17 additions and 1 deletion.
  1. +17 −1 lib/reddit/session.rb
View
18 lib/reddit/session.rb
@@ -15,11 +15,12 @@ class AuthenticationException < StandardError; end
# A reddit browsing session.
class Session
-
+
# initialize the session with a username and password. Currently not used.
def initialize(username = "", password = "")
@username = username
@password = password
+ @logged_in = false
end
# return the main reddit.
@@ -37,5 +38,20 @@ def user(username)
return User.new(username)
end
+ def logged_in?
+ @logged_in
+ end
+
+ def login
+ url = "http://www.reddit.com/api/login.json"
+ params = { 'reason' => '', 'op' => 'login-main', 'dest' => '/', 'user_login' => @username, 'passwd_login' => @password, 'rem' => '1' }
+ result = Net::HTTP.post_form(URI.parse(url), params)
+ resources = JSON.parse(result.body, :max_nesting => 0)
+ if resources['error']
+ raise AuthenticationException, resources['error']['message']
+ end
+ pp resources
+ return true
+ end
end
end

0 comments on commit 3f0ad10

Please sign in to comment.
Something went wrong with that request. Please try again.