diggr 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt ADDED
@@ -0,0 +1,6 @@
1
+ === 0.1.0 / 2008-08-26
2
+
3
+ * 1 major enhancement
4
+
5
+ * Diggr allows querying of the Digg API
6
+
data/Manifest.txt ADDED
@@ -0,0 +1,31 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.txt
4
+ Rakefile
5
+ lib/diggr.rb
6
+ lib/diggr/api.rb
7
+ lib/diggr/api_error.rb
8
+ lib/diggr/constants.rb
9
+ lib/diggr/json_parser.rb
10
+ lib/diggr/request.rb
11
+ lib/diggr/response_classes/comment.rb
12
+ lib/diggr/response_classes/container.rb
13
+ lib/diggr/response_classes/digg.rb
14
+ lib/diggr/response_classes/error.rb
15
+ lib/diggr/response_classes/medium.rb
16
+ lib/diggr/response_classes/photo.rb
17
+ lib/diggr/response_classes/story.rb
18
+ lib/diggr/response_classes/topic.rb
19
+ lib/diggr/response_classes/user.rb
20
+ test/test_api.rb
21
+ test/test_comment.rb
22
+ test/test_container.rb
23
+ test/test_digg.rb
24
+ test/test_error.rb
25
+ test/test_json_parser.rb
26
+ test/test_medium.rb
27
+ test/test_photo.rb
28
+ test/test_request.rb
29
+ test/test_story.rb
30
+ test/test_topic.rb
31
+ test/test_user.rb
data/README.txt ADDED
@@ -0,0 +1,113 @@
1
+ = diggr
2
+
3
+ * http://diggr.rubyforge.org
4
+
5
+ == DESCRIPTION:
6
+
7
+ Diggr is a ruby wrapper for the Digg API.
8
+
9
+ Diggr strives to remain consistent with the Digg API endpoints listed here:
10
+ http://apidoc.digg.com/CompleteList. Endpoints are created in Diggr with method calls.
11
+ Each node in an endpoint becomes a method call and each node which is an argument becomes
12
+ an argument to the previous method. As an example, the following endpoint
13
+
14
+ /user/{user name}
15
+
16
+ in which the user name is "johndoe" would be created with this Diggr call:
17
+
18
+ diggr.user("johndoe")
19
+
20
+ To send the request to the Digg API and retrieve the results of the call, Diggr requests are
21
+ terminated in one of two ways.
22
+
23
+ 1. Using the fetch method. By ending your request with the fetch method, your result will be
24
+ returned to you. If the request is singular, you will receive a single object as a
25
+ response. If the request is plural, you will receive a collection of objects stored in an
26
+ array.
27
+
28
+ 2. Using any Enumerable method. This works only on plural requests. In this case, it is
29
+ unnecessary to use the fetch method.
30
+
31
+ See the synopsis for examples of each of these types of calls.
32
+
33
+ Options such as count or offset can be set using the options method and providing a hash of
34
+ arguments. See synopsis for more information.
35
+
36
+ Note: In an effort to remain consistent with the Digg API, some method names do not follow
37
+ the ruby idiom of underscores. Although somewhat ugly, this allows a user read the Digg API
38
+ and understand the exact methods to call in Diggr to achieve their desired results.
39
+
40
+ == FEATURES/PROBLEMS:
41
+
42
+ * Diggr wraps the Digg API and returns both single elements and collections based on the request
43
+
44
+ == SYNOPSIS:
45
+
46
+ require 'rubygems'
47
+ require 'diggr'
48
+
49
+ diggr = Diggr::API.new
50
+
51
+ # retrieve a single user by user name and print the number of profile views
52
+ user = diggr.user("johndoe").fetch
53
+ puts user.profileviews
54
+
55
+ # iterator over the most recent 10 stories (default return size) and print their titles
56
+ diggr.stories.each do |story|
57
+ puts story.title
58
+ end
59
+
60
+ # print the title of the 3 most recent hot stories
61
+ diggr.stories.hot.options(:count => 3).each do |story|
62
+ puts story.title
63
+ end
64
+
65
+ # build an array of stories whos title contains "foo"
66
+ diggr.stories.inject([]) do |array,story|
67
+ array << story if story.title =~ /foo/
68
+ array
69
+ end
70
+
71
+ # print the title of the 2nd and 3rd most recent stories
72
+ diggr.stories.options(:count => 2, :offset => 2).each do |story|
73
+ puts story.title
74
+ end
75
+
76
+ == REQUIREMENTS:
77
+
78
+ * need 1.0.2 or greater
79
+ * json 1.1.3 or greater
80
+ * activesupport 2.1.0 or greater
81
+
82
+ == INSTALL:
83
+
84
+ * install from rubyforge (major releases)
85
+ * sudo gem install diggr
86
+
87
+ * install from github (major and minor releases)
88
+ * sudo gem install dfg59-diggr --source=http://gems.github.com
89
+
90
+ == LICENSE:
91
+
92
+ (The MIT License)
93
+
94
+ Copyright (c) 2008 FIX
95
+
96
+ Permission is hereby granted, free of charge, to any person obtaining
97
+ a copy of this software and associated documentation files (the
98
+ 'Software'), to deal in the Software without restriction, including
99
+ without limitation the rights to use, copy, modify, merge, publish,
100
+ distribute, sublicense, and/or sell copies of the Software, and to
101
+ permit persons to whom the Software is furnished to do so, subject to
102
+ the following conditions:
103
+
104
+ The above copyright notice and this permission notice shall be
105
+ included in all copies or substantial portions of the Software.
106
+
107
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
108
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
109
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
110
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
111
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
112
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
113
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'rubygems'
4
+ require 'hoe'
5
+ require 'need'
6
+ need { 'lib/diggr.rb' }
7
+
8
+ Hoe.new('diggr', Diggr::VERSION) do |p|
9
+ p.developer('Drew Olson', 'drew@drewolson.org')
10
+ p.extra_deps << ['need', '>= 1.0.2']
11
+ p.extra_deps << ['json', '>= 1.1.3']
12
+ p.extra_deps << ['activesupport', '>= 2.1.0']
13
+ p.remote_rdoc_dir = ''
14
+ end
15
+
16
+ # vim: syntax=Ruby
data/lib/diggr.rb ADDED
@@ -0,0 +1,6 @@
1
+ require 'need'
2
+ need { File.join('diggr','api') }
3
+
4
+ module Diggr
5
+ VERSION = '0.1.0'
6
+ end
data/lib/diggr/api.rb ADDED
@@ -0,0 +1,15 @@
1
+ require 'rubygems'
2
+ require 'need'
3
+ need { 'constants' }
4
+ need { 'request' }
5
+
6
+ module Diggr
7
+ class API
8
+ Diggr::Constants::VALID_ROOT_METHODS.each do |method|
9
+ define_method(method) do |*args|
10
+ request = Diggr::Request.new
11
+ request.send(method,*args)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1 @@
1
+ class APIError < StandardError; end
@@ -0,0 +1,36 @@
1
+ module Diggr
2
+ module Constants
3
+ VALID_ROOT_METHODS = [
4
+ :containers,
5
+ :container,
6
+ :errors,
7
+ :error,
8
+ :galleryphotos,
9
+ :galleryphoto,
10
+ :media,
11
+ :medium,
12
+ :stories,
13
+ :story,
14
+ :topic,
15
+ :topics,
16
+ :users,
17
+ :user
18
+ ]
19
+ RESPONSE_CLASSES = [
20
+ "containers",
21
+ "errors",
22
+ "diggs",
23
+ "comments",
24
+ "photos",
25
+ "media",
26
+ "stories",
27
+ "topics",
28
+ "users"
29
+ ]
30
+ HOST = "services.digg.com"
31
+ PORT = 80
32
+ USER_AGENT = 'diggr'
33
+ APP_KEY = 'http://diggr.rubyforge.org'
34
+ RESPONSE_TYPE = 'application/json'
35
+ end
36
+ end
@@ -0,0 +1,51 @@
1
+ require 'rubygems'
2
+ require 'active_support/inflector'
3
+ require 'json'
4
+ require 'need'
5
+ need { 'api_error' }
6
+ need { 'constants' }
7
+
8
+ module Diggr
9
+ class JSONParser
10
+ def parse(data)
11
+ build_objects_from_parsed_json(JSON.parse(data))
12
+ end
13
+
14
+ private
15
+
16
+ def build_objects_from_parsed_json(parsed_data)
17
+ if parsed_data.has_key?('code') && parsed_data['code'] == 404
18
+ raise APIError, "specified path returned a 404 error"
19
+ end
20
+
21
+ collection_type, collection_data = cleanse_json(parsed_data)
22
+
23
+ if collection_type.nil?
24
+ nil
25
+ else
26
+ build_result(collection_type, collection_data)
27
+ end
28
+ end
29
+
30
+ def build_result(collection_type, collection_data)
31
+ klass = find_class(collection_type)
32
+
33
+ object_collection = collection_data.inject([]) do |collection,object_data|
34
+ collection << klass.new_from_parsed_json(object_data)
35
+ end
36
+
37
+ object_collection.size == 1 ? object_collection.first : object_collection
38
+ end
39
+
40
+ def cleanse_json(parsed_data)
41
+ parsed_data.select { |key,val| Diggr::Constants::RESPONSE_CLASSES.include?(key) }[0]
42
+ end
43
+
44
+ def find_class(collection_type)
45
+ file_name = Inflector.singularize(collection_type)
46
+ need { File.join('response_classes',file_name) }
47
+ Inflector.constantize('Diggr::'+ Inflector.camelize(file_name))
48
+ end
49
+ end
50
+ end
51
+
@@ -0,0 +1,67 @@
1
+ require 'enumerator'
2
+ require 'cgi'
3
+ require 'net/http'
4
+ require 'rubygems'
5
+ require 'need'
6
+ need { 'constants' }
7
+ need { 'json_parser' }
8
+
9
+ module Diggr
10
+ class Request
11
+ include Enumerable
12
+
13
+ def initialize
14
+ @end_point = ''
15
+ @options = nil
16
+ end
17
+
18
+ def options(params)
19
+ @options = params.inject("") do |options,(key,val)|
20
+ options + "&#{key}=#{cleanse(val)}"
21
+ end
22
+ self
23
+ end
24
+
25
+ def method_missing(name,*args,&block)
26
+ @end_point += "/#{name}"
27
+ unless args.empty?
28
+ @end_point += "/#{args.join(',')}"
29
+ end
30
+ self
31
+ end
32
+
33
+ def each(&block)
34
+ fetch.each(&block)
35
+ end
36
+
37
+ def fetch
38
+ response = make_request
39
+ Diggr::JSONParser.new.parse(response)
40
+ end
41
+
42
+ private
43
+
44
+ def make_request
45
+ Net::HTTP.start(Diggr::Constants::HOST,Diggr::Constants::PORT) do |http|
46
+ http.get(
47
+ path,
48
+ 'User-Agent' => Diggr::Constants::USER_AGENT,
49
+ 'Accept' => Diggr::Constants::RESPONSE_TYPE
50
+ ).body
51
+ end
52
+ end
53
+
54
+ def cleanse(val)
55
+ if val.kind_of? String
56
+ val = CGI.escape(val)
57
+ end
58
+ val
59
+ end
60
+
61
+ def path
62
+ path = @end_point + "?appkey=#{cleanse(Diggr::Constants::APP_KEY)}"
63
+ path += @options if @options
64
+ path
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,15 @@
1
+ module Diggr
2
+ class Comment
3
+ attr_accessor :date, :id, :story, :up, :down, :replies, :replyto, :user, :content, :level, :root
4
+
5
+ def self.new_from_parsed_json(data)
6
+ comment = Comment.new
7
+
8
+ data.each do |key,val|
9
+ comment.send("#{key}=",val)
10
+ end
11
+
12
+ comment
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,27 @@
1
+ require 'rubygems'
2
+ require 'need'
3
+ need { 'topic' }
4
+
5
+ module Diggr
6
+ class Container
7
+ attr_accessor :name, :short_name, :topics
8
+
9
+ def initialize
10
+ self.topics = []
11
+ end
12
+
13
+ def self.new_from_parsed_json(data)
14
+ container = Container.new
15
+ container.name = data['name']
16
+ container.short_name = data['short_name']
17
+
18
+ if data.has_key? 'topics'
19
+ data['topics'].each do |topic_data|
20
+ container.topics << Topic.new_from_parsed_json(topic_data)
21
+ end
22
+ end
23
+
24
+ container
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,15 @@
1
+ module Diggr
2
+ class Digg
3
+ attr_accessor :date, :story, :id, :user, :status
4
+
5
+ def self.new_from_parsed_json(data)
6
+ digg = Digg.new
7
+
8
+ data.each do |key,val|
9
+ digg.send("#{key}=",val)
10
+ end
11
+
12
+ digg
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ module Diggr
2
+ class Error
3
+ attr_accessor :code, :message
4
+
5
+ def self.new_from_parsed_json(data)
6
+ error = Error.new
7
+
8
+ data.each do |key,val|
9
+ error.send("#{key}=",val)
10
+ end
11
+
12
+ error
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ module Diggr
2
+ class Medium
3
+ attr_accessor :name, :short_name
4
+
5
+ def self.new_from_parsed_json(data)
6
+ medium = Medium.new
7
+
8
+ data.each do |key,val|
9
+ medium.send("#{key}=",val)
10
+ end
11
+
12
+ medium
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,26 @@
1
+ require 'rubygems'
2
+ require 'need'
3
+ need { 'user' }
4
+
5
+ module Diggr
6
+ class Photo
7
+ attr_accessor :id, :submit_date, :comments, :title, :user, :src, :href, :originalheight, :originalwidth,
8
+ :height, :width, :contentType
9
+
10
+ def self.new_from_parsed_json(data)
11
+ photo = Photo.new
12
+
13
+ data.each do |key,val|
14
+ unless key == 'user'
15
+ photo.send("#{key}=",val)
16
+ end
17
+ end
18
+
19
+ if data.has_key? 'user'
20
+ photo.user = Diggr::User.new_from_parsed_json(data['user'])
21
+ end
22
+
23
+ photo
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,34 @@
1
+ require 'rubygems'
2
+ require 'need'
3
+ need { 'user' }
4
+ need { 'topic' }
5
+ need { 'container' }
6
+ need { 'photo' }
7
+
8
+ module Diggr
9
+ class Story
10
+ attr_accessor :id, :link, :submit_date, :diggs, :comments, :title, :description,
11
+ :status, :media, :user, :topic, :container, :thumbnail, :href
12
+
13
+ def self.new_from_parsed_json(data)
14
+ story = Story.new
15
+
16
+ story.id = data['id'] if data['id']
17
+ story.link = data['link'] if data['link']
18
+ story.submit_date = data['submit_date'] if data['submit_date']
19
+ story.diggs = data['diggs'] if data['diggs']
20
+ story.comments = data['comments'] if data['comments']
21
+ story.title = data['title'] if data['title']
22
+ story.description = data['description'] if data['description']
23
+ story.status = data['status'] if data['status']
24
+ story.media = data['media'] if data['media']
25
+ story.user = Diggr::User.new_from_parsed_json(data['user']) if data['user']
26
+ story.topic = Diggr::Topic.new_from_parsed_json(data['topic']) if data['topic']
27
+ story.container = Diggr::Container.new_from_parsed_json(data['container']) if data['container']
28
+ story.thumbnail = Diggr::Photo.new_from_parsed_json(data['thumbnail']) if data['thumbnail']
29
+ story.href = data['href'] if data['href']
30
+
31
+ story
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,21 @@
1
+ require 'rubygems'
2
+ require 'need'
3
+ need { 'container' }
4
+
5
+ module Diggr
6
+ class Topic
7
+ attr_accessor :name, :short_name, :container
8
+
9
+ def self.new_from_parsed_json(data)
10
+ topic = Topic.new
11
+ topic.name = data['name']
12
+ topic.short_name = data['short_name']
13
+
14
+ if data.has_key? 'container'
15
+ topic.container = Diggr::Container.new_from_parsed_json(data['container'])
16
+ end
17
+
18
+ topic
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,15 @@
1
+ module Diggr
2
+ class User
3
+ attr_accessor :name, :fullname, :icon, :registered, :profileviews, :links
4
+
5
+ def self.new_from_parsed_json(data)
6
+ user = User.new
7
+
8
+ data.each do |key,val|
9
+ user.send("#{key}=",val)
10
+ end
11
+
12
+ user
13
+ end
14
+ end
15
+ end
data/test/test_api.rb ADDED
@@ -0,0 +1,44 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'need'
4
+ need { '../lib/diggr/api' }
5
+ need { '../lib/diggr/request' }
6
+ need { '../lib/diggr/constants' }
7
+
8
+ class TestDigg < Test::Unit::TestCase
9
+ def test_instantiate_class
10
+ assert_nothing_raised do
11
+ Diggr::API.new
12
+ end
13
+ end
14
+
15
+ def test_valid_root_method_returns_a_request_instance
16
+ digg = Diggr::API.new
17
+
18
+ Diggr::Constants::VALID_ROOT_METHODS.each do |method|
19
+ assert_instance_of Diggr::Request, digg.send(method)
20
+ end
21
+ end
22
+
23
+ def test_invalid_root_method_throws_error
24
+ digg = Diggr::API.new
25
+
26
+ assert_raises NoMethodError do
27
+ digg.foo
28
+ end
29
+ end
30
+
31
+ def test_valid_root_method_returns_request_with_corrent_endpoint
32
+ digg = Diggr::API.new
33
+ request = digg.media
34
+
35
+ assert_equal "/media", request.send(:instance_variable_get,"@end_point")
36
+ end
37
+
38
+ def test_valid_root_method_with_args_returns_request_with_corrent_endpoint
39
+ digg = Diggr::API.new
40
+ request = digg.media('123')
41
+
42
+ assert_equal "/media/123", request.send(:instance_variable_get,"@end_point")
43
+ end
44
+ end
@@ -0,0 +1,34 @@
1
+ require 'test/unit'
2
+ require 'rubygems'
3
+ require 'need'
4
+ need { '../lib/diggr/response_classes/comment' }
5
+
6
+ class TestComment < Test::Unit::TestCase
7
+ def test_instantiation
8
+ assert_nothing_raised do
9
+ Diggr::Comment.new
10
+ end
11
+ end
12
+
13
+ def test_new_from_parsed_json_data
14
+ parsed_json_data = {
15
+ 'date' => '07292008',
16
+ 'id' => '2',
17
+ 'story' => '1',
18
+ 'up' => '2',
19
+ 'down' => '3',
20
+ 'replies' => '5',
21
+ 'replyto' => 'null',
22
+ 'user' => 'johndoe',
23
+ 'content' => 'this is a comment',
24
+ 'level' => '0',
25
+ 'root' => '0'
26
+ }
27
+
28
+ comment = Diggr::Comment.new_from_parsed_json(parsed_json_data)
29
+
30
+ parsed_json_data.each do |key,val|
31
+ assert_equal val, comment.send(key)
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,35 @@
1
+ require 'test/unit'
2
+ require 'rubygems'
3
+ require 'need'
4
+ need { '../lib/diggr/response_classes/container' }
5
+
6
+ class TestContainer < Test::Unit::TestCase
7
+ def test_instantiation
8
+ assert_nothing_raised do
9
+ Diggr::Container.new
10
+ end
11
+ end
12
+
13
+ def test_new_from_parsed_json
14
+ parsed_json_data = {
15
+ 'name' => 'Test Container',
16
+ 'short_name' => 'test',
17
+ 'topics' => [
18
+ {
19
+ 'name' => 'Test Topic',
20
+ 'short_name' => 'test'
21
+ }
22
+ ]
23
+ }
24
+
25
+ container = Diggr::Container.new_from_parsed_json(parsed_json_data)
26
+
27
+ parsed_json_data.each do |key,val|
28
+ if key == 'topics'
29
+ assert_instance_of Array, container.topics
30
+ else
31
+ assert_equal val, container.send(key)
32
+ end
33
+ end
34
+ end
35
+ end
data/test/test_digg.rb ADDED
@@ -0,0 +1,28 @@
1
+ require 'test/unit'
2
+ require 'rubygems'
3
+ require 'need'
4
+ need { '../lib/diggr/response_classes/digg' }
5
+
6
+ class TestDigg < Test::Unit::TestCase
7
+ def test_instantiation
8
+ assert_nothing_raised do
9
+ Diggr::Digg.new
10
+ end
11
+ end
12
+
13
+ def test_new_from_parsed_json_data
14
+ parsed_json_data = {
15
+ 'date' => '07292008',
16
+ 'story' => '1',
17
+ 'id' => '2',
18
+ 'user' => 'johndoe',
19
+ 'status' => 'popular'
20
+ }
21
+
22
+ digg = Diggr::Digg.new_from_parsed_json(parsed_json_data)
23
+
24
+ parsed_json_data.each do |key,val|
25
+ assert_equal val, digg.send(key)
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,25 @@
1
+ require 'test/unit'
2
+ require 'rubygems'
3
+ require 'need'
4
+ need { '../lib/diggr/response_classes/error' }
5
+
6
+ class TestError < Test::Unit::TestCase
7
+ def test_instantiation
8
+ assert_nothing_raised do
9
+ Diggr::Error.new
10
+ end
11
+ end
12
+
13
+ def test_new_from_parsed_json
14
+ parsed_json_data = {
15
+ 'code' => '1234',
16
+ 'message' => 'test',
17
+ }
18
+
19
+ error = Diggr::Error.new_from_parsed_json(parsed_json_data)
20
+
21
+ parsed_json_data.each do |key,val|
22
+ assert_equal val, error.send(key)
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,25 @@
1
+ require 'test/unit'
2
+ require 'rubygems'
3
+ require 'need'
4
+ need { '../lib/diggr/json_parser' }
5
+ need { '../lib/diggr/response_classes/topic' }
6
+
7
+ class TestJSONParser < Test::Unit::TestCase
8
+ def test_find_class
9
+ parser = Diggr::JSONParser.new
10
+
11
+ assert_equal Diggr::Topic, parser.send(:find_class, 'topics')
12
+ end
13
+
14
+ def test_cleanse_json
15
+ parser = Diggr::JSONParser.new
16
+
17
+ data = {
18
+ "stories" => { :test => "foo" },
19
+ "foo" => "bar",
20
+ "baz" => "boom"
21
+ }
22
+
23
+ assert_equal(["stories", { :test => "foo" }], parser.send(:cleanse_json, data))
24
+ end
25
+ end
@@ -0,0 +1,25 @@
1
+ require 'test/unit'
2
+ require 'rubygems'
3
+ require 'need'
4
+ need { '../lib/diggr/response_classes/medium' }
5
+
6
+ class TestMedium < Test::Unit::TestCase
7
+ def test_instantiation
8
+ assert_nothing_raised do
9
+ Diggr::Medium.new
10
+ end
11
+ end
12
+
13
+ def test_new_from_parsed_json_data
14
+ parsed_json_data = {
15
+ 'name' => 'Test Name',
16
+ 'short_name' => 'test'
17
+ }
18
+
19
+ digg = Diggr::Medium.new_from_parsed_json(parsed_json_data)
20
+
21
+ parsed_json_data.each do |key,val|
22
+ assert_equal val, digg.send(key)
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,39 @@
1
+ require 'test/unit'
2
+ require 'rubygems'
3
+ require 'need'
4
+ need { '../lib/diggr/response_classes/photo' }
5
+
6
+ class TestPhoto < Test::Unit::TestCase
7
+ def test_instantiation
8
+ assert_nothing_raised do
9
+ Diggr::Photo.new
10
+ end
11
+ end
12
+
13
+ def test_new_from_parsed_json
14
+ parsed_json_data = {
15
+ 'id' => '1',
16
+ 'submit_date' => '12345',
17
+ 'comments' => '5',
18
+ 'title' => 'Test Photo',
19
+ 'user' => {
20
+ 'name' => 'johndoe',
21
+ 'icon' => 'http://www.gravitar.com',
22
+ 'registered' => '1234567',
23
+ 'profileviews' => '13',
24
+ },
25
+ 'src' => 'http://www.digg.com/some/stuff/here',
26
+ 'href' => 'http://www.digg.com/more/stuff/here'
27
+ }
28
+
29
+ photo = Diggr::Photo.new_from_parsed_json(parsed_json_data)
30
+
31
+ parsed_json_data.each do |key,val|
32
+ if key == 'user'
33
+ assert_instance_of Diggr::User, photo.user
34
+ else
35
+ assert_equal val, photo.send(key)
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,111 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'mocha'
4
+ require 'need'
5
+ need { '../lib/diggr/request' }
6
+ need { '../lib/diggr/response_classes/story' }
7
+ need { '../lib/diggr/constants' }
8
+ need { '../lib/diggr/json_parser' }
9
+
10
+ class TestRequest < Test::Unit::TestCase
11
+ def test_request_instantiation
12
+ assert_nothing_raised do
13
+ Diggr::Request.new
14
+ end
15
+ end
16
+
17
+ def test_simple_end_point
18
+ request = Diggr::Request.new
19
+ request.media
20
+
21
+ assert_equal '/media', request.send(:instance_variable_get,"@end_point")
22
+ end
23
+
24
+ def test_simple_end_point_with_args
25
+ request = Diggr::Request.new
26
+ request.error('123')
27
+
28
+ assert_equal '/error/123', request.send(:instance_variable_get,"@end_point")
29
+ end
30
+
31
+ def test_complex_end_point
32
+ request = Diggr::Request.new
33
+ request.stories.popular
34
+
35
+ assert_equal '/stories/popular', request.send(:instance_variable_get,"@end_point")
36
+ end
37
+
38
+ def test_complex_end_point_with_args
39
+ request = Diggr::Request.new
40
+ request.stories.topic("technology").hot
41
+
42
+ assert_equal '/stories/topic/technology/hot', request.send(:instance_variable_get,"@end_point")
43
+ end
44
+
45
+ def test_request_with_options
46
+ request = Diggr::Request.new
47
+ request.topics.options(:count => 5, :offset => 3)
48
+
49
+ assert_equal '&count=5&offset=3', request.send(:instance_variable_get,"@options")
50
+ end
51
+
52
+ def test_cleanse_string_with_forward_slash
53
+ request = Diggr::Request.new
54
+
55
+ assert_equal '%2F', request.send(:cleanse,"/")
56
+ end
57
+
58
+ def test_cleanse_number
59
+ request = Diggr::Request.new
60
+
61
+ assert_equal 2, request.send(:cleanse,2)
62
+ end
63
+
64
+ def test_simple_path
65
+ request = Diggr::Request.new
66
+ request.stories
67
+
68
+ assert_equal '/stories?appkey=http%3A%2F%2Fdiggr.rubyforge.org', request.send(:path)
69
+ end
70
+
71
+ def test_simple_path_with_options
72
+ request = Diggr::Request.new
73
+ request.stories.options(:count => 3)
74
+
75
+ assert_equal '/stories?appkey=http%3A%2F%2Fdiggr.rubyforge.org&count=3', request.send(:path)
76
+ end
77
+
78
+ def test_complex_path
79
+ request = Diggr::Request.new
80
+ request.story(1).diggs
81
+
82
+ assert_equal '/story/1/diggs?appkey=http%3A%2F%2Fdiggr.rubyforge.org', request.send(:path)
83
+ end
84
+
85
+ def test_comma_seperated_args_product_correct_endpoint
86
+ request = Diggr::Request.new
87
+ request.stories(1,2,3,4)
88
+
89
+ assert_equal '/stories/1,2,3,4', request.send(:instance_variable_get,"@end_point")
90
+ end
91
+
92
+ def test_fetch
93
+ story = Diggr::Story.new
94
+ Diggr::JSONParser.any_instance.stubs(:parse).with(story).returns(story)
95
+ request = Diggr::Request.new
96
+ request.stubs(:make_request).returns(story)
97
+
98
+ assert_equal story, request.fetch
99
+ end
100
+
101
+ def test_each
102
+ stories = [Diggr::Story.new,Diggr::Story.new]
103
+ Diggr::JSONParser.any_instance.stubs(:parse).with(stories).returns(stories)
104
+ request = Diggr::Request.new
105
+ request.stubs(:make_request).returns(stories)
106
+
107
+ request.each do |item|
108
+ assert_instance_of Diggr::Story, item
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,57 @@
1
+ require 'test/unit'
2
+ require 'rubygems'
3
+ require 'need'
4
+ need { '../lib/diggr/response_classes/story' }
5
+
6
+ class TestStory < Test::Unit::TestCase
7
+ def test_instantiation
8
+ assert_nothing_raised do
9
+ Diggr::Story.new
10
+ end
11
+ end
12
+
13
+ def test_new_from_parsed_json
14
+ parsed_json_data = {
15
+ "id" => "4368401",
16
+ "link" => "http://maxsangalli.altervista.org/?p=45",
17
+ "submit_date" => 1196891534,
18
+ "diggs" => 1,
19
+ "comments" => 0,
20
+ "title" => "Jukebox con Linux",
21
+ "description" => "Jukebox with Linux",
22
+ "status" => "upcoming",
23
+ "media" => "news",
24
+ "user" => {
25
+ "name" => "ilsanga",
26
+ "icon" => "http://digg.com/img/udl.png",
27
+ "registered" => 1196891377,
28
+ "profileviews" => 0
29
+ },
30
+ "topic" => {
31
+ "name" => "Linux/Unix",
32
+ "short_name" => "linux_unix"
33
+ },
34
+ "container" => {
35
+ "name" => "Technology",
36
+ "short_name" => "technology"
37
+ },
38
+ "thumbnail" => {
39
+ "originalwidth" => 390,
40
+ "originalheight" => 387,
41
+ "contentType" => "image/jpeg",
42
+ "src" => "http://digg.com/linux_unix/Jukebox_con_Linux/t.jpg",
43
+ "width" => 80,
44
+ "height" => 80
45
+ },
46
+ "href" => "http://digg.com/linux_unix/Jukebox_con_Linux"
47
+ }
48
+
49
+ photo = Diggr::Story.new_from_parsed_json(parsed_json_data)
50
+
51
+ parsed_json_data.each do |key,val|
52
+ unless val.kind_of? Hash
53
+ assert_equal val, photo.send(key)
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,34 @@
1
+ require 'test/unit'
2
+ require 'rubygems'
3
+ require 'need'
4
+ need { '../lib/diggr/response_classes/topic' }
5
+ need { '../lib/diggr/response_classes/container' }
6
+
7
+ class TestTopic < Test::Unit::TestCase
8
+ def test_instantiation
9
+ assert_nothing_raised do
10
+ Diggr::Topic.new
11
+ end
12
+ end
13
+
14
+ def test_new_from_parsed_json
15
+ parsed_json_data = {
16
+ 'name' => 'Test Topic',
17
+ 'short_name' => 'test',
18
+ 'container' => {
19
+ 'name' => 'Test Container',
20
+ 'short_name' => 'test'
21
+ }
22
+ }
23
+
24
+ topic = Diggr::Topic.new_from_parsed_json(parsed_json_data)
25
+
26
+ parsed_json_data.each do |key,val|
27
+ if key == 'container'
28
+ assert_instance_of Diggr::Container, topic.container
29
+ else
30
+ assert_equal val, topic.send(key)
31
+ end
32
+ end
33
+ end
34
+ end
data/test/test_user.rb ADDED
@@ -0,0 +1,27 @@
1
+ require 'test/unit'
2
+ require 'rubygems'
3
+ require 'need'
4
+ need { '../lib/diggr/response_classes/user' }
5
+
6
+ class TestUser < Test::Unit::TestCase
7
+ def test_instantiated
8
+ assert_nothing_raised do
9
+ Diggr::User.new
10
+ end
11
+ end
12
+
13
+ def test_new_from_parsed_json
14
+ parsed_json_data = {
15
+ 'name' => 'johndoe',
16
+ 'icon' => 'http://www.gravitar.com',
17
+ 'registered' => '1234567',
18
+ 'profileviews' => '13',
19
+ }
20
+
21
+ user = Diggr::User.new_from_parsed_json(parsed_json_data)
22
+
23
+ parsed_json_data.each do |key,val|
24
+ assert_equal val, user.send(key)
25
+ end
26
+ end
27
+ end
metadata ADDED
@@ -0,0 +1,137 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: diggr
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Drew Olson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-09-05 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: need
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.0.2
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: json
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.1.3
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: activesupport
37
+ type: :runtime
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 2.1.0
44
+ version:
45
+ - !ruby/object:Gem::Dependency
46
+ name: hoe
47
+ type: :development
48
+ version_requirement:
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: 1.7.0
54
+ version:
55
+ description: "Diggr is a ruby wrapper for the Digg API. Diggr strives to remain consistent with the Digg API endpoints listed here: http://apidoc.digg.com/CompleteList. Endpoints are created in Diggr with method calls. Each node in an endpoint becomes a method call and each node which is an argument becomes an argument to the previous method. As an example, the following endpoint /user/{user name} in which the user name is \"johndoe\" would be created with this Diggr call: diggr.user(\"johndoe\") To send the request to the Digg API and retrieve the results of the call, Diggr requests are terminated in one of two ways. 1. Using the fetch method. By ending your request with the fetch method, your result will be returned to you. If the request is singular, you will receive a single object as a response. If the request is plural, you will receive a collection of objects stored in an array. 2. Using any Enumerable method. This works only on plural requests. In this case, it is unnecessary to use the fetch method. See the synopsis for examples of each of these types of calls. Options such as count or offset can be set using the options method and providing a hash of arguments. See synopsis for more information. Note: In an effort to remain consistent with the Digg API, some method names do not follow the ruby idiom of underscores. Although somewhat ugly, this allows a user read the Digg API and understand the exact methods to call in Diggr to achieve their desired results."
56
+ email:
57
+ - drew@drewolson.org
58
+ executables: []
59
+
60
+ extensions: []
61
+
62
+ extra_rdoc_files:
63
+ - History.txt
64
+ - Manifest.txt
65
+ - README.txt
66
+ files:
67
+ - History.txt
68
+ - Manifest.txt
69
+ - README.txt
70
+ - Rakefile
71
+ - lib/diggr.rb
72
+ - lib/diggr/api.rb
73
+ - lib/diggr/api_error.rb
74
+ - lib/diggr/constants.rb
75
+ - lib/diggr/json_parser.rb
76
+ - lib/diggr/request.rb
77
+ - lib/diggr/response_classes/comment.rb
78
+ - lib/diggr/response_classes/container.rb
79
+ - lib/diggr/response_classes/digg.rb
80
+ - lib/diggr/response_classes/error.rb
81
+ - lib/diggr/response_classes/medium.rb
82
+ - lib/diggr/response_classes/photo.rb
83
+ - lib/diggr/response_classes/story.rb
84
+ - lib/diggr/response_classes/topic.rb
85
+ - lib/diggr/response_classes/user.rb
86
+ - test/test_api.rb
87
+ - test/test_comment.rb
88
+ - test/test_container.rb
89
+ - test/test_digg.rb
90
+ - test/test_error.rb
91
+ - test/test_json_parser.rb
92
+ - test/test_medium.rb
93
+ - test/test_photo.rb
94
+ - test/test_request.rb
95
+ - test/test_story.rb
96
+ - test/test_topic.rb
97
+ - test/test_user.rb
98
+ has_rdoc: true
99
+ homepage: http://diggr.rubyforge.org
100
+ post_install_message:
101
+ rdoc_options:
102
+ - --main
103
+ - README.txt
104
+ require_paths:
105
+ - lib
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: "0"
111
+ version:
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: "0"
117
+ version:
118
+ requirements: []
119
+
120
+ rubyforge_project: diggr
121
+ rubygems_version: 1.2.0
122
+ signing_key:
123
+ specification_version: 2
124
+ summary: Diggr is a ruby wrapper for the Digg API
125
+ test_files:
126
+ - test/test_api.rb
127
+ - test/test_comment.rb
128
+ - test/test_container.rb
129
+ - test/test_digg.rb
130
+ - test/test_error.rb
131
+ - test/test_json_parser.rb
132
+ - test/test_medium.rb
133
+ - test/test_photo.rb
134
+ - test/test_request.rb
135
+ - test/test_story.rb
136
+ - test/test_topic.rb
137
+ - test/test_user.rb