Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 0a537c9
Showing
26 changed files
with
585 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
== 0.1.0 2008-05-22 | ||
|
||
* 1 major enhancement: | ||
* Initial release |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
Copyright (c) 2008 Brian Terlson | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the | ||
"Software"), to deal in the Software without restriction, including | ||
without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so, subject to | ||
the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
History.txt | ||
License.txt | ||
Manifest.txt | ||
PostInstall.txt | ||
README.txt | ||
Rakefile | ||
config/hoe.rb | ||
config/requirements.rb | ||
lib/reddit.rb | ||
lib/reddit/article.rb | ||
lib/reddit/comment.rb | ||
lib/reddit/comment_list.rb | ||
lib/reddit/reddit.rb | ||
lib/reddit/resource_list.rb | ||
lib/reddit/session.rb | ||
lib/reddit/user.rb | ||
lib/reddit/version.rb | ||
script/console | ||
script/destroy | ||
script/generate | ||
script/txt2html | ||
setup.rb | ||
spec/reddit_spec.rb | ||
spec/spec.opts | ||
spec/spec_helper.rb | ||
tasks/deployment.rake | ||
tasks/environment.rake | ||
tasks/rspec.rake | ||
tasks/website.rake |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
|
||
For more information on reddit, see http://reddit.rubyforge.org | ||
|
||
NOTE: Change this information in PostInstall.txt | ||
You can also delete it if you don't want it. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
|
||
* FIX (url) | ||
|
||
== DESCRIPTION: | ||
|
||
FIX (describe your package) | ||
|
||
== FEATURES/PROBLEMS: | ||
|
||
* FIX (list of features or problems) | ||
|
||
== SYNOPSIS: | ||
|
||
FIX (code sample of usage) | ||
|
||
== REQUIREMENTS: | ||
|
||
* FIX (list of requirements) | ||
|
||
== INSTALL: | ||
|
||
* FIX (sudo gem install, anything else) | ||
|
||
== LICENSE: | ||
|
||
(The MIT License) | ||
|
||
Copyright (c) 2008 FIX | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the | ||
'Software'), to deal in the Software without restriction, including | ||
without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so, subject to | ||
the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY | ||
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | ||
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | ||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
require 'rake/gempackagetask' | ||
|
||
spec = Gem::Specification.new do |s| | ||
s.name = "Reddit" | ||
s.version = "0.1.0" | ||
s.author = "Brian Terlson" | ||
s.email = "btthalion@gmail.com" | ||
s.homepage = "http://github.com/bterlson/reddit/" | ||
s.platform = Gem::Platform::RUBY | ||
s.summary = "Interface with the Reddit.com API." | ||
s.files = FileList["{bin,lib}/**/*"].to_a | ||
s.require_path = "lib" | ||
s.has_rdoc = true | ||
s.extra_rdoc_files = ["README.txt"] | ||
s.add_dependency("json", ">= 1.1.2") | ||
end | ||
|
||
Rake::GemPackageTask.new(spec) do |pkg| | ||
pkg.need_tar = true | ||
end | ||
|
||
Dir['tasks/**/*.rake'].each { |rake| load rake } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
$:.unshift(File.dirname(__FILE__)) unless | ||
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__))) | ||
|
||
require 'rubygems' | ||
require 'json' | ||
require 'net/http' | ||
require 'reddit/resource_list' | ||
require 'reddit/comment_list' | ||
require 'reddit/comment' | ||
require 'reddit/session' | ||
require 'reddit/version' | ||
require 'reddit/reddit' | ||
require 'reddit/article' | ||
require 'reddit/user' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
module Reddit | ||
# A reddit article or submission. | ||
class Article | ||
attr_reader :score, :name, :title, :comment_count, :ups, :downs, :url, :domain, :author, :id, :created_at | ||
|
||
# Initializes the data for the article. Takes a hash of the various attributes as taken from the API. | ||
def initialize(attributes) | ||
@score = attributes['score'] | ||
@name = attributes['name'] | ||
@title = attributes['title'] | ||
@comment_count = attributes['num_comments'] | ||
@ups = attributes['ups'] | ||
@downs = attributes['downs'] | ||
@url = attributes['url'] | ||
@domain = attributes['domain'] | ||
@author = User.new(attributes['author']) unless attributes['author'].nil? | ||
@id = attributes['id'] | ||
@created_at = Time.at(attributes['created']) unless attributes['created'].nil? | ||
@saved = attributes['saved'] | ||
@clicked = attributes['clicked'] | ||
@hidden = attributes['hidden'] | ||
end | ||
|
||
# indicates if the current logged in user has saved the article. | ||
def saved? | ||
return @saved | ||
end | ||
|
||
# indicates if the current logged in user has clicked the article. | ||
def clicked? | ||
return @clicked | ||
end | ||
|
||
# indicates if the current logged in user has hidden the article. | ||
def hidden? | ||
return @hidden | ||
end | ||
|
||
# returns a CommentList of this article's comments. | ||
def comments | ||
return CommentList.new(@id) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
module Reddit | ||
|
||
# A reddit comment. | ||
class Comment | ||
attr_reader :body, :name, :ups, :downs, :url, :domain, :author, :id, :created_at, :replies | ||
|
||
# Initializes the data for the comment. Takes a hash of the various attributes as taken from the API. | ||
def initialize(attributes) | ||
@score = attributes['score'] | ||
@name = attributes['name'] | ||
@ups = attributes['ups'] | ||
@downs = attributes['downs'] | ||
@author = User.new(attributes['author']) unless attributes['author'].nil? | ||
@id = attributes['id'] | ||
@created_at = Time.at(attributes['created']) unless attributes['created'].nil? | ||
@body = attributes['body'] | ||
@replies = [] | ||
|
||
unless attributes['replies'].nil? | ||
attributes['replies']['data']['children'].each do |reply| | ||
@replies << Comment.new(reply['data']) | ||
end | ||
end | ||
|
||
end | ||
|
||
# Returns the score for this comment. | ||
def score | ||
return @ups - @downs | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
module Reddit | ||
|
||
# The list of comments found for an article. | ||
class CommentList < ResourceList | ||
|
||
# Initializes a comments list. Takes the ID of the article for which the comments belong to. | ||
def initialize(article_id) | ||
@article_id = article_id | ||
@url = COMMENTS_URL.gsub('[id]', @article_id) | ||
end | ||
|
||
# returns the top level comments for the thread. | ||
def top_level | ||
resources = get_resources(@url) | ||
|
||
comments = [] | ||
resources.each do |comment| | ||
comments << Comment.new(comment['data']) | ||
end | ||
|
||
return comments | ||
end | ||
|
||
private | ||
|
||
# forward any method calls to the top level comments array. | ||
def method_missing(meth, *args, &block) | ||
top_level.send(meth, *args, &block) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
module Reddit | ||
|
||
# The main reddit or a subreddit. | ||
class Reddit < ResourceList | ||
|
||
# Initialize the reddit. If no name is specified, the reddit will be the main reddit. | ||
def initialize(name = nil) | ||
@name = name | ||
@url = @name.nil? ? BASE_URL : SUBREDDIT_URL.gsub('[subreddit]', @name) | ||
end | ||
|
||
# Returns the articles found in this reddit. | ||
def articles | ||
resources = get_resources(@url) | ||
|
||
articles = [] | ||
|
||
resources.each do |article| | ||
articles << Article.new(article['data']) | ||
end | ||
|
||
return articles | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
module Reddit | ||
|
||
# A list of resources, such as a list of comments from an article or a user's page. | ||
class ResourceList | ||
attr_reader :url | ||
|
||
private | ||
|
||
# Grabs the resources at the URL and returns the parsed json. | ||
def get_resources(url) | ||
url = URI.parse(url) | ||
|
||
res = Net::HTTP.start(url.host, url.port) {|http| | ||
http.get(url.path + ".json") | ||
} | ||
|
||
raise SubredditNotFound if res.is_a?(Net::HTTPRedirection) | ||
resources = JSON.parse(res.body, :max_nesting => 0) | ||
|
||
# comments pages are contained in an array where the first element is the article | ||
# and the second is the actual comments. This is hackish. | ||
resources = resources.last if resources.is_a?(Array) | ||
|
||
return resources['data']['children'] | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
module Reddit | ||
BASE_URL = "http://www.beta.reddit.com/" | ||
PROFILE_URL = BASE_URL + "user/[username]/" | ||
SUBREDDIT_URL = BASE_URL + "r/[subreddit]/" | ||
COMMENTS_URL = BASE_URL + "info/[id]/comments/" | ||
|
||
# raised when attempting to interact with a subreddit that doesn't exist. | ||
class SubredditNotFound < StandardError; end | ||
|
||
# raised when attempting to interact with a profile that doesn't exist. | ||
class ProfileNotFound < StandardError; end | ||
|
||
# raised when attempting to log in with incorrect credentials. | ||
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 | ||
end | ||
|
||
# return the main reddit. | ||
def main | ||
return Reddit.new() | ||
end | ||
|
||
# return a specific subreddit. | ||
def subreddit(subreddit) | ||
return Reddit.new(subreddit) | ||
end | ||
|
||
# return a specific user's page. | ||
def user(username) | ||
return User.new(username) | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
module Reddit | ||
|
||
# A user's page. | ||
class User < ResourceList | ||
attr_reader :name | ||
|
||
# Initialize based on the user's name. | ||
def initialize(name) | ||
@name = name | ||
@url = PROFILE_URL.gsub('[username]', @name) | ||
end | ||
|
||
# Get the user's comments. | ||
def comments | ||
resources = get_resources(@url) | ||
|
||
comments = [] | ||
resources.each do |comment| | ||
comments << Comment.new(comment['data']) | ||
end | ||
|
||
return comments | ||
end | ||
end | ||
end |
Oops, something went wrong.