drewolson-diggr 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt ADDED
@@ -0,0 +1,24 @@
1
+ === 0.1.3 / 2008-09-09
2
+
3
+ * 1 minor bug-fix
4
+
5
+ * Added more attributes to user class
6
+
7
+ === 0.1.2 / 2008-09-09
8
+
9
+ * 1 minor bug-fix
10
+
11
+ * Singular responses work with Enumerable methods
12
+
13
+ === 0.1.1 / 2008-09-08
14
+
15
+ * 1 major bug-fix
16
+
17
+ * Updated to work with activesupport 2.1.1
18
+
19
+ === 0.1.0 / 2008-08-26
20
+
21
+ * 1 major enhancement
22
+
23
+ * Diggr allows querying of the Digg API
24
+
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/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'rubygems'
4
+ require 'need'
5
+ require 'rake/testtask'
6
+ need { 'lib/diggr.rb' }
7
+
8
+ Rake::TestTask.new do |t|
9
+ t.test_files = FileList['test/**/test_*.rb']
10
+ end
11
+
12
+ # 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.2'
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'
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
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 = collection_type.singularize
46
+ need { File.join('response_classes',file_name) }
47
+ ('Diggr::' + file_name.camelize).constantize
48
+ end
49
+ end
50
+ end
51
+
@@ -0,0 +1,71 @@
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 = ''
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
+ result = fetch
35
+
36
+ if result.kind_of? Array
37
+ fetch.each(&block)
38
+ else
39
+ yield result
40
+ end
41
+ end
42
+
43
+ def fetch
44
+ response = make_request
45
+ Diggr::JSONParser.new.parse(response)
46
+ end
47
+
48
+ private
49
+
50
+ def make_request
51
+ Net::HTTP.start(Diggr::Constants::HOST,Diggr::Constants::PORT) do |http|
52
+ http.get(
53
+ path,
54
+ 'User-Agent' => Diggr::Constants::USER_AGENT,
55
+ 'Accept' => Diggr::Constants::RESPONSE_TYPE
56
+ ).body
57
+ end
58
+ end
59
+
60
+ def cleanse(val)
61
+ if val.kind_of? String
62
+ val = CGI.escape(val)
63
+ end
64
+ val
65
+ end
66
+
67
+ def path
68
+ @end_point + "?appkey=#{cleanse(Diggr::Constants::APP_KEY)}" + @options
69
+ end
70
+ end
71
+ 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,28 @@
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
+ %w(id link submit_date diggs comments title description status media href).each do |attribute|
17
+ story.send("#{attribute}=",data[attribute]) if data[attribute]
18
+ end
19
+
20
+ story.user = Diggr::User.new_from_parsed_json(data['user']) if data['user']
21
+ story.topic = Diggr::Topic.new_from_parsed_json(data['topic']) if data['topic']
22
+ story.container = Diggr::Container.new_from_parsed_json(data['container']) if data['container']
23
+ story.thumbnail = Diggr::Photo.new_from_parsed_json(data['thumbnail']) if data['thumbnail']
24
+
25
+ story
26
+ end
27
+ end
28
+ 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, :mutual, :date
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,122 @@
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
+
112
+ def test_each_with_singular_response
113
+ story = Diggr::Story.new
114
+ Diggr::JSONParser.any_instance.stubs(:parse).with(story).returns(story)
115
+ request = Diggr::Request.new
116
+ request.stubs(:make_request).returns(story)
117
+
118
+ request.each do |item|
119
+ assert_instance_of Diggr::Story, item
120
+ end
121
+ end
122
+ 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,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: drewolson-diggr
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.3
5
+ platform: ruby
6
+ authors:
7
+ - Drew Olson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-12-02 00:00:00 -08: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.1
44
+ version:
45
+ 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. 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 to read the Digg API and understand the exact methods to call in Diggr to achieve their desired results."
46
+ email:
47
+ - drew@drewolson.org
48
+ executables: []
49
+
50
+ extensions: []
51
+
52
+ extra_rdoc_files: []
53
+
54
+ files:
55
+ - History.txt
56
+ - Manifest.txt
57
+ - Rakefile
58
+ - lib/diggr.rb
59
+ - lib/diggr/api.rb
60
+ - lib/diggr/api_error.rb
61
+ - lib/diggr/constants.rb
62
+ - lib/diggr/json_parser.rb
63
+ - lib/diggr/request.rb
64
+ - lib/diggr/response_classes/comment.rb
65
+ - lib/diggr/response_classes/container.rb
66
+ - lib/diggr/response_classes/digg.rb
67
+ - lib/diggr/response_classes/error.rb
68
+ - lib/diggr/response_classes/medium.rb
69
+ - lib/diggr/response_classes/photo.rb
70
+ - lib/diggr/response_classes/story.rb
71
+ - lib/diggr/response_classes/topic.rb
72
+ - lib/diggr/response_classes/user.rb
73
+ - test/test_api.rb
74
+ - test/test_comment.rb
75
+ - test/test_container.rb
76
+ - test/test_digg.rb
77
+ - test/test_error.rb
78
+ - test/test_json_parser.rb
79
+ - test/test_medium.rb
80
+ - test/test_photo.rb
81
+ - test/test_request.rb
82
+ - test/test_story.rb
83
+ - test/test_topic.rb
84
+ - test/test_user.rb
85
+ has_rdoc: true
86
+ homepage: http://github.com/drewolson/diggr
87
+ post_install_message:
88
+ rdoc_options:
89
+ - --main
90
+ - README.txt
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: "0"
98
+ version:
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: "0"
104
+ version:
105
+ requirements: []
106
+
107
+ rubyforge_project: diggr
108
+ rubygems_version: 1.2.0
109
+ signing_key:
110
+ specification_version: 2
111
+ summary: Diggr is a ruby wrapper for the Digg API
112
+ test_files:
113
+ - test/test_api.rb
114
+ - test/test_comment.rb
115
+ - test/test_container.rb
116
+ - test/test_digg.rb
117
+ - test/test_error.rb
118
+ - test/test_json_parser.rb
119
+ - test/test_medium.rb
120
+ - test/test_photo.rb
121
+ - test/test_request.rb
122
+ - test/test_story.rb
123
+ - test/test_topic.rb
124
+ - test/test_user.rb