nanowrimo 0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,49 @@
1
+ #! /usr/bin/env ruby -w
2
+
3
+ require 'nanowrimo'
4
+ require 'test/unit'
5
+
6
+ class TestCache < Test::Unit::TestCase
7
+ def test_cache_data_exists
8
+ assert Nanowrimo::Cache.cache_data
9
+ assert Nanowrimo::Cache.cache_data.instance_of?(Hash)
10
+ end
11
+
12
+ def test_write_cache_to_disk
13
+ File.delete(Nanowrimo::Cache::CACHE_FILE) if File.exist?(Nanowrimo::Cache::CACHE_FILE)
14
+ Nanowrimo::Cache.save_cache_to_disk
15
+ assert File.exist?(Nanowrimo::Cache::CACHE_FILE)
16
+ end
17
+
18
+ def test_add_to_cache
19
+ type="foo"
20
+ key="bar"
21
+ data = [{:uid => "240659", :uname => "hollowedout"}]
22
+ Nanowrimo::Cache.add_to_cache(type, key, data)
23
+ assert_equal data, Nanowrimo::Cache.cache_data[type][key][:data]
24
+ end
25
+
26
+ def test_find_data_returns_cached_data
27
+ type="foo"
28
+ key="bar"
29
+ data = [{:uid => "12345", :uname => "foo", :user_wordcount => "123456"}]
30
+ Nanowrimo::Cache.add_to_cache(type, key, data)
31
+ actual = Nanowrimo::Cache.find_data(type, key)
32
+ assert actual
33
+ assert "12345", actual.first[:uid]
34
+ end
35
+
36
+ def test_find_data_finds_nothing_for_type
37
+ type="bar"
38
+ key="foo"
39
+ actual = Nanowrimo::Cache.find_data(type, key)
40
+ assert actual.nil?
41
+ end
42
+
43
+ def test_find_data_finds_nothing_for_key
44
+ type="foo"
45
+ key="bar"
46
+ actual = Nanowrimo::Cache.find_data(type,key)
47
+ assert !actual.nil?
48
+ end
49
+ end
@@ -0,0 +1,16 @@
1
+ #! /usr/bin/env ruby -w
2
+
3
+ require 'nanowrimo'
4
+ require 'test/unit'
5
+
6
+ class TestCore < Test::Unit::TestCase
7
+ # basically we're just making sure that the methods we need are present. the class was
8
+ # created as a refactor of the common methods in each of the Nanowrimo::* classes.
9
+ def test_core_has_load_method
10
+ assert Nanowrimo::Core.new.respond_to?(:load)
11
+ end
12
+
13
+ def test_core_has_load_history_method
14
+ assert Nanowrimo::Core.new.respond_to?(:load_history)
15
+ end
16
+ end
@@ -0,0 +1,42 @@
1
+ #! /usr/bin/env ruby -w
2
+
3
+ require 'nanowrimo'
4
+ require 'test/unit'
5
+ require 'fakeweb'
6
+
7
+ class TestGenre < Test::Unit::TestCase
8
+ def setup
9
+ @genre = Nanowrimo::Genre.new("13")
10
+ end
11
+
12
+ def test_genre_has_appropriate_fields
13
+ expected = %w[gid gname genre_wordcount max min stddev average count]
14
+ assert_equal expected, Nanowrimo::Genre::FIELDS
15
+ end
16
+
17
+ def test_genre_has_appropriate_history_fields
18
+ expected = %w[wc wcdate max min stddev average count]
19
+ assert_equal expected, Nanowrimo::Genre::HISTORY_FIELDS
20
+ end
21
+
22
+ def test_genre_loads_current_data
23
+ file = 'test/fixtures/genre_wc.xml'
24
+ FakeWeb.register_uri("#{Nanowrimo::API_URI}/wcgenre/13", :file => file)
25
+ @genre.load
26
+ Nanowrimo::Genre::FIELDS.each do |f|
27
+ assert @genre.send(:"#{f}")
28
+ end
29
+ end
30
+
31
+ def test_genre_loads_historical_data
32
+ file = 'test/fixtures/genre_wc_history.xml'
33
+ FakeWeb.register_uri("#{Nanowrimo::API_URI}/wcgenrehist/13", :file => file)
34
+ @genre.load_history
35
+ assert @genre.history
36
+ # So...
37
+ # It looks like the API for the genre history data is borked. I've sent an email to the
38
+ # nanowrimo peoples, and when they bring it back up properly I'll test more thoroughly.
39
+ assert_equal 0, @genre.history.size
40
+ #assert_equal 7, @genre.history.first.size
41
+ end
42
+ end
@@ -0,0 +1,109 @@
1
+ #! /usr/bin/env ruby -w
2
+
3
+ require 'nanowrimo'
4
+ require 'test/unit'
5
+ require 'fakeweb'
6
+
7
+ class TestNanowrimo < Test::Unit::TestCase
8
+ # the purpose for this test class is to make sure that Nanowrimo has a method to
9
+ # open an xml document and parse it given a set of fields and a search string.
10
+ # It's going to be a loose wrapper around Nokogiri which should keep the api DRY.
11
+
12
+ def setup
13
+ FakeWeb.allow_net_connect = false
14
+ FakeWeb.clean_registry
15
+ end
16
+
17
+ def test_nanowrimo_has_parse_method
18
+ assert Nanowrimo.respond_to?(:parse)
19
+ end
20
+
21
+ def test_nanowrimo_has_data_loading_methods
22
+ assert Nanowrimo.respond_to?(:data_from_cache)
23
+ assert Nanowrimo.respond_to?(:data_from_internets)
24
+ end
25
+
26
+ def test_nanowrimo_data_load_from_internets_returns_hash_with_data
27
+ attribs = %w[uid uname user_wordcount]
28
+ path = "wc"
29
+ key = 240659
30
+ file = "test/fixtures/user_wc.xml"
31
+ FakeWeb.register_uri("#{Nanowrimo::API_URI}/wc/#{key}", :file => file)
32
+ actual = Nanowrimo.data_from_internets(path, key, attribs).first
33
+ expected = {
34
+ :uid => "240659",
35
+ :uname => "hollowedout",
36
+ :user_wordcount => "55415"
37
+ }
38
+ assert_equal expected, actual
39
+ end
40
+
41
+ def test_nanowrimo_parse_does_the_same_as_data_from_internets
42
+ attribs = %w[uid uname user_wordcount]
43
+ path = "wc"
44
+ key = 240659
45
+ file = "test/fixtures/user_wc.xml"
46
+ FakeWeb.register_uri("#{Nanowrimo::API_URI}/wc/#{key}", :file => file)
47
+ actual = Nanowrimo.parse(path, key, attribs).first
48
+ expected = {
49
+ :uid => "240659",
50
+ :uname => "hollowedout",
51
+ :user_wordcount => "55415"
52
+ }
53
+ assert_equal expected, actual
54
+ end
55
+
56
+ def test_nanowrimo_data_from_cache_returns_hash_with_data
57
+ attribs = %w[uid uname user_wordcount]
58
+ path = "wc"
59
+ key = 240659
60
+ file = "test/fixtures/user_wc.xml"
61
+ File.delete(Nanowrimo::Cache::CACHE_FILE) if File.exist?(Nanowrimo::Cache::CACHE_FILE)
62
+ FakeWeb.register_uri("#{Nanowrimo::API_URI}/wc/#{key}", :file => file)
63
+ Nanowrimo.data_from_internets(path, key, attribs)
64
+ FakeWeb.clean_registry
65
+ actual = Nanowrimo.data_from_cache(path, key).first
66
+ expected = {
67
+ :uid => "240659",
68
+ :uname => "hollowedout",
69
+ :user_wordcount => "55415"
70
+ }
71
+ assert_equal expected, actual
72
+ end
73
+
74
+ def test_nanowrimo_parse_uses_cache_after_loading_from_internets_the_first_time
75
+ attribs = %w[uid uname user_wordcount]
76
+ path = "wc"
77
+ key = 240659
78
+ file = "test/fixtures/user_wc.xml"
79
+ File.delete(Nanowrimo::Cache::CACHE_FILE) if File.exist?(Nanowrimo::Cache::CACHE_FILE)
80
+ FakeWeb.register_uri("#{Nanowrimo::API_URI}/wc/#{key}", :file => file)
81
+ Nanowrimo.parse(path, key, attribs)
82
+ FakeWeb.clean_registry
83
+ actual = {}
84
+ assert_nothing_raised do
85
+ actual = Nanowrimo.parse(path, key, attribs).first
86
+ end
87
+ expected = {
88
+ :uid => "240659",
89
+ :uname => "hollowedout",
90
+ :user_wordcount => "55415"
91
+ }
92
+ assert_equal expected, actual
93
+ end
94
+
95
+ def test_nanowrimo_parse_history_returns_array_of_hashes_with_data
96
+ attribs = %w[wc wcdate]
97
+ path = "wchistory/wordcounts/wcentry"
98
+ key = 240659
99
+ file = "test/fixtures/user_wc_history.xml"
100
+ FakeWeb.register_uri("#{Nanowrimo::API_URI}/wchistory/#{key}", :file => file)
101
+ data = Nanowrimo.parse(path, key, attribs)
102
+ assert_equal 30, data.size
103
+ data.each do |d|
104
+ assert_equal 2, d.keys.size
105
+ end
106
+ expected = {:wc => "0", :wcdate => "2008-11-01"}
107
+ assert_equal expected, data.first
108
+ end
109
+ end
@@ -0,0 +1,39 @@
1
+ #! /usr/bin/env ruby -w
2
+
3
+ require 'nanowrimo'
4
+ require 'test/unit'
5
+ require 'fakeweb'
6
+
7
+ class TestRegion < Test::Unit::TestCase
8
+ def setup
9
+ @region = Nanowrimo::Region.new("84")
10
+ end
11
+
12
+ def test_regions_has_appropriate_fields
13
+ expected = %w[rid rname region_wordcount max min stddev average count donations numdonors]
14
+ assert_equal expected, Nanowrimo::Region::FIELDS
15
+ end
16
+
17
+ def test_regions_has_appropriate_history_fields
18
+ expected = %w[wc wcdate max min stddev average count donations donors]
19
+ assert_equal expected, Nanowrimo::Region::HISTORY_FIELDS
20
+ end
21
+
22
+ def test_region_loads_current_data
23
+ file = 'test/fixtures/region_wc.xml'
24
+ FakeWeb.register_uri("#{Nanowrimo::API_URI}/wcregion/84", :file => file)
25
+ @region.load
26
+ Nanowrimo::Region::FIELDS.each do |f|
27
+ assert @region.send(:"#{f}")
28
+ end
29
+ end
30
+
31
+ def test_region_loads_historical_data
32
+ file = 'test/fixtures/region_wc_history.xml'
33
+ FakeWeb.register_uri("#{Nanowrimo::API_URI}/wcregionhist/84", :file => file)
34
+ @region.load_history
35
+ assert @region.history
36
+ assert_equal 9, @region.history.first.size
37
+ assert_equal 30, @region.history.size
38
+ end
39
+ end
@@ -0,0 +1,39 @@
1
+ #! /usr/bin/env ruby -w
2
+
3
+ require 'nanowrimo'
4
+ require 'test/unit'
5
+ require 'fakeweb'
6
+
7
+ class TestSite < Test::Unit::TestCase
8
+ def setup
9
+ @site = Nanowrimo::Site.new
10
+ end
11
+
12
+ def test_site_has_appropriate_fields
13
+ expected = %w[site_wordcount max min stddev average count]
14
+ assert_equal expected, Nanowrimo::Site::FIELDS
15
+ end
16
+
17
+ def test_site_has_appropriate_history_fields
18
+ expected = %w[wc wcdate max min stddev average count]
19
+ assert_equal expected, Nanowrimo::Site::HISTORY_FIELDS
20
+ end
21
+
22
+ def test_site_loads_data
23
+ file = 'test/fixtures/site_wc.xml'
24
+ FakeWeb.register_uri("#{Nanowrimo::API_URI}/wcstatssummary", :file => file)
25
+ @site.load
26
+ Nanowrimo::Site::FIELDS.each do |f|
27
+ assert @site.send(:"#{f}"), "Failed on #{f}"
28
+ end
29
+ end
30
+
31
+ def test_site_loads_historical_data
32
+ file = 'test/fixtures/site_wc_history.xml'
33
+ FakeWeb.register_uri("#{Nanowrimo::API_URI}/wcstats", :file => file)
34
+ @site.load_history
35
+ assert @site.history
36
+ assert_equal 30, @site.history.size
37
+ assert_equal 7, @site.history.first.keys.size
38
+ end
39
+ end
@@ -0,0 +1,99 @@
1
+ #! /usr/bin/env ruby -w
2
+
3
+ require 'nanowrimo'
4
+ require 'test/unit'
5
+ require 'fakeweb'
6
+
7
+ class TestUser < Test::Unit::TestCase
8
+ def setup
9
+ @user = Nanowrimo::User.new("240659")
10
+ end
11
+
12
+ def test_user_has_appropriate_fields
13
+ expected = %w[uid uname user_wordcount]
14
+ assert_equal expected, Nanowrimo::User::FIELDS
15
+ end
16
+
17
+ def test_user_has_appropriate_historical_fields
18
+ expected = %w[wc wcdate]
19
+ assert_equal expected, Nanowrimo::User::HISTORY_FIELDS
20
+ end
21
+
22
+ def test_user_has_uid
23
+ assert_equal "240659", @user.uid
24
+ end
25
+
26
+ def test_user_gets_data
27
+ file = "test/fixtures/user_wc.xml"
28
+ FakeWeb.register_uri("#{Nanowrimo::API_URI}/wc/240659", :file => file)
29
+ @user.load
30
+ assert @user.uname
31
+ assert_equal "hollowedout", @user.uname
32
+ assert @user.user_wordcount
33
+ assert_equal "55415", @user.user_wordcount
34
+ end
35
+
36
+ def test_user_loads_history_data
37
+ file = "test/fixtures/user_wc_history.xml"
38
+ FakeWeb.register_uri("#{Nanowrimo::API_URI}/wchistory/240659", :file => file)
39
+ @user.load_history
40
+ assert @user.history
41
+ assert_equal 30, @user.history.size
42
+ end
43
+
44
+ def test_user_is_a_winner
45
+ @user.load
46
+ assert @user.winner?
47
+ end
48
+
49
+ def test_user_has_profile_fields
50
+ expected = %w[rid novel genre buddies]
51
+ assert_equal expected, Nanowrimo::User::USER_FIELDS
52
+ end
53
+
54
+ def test_profile_fields_default_properly
55
+ assert @user.novel.instance_of?(Hash)
56
+ assert @user.genre.instance_of?(Hash)
57
+ assert @user.buddies.instance_of?(Array)
58
+ end
59
+
60
+ def test_user_load_profile_data
61
+ profile_uri_setup
62
+ @user.load_profile_data
63
+ assert @user.profile_data.instance_of?(WWW::Mechanize::Page)
64
+ assert_match(/<div id="tcontent3"/, @user.profile_data.body)
65
+ end
66
+
67
+ def test_find_users_region
68
+ profile_uri_setup
69
+ @user.parse_profile
70
+ assert_equal "84", @user.rid
71
+ end
72
+
73
+ def test_find_users_novel_title
74
+ profile_uri_setup
75
+ @user.parse_profile
76
+ assert_equal "Interpolis", @user.novel[:title]
77
+ end
78
+
79
+ def test_find_users_genre
80
+ profile_uri_setup
81
+ @user.parse_profile
82
+ assert_equal "Science Fiction", @user.genre[:name]
83
+ end
84
+
85
+ def test_find_users_buddies
86
+ profile_uri_setup
87
+ @user.parse_profile
88
+ assert @user.buddies.instance_of?(Array)
89
+ assert_equal 11, @user.buddies.size
90
+ end
91
+
92
+ def profile_uri_setup
93
+ # this was a bit of weirdness - I had to curl -is the url in order to grab the headers, and
94
+ # even then it would register the file from FakeWeb as WWW::Mechanize::File, not WWW::Mechanize::Page
95
+ # discussion started at http://groups.google.com/group/fakeweb-users/browse_thread/thread/e01b6280e720ae6f
96
+ file = File.read("test/fixtures/user_page.htm")
97
+ FakeWeb.register_uri("http://www.nanowrimo.org/eng/user/240659", :response => file)
98
+ end
99
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nanowrimo
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.7"
5
+ platform: ruby
6
+ authors:
7
+ - Joshua Clingenpeel
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-29 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: hoe
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.7.0
24
+ version:
25
+ description: |-
26
+ A simple API wrapper for Nanowrimo.org. Nanowrimo Word Count API documentation here:
27
+ http://www.nanowrimo.org/eng/wordcount_api
28
+ email:
29
+ - joshua.clingenpeel@gmail.com
30
+ executables: []
31
+
32
+ extensions: []
33
+
34
+ extra_rdoc_files:
35
+ - History.txt
36
+ - Manifest.txt
37
+ - README.txt
38
+ files:
39
+ - History.txt
40
+ - Manifest.txt
41
+ - README.txt
42
+ - Rakefile
43
+ - lib/nanowrimo.rb
44
+ - lib/nanowrimo/cache.rb
45
+ - lib/nanowrimo/core.rb
46
+ - lib/nanowrimo/genre.rb
47
+ - lib/nanowrimo/region.rb
48
+ - lib/nanowrimo/site.rb
49
+ - lib/nanowrimo/user.rb
50
+ - test/test_cache.rb
51
+ - test/test_core.rb
52
+ - test/test_genre.rb
53
+ - test/test_nanowrimo.rb
54
+ - test/test_region.rb
55
+ - test/test_site.rb
56
+ - test/test_user.rb
57
+ - test/fixtures/genre_wc.xml
58
+ - test/fixtures/genre_wc_history.xml
59
+ - test/fixtures/region_wc.xml
60
+ - test/fixtures/region_wc_history.xml
61
+ - test/fixtures/site_wc.xml
62
+ - test/fixtures/site_wc_history.xml
63
+ - test/fixtures/user_page.htm
64
+ - test/fixtures/user_wc.xml
65
+ - test/fixtures/user_wc_history.xml
66
+ has_rdoc: true
67
+ homepage: http://nanowrimo.rubyforge.org
68
+ licenses: []
69
+
70
+ post_install_message:
71
+ rdoc_options:
72
+ - --main
73
+ - README.txt
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: "0"
81
+ version:
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: "0"
87
+ version:
88
+ requirements: []
89
+
90
+ rubyforge_project: nanowrimo
91
+ rubygems_version: 1.3.2
92
+ signing_key:
93
+ specification_version: 3
94
+ summary: A simple API wrapper for Nanowrimo.org
95
+ test_files:
96
+ - test/test_cache.rb
97
+ - test/test_core.rb
98
+ - test/test_genre.rb
99
+ - test/test_nanowrimo.rb
100
+ - test/test_region.rb
101
+ - test/test_site.rb
102
+ - test/test_user.rb