rramsden-diggr 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt ADDED
@@ -0,0 +1,36 @@
1
+ === 0.1.6 / 2009-06-26
2
+
3
+ * 1 minor feature
4
+
5
+ * Added support for providing your own app key
6
+
7
+ === 0.1.5 / 2009-06-25
8
+
9
+ * 1 minor feature
10
+
11
+ * Added support for short urls
12
+
13
+ === 0.1.3 / 2008-09-09
14
+
15
+ * 1 minor bug-fix
16
+
17
+ * Added more attributes to user class
18
+
19
+ === 0.1.2 / 2008-09-09
20
+
21
+ * 1 minor bug-fix
22
+
23
+ * Singular responses work with Enumerable methods
24
+
25
+ === 0.1.1 / 2008-09-08
26
+
27
+ * 1 major bug-fix
28
+
29
+ * Updated to work with activesupport 2.1.1
30
+
31
+ === 0.1.0 / 2008-08-26
32
+
33
+ * 1 major enhancement
34
+
35
+ * Diggr allows querying of the Digg API
36
+
data/Manifest.txt ADDED
@@ -0,0 +1,33 @@
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
+ lib/diggr/response_classes/short_url.rb
21
+ test/test_api.rb
22
+ test/test_comment.rb
23
+ test/test_container.rb
24
+ test/test_digg.rb
25
+ test/test_error.rb
26
+ test/test_json_parser.rb
27
+ test/test_medium.rb
28
+ test/test_photo.rb
29
+ test/test_request.rb
30
+ test/test_story.rb
31
+ test/test_topic.rb
32
+ test/test_user.rb
33
+ test/test_short_url.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/api.rb ADDED
@@ -0,0 +1,19 @@
1
+ require 'rubygems'
2
+ require 'need'
3
+ need { 'constants' }
4
+ need { 'request' }
5
+
6
+ module Diggr
7
+ class API
8
+ def initialize(app_key = Diggr::Constants::APP_KEY)
9
+ @app_key = app_key
10
+ end
11
+
12
+ Diggr::Constants::VALID_ROOT_METHODS.each do |method|
13
+ define_method(method) do |*args|
14
+ request = Diggr::Request.new(@app_key)
15
+ request.send(method,*args)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1 @@
1
+ class APIError < StandardError; end
@@ -0,0 +1,39 @@
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
+ :shorturl,
13
+ :shorturls,
14
+ :stories,
15
+ :story,
16
+ :topic,
17
+ :topics,
18
+ :users,
19
+ :user
20
+ ]
21
+ RESPONSE_CLASSES = [
22
+ "containers",
23
+ "errors",
24
+ "diggs",
25
+ "comments",
26
+ "photos",
27
+ "media",
28
+ "shorturls",
29
+ "stories",
30
+ "topics",
31
+ "users"
32
+ ]
33
+ HOST = "services.digg.com"
34
+ PORT = 80
35
+ USER_AGENT = 'diggr'
36
+ APP_KEY = 'http://diggr.rubyforge.org'
37
+ RESPONSE_TYPE = 'application/json'
38
+ end
39
+ 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,72 @@
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(app_key)
14
+ @end_point = ''
15
+ @options = ''
16
+ @app_key = app_key
17
+ end
18
+
19
+ def options(params)
20
+ @options = params.inject("") do |options,(key,val)|
21
+ options + "&#{key}=#{cleanse(val)}"
22
+ end
23
+ self
24
+ end
25
+
26
+ def method_missing(name,*args,&block)
27
+ @end_point += "/#{name}"
28
+ unless args.empty?
29
+ @end_point += "/#{args.join(',')}"
30
+ end
31
+ self
32
+ end
33
+
34
+ def each(&block)
35
+ result = fetch
36
+
37
+ if result.kind_of? Array
38
+ fetch.each(&block)
39
+ else
40
+ yield result
41
+ end
42
+ end
43
+
44
+ def fetch
45
+ response = make_request
46
+ Diggr::JSONParser.new.parse(response)
47
+ end
48
+
49
+ private
50
+
51
+ def make_request
52
+ Net::HTTP.start(Diggr::Constants::HOST,Diggr::Constants::PORT) do |http|
53
+ http.get(
54
+ path,
55
+ 'User-Agent' => Diggr::Constants::USER_AGENT,
56
+ 'Accept' => Diggr::Constants::RESPONSE_TYPE
57
+ ).body
58
+ end
59
+ end
60
+
61
+ def cleanse(val)
62
+ if val.kind_of? String
63
+ val = CGI.escape(val)
64
+ end
65
+ val
66
+ end
67
+
68
+ def path
69
+ @end_point + "?appkey=#{cleanse(@app_key)}" + @options
70
+ end
71
+ end
72
+ 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,12 @@
1
+ module Diggr
2
+ class ShortUrl
3
+ attr_accessor :short_url, :views
4
+
5
+ def self.new_from_parsed_json(data)
6
+ short_url = ShortUrl.new
7
+ short_url.short_url = data['short_url']
8
+ short_url.views = data['views']
9
+ short_url
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,30 @@
1
+ require 'rubygems'
2
+ require 'need'
3
+ need { 'user' }
4
+ need { 'topic' }
5
+ need { 'container' }
6
+ need { 'photo' }
7
+ need { 'short_url' }
8
+
9
+ module Diggr
10
+ class Story
11
+ attr_accessor :id, :link, :submit_date, :diggs, :comments, :title, :description,
12
+ :status, :media, :user, :topic, :container, :thumbnail, :href, :shorturl
13
+
14
+ def self.new_from_parsed_json(data)
15
+ story = Story.new
16
+
17
+ %w(id link submit_date diggs comments title description status media href).each do |attribute|
18
+ story.send("#{attribute}=",data[attribute]) if data[attribute]
19
+ end
20
+
21
+ story.user = Diggr::User.new_from_parsed_json(data['user']) if data['user']
22
+ story.topic = Diggr::Topic.new_from_parsed_json(data['topic']) if data['topic']
23
+ story.container = Diggr::Container.new_from_parsed_json(data['container']) if data['container']
24
+ story.thumbnail = Diggr::Photo.new_from_parsed_json(data['thumbnail']) if data['thumbnail']
25
+ story.shorturl = Diggr::ShortUrl.new_from_parsed_json(data['shorturl'][0]) if data['shorturl']
26
+
27
+ story
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,15 @@
1
+ require 'rubygems'
2
+ require 'need'
3
+
4
+ module Diggr
5
+ class Topic
6
+ attr_accessor :name, :short_name
7
+
8
+ def self.new_from_parsed_json(data)
9
+ topic = Topic.new
10
+ topic.name = data['name']
11
+ topic.short_name = data['short_name']
12
+ topic
13
+ end
14
+ end
15
+ 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/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/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,123 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'mocha'
4
+ require 'need'
5
+ need { '../lib/diggr/request' }
6
+ need { '../lib/diggr/constants' }
7
+ need { '../lib/diggr/response_classes/story' }
8
+ need { '../lib/diggr/constants' }
9
+ need { '../lib/diggr/json_parser' }
10
+
11
+ class TestRequest < Test::Unit::TestCase
12
+ def test_request_instantiation
13
+ assert_nothing_raised do
14
+ Diggr::Request.new(Diggr::Constants::APP_KEY)
15
+ end
16
+ end
17
+
18
+ def test_simple_end_point
19
+ request = Diggr::Request.new(Diggr::Constants::APP_KEY)
20
+ request.media
21
+
22
+ assert_equal '/media', request.send(:instance_variable_get,"@end_point")
23
+ end
24
+
25
+ def test_simple_end_point_with_args
26
+ request = Diggr::Request.new(Diggr::Constants::APP_KEY)
27
+ request.error('123')
28
+
29
+ assert_equal '/error/123', request.send(:instance_variable_get,"@end_point")
30
+ end
31
+
32
+ def test_complex_end_point
33
+ request = Diggr::Request.new(Diggr::Constants::APP_KEY)
34
+ request.stories.popular
35
+
36
+ assert_equal '/stories/popular', request.send(:instance_variable_get,"@end_point")
37
+ end
38
+
39
+ def test_complex_end_point_with_args
40
+ request = Diggr::Request.new(Diggr::Constants::APP_KEY)
41
+ request.stories.topic("technology").hot
42
+
43
+ assert_equal '/stories/topic/technology/hot', request.send(:instance_variable_get,"@end_point")
44
+ end
45
+
46
+ def test_request_with_options
47
+ request = Diggr::Request.new(Diggr::Constants::APP_KEY)
48
+ request.topics.options(:count => 5, :offset => 3)
49
+
50
+ assert_equal '&count=5&offset=3', request.send(:instance_variable_get,"@options")
51
+ end
52
+
53
+ def test_cleanse_string_with_forward_slash
54
+ request = Diggr::Request.new(Diggr::Constants::APP_KEY)
55
+
56
+ assert_equal '%2F', request.send(:cleanse,"/")
57
+ end
58
+
59
+ def test_cleanse_number
60
+ request = Diggr::Request.new(Diggr::Constants::APP_KEY)
61
+
62
+ assert_equal 2, request.send(:cleanse,2)
63
+ end
64
+
65
+ def test_simple_path
66
+ request = Diggr::Request.new(Diggr::Constants::APP_KEY)
67
+ request.stories
68
+
69
+ assert_equal '/stories?appkey=http%3A%2F%2Fdiggr.rubyforge.org', request.send(:path)
70
+ end
71
+
72
+ def test_simple_path_with_options
73
+ request = Diggr::Request.new(Diggr::Constants::APP_KEY)
74
+ request.stories.options(:count => 3)
75
+
76
+ assert_equal '/stories?appkey=http%3A%2F%2Fdiggr.rubyforge.org&count=3', request.send(:path)
77
+ end
78
+
79
+ def test_complex_path
80
+ request = Diggr::Request.new(Diggr::Constants::APP_KEY)
81
+ request.story(1).diggs
82
+
83
+ assert_equal '/story/1/diggs?appkey=http%3A%2F%2Fdiggr.rubyforge.org', request.send(:path)
84
+ end
85
+
86
+ def test_comma_seperated_args_product_correct_endpoint
87
+ request = Diggr::Request.new(Diggr::Constants::APP_KEY)
88
+ request.stories(1,2,3,4)
89
+
90
+ assert_equal '/stories/1,2,3,4', request.send(:instance_variable_get,"@end_point")
91
+ end
92
+
93
+ def test_fetch
94
+ story = Diggr::Story.new
95
+ Diggr::JSONParser.any_instance.stubs(:parse).with(story).returns(story)
96
+ request = Diggr::Request.new(Diggr::Constants::APP_KEY)
97
+ request.stubs(:make_request).returns(story)
98
+
99
+ assert_equal story, request.fetch
100
+ end
101
+
102
+ def test_each
103
+ stories = [Diggr::Story.new,Diggr::Story.new]
104
+ Diggr::JSONParser.any_instance.stubs(:parse).with(stories).returns(stories)
105
+ request = Diggr::Request.new(Diggr::Constants::APP_KEY)
106
+ request.stubs(:make_request).returns(stories)
107
+
108
+ request.each do |item|
109
+ assert_instance_of Diggr::Story, item
110
+ end
111
+ end
112
+
113
+ def test_each_with_singular_response
114
+ story = Diggr::Story.new
115
+ Diggr::JSONParser.any_instance.stubs(:parse).with(story).returns(story)
116
+ request = Diggr::Request.new(Diggr::Constants::APP_KEY)
117
+ request.stubs(:make_request).returns(story)
118
+
119
+ request.each do |item|
120
+ assert_instance_of Diggr::Story, item
121
+ end
122
+ end
123
+ end
@@ -0,0 +1,25 @@
1
+ require 'test/unit'
2
+ require 'rubygems'
3
+ require 'need'
4
+ need { '../lib/diggr/response_classes/short_url' }
5
+
6
+ class TestShortUrl < Test::Unit::TestCase
7
+ def test_instantiation
8
+ assert_nothing_raised do
9
+ Diggr::ShortUrl.new
10
+ end
11
+ end
12
+
13
+ def test_new_from_parsed_json_data
14
+ parsed_json_data = {
15
+ 'short_url' => 'http://digg.com/D123JKL',
16
+ 'views' => 1
17
+ }
18
+
19
+ digg = Diggr::ShortUrl.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,61 @@
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
+ "shorturl" => [{
47
+ "short_url" => "http://digg.com/D123JKL",
48
+ "views" => 1
49
+ }],
50
+ "href" => "http://digg.com/linux_unix/Jukebox_con_Linux"
51
+ }
52
+
53
+ photo = Diggr::Story.new_from_parsed_json(parsed_json_data)
54
+
55
+ parsed_json_data.each do |key,val|
56
+ unless val.kind_of? Hash or val.kind_of? Array
57
+ assert_equal val, photo.send(key)
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,25 @@
1
+ require 'test/unit'
2
+ require 'rubygems'
3
+ require 'need'
4
+ need { '../lib/diggr/response_classes/topic' }
5
+
6
+ class TestTopic < Test::Unit::TestCase
7
+ def test_instantiation
8
+ assert_nothing_raised do
9
+ Diggr::Topic.new
10
+ end
11
+ end
12
+
13
+ def test_new_from_parsed_json
14
+ parsed_json_data = {
15
+ 'name' => 'Test Topic',
16
+ 'short_name' => 'test'
17
+ }
18
+
19
+ topic = Diggr::Topic.new_from_parsed_json(parsed_json_data)
20
+
21
+ parsed_json_data.each do |key,val|
22
+ assert_equal val, topic.send(key)
23
+ end
24
+ end
25
+ 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,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rramsden-diggr
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.8
5
+ platform: ruby
6
+ authors:
7
+ - Drew Olson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-06-16 00:00:00 -07: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
+ - lib/diggr/response_classes/short_url.rb
74
+ - test/test_api.rb
75
+ - test/test_comment.rb
76
+ - test/test_container.rb
77
+ - test/test_digg.rb
78
+ - test/test_error.rb
79
+ - test/test_json_parser.rb
80
+ - test/test_medium.rb
81
+ - test/test_photo.rb
82
+ - test/test_request.rb
83
+ - test/test_story.rb
84
+ - test/test_topic.rb
85
+ - test/test_user.rb
86
+ - test/test_short_url.rb
87
+ has_rdoc: true
88
+ homepage: http://github.com/drewolson/diggr
89
+ licenses:
90
+ post_install_message:
91
+ rdoc_options:
92
+ - --main
93
+ - README.txt
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: "0"
101
+ version:
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: "0"
107
+ version:
108
+ requirements: []
109
+
110
+ rubyforge_project: diggr
111
+ rubygems_version: 1.3.5
112
+ signing_key:
113
+ specification_version: 2
114
+ summary: Diggr is a ruby wrapper for the Digg API
115
+ test_files:
116
+ - test/test_api.rb
117
+ - test/test_comment.rb
118
+ - test/test_container.rb
119
+ - test/test_digg.rb
120
+ - test/test_error.rb
121
+ - test/test_json_parser.rb
122
+ - test/test_medium.rb
123
+ - test/test_photo.rb
124
+ - test/test_request.rb
125
+ - test/test_story.rb
126
+ - test/test_topic.rb
127
+ - test/test_user.rb
128
+ - test/test_short_url.rb