ruby_reddit_api 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,25 @@
1
+ == Ruby Reddit Client v0.2.3
2
+ Tested with ruby 1.8.7 & 1.9.2
3
+
4
+ == Usage
5
+ require "ruby_reddit_api"
6
+ r = Reddit::Api.new "user", "password"
7
+ results = r.browse "ruby"
8
+ results[0].upvote
9
+
10
+ == Features
11
+ * Authentication
12
+ * Browse subreddits
13
+ * Searching
14
+ * Read & reply to comments
15
+ * Friend & unfriend redditors
16
+ * Comment & submission voting
17
+
18
+
19
+ == Running Tests
20
+ 1. Set up the reddit VM (http://blog.reddit.com/2010/05/admins-never-do-what-you-want-now-it-is.html)
21
+ 2. Ensure your hosts file has reddit.local pointing to the VM
22
+ 3. rake cucumber
23
+
24
+ == License
25
+ Ruby Reddit API is released under the MIT License.
@@ -0,0 +1,6 @@
1
+ Feature: Searching
2
+ I want to be able to search submissions
3
+
4
+ Scenario: Valid subreddit
5
+ Given I search 'test' with 'test'
6
+ Then I should get back search results
@@ -0,0 +1,13 @@
1
+ Given /^I search '([^']+)' with '([^']+)'$/ do |subreddit, terms|
2
+ # FIXME searching isnt working for me by default in the reddit vm
3
+ Reddit::Base.base_uri "reddit.com"
4
+ Reddit::Api.base_uri "reddit.com"
5
+ @api = Reddit::Api.new
6
+ @subreddit = subreddit
7
+ @terms = terms
8
+ end
9
+
10
+ Then /^I should get back search results$/ do
11
+ results = @api.search(@terms, :in => @subreddit)
12
+ results[0].class.should == Reddit::Submission
13
+ end
@@ -2,6 +2,7 @@ module Reddit
2
2
 
3
3
  # @author James Cook
4
4
  class Api < Base
5
+ attr_accessor :user, :password
5
6
  attr_reader :last_action, :debug
6
7
 
7
8
  def initialize(user=nil,password=nil, options={})
@@ -11,7 +12,7 @@ module Reddit
11
12
  end
12
13
 
13
14
  def inspect
14
- "<Reddit::Api user='#{user}'>"
15
+ "<Reddit::Api>"
15
16
  end
16
17
 
17
18
  # Browse submissions by subreddit
@@ -26,6 +27,28 @@ module Reddit
26
27
  read("/r/#{subreddit}.json", options )
27
28
  end
28
29
 
30
+ # Search reddit
31
+ # @param [String, Hash] Search terms and options
32
+ # @example
33
+ # search("programming", :in => "ruby", :sort => "relevance")
34
+ # @return [Array<Reddit::Submission>]
35
+ def search(terms=nil, options={})
36
+ http_options = {:verb => "get", :query => {}}
37
+ subreddit = options[:in]
38
+ sort = options.fetch(:sort){ "relevance" }
39
+ http_options[:query].merge!({:sort => sort})
40
+
41
+ if subreddit
42
+ http_options[:query].merge!({:restrict_sr => "1"})
43
+ end
44
+
45
+ if terms
46
+ http_options[:query].merge!({:q => terms})
47
+ end
48
+ path = subreddit.to_s == "" ? "/r/search.json" : "/r/#{subreddit}/search.json"
49
+ read(path, http_options)
50
+ end
51
+
29
52
  # Read sent messages
30
53
  # @return [Array<Reddit::Message>]
31
54
  def sent_messages
@@ -7,7 +7,7 @@ module Reddit
7
7
 
8
8
  attr_reader :last_action, :debug
9
9
  base_uri "www.reddit.com"
10
- class << self; attr_reader :cookie, :modhash, :user_id, :user, end
10
+ class << self; attr_reader :cookie, :modhash, :user_id, :user end
11
11
 
12
12
  def initialize(options={})
13
13
  @debug = StringIO.new
@@ -15,7 +15,7 @@ module Reddit
15
15
 
16
16
  # @return [String]
17
17
  def inspect
18
- "<Reddit::Base user='#{user}'>"
18
+ "<Reddit::Base>"
19
19
  end
20
20
 
21
21
  # Login to Reddit and capture the cookie
@@ -95,10 +95,12 @@ module Reddit
95
95
  def short_body
96
96
  str = body.to_s.strip
97
97
  if str.size > 15
98
- str[0..15] + "..."
98
+ sb = str[0..15] + "..."
99
99
  else
100
- body
100
+ sb = body
101
101
  end
102
+
103
+ sb.gsub(/[\n\r]/,'\n')
102
104
  end
103
105
 
104
106
  def add_distinction(verb)
@@ -58,6 +58,7 @@ module Reddit
58
58
  def comments
59
59
  _comments = read( permalink + ".json", {:handler => "Comment", :query => {:limit => 250}} )
60
60
  @last_comment_id = _comments.last.id if _comments && _comments.any?
61
+ _comments.shift # First 'comment' is actually the submission
61
62
  return _comments
62
63
  end
63
64
 
@@ -17,7 +17,8 @@ module Reddit
17
17
  # The author of the entity. The data is lazy-loaded and cached on the object
18
18
  # @return [Reddit::User]
19
19
  def author
20
- @author_data ||= read("/user/#{@author}/about.json", :handler => "User")[0]
20
+ response = read("/user/#{@author}/about.json", :handler => "User") if @author
21
+ @author_data ||= response[0] if response
21
22
  end
22
23
 
23
24
  # Upvote thing
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 2
8
- - 2
9
- version: 0.2.2
8
+ - 3
9
+ version: 0.2.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - James Cook
@@ -64,22 +64,24 @@ files:
64
64
  - lib/ruby_reddit_api/api.rb
65
65
  - lib/ruby_reddit_api/json_listing.rb
66
66
  - lib/ruby_reddit_api/base.rb
67
- - README
67
+ - README.rdoc
68
68
  - features/reddit.yml
69
69
  - features/submission.feature
70
70
  - features/comment.feature
71
71
  - features/user.feature
72
72
  - features/base.feature
73
73
  - features/message.feature
74
+ - features/search.feature
74
75
  - features/api.feature
75
76
  - features/step_definitions/message_steps.rb
76
77
  - features/step_definitions/submission_steps.rb
78
+ - features/step_definitions/search_steps.rb
77
79
  - features/step_definitions/api_steps.rb
78
80
  - features/step_definitions/user_steps.rb
79
81
  - features/step_definitions/comment_steps.rb
80
82
  - features/step_definitions/base_steps.rb
81
83
  - features/support/base_helpers.rb
82
- has_rdoc: false
84
+ has_rdoc: yard
83
85
  homepage: http://github.com/jamescook/RubyRedditAPI
84
86
  licenses: []
85
87
 
@@ -94,8 +96,10 @@ required_ruby_version: !ruby/object:Gem::Requirement
94
96
  - - ">="
95
97
  - !ruby/object:Gem::Version
96
98
  segments:
97
- - 0
98
- version: "0"
99
+ - 1
100
+ - 8
101
+ - 7
102
+ version: 1.8.7
99
103
  required_rubygems_version: !ruby/object:Gem::Requirement
100
104
  none: false
101
105
  requirements:
@@ -108,7 +112,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
108
112
  version: 1.3.6
109
113
  requirements: []
110
114
 
111
- rubyforge_project:
115
+ rubyforge_project: ruby_reddit_api
112
116
  rubygems_version: 1.3.7
113
117
  signing_key:
114
118
  specification_version: 3
@@ -120,9 +124,11 @@ test_files:
120
124
  - features/user.feature
121
125
  - features/base.feature
122
126
  - features/message.feature
127
+ - features/search.feature
123
128
  - features/api.feature
124
129
  - features/step_definitions/message_steps.rb
125
130
  - features/step_definitions/submission_steps.rb
131
+ - features/step_definitions/search_steps.rb
126
132
  - features/step_definitions/api_steps.rb
127
133
  - features/step_definitions/user_steps.rb
128
134
  - features/step_definitions/comment_steps.rb
data/README DELETED
@@ -1,48 +0,0 @@
1
- Ruby Reddit Client v0.2.2
2
- ==================
3
- Tested with ruby 1.9.2
4
-
5
- Usage:
6
- =======
7
- > require "ruby_reddit_api"
8
- > r = Reddit::Api.new "user", "password"
9
- > results = r.browse "ruby"
10
- > results[0].upvote
11
-
12
- Features:
13
- ========
14
- - Authentication
15
- - Browse submissions
16
- - Read comments
17
- - Friend/unfriend redditors
18
- - Comment & submission voting
19
-
20
-
21
- Running Tests:
22
- =============
23
- - Set up the reddit VM (http://blog.reddit.com/2010/05/admins-never-do-what-you-want-now-it-is.html)
24
- - Ensure your hosts file has reddit.local pointing to the VM
25
- - rake cucumber
26
-
27
- License
28
- (The MIT License)
29
-
30
- Copyright (c) 2010 James Cook
31
-
32
- Permission is hereby granted, free of charge, to any person obtaining a copy
33
- of this software and associated documentation files (the "Software"), to deal
34
- in the Software without restriction, including without limitation the rights
35
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
36
- copies of the Software, and to permit persons to whom the Software is
37
- furnished to do so, subject to the following conditions:
38
-
39
- The above copyright notice and this permission notice shall be included in
40
- all copies or substantial portions of the Software.
41
-
42
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
43
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
44
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
45
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
46
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
47
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
48
- THE SOFTWARE.