nanowrimo 0.7 → 0.7.5
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +6 -0
- data/Manifest.txt +7 -1
- data/README.txt +2 -1
- data/lib/nanowrimo.rb +6 -2
- data/lib/nanowrimo/core.rb +12 -0
- data/test/fixtures/genre_wc_error.xml +16 -0
- data/test/fixtures/genre_wc_history_error.xml +20 -0
- data/test/fixtures/region_wc_error.xml +18 -0
- data/test/fixtures/region_wc_history_error.xml +22 -0
- data/test/fixtures/user_wc_error.xml +11 -0
- data/test/fixtures/user_wc_history_error.xml +15 -0
- data/test/test_core.rb +13 -1
- data/test/test_genre.rb +18 -0
- data/test/test_nanowrimo.rb +13 -0
- data/test/test_region.rb +22 -4
- data/test/test_user.rb +23 -4
- metadata +8 -2
data/History.txt
CHANGED
data/Manifest.txt
CHANGED
@@ -17,11 +17,17 @@ test/test_region.rb
|
|
17
17
|
test/test_site.rb
|
18
18
|
test/test_user.rb
|
19
19
|
test/fixtures/genre_wc.xml
|
20
|
+
test/fixtures/genre_wc_error.xml
|
20
21
|
test/fixtures/genre_wc_history.xml
|
22
|
+
test/fixtures/genre_wc_history_error.xml
|
21
23
|
test/fixtures/region_wc.xml
|
24
|
+
test/fixtures/region_wc_error.xml
|
22
25
|
test/fixtures/region_wc_history.xml
|
26
|
+
test/fixtures/region_wc_history_error.xml
|
23
27
|
test/fixtures/site_wc.xml
|
24
28
|
test/fixtures/site_wc_history.xml
|
25
29
|
test/fixtures/user_page.htm
|
26
30
|
test/fixtures/user_wc.xml
|
27
|
-
test/fixtures/
|
31
|
+
test/fixtures/user_wc_error.xml
|
32
|
+
test/fixtures/user_wc_history.xml
|
33
|
+
test/fixtures/user_wc_history_error.xml
|
data/README.txt
CHANGED
@@ -15,6 +15,7 @@ Features:
|
|
15
15
|
* Simple! Clean! Well-tested!
|
16
16
|
* Easy to roll into a Rails application (that's next)
|
17
17
|
* Separate APIs for Users, Site, Regions, and Genres
|
18
|
+
* API handles generated error messages from the WCAPI
|
18
19
|
* Page scraping place for basic user data from the profile page
|
19
20
|
* Caches data to avoid November bandwidth issues
|
20
21
|
|
@@ -34,7 +35,7 @@ Problems:
|
|
34
35
|
>> me.winner?
|
35
36
|
=> true
|
36
37
|
# YAY!
|
37
|
-
|
38
|
+
|
38
39
|
# Want to get a list of your writing buddies?
|
39
40
|
>> me.parse_profile
|
40
41
|
>> me.buddies
|
data/lib/nanowrimo.rb
CHANGED
@@ -23,7 +23,7 @@ require 'nanowrimo/cache'
|
|
23
23
|
|
24
24
|
module Nanowrimo
|
25
25
|
# Current API version
|
26
|
-
VERSION = '0.7'
|
26
|
+
VERSION = '0.7.5'
|
27
27
|
# Current static root WCAPI uri
|
28
28
|
API_URI = 'http://www.nanowrimo.org/wordcount_api'
|
29
29
|
# Current individual user word count goal. For fun!
|
@@ -51,10 +51,14 @@ module Nanowrimo
|
|
51
51
|
begin
|
52
52
|
timeout(2) {
|
53
53
|
doc = Nokogiri::XML(open(uri))
|
54
|
+
doc.xpath("#{type}/error").each {|e|
|
55
|
+
result << {:error => e.content}
|
56
|
+
}
|
57
|
+
return result unless result.empty?
|
54
58
|
doc.xpath(path).each {|n|
|
55
59
|
node = {}
|
56
60
|
attribs.each {|d|
|
57
|
-
node[d.intern] = n.at(d).content
|
61
|
+
node[d.intern] = n.at(d).content unless n.at(d).nil?
|
58
62
|
}
|
59
63
|
result << node
|
60
64
|
}
|
data/lib/nanowrimo/core.rb
CHANGED
@@ -3,9 +3,13 @@
|
|
3
3
|
module Nanowrimo
|
4
4
|
# Core load methods
|
5
5
|
class Core
|
6
|
+
# attribute for storing error message returned by WCAPI on any response.
|
7
|
+
attr_accessor :error
|
8
|
+
|
6
9
|
# Returns the values for all attributes for a given WCAPI type
|
7
10
|
def load
|
8
11
|
attribs = Nanowrimo.parse(load_field,id,self.class::FIELDS).first
|
12
|
+
self.error = attribs[:error]
|
9
13
|
self.class::FIELDS.each do |attrib|
|
10
14
|
self.send(:"#{attrib}=", attribs[attrib.intern])
|
11
15
|
end
|
@@ -14,6 +18,14 @@ module Nanowrimo
|
|
14
18
|
# Returns the values for all attributes for a given WCAPI type's history
|
15
19
|
def load_history
|
16
20
|
self.history = Nanowrimo.parse(load_history_field,id,self.class::HISTORY_FIELDS)
|
21
|
+
if maybe_error = self.history.first
|
22
|
+
self.error = maybe_error[:error]
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
# Tells us if the current object has any errors from the WCAPI
|
27
|
+
def has_error?
|
28
|
+
!error.nil?
|
17
29
|
end
|
18
30
|
end
|
19
31
|
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
<?xml version="1.0" standalone="yes"?><!DOCTYPE wcgenre [
|
2
|
+
<!ELEMENT wcgenre (gid, error, gname, genre_wordcount, max, min, stddev, average, count)>
|
3
|
+
<!ELEMENT gid (#PCDATA)>
|
4
|
+
<!ELEMENT error (#PCDATA)>
|
5
|
+
<!ELEMENT gname (#PCDATA)>
|
6
|
+
<!ELEMENT genre_wordcount (#PCDATA)>
|
7
|
+
<!ELEMENT max (#PCDATA)>
|
8
|
+
<!ELEMENT min (#PCDATA)>
|
9
|
+
<!ELEMENT stddev (#PCDATA)>
|
10
|
+
<!ELEMENT average (#PCDATA)>
|
11
|
+
<!ELEMENT count (#PCDATA)>
|
12
|
+
]>
|
13
|
+
<wcgenre>
|
14
|
+
<gid>999999</gid>
|
15
|
+
<error>genre not found</error>
|
16
|
+
</wcgenre>
|
@@ -0,0 +1,20 @@
|
|
1
|
+
<?xml version="1.0" standalone="yes"?><!DOCTYPE wcgenrehist [
|
2
|
+
<!ELEMENT wcgenrehist (gid, error, gname, genre_wordcount, wordcounts)>
|
3
|
+
<!ELEMENT gid (#PCDATA)>
|
4
|
+
<!ELEMENT error (#PCDATA)>
|
5
|
+
<!ELEMENT gname (#PCDATA)>
|
6
|
+
<!ELEMENT genre_wordcount (#PCDATA)>
|
7
|
+
<!ELEMENT wordcounts (wcentry+)>
|
8
|
+
<!ELEMENT wcentry (wc, wcdate, max, min, stddev, average, count)>
|
9
|
+
<!ELEMENT wc (#PCDATA)>
|
10
|
+
<!ELEMENT wcdate (#PCDATA)>
|
11
|
+
<!ELEMENT max (#PCDATA)>
|
12
|
+
<!ELEMENT min (#PCDATA)>
|
13
|
+
<!ELEMENT stddev (#PCDATA)>
|
14
|
+
<!ELEMENT average (#PCDATA)>
|
15
|
+
<!ELEMENT count (#PCDATA)>
|
16
|
+
]>
|
17
|
+
<wcgenrehist>
|
18
|
+
<gid>999999</gid>
|
19
|
+
<error>genre not found</error>
|
20
|
+
</wcgenrehist>
|
@@ -0,0 +1,18 @@
|
|
1
|
+
<?xml version="1.0" standalone="yes"?><!DOCTYPE wcregion [
|
2
|
+
<!ELEMENT wcregion (rid, error, rname, region_wordcount, max, min, stddev, average, count, donations, numdonors)>
|
3
|
+
<!ELEMENT rid (#PCDATA)>
|
4
|
+
<!ELEMENT error (#PCDATA)>
|
5
|
+
<!ELEMENT rname (#PCDATA)>
|
6
|
+
<!ELEMENT region_wordcount (#PCDATA)>
|
7
|
+
<!ELEMENT max (#PCDATA)>
|
8
|
+
<!ELEMENT min (#PCDATA)>
|
9
|
+
<!ELEMENT stddev (#PCDATA)>
|
10
|
+
<!ELEMENT average (#PCDATA)>
|
11
|
+
<!ELEMENT count (#PCDATA)>
|
12
|
+
<!ELEMENT donations (#PCDATA)>
|
13
|
+
<!ELEMENT numdonors (#PCDATA)>
|
14
|
+
]>
|
15
|
+
<wcregion>
|
16
|
+
<rid>999999</rid>
|
17
|
+
<error>region not found</error>
|
18
|
+
</wcregion>
|
@@ -0,0 +1,22 @@
|
|
1
|
+
<?xml version="1.0" standalone="yes"?><!DOCTYPE wcregionhist [
|
2
|
+
<!ELEMENT wcregionhist (rid, error, rname, region_wordcount, wordcounts)>
|
3
|
+
<!ELEMENT rid (#PCDATA)>
|
4
|
+
<!ELEMENT error (#PCDATA)>
|
5
|
+
<!ELEMENT rname (#PCDATA)>
|
6
|
+
<!ELEMENT region_wordcount (#PCDATA)>
|
7
|
+
<!ELEMENT wordcounts (wcentry+)>
|
8
|
+
<!ELEMENT wcentry (wc, wcdate, max, min, stddev, average, count, donations, numdonors)>
|
9
|
+
<!ELEMENT wc (#PCDATA)>
|
10
|
+
<!ELEMENT wcdate (#PCDATA)>
|
11
|
+
<!ELEMENT max (#PCDATA)>
|
12
|
+
<!ELEMENT min (#PCDATA)>
|
13
|
+
<!ELEMENT stddev (#PCDATA)>
|
14
|
+
<!ELEMENT average (#PCDATA)>
|
15
|
+
<!ELEMENT count (#PCDATA)>
|
16
|
+
<!ELEMENT donations (#PCDATA)>
|
17
|
+
<!ELEMENT numdonors (#PCDATA)>
|
18
|
+
]>
|
19
|
+
<wcregionhist>
|
20
|
+
<rid>999999</rid>
|
21
|
+
<error>region not found</error>
|
22
|
+
</wcregionhist>
|
@@ -0,0 +1,11 @@
|
|
1
|
+
<?xml version="1.0" standalone="yes"?><!DOCTYPE wc [
|
2
|
+
<!ELEMENT wc (uid, error, uname, user_wordcount)>
|
3
|
+
<!ELEMENT uid (#PCDATA)>
|
4
|
+
<!ELEMENT error (#PCDATA)>
|
5
|
+
<!ELEMENT uname (#PCDATA)>
|
6
|
+
<!ELEMENT user_wordcount (#PCDATA)>
|
7
|
+
]>
|
8
|
+
<wc>
|
9
|
+
<uid>999999</uid>
|
10
|
+
<error>user not found or is inactive</error>
|
11
|
+
</wc>
|
@@ -0,0 +1,15 @@
|
|
1
|
+
<?xml version="1.0" standalone="yes"?><!DOCTYPE wchistory [
|
2
|
+
<!ELEMENT wchistory (uid, error, uname, user_wordcount, wordcounts)>
|
3
|
+
<!ELEMENT uid (#PCDATA)>
|
4
|
+
<!ELEMENT error (#PCDATA)>
|
5
|
+
<!ELEMENT uname (#PCDATA)>
|
6
|
+
<!ELEMENT user_wordcount (#PCDATA)>
|
7
|
+
<!ELEMENT wordcounts (wcentry+)>
|
8
|
+
<!ELEMENT wcentry (wc,wcdate)>
|
9
|
+
<!ELEMENT wc (#PCDATA)>
|
10
|
+
<!ELEMENT wcdate (#PCDATA)>
|
11
|
+
]>
|
12
|
+
<wchistory>
|
13
|
+
<uid>999999</uid>
|
14
|
+
<error>user not found or is inactive</error>
|
15
|
+
</wchistory>
|
data/test/test_core.rb
CHANGED
@@ -9,8 +9,20 @@ class TestCore < Test::Unit::TestCase
|
|
9
9
|
def test_core_has_load_method
|
10
10
|
assert Nanowrimo::Core.new.respond_to?(:load)
|
11
11
|
end
|
12
|
-
|
12
|
+
|
13
13
|
def test_core_has_load_history_method
|
14
14
|
assert Nanowrimo::Core.new.respond_to?(:load_history)
|
15
15
|
end
|
16
|
+
|
17
|
+
def test_core_has_error_attribute
|
18
|
+
assert Nanowrimo::Core.new.respond_to?(:error)
|
19
|
+
assert Nanowrimo::Core.new.respond_to?(:error=)
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_core_has_error_works
|
23
|
+
actual = Nanowrimo::Core.new
|
24
|
+
assert actual.respond_to?(:has_error?)
|
25
|
+
actual.error = "Foo"
|
26
|
+
assert actual.has_error?
|
27
|
+
end
|
16
28
|
end
|
data/test/test_genre.rb
CHANGED
@@ -39,4 +39,22 @@ class TestGenre < Test::Unit::TestCase
|
|
39
39
|
assert_equal 0, @genre.history.size
|
40
40
|
#assert_equal 7, @genre.history.first.size
|
41
41
|
end
|
42
|
+
|
43
|
+
def test_unknown_genre_produces_error_data
|
44
|
+
bad_genre = Nanowrimo::Genre.new("999999")
|
45
|
+
file = "test/fixtures/genre_wc_error.xml"
|
46
|
+
FakeWeb.register_uri("#{Nanowrimo::API_URI}/wcgenre/999999", :file => file)
|
47
|
+
bad_genre.load
|
48
|
+
assert bad_genre.has_error?
|
49
|
+
assert_equal "genre not found", bad_genre.error
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_unknown_genre_produces_historical_error_data
|
53
|
+
bad_genre = Nanowrimo::Genre.new("999999")
|
54
|
+
file = "test/fixtures/genre_wc_history_error.xml"
|
55
|
+
FakeWeb.register_uri("#{Nanowrimo::API_URI}/wcgenrehist/999999", :file => file)
|
56
|
+
bad_genre.load_history
|
57
|
+
assert bad_genre.has_error?
|
58
|
+
assert_equal "genre not found", bad_genre.error
|
59
|
+
end
|
42
60
|
end
|
data/test/test_nanowrimo.rb
CHANGED
@@ -106,4 +106,17 @@ class TestNanowrimo < Test::Unit::TestCase
|
|
106
106
|
expected = {:wc => "0", :wcdate => "2008-11-01"}
|
107
107
|
assert_equal expected, data.first
|
108
108
|
end
|
109
|
+
|
110
|
+
def test_nanowrimo_parse_handles_wcapi_error_message
|
111
|
+
attribs = %w[uid uname user_wordcount]
|
112
|
+
path = "wc"
|
113
|
+
key = 999999
|
114
|
+
file = "test/fixtures/user_wc_error.xml"
|
115
|
+
FakeWeb.register_uri("#{Nanowrimo::API_URI}/wc/#{key}", :file => file)
|
116
|
+
actual = Nanowrimo.data_from_internets(path, key, attribs).first
|
117
|
+
expected = {
|
118
|
+
:error => "user not found or is inactive"
|
119
|
+
}
|
120
|
+
assert_equal expected, actual
|
121
|
+
end
|
109
122
|
end
|
data/test/test_region.rb
CHANGED
@@ -8,17 +8,17 @@ class TestRegion < Test::Unit::TestCase
|
|
8
8
|
def setup
|
9
9
|
@region = Nanowrimo::Region.new("84")
|
10
10
|
end
|
11
|
-
|
11
|
+
|
12
12
|
def test_regions_has_appropriate_fields
|
13
13
|
expected = %w[rid rname region_wordcount max min stddev average count donations numdonors]
|
14
14
|
assert_equal expected, Nanowrimo::Region::FIELDS
|
15
15
|
end
|
16
|
-
|
16
|
+
|
17
17
|
def test_regions_has_appropriate_history_fields
|
18
18
|
expected = %w[wc wcdate max min stddev average count donations donors]
|
19
19
|
assert_equal expected, Nanowrimo::Region::HISTORY_FIELDS
|
20
20
|
end
|
21
|
-
|
21
|
+
|
22
22
|
def test_region_loads_current_data
|
23
23
|
file = 'test/fixtures/region_wc.xml'
|
24
24
|
FakeWeb.register_uri("#{Nanowrimo::API_URI}/wcregion/84", :file => file)
|
@@ -27,7 +27,7 @@ class TestRegion < Test::Unit::TestCase
|
|
27
27
|
assert @region.send(:"#{f}")
|
28
28
|
end
|
29
29
|
end
|
30
|
-
|
30
|
+
|
31
31
|
def test_region_loads_historical_data
|
32
32
|
file = 'test/fixtures/region_wc_history.xml'
|
33
33
|
FakeWeb.register_uri("#{Nanowrimo::API_URI}/wcregionhist/84", :file => file)
|
@@ -36,4 +36,22 @@ class TestRegion < Test::Unit::TestCase
|
|
36
36
|
assert_equal 9, @region.history.first.size
|
37
37
|
assert_equal 30, @region.history.size
|
38
38
|
end
|
39
|
+
|
40
|
+
def test_unknown_region_produces_error_data
|
41
|
+
bad_region = Nanowrimo::Region.new("999999")
|
42
|
+
file = "test/fixtures/region_wc_error.xml"
|
43
|
+
FakeWeb.register_uri("#{Nanowrimo::API_URI}/wcregion/999999", :file => file)
|
44
|
+
bad_region.load
|
45
|
+
assert bad_region.has_error?
|
46
|
+
assert_equal "region not found", bad_region.error
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_unknown_region_produces_historical_error_data
|
50
|
+
bad_region = Nanowrimo::Region.new("999999")
|
51
|
+
file = "test/fixtures/region_wc_history_error.xml"
|
52
|
+
FakeWeb.register_uri("#{Nanowrimo::API_URI}/wcregionhist/999999", :file => file)
|
53
|
+
bad_region.load_history
|
54
|
+
assert bad_region.has_error?
|
55
|
+
assert_equal "region not found", bad_region.error
|
56
|
+
end
|
39
57
|
end
|
data/test/test_user.rb
CHANGED
@@ -7,6 +7,7 @@ require 'fakeweb'
|
|
7
7
|
class TestUser < Test::Unit::TestCase
|
8
8
|
def setup
|
9
9
|
@user = Nanowrimo::User.new("240659")
|
10
|
+
FakeWeb.allow_net_connect = false
|
10
11
|
end
|
11
12
|
|
12
13
|
def test_user_has_appropriate_fields
|
@@ -45,18 +46,18 @@ class TestUser < Test::Unit::TestCase
|
|
45
46
|
@user.load
|
46
47
|
assert @user.winner?
|
47
48
|
end
|
48
|
-
|
49
|
+
|
49
50
|
def test_user_has_profile_fields
|
50
51
|
expected = %w[rid novel genre buddies]
|
51
52
|
assert_equal expected, Nanowrimo::User::USER_FIELDS
|
52
53
|
end
|
53
|
-
|
54
|
+
|
54
55
|
def test_profile_fields_default_properly
|
55
56
|
assert @user.novel.instance_of?(Hash)
|
56
57
|
assert @user.genre.instance_of?(Hash)
|
57
58
|
assert @user.buddies.instance_of?(Array)
|
58
59
|
end
|
59
|
-
|
60
|
+
|
60
61
|
def test_user_load_profile_data
|
61
62
|
profile_uri_setup
|
62
63
|
@user.load_profile_data
|
@@ -88,7 +89,25 @@ class TestUser < Test::Unit::TestCase
|
|
88
89
|
assert @user.buddies.instance_of?(Array)
|
89
90
|
assert_equal 11, @user.buddies.size
|
90
91
|
end
|
91
|
-
|
92
|
+
|
93
|
+
def test_unknown_user_produces_error_data
|
94
|
+
bad_user = Nanowrimo::User.new("999999")
|
95
|
+
file = "test/fixtures/user_wc_error.xml"
|
96
|
+
FakeWeb.register_uri("#{Nanowrimo::API_URI}/wc/999999", :file => file)
|
97
|
+
bad_user.load
|
98
|
+
assert bad_user.has_error?
|
99
|
+
assert_equal "user not found or is inactive", bad_user.error
|
100
|
+
end
|
101
|
+
|
102
|
+
def test_unknown_user_produces_historical_error_data
|
103
|
+
bad_user = Nanowrimo::User.new("999999")
|
104
|
+
file = "test/fixtures/user_wc_history_error.xml"
|
105
|
+
FakeWeb.register_uri("#{Nanowrimo::API_URI}/wchistory/999999", :file => file)
|
106
|
+
bad_user.load_history
|
107
|
+
assert bad_user.has_error?
|
108
|
+
assert_equal "user not found or is inactive", bad_user.error
|
109
|
+
end
|
110
|
+
|
92
111
|
def profile_uri_setup
|
93
112
|
# this was a bit of weirdness - I had to curl -is the url in order to grab the headers, and
|
94
113
|
# even then it would register the file from FakeWeb as WWW::Mechanize::File, not WWW::Mechanize::Page
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nanowrimo
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 0.7.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joshua Clingenpeel
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-06-02 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -55,14 +55,20 @@ files:
|
|
55
55
|
- test/test_site.rb
|
56
56
|
- test/test_user.rb
|
57
57
|
- test/fixtures/genre_wc.xml
|
58
|
+
- test/fixtures/genre_wc_error.xml
|
58
59
|
- test/fixtures/genre_wc_history.xml
|
60
|
+
- test/fixtures/genre_wc_history_error.xml
|
59
61
|
- test/fixtures/region_wc.xml
|
62
|
+
- test/fixtures/region_wc_error.xml
|
60
63
|
- test/fixtures/region_wc_history.xml
|
64
|
+
- test/fixtures/region_wc_history_error.xml
|
61
65
|
- test/fixtures/site_wc.xml
|
62
66
|
- test/fixtures/site_wc_history.xml
|
63
67
|
- test/fixtures/user_page.htm
|
64
68
|
- test/fixtures/user_wc.xml
|
69
|
+
- test/fixtures/user_wc_error.xml
|
65
70
|
- test/fixtures/user_wc_history.xml
|
71
|
+
- test/fixtures/user_wc_history_error.xml
|
66
72
|
has_rdoc: true
|
67
73
|
homepage: http://nanowrimo.rubyforge.org
|
68
74
|
licenses: []
|