atdis 0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,390 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe ATDIS::Page do
4
+ describe "validations" do
5
+ context "two valid applications no paging" do
6
+ before :each do
7
+ ATDIS::Application.should_receive(:interpret).with(:description => "application1").and_return(double(:valid? => true))
8
+ ATDIS::Application.should_receive(:interpret).with(:description => "application2").and_return(double(:valid? => true))
9
+ end
10
+ let(:page) { ATDIS::Page.new(:results => [{:description => "application1"}, {:description => "application2"}]) }
11
+
12
+ it {page.should be_valid}
13
+
14
+ context "with pagination" do
15
+ before :each do
16
+ page.count = 2
17
+ page.no_results_per_page = 25
18
+ page.current_page_no = 1
19
+ page.total_no_results = 2
20
+ page.total_no_pages = 1
21
+ end
22
+ it { page.should be_valid }
23
+
24
+ context "count is not consistent" do
25
+ before :each do
26
+ page.count = 1
27
+ end
28
+ it do
29
+ page.should_not be_valid
30
+ page.errors.messages.should == {:count => ["is not the same as the number of applications returned"]}
31
+ end
32
+ end
33
+
34
+ context "count is larger than number of results per page" do
35
+ before :each do
36
+ page.no_results_per_page = 1
37
+ page.total_no_results = 1
38
+ end
39
+ it do
40
+ page.should_not be_valid
41
+ page.errors.messages.should == {:count => ["should not be larger than the number of results per page"]}
42
+ end
43
+ end
44
+
45
+ context "count is absent" do
46
+ before :each do
47
+ page.count = nil
48
+ end
49
+
50
+ it do
51
+ page.should_not be_valid
52
+ page.errors.messages.should == {:count => ["should be present if pagination is being used"]}
53
+ end
54
+ end
55
+
56
+ context "previous page number is pointing to a weird page number" do
57
+ before :each do
58
+ page.previous_page_no = 5
59
+ page.current_page_no = 2
60
+ page.total_no_results = 50
61
+ page.total_no_pages = 2
62
+ end
63
+ it do
64
+ page.should_not be_valid
65
+ page.errors.messages.should == {:previous_page_no => ["should be one less than current page number or null if first page"]}
66
+ end
67
+ end
68
+
69
+ context "previous page number if nil but not on first page" do
70
+ before :each do
71
+ page.current_page_no = 4
72
+ page.next_page_no = 5
73
+ page.previous_page_no = nil
74
+ page.total_no_results = 240
75
+ page.total_no_pages = 10
76
+ end
77
+ it do
78
+ page.should_not be_valid
79
+ page.errors.messages.should == {:previous_page_no => ["can't be null if not on the first page"]}
80
+ end
81
+ end
82
+
83
+ context "previous page number not nil but on first page" do
84
+ before :each do
85
+ page.current_page_no = 1
86
+ page.next_page_no = 2
87
+ page.previous_page_no = 0
88
+ page.total_no_results = 240
89
+ page.total_no_pages = 10
90
+ end
91
+ it do
92
+ page.should_not be_valid
93
+ page.errors.messages.should == {:previous_page_no => ["should be null if on the first page"]}
94
+ end
95
+ end
96
+
97
+ context "next page number is pointing to a weird page number" do
98
+ before :each do
99
+ page.next_page_no = 5
100
+ page.total_no_results = 50
101
+ page.total_no_pages = 2
102
+ end
103
+ it do
104
+ page.should_not be_valid
105
+ page.errors.messages.should == {:next_page_no => ["should be one greater than current page number or null if last page"]}
106
+ end
107
+ end
108
+
109
+ context "next page number is nil but not on last page" do
110
+ before :each do
111
+ page.current_page_no = 4
112
+ page.previous_page_no = 3
113
+ page.next_page_no = nil
114
+ page.total_no_results = 140
115
+ page.total_no_pages = 6
116
+ end
117
+ it do
118
+ page.should_not be_valid
119
+ page.errors.messages.should == {:next_page_no => ["can't be null if not on the last page"]}
120
+ end
121
+ end
122
+
123
+ context "next page number is not nil but on last page" do
124
+ before :each do
125
+ page.previous_page_no = 3
126
+ page.current_page_no = 4
127
+ page.next_page_no = 5
128
+ page.total_no_results = 100
129
+ page.total_no_pages = 4
130
+ end
131
+ it do
132
+ page.should_not be_valid
133
+ page.errors.messages.should == {:next_page_no => ["should be null if on the last page"]}
134
+ end
135
+ end
136
+
137
+ context "current page is larger than the number of pages" do
138
+ before :each do
139
+ page.current_page_no = 2
140
+ page.previous_page_no = 1
141
+ page.next_page_no = 3
142
+ page.total_no_pages = 1
143
+ end
144
+ it do
145
+ page.should_not be_valid
146
+ page.errors.messages.should == {:current_page_no => ["is larger than the number of pages"]}
147
+ end
148
+ end
149
+
150
+ context "current page is zero" do
151
+ before :each do
152
+ page.current_page_no = 0
153
+ page.next_page_no = 1
154
+ end
155
+ it do
156
+ page.should_not be_valid
157
+ page.errors.messages.should == {:current_page_no => ["can not be less than 1"]}
158
+ end
159
+ end
160
+
161
+ context "total_no_results is larger than would be expected" do
162
+ before :each do
163
+ page.current_page_no = 1
164
+ page.next_page_no = 2
165
+ page.no_results_per_page = 25
166
+ page.total_no_pages = 4
167
+ page.total_no_results = 101
168
+ end
169
+ it do
170
+ page.should_not be_valid
171
+ page.errors.messages.should == {:total_no_results => ["is larger than can be retrieved through paging"]}
172
+ end
173
+ end
174
+
175
+ context "total no_results is less than would be expected" do
176
+ before :each do
177
+ page.current_page_no = 1
178
+ page.next_page_no = 2
179
+ page.no_results_per_page = 25
180
+ page.total_no_pages = 4
181
+ page.total_no_results = 75
182
+ end
183
+ it do
184
+ page.should_not be_valid
185
+ page.errors.messages.should == {:total_no_results => ["could fit into a smaller number of pages"]}
186
+ end
187
+ end
188
+ end
189
+ end
190
+
191
+ context "one valid application out of two no paging" do
192
+ before :each do
193
+ ATDIS::Application.should_receive(:interpret).with(:description => "application1").and_return(double(:valid? => true))
194
+ ATDIS::Application.should_receive(:interpret).with(:description => "application2").and_return(double(:valid? => false))
195
+ end
196
+ let(:page) { ATDIS::Page.new(:results => [{:description => "application1"}, {:description => "application2"}]) }
197
+
198
+ it do
199
+ page.should_not be_valid
200
+ page.errors.messages.should == {:results => ["is not valid"]}
201
+ end
202
+ end
203
+
204
+ context "two invalid applications no paging" do
205
+ before :each do
206
+ ATDIS::Application.should_receive(:interpret).with(:description => "application1").and_return(double(:valid? => false))
207
+ ATDIS::Application.should_receive(:interpret).with(:description => "application2").and_return(double(:valid? => false))
208
+ end
209
+ let(:page) { ATDIS::Page.new(:results => [{:description => "application1"}, {:description => "application2"}]) }
210
+
211
+ it do
212
+ page.should_not be_valid
213
+ page.errors.messages.should == {:results => ["is not valid"]}
214
+ end
215
+ end
216
+ end
217
+
218
+ context "paging supported by vendor" do
219
+ context "read from a json string" do
220
+ let(:page) { ATDIS::Page.read_json(<<-EOF
221
+ {
222
+ "response": [
223
+ {
224
+ "application": {
225
+ "description": "application1"
226
+ }
227
+ },
228
+ {
229
+ "application": {
230
+ "description": "application2"
231
+ }
232
+ }
233
+ ],
234
+ "count": 2,
235
+ "pagination": {
236
+ "previous": 1,
237
+ "next": 3,
238
+ "current": 2,
239
+ "per_page": 2,
240
+ "count": 50,
241
+ "pages": 25
242
+ }
243
+ }
244
+ EOF
245
+ )}
246
+ it ".results" do
247
+ application1 = double("Application")
248
+ application2 = double("Application")
249
+ ATDIS::Application.should_receive(:interpret).with(:application => {:description => "application1"}).and_return(application1)
250
+ ATDIS::Application.should_receive(:interpret).with(:application => {:description => "application2"}).and_return(application2)
251
+
252
+ page.results.should == [application1, application2]
253
+ end
254
+
255
+ it ".next" do
256
+ expect { page.next }.to raise_error "Can't use next_url when loaded with read_json"
257
+ end
258
+ end
259
+
260
+ context "read from a url" do
261
+ before :each do
262
+ # Mock network response
263
+ RestClient.should_receive(:get).with("http://www.council.nsw.gov.au/atdis/1.0/applications.json").and_return(double(:to_str => <<-EOF
264
+ {
265
+ "response": [
266
+ {
267
+ "application": {
268
+ "description": "application1"
269
+ }
270
+ },
271
+ {
272
+ "application": {
273
+ "description": "application2"
274
+ }
275
+ }
276
+ ]
277
+ }
278
+ EOF
279
+ ))
280
+ end
281
+
282
+ let(:applications_results) { ATDIS::Page.read_url("http://www.council.nsw.gov.au/atdis/1.0/applications.json") }
283
+
284
+ it ".results" do
285
+ application1 = double("Application")
286
+ application2 = double("Application")
287
+ ATDIS::Application.should_receive(:interpret).with(:application => {:description => "application1"}).and_return(application1)
288
+ ATDIS::Application.should_receive(:interpret).with(:application => {:description => "application2"}).and_return(application2)
289
+
290
+ applications_results.results.should == [application1, application2]
291
+ end
292
+
293
+ it ".next" do
294
+ applications_results.next.should be_nil
295
+ end
296
+
297
+ it ".previous_page_no" do
298
+ applications_results.previous_page_no.should be_nil
299
+ end
300
+
301
+ it ".next_page_no" do
302
+ applications_results.next_page_no.should be_nil
303
+ end
304
+
305
+ it ".current_page_no" do
306
+ applications_results.current_page_no.should be_nil
307
+ end
308
+
309
+ it ".no_results_per_page" do
310
+ applications_results.no_results_per_page.should be_nil
311
+ end
312
+
313
+ it ".total_no_results" do
314
+ applications_results.total_no_results.should be_nil
315
+ end
316
+
317
+ it ".total_no_pages" do
318
+ applications_results.total_no_pages.should be_nil
319
+ end
320
+ end
321
+ end
322
+
323
+ context "paging supported by vendor" do
324
+ before :each do
325
+ RestClient.should_receive(:get).with("http://www.council.nsw.gov.au/atdis/1.0/applications.json?page=2").and_return(double(:to_str => <<-EOF
326
+ {
327
+ "response": [
328
+ {
329
+ "application": {
330
+ "description": "application1"
331
+ }
332
+ },
333
+ {
334
+ "application": {
335
+ "description": "application2"
336
+ }
337
+ }
338
+ ],
339
+ "count": 2,
340
+ "pagination": {
341
+ "previous": 1,
342
+ "next": 3,
343
+ "current": 2,
344
+ "per_page": 2,
345
+ "count": 50,
346
+ "pages": 25
347
+ }
348
+ }
349
+ EOF
350
+ ))
351
+ end
352
+
353
+ let(:applications_results) { ATDIS::Page.read_url("http://www.council.nsw.gov.au/atdis/1.0/applications.json?page=2") }
354
+
355
+ it ".previous_page_no" do
356
+ applications_results.previous_page_no.should == 1
357
+ end
358
+
359
+ it ".next_page_no" do
360
+ applications_results.next_page_no.should == 3
361
+ end
362
+
363
+ it ".current_page_no" do
364
+ applications_results.current_page_no.should == 2
365
+ end
366
+
367
+ it ".no_results_per_page" do
368
+ applications_results.no_results_per_page.should == 2
369
+ end
370
+
371
+ it ".total_no_results" do
372
+ applications_results.total_no_results.should == 50
373
+ end
374
+
375
+ it ".total_no_pages" do
376
+ applications_results.total_no_pages.should == 25
377
+ end
378
+
379
+ it ".next_url" do
380
+ applications_results.next_url.should == "http://www.council.nsw.gov.au/atdis/1.0/applications.json?page=3"
381
+ end
382
+
383
+ it ".next" do
384
+ n = double("Page")
385
+ applications_results
386
+ ATDIS::Page.should_receive(:read_url).with("http://www.council.nsw.gov.au/atdis/1.0/applications.json?page=3").and_return(n)
387
+ applications_results.next.should == n
388
+ end
389
+ end
390
+ end
@@ -0,0 +1,15 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe ATDIS::Person do
4
+ it ".name" do
5
+ ATDIS::Person.interpret(:name => "Tuttle").name.should == "Tuttle"
6
+ end
7
+
8
+ it ".role" do
9
+ ATDIS::Person.interpret(:role => "Heating Engineer").role.should == "Heating Engineer"
10
+ end
11
+
12
+ it ".contact" do
13
+ ATDIS::Person.interpret(:contact => "94-FLUSH").contact.should == "94-FLUSH"
14
+ end
15
+ end
@@ -0,0 +1,40 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe ATDIS::SeparatedURL do
4
+ describe "#merge" do
5
+ it "should add a new parameter" do
6
+ ATDIS::SeparatedURL.merge("http://foo.com/bar?foo=twenty&bar=12", :page => 2).should ==
7
+ "http://foo.com/bar?bar=12&foo=twenty&page=2"
8
+ end
9
+
10
+ it "should overwrite an existing one" do
11
+ ATDIS::SeparatedURL.merge("http://foo.com/bar?foo=twenty&bar=12", :bar => 24).should ==
12
+ "http://foo.com/bar?bar=24&foo=twenty"
13
+ end
14
+
15
+ it "should encode spaces for example" do
16
+ ATDIS::SeparatedURL.merge("http://foo.com/bar?foo=twenty&bar=12", :bar => "hello sir").should ==
17
+ "http://foo.com/bar?bar=hello+sir&foo=twenty"
18
+ end
19
+
20
+ it "should be fine if there are no parameters" do
21
+ ATDIS::SeparatedURL.merge("http://foo.com/bar", :page => 2).should ==
22
+ "http://foo.com/bar?page=2"
23
+ end
24
+
25
+ it "should be fine if there are no parameters" do
26
+ ATDIS::SeparatedURL.merge("http://foo.com/bar", {}).should ==
27
+ "http://foo.com/bar"
28
+ end
29
+
30
+ it "should be able to handle port numbers in URLS" do
31
+ ATDIS::SeparatedURL.merge("http://foo.com:3000/bar", {}).should ==
32
+ "http://foo.com:3000/bar"
33
+ end
34
+
35
+ it "should be able to handle https" do
36
+ ATDIS::SeparatedURL.merge("https://foo.com/bar", {}).should ==
37
+ "https://foo.com/bar"
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,18 @@
1
+ require 'simplecov'
2
+ require 'coveralls'
3
+
4
+ # Generate coverage locally in html as well as in coveralls.io
5
+ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
6
+ SimpleCov::Formatter::HTMLFormatter,
7
+ Coveralls::SimpleCov::Formatter
8
+ ]
9
+ SimpleCov.start
10
+
11
+ require 'rubygems'
12
+ require 'bundler/setup'
13
+
14
+ require 'atdis' # and any other gems you need
15
+
16
+ RSpec.configure do |config|
17
+ # some (optional) config here
18
+ end
metadata ADDED
@@ -0,0 +1,191 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: atdis
3
+ version: !ruby/object:Gem::Version
4
+ hash: 9
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ version: "0.1"
10
+ platform: ruby
11
+ authors:
12
+ - Matthew Landauer
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2013-09-11 00:00:00 Z
18
+ dependencies:
19
+ - !ruby/object:Gem::Dependency
20
+ name: bundler
21
+ prerelease: false
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ~>
26
+ - !ruby/object:Gem::Version
27
+ hash: 9
28
+ segments:
29
+ - 1
30
+ - 3
31
+ version: "1.3"
32
+ type: :development
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: rake
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ hash: 3
43
+ segments:
44
+ - 0
45
+ version: "0"
46
+ type: :development
47
+ version_requirements: *id002
48
+ - !ruby/object:Gem::Dependency
49
+ name: multi_json
50
+ prerelease: false
51
+ requirement: &id003 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ~>
55
+ - !ruby/object:Gem::Version
56
+ hash: 1
57
+ segments:
58
+ - 1
59
+ - 7
60
+ version: "1.7"
61
+ type: :runtime
62
+ version_requirements: *id003
63
+ - !ruby/object:Gem::Dependency
64
+ name: rest-client
65
+ prerelease: false
66
+ requirement: &id004 !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ hash: 3
72
+ segments:
73
+ - 0
74
+ version: "0"
75
+ type: :runtime
76
+ version_requirements: *id004
77
+ - !ruby/object:Gem::Dependency
78
+ name: rgeo-geojson
79
+ prerelease: false
80
+ requirement: &id005 !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ hash: 3
86
+ segments:
87
+ - 0
88
+ version: "0"
89
+ type: :runtime
90
+ version_requirements: *id005
91
+ - !ruby/object:Gem::Dependency
92
+ name: activemodel
93
+ prerelease: false
94
+ requirement: &id006 !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ~>
98
+ - !ruby/object:Gem::Version
99
+ hash: 5
100
+ segments:
101
+ - 3
102
+ version: "3"
103
+ type: :runtime
104
+ version_requirements: *id006
105
+ description: A ruby interface to the application tracking data interchange specification (ATDIS) API
106
+ email:
107
+ - matthew@openaustraliafoundation.org.au
108
+ executables: []
109
+
110
+ extensions: []
111
+
112
+ extra_rdoc_files: []
113
+
114
+ files:
115
+ - .gitignore
116
+ - .ruby-version
117
+ - .travis.yml
118
+ - Gemfile
119
+ - Guardfile
120
+ - LICENSE.txt
121
+ - README.md
122
+ - Rakefile
123
+ - atdis.gemspec
124
+ - docs/ATDIS-1.0.4 Application Tracking Data Interchange Specification.docx
125
+ - docs/ATDIS-1.0.4 Application Tracking Data Interchange Specification.pdf
126
+ - lib/atdis.rb
127
+ - lib/atdis/application.rb
128
+ - lib/atdis/document.rb
129
+ - lib/atdis/event.rb
130
+ - lib/atdis/feed.rb
131
+ - lib/atdis/location.rb
132
+ - lib/atdis/model.rb
133
+ - lib/atdis/page.rb
134
+ - lib/atdis/person.rb
135
+ - lib/atdis/separated_url.rb
136
+ - lib/atdis/validators.rb
137
+ - lib/atdis/version.rb
138
+ - spec/atdis/application_spec.rb
139
+ - spec/atdis/document_spec.rb
140
+ - spec/atdis/event_spec.rb
141
+ - spec/atdis/feed_spec.rb
142
+ - spec/atdis/location_spec.rb
143
+ - spec/atdis/model_spec.rb
144
+ - spec/atdis/page_spec.rb
145
+ - spec/atdis/person_spec.rb
146
+ - spec/atdis/separated_url_spec.rb
147
+ - spec/spec_helper.rb
148
+ homepage: http://github.com/openaustralia/atdis
149
+ licenses:
150
+ - MIT
151
+ post_install_message:
152
+ rdoc_options: []
153
+
154
+ require_paths:
155
+ - lib
156
+ required_ruby_version: !ruby/object:Gem::Requirement
157
+ none: false
158
+ requirements:
159
+ - - ">="
160
+ - !ruby/object:Gem::Version
161
+ hash: 3
162
+ segments:
163
+ - 0
164
+ version: "0"
165
+ required_rubygems_version: !ruby/object:Gem::Requirement
166
+ none: false
167
+ requirements:
168
+ - - ">="
169
+ - !ruby/object:Gem::Version
170
+ hash: 3
171
+ segments:
172
+ - 0
173
+ version: "0"
174
+ requirements: []
175
+
176
+ rubyforge_project:
177
+ rubygems_version: 1.8.25
178
+ signing_key:
179
+ specification_version: 3
180
+ summary: A ruby interface to the application tracking data interchange specification (ATDIS) API
181
+ test_files:
182
+ - spec/atdis/application_spec.rb
183
+ - spec/atdis/document_spec.rb
184
+ - spec/atdis/event_spec.rb
185
+ - spec/atdis/feed_spec.rb
186
+ - spec/atdis/location_spec.rb
187
+ - spec/atdis/model_spec.rb
188
+ - spec/atdis/page_spec.rb
189
+ - spec/atdis/person_spec.rb
190
+ - spec/atdis/separated_url_spec.rb
191
+ - spec/spec_helper.rb