unapi 0.0.3 → 0.0.4

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.
data/lib/unapi/page.rb CHANGED
@@ -48,7 +48,10 @@ module UnAPI
48
48
  uris = []
49
49
  return uris if not @document
50
50
  @document.find_all('span') do |span|
51
- if span['class'] == 'unapi-uri' and span['title']
51
+ next unless span['class'] and span['title']
52
+ # can have multiple css classes
53
+ classes = span['class'].split(/\s+/)
54
+ if classes.member? 'unapi-uri'
52
55
  uris << span['title']
53
56
  end
54
57
  end
data/lib/unapi/service.rb CHANGED
@@ -41,7 +41,7 @@ module UnAPI
41
41
  format_esc = CGI.escape(format)
42
42
  request_url = @url + "?uri=#{uri_esc}&format=#{format_esc}"
43
43
  @document = nil
44
- @status_code, body, @content_type = Utils.get url
44
+ @status_code, body, @content_type = Utils.get request_url
45
45
  @last_url = request_url
46
46
  return body
47
47
  end
@@ -7,9 +7,10 @@ module UnAPI
7
7
  class Message < String
8
8
  attr_accessor :url
9
9
 
10
- def initialize(string, url=nil)
10
+ def initialize(string, url=nil, advice=nil)
11
11
  super string
12
12
  @url = url
13
+ @advice = advice
13
14
  end
14
15
  end
15
16
 
@@ -19,12 +20,32 @@ module UnAPI
19
20
  class Fail < Message
20
21
  end
21
22
 
23
+ class Warn < Message
24
+ end
25
+
22
26
  class ValidatorHandler
23
- attr_reader :passes, :failures
27
+ attr_reader :passes, :failures, :warnings
24
28
 
25
29
  def initialize
26
30
  @passes = 0
27
31
  @failures = 0
32
+ @warnings = 0
33
+ end
34
+
35
+ def test(ok, msg, url=nil, advice=nil)
36
+ if ok
37
+ pass(msg, url)
38
+ else
39
+ fail(msg, url, advice)
40
+ end
41
+ end
42
+
43
+ def check(ok, msg, url=nil, advice=nil)
44
+ if ok
45
+ pass(msg, url)
46
+ else
47
+ warn(msg, url, advice)
48
+ end
28
49
  end
29
50
 
30
51
  def pass(msg, url=nil)
@@ -32,9 +53,14 @@ module UnAPI
32
53
  handle Pass.new(msg, url)
33
54
  end
34
55
 
35
- def fail(msg, url=nil)
56
+ def fail(msg, url=nil, advice=nil)
36
57
  @failures += 1
37
- handle Fail.new(msg, url)
58
+ handle Fail.new(msg, url, advice)
59
+ end
60
+
61
+ def warn(msg, url=nil, advice=nil)
62
+ @warnings += 1
63
+ handle Warn.new(msg, url, advice)
38
64
  end
39
65
 
40
66
  def count
@@ -70,7 +96,6 @@ module UnAPI
70
96
  end
71
97
 
72
98
  class Validator
73
-
74
99
  def initialize(handler)
75
100
  @handler = handler
76
101
  end
@@ -86,12 +111,18 @@ module UnAPI
86
111
  def check_service(page)
87
112
  # make sure there is a service url
88
113
  service_url = page.service_url
89
- test service_url, "<link> url for unapi service", service_url
90
- return unless service_url
114
+ @handler.test(
115
+ service_url,
116
+ "<link> url for unapi service",
117
+ service_url,
118
+ "add an unAPI <link> element to the <head> of your html document")
91
119
 
92
120
  # get the unapi service from the page
93
121
  service = page.service
94
122
 
123
+ # no shoes, no shirt, no service
124
+ return nil if not service
125
+
95
126
  # make sure service responds to a format request
96
127
  formats = service.formats
97
128
  check_formats(formats, service, 'unapi service')
@@ -100,8 +131,12 @@ module UnAPI
100
131
  def check_uris(page)
101
132
  # look for unap-uris on the page
102
133
  service = page.service
134
+ if not service:
135
+ @handler.fail "couldn't find service"
136
+ end
137
+
103
138
  uris = page.uris
104
- test uris.length > 0, "found unapi-uris on the page"
139
+ @handler.test uris.length > 0, "found unapi-uris on the page"
105
140
 
106
141
  # go through each identifier
107
142
  uris.each do |uri|
@@ -113,26 +148,26 @@ module UnAPI
113
148
 
114
149
  # request an invalid format
115
150
  content = service.get_uri_in_format(uri, 'thisformatdoesnotexist')
116
- test service.status_code == 415,
151
+ @handler.check service.status_code == 415,
117
152
  "request for uri #{uri} with bad format returns 415"
118
153
 
119
154
  # verify that each format is available
120
155
  content = ''
121
156
  formats.each do |format|
122
157
  content = service.get_uri_in_format(uri, format.name)
123
- test service.status_code == 200,
158
+ @handler.check service.status_code == 200,
124
159
  "request for #{uri} content responded with 200 status code",
125
160
  service.last_url
126
- test (content != nil and content.length > 0),
161
+ @handler.test((content != nil and content.length > 0),
127
162
  "request for #{uri} returned data",
128
- service.last_url
129
- test service.content_type == format.type,
163
+ service.last_url)
164
+ @handler.test service.content_type == format.type,
130
165
  "request for #{uri} returned mime type #{service.content_type}",
131
166
  service.last_url
132
167
 
133
168
  # request an invalid uri with a valid format
134
169
  service.get_uri_in_format('invaliduriinvaliduri', format.name)
135
- test service.status_code == 404,
170
+ @handler.check service.status_code == 404,
136
171
  "request for invalid uri with valid format #{format.name} " +
137
172
  "returned 404", service.last_url
138
173
  end
@@ -140,16 +175,16 @@ module UnAPI
140
175
  end
141
176
 
142
177
  def check_formats(formats, service, target)
143
- test service.content_type == 'application/xml',
178
+ @handler.test service.content_type == 'application/xml',
144
179
  "formats for #{target} has content-type #{service.content_type}",
145
- service.last_url
180
+ service.last_url,
146
181
 
147
182
  if target == 'unapi service'
148
- test service.status_code == 200,
183
+ @handler.check service.status_code == 200,
149
184
  "formats request for #{target} returned 200 status code",
150
185
  service.last_url
151
186
  else
152
- test service.status_code == 300,
187
+ @handler.check service.status_code == 300,
153
188
  "formats request for #{target} return 300 status code",
154
189
  service.last_url
155
190
  end
@@ -159,9 +194,9 @@ module UnAPI
159
194
  count = 0
160
195
  formats.each do |format|
161
196
  count += 1
162
- test format.name,
197
+ @handler.test format.name,
163
198
  "format #{count} for #{target} has a name"
164
- test format.type,
199
+ @handler.test format.type,
165
200
  "format #{count} for #{target} has a type"
166
201
  end
167
202
  else
@@ -169,14 +204,6 @@ module UnAPI
169
204
  end
170
205
  end
171
206
 
172
- def test(ok, msg, url=nil)
173
- if ok
174
- @handler.pass(msg, url)
175
- else
176
- @handler.fail(msg, url)
177
- end
178
- end
179
-
180
207
  end
181
208
  end
182
209
 
data/test/index.html CHANGED
@@ -490,7 +490,7 @@ Sign in to get <a href=/exec/obidos/flex-sign-in?opt=a&page=recs/sign-in-secure.
490
490
  <br><br>
491
491
  <div class="small">
492
492
  <b class="h1">2006 Spring Preview: What to Read Next</b><br clear=left>
493
- <span class="unapi-uri" title="urn:isbn:0553804790">
493
+ <span class="unapi-uri display" title="urn:isbn:0553804790">
494
494
  <a href="http://www.amazon.com/exec/obidos/ASIN/0553804790">
495
495
  <img src="http://images.amazon.com/images/P/0553804790.01.37TRZZZZ.jpg" width=81 height=121 align=left valign=top hspace=5 vspace=3 border=0 border=0>
496
496
  </a>
@@ -92,11 +92,11 @@ class UnAPIServlet < WEBrick::HTTPServlet::AbstractServlet
92
92
  end
93
93
 
94
94
 
95
- # run this file and it will start an http server on port 9999
96
- # with the servlet at http://localhost:9999/unapi
95
+ # run this file and it will start an http server on port 9000
96
+ # with the servlet at http://localhost:9000/unapi
97
97
 
98
98
  if $0 == __FILE__
99
- server = WEBrick::HTTPServer.new(:Port => 9999, :DocumentRoot => 'test')
99
+ server = WEBrick::HTTPServer.new(:Port => 9000, :DocumentRoot => 'test')
100
100
  server.mount("/unapi", UnAPIServlet)
101
101
  trap("INT") {server.shutdown}
102
102
  server.start
metadata CHANGED
@@ -1,13 +1,13 @@
1
- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  rubygems_version: 0.8.11
3
3
  specification_version: 1
4
4
  name: unapi
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.0.3
7
- date: 2006-03-14 00:00:00 -05:00
6
+ version: 0.0.4
7
+ date: 2006-03-19 00:00:00 -06:00
8
8
  summary: A library for working with the unapi protocol
9
9
  require_paths:
10
- - lib
10
+ - lib
11
11
  email: ehs@pobox.com
12
12
  homepage: http://www.textualize.com/unapi
13
13
  rubyforge_project:
@@ -18,46 +18,43 @@ bindir: bin
18
18
  has_rdoc: true
19
19
  required_ruby_version: !ruby/object:Gem::Version::Requirement
20
20
  requirements:
21
- - - ">"
22
- - !ruby/object:Gem::Version
23
- version: 0.0.0
21
+ -
22
+ - ">"
23
+ - !ruby/object:Gem::Version
24
+ version: 0.0.0
24
25
  version:
25
26
  platform: ruby
26
27
  signing_key:
27
28
  cert_chain:
28
29
  authors:
29
- - Ed Summers
30
+ - Ed Summers
30
31
  files:
31
- - lib/unapi
32
- - lib/unapi.rb
33
- - lib/unapi/validator.rb
34
- - lib/unapi/service.rb
35
- - lib/unapi/utils.rb
36
- - lib/unapi/format.rb
37
- - lib/unapi/page.rb
38
- - test/unapi_servlet.rb
39
- - test/index.html
40
- - test/unapi_test.rb
41
- - test/validate_test.rb
32
+ - lib/unapi
33
+ - lib/unapi.rb
34
+ - lib/unapi/format.rb
35
+ - lib/unapi/page.rb
36
+ - lib/unapi/service.rb
37
+ - lib/unapi/utils.rb
38
+ - lib/unapi/validator.rb
39
+ - test/index.html
40
+ - test/unapi_servlet.rb
41
+ - test/unapi_test.rb
42
+ - test/validate_test.rb
42
43
  test_files:
43
- - test.rb
44
+ - test.rb
44
45
  rdoc_options: []
45
-
46
46
  extra_rdoc_files: []
47
-
48
47
  executables: []
49
-
50
48
  extensions: []
51
-
52
49
  requirements: []
53
-
54
50
  dependencies:
55
- - !ruby/object:Gem::Dependency
56
- name: rubyful_soup
57
- version_requirement:
58
- version_requirements: !ruby/object:Gem::Version::Requirement
59
- requirements:
60
- - - ">="
61
- - !ruby/object:Gem::Version
62
- version: 1.0.4
63
- version:
51
+ - !ruby/object:Gem::Dependency
52
+ name: rubyful_soup
53
+ version_requirement:
54
+ version_requirements: !ruby/object:Gem::Version::Requirement
55
+ requirements:
56
+ -
57
+ - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: 1.0.4
60
+ version: