Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
bterlson committed May 22, 2008
0 parents commit 0a537c9
Show file tree
Hide file tree
Showing 26 changed files with 585 additions and 0 deletions.
4 changes: 4 additions & 0 deletions History.txt
@@ -0,0 +1,4 @@
== 0.1.0 2008-05-22

* 1 major enhancement:
* Initial release
20 changes: 20 additions & 0 deletions License.txt
@@ -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.
29 changes: 29 additions & 0 deletions Manifest.txt
@@ -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
7 changes: 7 additions & 0 deletions PostInstall.txt
@@ -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.


48 changes: 48 additions & 0 deletions README.txt
@@ -0,0 +1,48 @@
= reddit

* 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.
22 changes: 22 additions & 0 deletions Rakefile
@@ -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 }
14 changes: 14 additions & 0 deletions lib/reddit.rb
@@ -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'
44 changes: 44 additions & 0 deletions lib/reddit/article.rb
@@ -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
33 changes: 33 additions & 0 deletions lib/reddit/comment.rb
@@ -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
31 changes: 31 additions & 0 deletions lib/reddit/comment_list.rb
@@ -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
25 changes: 25 additions & 0 deletions lib/reddit/reddit.rb
@@ -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
27 changes: 27 additions & 0 deletions lib/reddit/resource_list.rb
@@ -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
41 changes: 41 additions & 0 deletions lib/reddit/session.rb
@@ -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
25 changes: 25 additions & 0 deletions lib/reddit/user.rb
@@ -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

0 comments on commit 0a537c9

Please sign in to comment.