snoo 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,60 @@
1
+ module Snoo
2
+ # Methods for interacting with users, such as adding and removing friends, getting user info, etc
3
+ #
4
+ # @author (see Snoo)
5
+ module User
6
+
7
+ # Friend a user
8
+ #
9
+ # @param name [String] The username to target
10
+ # @param note [String] A friend tag. Requires reddit gold.
11
+ # @return (see #clear_sessions)
12
+ def friend name, friend_id, note = nil
13
+ friend_wrapper(api_name = name, api_container = @userid, api_note = note, api_type = "friend")
14
+ end
15
+
16
+ # Unfriend a user
17
+ #
18
+ # @param id [String] The friend id to remove. Must be in t2_id form
19
+ # @return (see #clear_sessions)
20
+ def unfriend id
21
+ unfriend_wrapper api_id = id, api_container = @userid, api_type = "friend"
22
+ end
23
+
24
+ # Get a list of friends
25
+ #
26
+ # @return (see #clear_sessions)
27
+ def get_friends
28
+ logged_in?
29
+ get('/prefs/friends.json')
30
+ end
31
+
32
+ # Get info about a user account
33
+ #
34
+ # @param username [String] The username to target.
35
+ def get_user_info username
36
+ get("/user/#{username}/about.json")
37
+ end
38
+
39
+ # Get a listing of user posts. Some options may be restricted
40
+ #
41
+ # @param (see #get_user_info)
42
+ # @param (see LinksComments#info)
43
+ # @option opts [overview, submitted, comments, liked, disliked, hidden, saved] :type Type of post to return. Most users only allow the first 3 types.
44
+ # @option opts [new, hot, top, controversial] :sort The sort order
45
+ # @option opts [String] :after Return things *after* this id
46
+ # @option opts [String] :before Return things *before* this id
47
+ # @option opts [1..100] :limit Number of results to return
48
+ # @return (see #clear_sessions)
49
+ def get_user_listing username, opts = {}
50
+ raise ArgumentError, "type is invalid" unless %w{overview submitted commented liked disliked hidden saved}.include?(opts[:type]) or opts[:type].nil?
51
+ raise ArgumentError, "sort is invalid" unless %w{new hot top controversial}.include?(opts[:sort]) or opts[:type].nil?
52
+ raise ArgumentError, "limit must be within 1..100; is #{opts[:limit]}" unless (1..100).include?(opts[:limit]) or opts[:limit].nil?
53
+ opts[:type] = 'overview' if opts[:type].nil?
54
+ url = "/user/%s%s.json" % [username, ('/' + opts[:type] if opts[:type] != 'overview')]
55
+ opts.delete :type
56
+ query = opts
57
+ get(url, query: query)
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,73 @@
1
+ module Snoo
2
+ # Utility functions.
3
+ # These are all private
4
+ #
5
+ # @author (see Snoo)
6
+ module Utilities
7
+ private
8
+ # HTTParty get wrapper. This serves to clean up code, as well as throw webserver errors wherever needed
9
+ #
10
+ def get *args, &block
11
+ response = self.class.get *args, &block
12
+ raise WebserverError, response.code unless response.code == 200
13
+ response
14
+ end
15
+
16
+ # HTTParty POST wrapper. This serves to clean up code, as well as throw webserver errors wherever needed, same as {#get}
17
+ #
18
+ def post *args, &block
19
+ response = self.class.post *args, &block
20
+ raise WebserverError, response.code unless response.code == 200
21
+ response
22
+ end
23
+ # Set the cookie header and instance variable
24
+ #
25
+ # @param cookie [String] The cookie text, as show in a 'set-cookie' header
26
+ def cookies cookie
27
+ @cookie = cookie
28
+ self.class.headers 'Cookie' => cookie
29
+ end
30
+
31
+ # Raises an error if we aren't currently logged in
32
+ #
33
+ def logged_in?
34
+ raise NotAuthenticated if @cookie.nil? or @modhash.nil?
35
+ end
36
+
37
+ # Posts to '/api/friend'. This method exists because there are tons of things that use this
38
+ #
39
+ # @param api_type [friend, moderator, contributor, banned] Type of action to use. All but friend target subreddits
40
+ # @param api_container [String] Thing ID, either subreddit (t5_) or user adding friend (t2_)
41
+ # @param api_name [String] Username to add
42
+ # @param api_note [String] Note to leave. Requires reddit gold
43
+ # @param api_subreddit [String] Subreddit name
44
+ # @return [HTTParty::Response] The response object.
45
+ def friend_wrapper api_type, api_container = nil, api_name = nil, api_note = nil, api_subreddit = nil
46
+ logged_in?
47
+ params = {type: api_type, uh: @modhash}
48
+ params[:container] = api_container if api_container
49
+ params[:name] = api_name if api_name
50
+ params[:note] = api_note if api_note
51
+ params[:r] = api_subreddit if api_subreddit
52
+ post('/api/friend', body: params)
53
+ end
54
+
55
+ # Posts to '/api/unfriend'. This method exists because there are a ton of things that use this.
56
+ #
57
+ # @param api_type [friend, enemy, moderator, contributor, banned] Type of removal
58
+ # @param api_container [String] Thing ID, either subreddit (t5_) or user adding friend (t2_)
59
+ # @param api_name [String] User name
60
+ # @param api_id [String] User ID of user being removed
61
+ # @param api_subreddit [String] Subreddit name
62
+ # @return (see #friend_wrapper)
63
+ def unfriend_wrapper api_type, api_container = nil, api_name = nil, api_id = nil, api_subreddit = nil
64
+ logged_in?
65
+ params = { type: api_type, uh: @modhash}
66
+ params[:container] = api_container if api_container
67
+ params[:name] = api_name if api_name
68
+ params[:id] = api_id if api_id
69
+ params[:r] = api_subreddit if api_subreddit
70
+ post('/api/unfriend', body: params)
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,4 @@
1
+ module Snoo
2
+ # The version string (duh)
3
+ VERSION = "0.0.1"
4
+ end
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/snoo/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Jeff Sandberg"]
6
+ gem.email = ["paradox460@gmail.com"]
7
+ gem.description = %q{Snoo is yet another reddit API wrapper. Unlike the other wrappers out there, this one strives to be an almost exact clone of the reddit api, however, it also makes many attempts to be easier to use. You won't be instantiating Snoo::Comment or anything like that. Just one instantiation, and you can do everything from there.}
8
+ gem.summary = %q{A simple reddit api wrapper}
9
+ gem.homepage = "https://github.com/paradox460/snoo"
10
+ gem.files = `git ls-files`.split($\)
11
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
12
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
13
+ gem.name = "snoo"
14
+ gem.require_paths = ["lib"]
15
+ gem.version = Snoo::VERSION
16
+ gem.required_ruby_version = '>= 1.9'
17
+ gem.license = 'MIT'
18
+
19
+ ['httparty'].each do |dependency|
20
+ gem.add_runtime_dependency dependency
21
+ end
22
+ ['rspec'].each do |dependency|
23
+ gem.add_development_dependency dependency
24
+ end
25
+ end
@@ -0,0 +1,6 @@
1
+ # Test user account
2
+ $testuser = ''
3
+ # Test password
4
+ $testpass = ''
5
+ # Test subreddit (test account should have mod)
6
+ $testreddit = ''
@@ -0,0 +1,164 @@
1
+ require 'snoo'
2
+ require_relative 'auth'
3
+
4
+ describe Snoo::Client do
5
+ before :all do
6
+ @client = Snoo::Client.new
7
+ @client.log_in $testuser, $testpass
8
+ end
9
+
10
+ describe "#new" do
11
+ after :each do
12
+ sleep 2
13
+ end
14
+ it "Creates a new Snoo::Client object" do
15
+ Snoo::Client.new.should be_an_instance_of(Snoo::Client)
16
+ end
17
+ it "Creates an unauthorized Snoo::Client" do
18
+ client = Snoo::Client.new
19
+ client.modhash.should be_nil
20
+ client.username.should be_nil
21
+ client.userid.should be_nil
22
+ client.cookie.should be_nil
23
+ end
24
+ end
25
+
26
+ describe "Account" do
27
+ after :each do
28
+ sleep 2
29
+ end
30
+
31
+ # Due to the constraints the reddit API provides us, we only log in once, and then use it as much as we can.
32
+ describe "#log_in" do
33
+ it "logs in and ensures all the instance variables are set" do
34
+ @client.modhash.should_not be_nil
35
+ @client.userid.should_not be_nil
36
+ @client.username.should_not be_nil
37
+ @client.cookie.should_not be_nil
38
+ end
39
+ end
40
+
41
+ describe "#clear_sessions" do
42
+ it "clears other sessions and looks for 'all other sessions have been cleared' from the server" do
43
+ response = @client.clear_sessions $testpass
44
+ response['jquery'][14][3][0].should eq"all other sessions have been logged out"
45
+ end
46
+ end
47
+
48
+ describe "#me" do
49
+ it "gets info about currently logged in user, and makes sure this matches $testuser" do
50
+ me = @client.me
51
+ me['data']['name'].should eq $testuser
52
+ end
53
+ end
54
+ end
55
+
56
+ describe "Flair" do
57
+ after :each do
58
+ sleep 2
59
+ end
60
+ describe "#clear_flair_templates" do
61
+ after :each do
62
+ sleep 2
63
+ end
64
+ it "clears user flair templates" do
65
+ @client.clear_flair_templates('USER_FLAIR', $testreddit).code.should eq 200
66
+ end
67
+ it "clears link flair templates" do
68
+ @client.clear_flair_templates('LINK_FLAIR', $testreddit).code.should eq 200
69
+ end
70
+ end
71
+
72
+ describe "#flair" do
73
+ it "sets user flair on this user" do
74
+ flair = @client.flair $testreddit, css_class: "test", text: "test", name: $testuser
75
+ flair.code.should eq 200
76
+ end
77
+ end
78
+
79
+ describe "#delete_user_flair" do
80
+ it "deletes user flair from this user" do
81
+ @client.delete_user_flair($testuser, $testreddit).code.should eq 200
82
+ end
83
+ end
84
+
85
+ describe "#get_flair_list" do
86
+ it "should get a list of flair" do
87
+ @client.get_flair_list($testreddit).code.should eq 200
88
+ end
89
+ end
90
+ end
91
+
92
+ describe "Listings" do
93
+ after :each do
94
+ sleep 2
95
+ end
96
+ describe "#get_listing" do
97
+ it "gets a listing of links from reddit" do
98
+ listing = @client.get_listing subreddit: $testreddit
99
+ listing.code.should eq 200
100
+ end
101
+ end
102
+ describe "#search" do
103
+ it "searches reddit for 'test'" do
104
+ search = @client.search 'test'
105
+ search.code.should eq 200
106
+ end
107
+ end
108
+ end
109
+
110
+ describe "Private Messages" do
111
+ after :each do
112
+ sleep 2
113
+ end
114
+ describe "#send_pm" do
115
+ it "should send a private message to current user" do
116
+ send = @client.send_pm $testuser, 'test', 'testing'
117
+ send.code.should eq 200
118
+ end
119
+ end
120
+ describe "#get_messages" do
121
+ it "should get a list of private messages" do
122
+ messages = @client.get_messages
123
+ messages.code.should eq 200
124
+ end
125
+ end
126
+ end
127
+
128
+ describe "User" do
129
+ after :each do
130
+ sleep 2
131
+ end
132
+
133
+ describe "#get_friends" do
134
+ it "should get a list of friends" do
135
+ friends = @client.get_friends
136
+ friends.code.should eq 200
137
+ end
138
+ end
139
+
140
+ describe "#get_user_info" do
141
+ it "should get information about the current user account" do
142
+ info = @client.get_user_info $testuser
143
+ info.code.should eq 200
144
+ end
145
+ end
146
+
147
+ describe "#get_user_listing" do
148
+ it "should get a listing of user's posts" do
149
+ listing = @client.get_user_listing $testuser
150
+ listing.code.should eq 200
151
+ end
152
+ end
153
+ end
154
+
155
+ describe "#log_out" do
156
+ it "logs out and sets instance variables to nil" do
157
+ @client.log_out
158
+ @client.modhash.should be_nil
159
+ @client.userid.should be_nil
160
+ @client.username.should be_nil
161
+ @client.cookie.should be_nil
162
+ end
163
+ end
164
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: snoo
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jeff Sandberg
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-06-22 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: httparty
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: Snoo is yet another reddit API wrapper. Unlike the other wrappers out
47
+ there, this one strives to be an almost exact clone of the reddit api, however,
48
+ it also makes many attempts to be easier to use. You won't be instantiating Snoo::Comment
49
+ or anything like that. Just one instantiation, and you can do everything from there.
50
+ email:
51
+ - paradox460@gmail.com
52
+ executables: []
53
+ extensions: []
54
+ extra_rdoc_files: []
55
+ files:
56
+ - .gitignore
57
+ - .yardopts
58
+ - Gemfile
59
+ - LICENSE
60
+ - README.md
61
+ - Rakefile
62
+ - lib/snoo.rb
63
+ - lib/snoo/account.rb
64
+ - lib/snoo/exceptions.rb
65
+ - lib/snoo/flair.rb
66
+ - lib/snoo/links_comments.rb
67
+ - lib/snoo/listings.rb
68
+ - lib/snoo/moderation.rb
69
+ - lib/snoo/pms.rb
70
+ - lib/snoo/subreddits.rb
71
+ - lib/snoo/users.rb
72
+ - lib/snoo/utilities.rb
73
+ - lib/snoo/version.rb
74
+ - snoo.gemspec
75
+ - spec/auth.rb
76
+ - spec/snoo_spec.rb
77
+ homepage: https://github.com/paradox460/snoo
78
+ licenses:
79
+ - MIT
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ! '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '1.9'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ none: false
92
+ requirements:
93
+ - - ! '>='
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 1.8.24
99
+ signing_key:
100
+ specification_version: 3
101
+ summary: A simple reddit api wrapper
102
+ test_files:
103
+ - spec/auth.rb
104
+ - spec/snoo_spec.rb
105
+ has_rdoc: