atdis 0.1 → 0.2

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.
@@ -24,16 +24,147 @@ describe ATDIS::Model do
24
24
  end
25
25
  end
26
26
 
27
+ describe ".map_field" do
28
+ let(:mappings) { { :foo => :bar, :a => :b, :info => { :foo => :bar2, :a => :b2, :c => :c2 } } }
29
+
30
+ context "one version of data" do
31
+ let(:data) { { :foo => 2, :a => 3, :d => 4, :info => { :foo => 2, :a => 3, :d => 4 } } }
32
+
33
+ it { ATDIS::Model.map_field(:bar, data, mappings).should == 2 }
34
+ it { ATDIS::Model.map_field(:b, data, mappings).should == 3 }
35
+ it { ATDIS::Model.map_field(:bar2, data, mappings).should == 2 }
36
+ it { ATDIS::Model.map_field(:b2, data, mappings).should == 3 }
37
+ it { ATDIS::Model.map_field(:c2, data, mappings).should be_nil }
38
+ end
39
+
40
+ context "another version of data" do
41
+ let(:data) { { :foo => 2, :a => 3, :d => 4 } }
42
+
43
+ it { ATDIS::Model.map_field(:bar, data, mappings).should == 2 }
44
+ it { ATDIS::Model.map_field(:b, data, mappings).should == 3 }
45
+ it { ATDIS::Model.map_field(:bar2, data, mappings).should be_nil }
46
+ it { ATDIS::Model.map_field(:b2, data, mappings).should be_nil }
47
+ it { ATDIS::Model.map_field(:c2, data, mappings).should be_nil }
48
+ end
49
+ end
50
+
51
+ describe ".unused_data" do
52
+ it do
53
+ ATDIS::Model.unused_data(
54
+ {
55
+ :foo => 2,
56
+ :a => 3,
57
+ :d => 4
58
+ },
59
+ {
60
+ :foo => :bar,
61
+ :a => :b
62
+ }).should ==
63
+ {
64
+ :d => 4
65
+ }
66
+ end
67
+
68
+ it do
69
+ ATDIS::Model.unused_data(
70
+ {
71
+ :foo => 2,
72
+ :a => 3,
73
+ :d => 4,
74
+ :info => {
75
+ :foo => 2,
76
+ :a => 3,
77
+ :d => 4
78
+ }
79
+ },
80
+ {
81
+ :foo => :bar,
82
+ :a => :b,
83
+ :info => {
84
+ :foo => :bar2,
85
+ :a => :b2
86
+ }
87
+ }).should ==
88
+ {
89
+ :d => 4,
90
+ :info => {
91
+ :d => 4
92
+ }
93
+ }
94
+ end
95
+ end
96
+
97
+ describe ".attribute_names_from_mappings" do
98
+ it do
99
+ # Doing this nastiness to support Ruby 1.8
100
+ h = ActiveSupport::OrderedHash.new
101
+ h[:foo] = :bar
102
+ h[:a] = :b
103
+ h2 = ActiveSupport::OrderedHash.new
104
+ h2[:foo] = :bar2
105
+ h2[:a] = :b2
106
+ h[:info] = h2
107
+ ATDIS::Model.attribute_names_from_mappings(h).should == [:bar, :b, :bar2, :b2]
108
+ end
109
+ end
110
+
27
111
  describe ".map_fields" do
28
112
  it do
29
- ATDIS::Model.map_fields({:foo => :bar, :a => :b}, {:foo => 2, :a => 3, :d => 4}).should ==
30
- {:bar => 2, :b => 3, :json_left_overs => {:d => 4}}
113
+ ATDIS::Model.map_fields(
114
+ {
115
+ :foo => 2,
116
+ :a => 3,
117
+ :d => 4
118
+ },
119
+ {
120
+ :foo => :bar,
121
+ :a => :b
122
+ }).should ==
123
+ {
124
+ :bar => 2,
125
+ :b => 3,
126
+ }
31
127
  end
32
128
 
33
129
  it do
34
- ATDIS::Model.map_fields({:foo => :bar, :a => :b, :info => {:foo => :bar2, :a => :b2}},
35
- {:foo => 2, :a => 3, :d => 4, :info => {:foo => 2, :a => 3, :d => 4}}).should ==
36
- {:bar => 2, :b => 3, :bar2 => 2, :b2 => 3, :json_left_overs => {:d => 4, :info => {:d => 4}}}
130
+ ATDIS::Model.map_fields(
131
+ {
132
+ :foo => 2,
133
+ :a => 3,
134
+ :d => 4,
135
+ :info => {
136
+ :foo => 2,
137
+ :a => 3,
138
+ :d => 4
139
+ }
140
+ },
141
+ {
142
+ :foo => :bar,
143
+ :a => :b,
144
+ :info => {
145
+ :foo => :bar2,
146
+ :a => :b2
147
+ }
148
+ }).should ==
149
+ {
150
+ :bar => 2,
151
+ :b => 3,
152
+ :bar2 => 2,
153
+ :b2 => 3
154
+ }
155
+ end
156
+ end
157
+
158
+ describe "#json_attribute" do
159
+ let(:model) { ATDIS::Model.new }
160
+ let(:mapping) { {:previous => :previous_page_no, :next => :next_page_no, :foo => {:bar => :apple, :foo => :orange}} }
161
+
162
+ it "simple case" do
163
+ model.json_attribute(:previous_page_no, 12, mapping).should == {:previous => 12}
164
+ end
165
+
166
+ it "with recursion" do
167
+ model.json_attribute(:apple, 12, mapping).should == {:foo => {:bar => 12}}
37
168
  end
38
169
  end
39
170
  end
@@ -1,6 +1,11 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
2
 
3
3
  describe ATDIS::Page do
4
+ it ".attribute_names" do
5
+ ATDIS::Page.attribute_names.should == ["results", "count", "previous_page_no", "next_page_no", "current_page_no",
6
+ "no_results_per_page", "total_no_results", "total_no_pages"]
7
+ end
8
+
4
9
  describe "validations" do
5
10
  context "two valid applications no paging" do
6
11
  before :each do
@@ -27,7 +32,7 @@ describe ATDIS::Page do
27
32
  end
28
33
  it do
29
34
  page.should_not be_valid
30
- page.errors.messages.should == {:count => ["is not the same as the number of applications returned"]}
35
+ page.errors.messages.should == {:count => [ATDIS::ErrorMessage["is not the same as the number of applications returned", "6.5"]]}
31
36
  end
32
37
  end
33
38
 
@@ -38,7 +43,7 @@ describe ATDIS::Page do
38
43
  end
39
44
  it do
40
45
  page.should_not be_valid
41
- page.errors.messages.should == {:count => ["should not be larger than the number of results per page"]}
46
+ page.errors.messages.should == {:count => [ATDIS::ErrorMessage["should not be larger than the number of results per page", "6.5"]]}
42
47
  end
43
48
  end
44
49
 
@@ -49,7 +54,7 @@ describe ATDIS::Page do
49
54
 
50
55
  it do
51
56
  page.should_not be_valid
52
- page.errors.messages.should == {:count => ["should be present if pagination is being used"]}
57
+ page.errors.messages.should == {:count => [ATDIS::ErrorMessage["should be present if pagination is being used", "6.5"]]}
53
58
  end
54
59
  end
55
60
 
@@ -62,7 +67,8 @@ describe ATDIS::Page do
62
67
  end
63
68
  it do
64
69
  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"]}
70
+ page.errors.messages.should == {:previous_page_no => [ATDIS::ErrorMessage["should be one less than current page number or null if first page", "6.5"]]}
71
+ page.json_errors.should == [[{:pagination => {:previous => 5}}, [ATDIS::ErrorMessage["should be one less than current page number or null if first page", "6.5"]]]]
66
72
  end
67
73
  end
68
74
 
@@ -76,7 +82,7 @@ describe ATDIS::Page do
76
82
  end
77
83
  it do
78
84
  page.should_not be_valid
79
- page.errors.messages.should == {:previous_page_no => ["can't be null if not on the first page"]}
85
+ page.errors.messages.should == {:previous_page_no => [ATDIS::ErrorMessage["can't be null if not on the first page", "6.5"]]}
80
86
  end
81
87
  end
82
88
 
@@ -90,7 +96,7 @@ describe ATDIS::Page do
90
96
  end
91
97
  it do
92
98
  page.should_not be_valid
93
- page.errors.messages.should == {:previous_page_no => ["should be null if on the first page"]}
99
+ page.errors.messages.should == {:previous_page_no => [ATDIS::ErrorMessage["should be null if on the first page", "6.5"]]}
94
100
  end
95
101
  end
96
102
 
@@ -102,7 +108,7 @@ describe ATDIS::Page do
102
108
  end
103
109
  it do
104
110
  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"]}
111
+ page.errors.messages.should == {:next_page_no => [ATDIS::ErrorMessage["should be one greater than current page number or null if last page", "6.5"]]}
106
112
  end
107
113
  end
108
114
 
@@ -116,7 +122,7 @@ describe ATDIS::Page do
116
122
  end
117
123
  it do
118
124
  page.should_not be_valid
119
- page.errors.messages.should == {:next_page_no => ["can't be null if not on the last page"]}
125
+ page.errors.messages.should == {:next_page_no => [ATDIS::ErrorMessage["can't be null if not on the last page", "6.5"]]}
120
126
  end
121
127
  end
122
128
 
@@ -130,7 +136,7 @@ describe ATDIS::Page do
130
136
  end
131
137
  it do
132
138
  page.should_not be_valid
133
- page.errors.messages.should == {:next_page_no => ["should be null if on the last page"]}
139
+ page.errors.messages.should == {:next_page_no => [ATDIS::ErrorMessage["should be null if on the last page", "6.5"]]}
134
140
  end
135
141
  end
136
142
 
@@ -143,7 +149,7 @@ describe ATDIS::Page do
143
149
  end
144
150
  it do
145
151
  page.should_not be_valid
146
- page.errors.messages.should == {:current_page_no => ["is larger than the number of pages"]}
152
+ page.errors.messages.should == {:current_page_no => [ATDIS::ErrorMessage["is larger than the number of pages", "6.5"]]}
147
153
  end
148
154
  end
149
155
 
@@ -154,7 +160,7 @@ describe ATDIS::Page do
154
160
  end
155
161
  it do
156
162
  page.should_not be_valid
157
- page.errors.messages.should == {:current_page_no => ["can not be less than 1"]}
163
+ page.errors.messages.should == {:current_page_no => [ATDIS::ErrorMessage["can not be less than 1", "6.5"]]}
158
164
  end
159
165
  end
160
166
 
@@ -168,7 +174,7 @@ describe ATDIS::Page do
168
174
  end
169
175
  it do
170
176
  page.should_not be_valid
171
- page.errors.messages.should == {:total_no_results => ["is larger than can be retrieved through paging"]}
177
+ page.errors.messages.should == {:total_no_results => [ATDIS::ErrorMessage["is larger than can be retrieved through paging", "6.5"]]}
172
178
  end
173
179
  end
174
180
 
@@ -182,7 +188,7 @@ describe ATDIS::Page do
182
188
  end
183
189
  it do
184
190
  page.should_not be_valid
185
- page.errors.messages.should == {:total_no_results => ["could fit into a smaller number of pages"]}
191
+ page.errors.messages.should == {:total_no_results => [ATDIS::ErrorMessage["could fit into a smaller number of pages", "6.5"]]}
186
192
  end
187
193
  end
188
194
  end
@@ -197,25 +203,121 @@ describe ATDIS::Page do
197
203
 
198
204
  it do
199
205
  page.should_not be_valid
200
- page.errors.messages.should == {:results => ["is not valid"]}
206
+ page.errors.messages.should == {:results => [ATDIS::ErrorMessage["is not valid", nil]]}
201
207
  end
202
208
  end
203
209
 
204
210
  context "two invalid applications no paging" do
211
+ let(:a1) { double(:valid? => false, :json_errors => [[{:dat_id => "null"}, ["can not be empty"]]]) }
212
+ let(:a2) { double(:valid? => false) }
205
213
  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))
214
+ ATDIS::Application.should_receive(:interpret).with(:description => "application1").and_return(a1)
215
+ ATDIS::Application.should_receive(:interpret).with(:description => "application2").and_return(a2)
208
216
  end
209
217
  let(:page) { ATDIS::Page.new(:results => [{:description => "application1"}, {:description => "application2"}]) }
210
218
 
211
219
  it do
212
220
  page.should_not be_valid
213
- page.errors.messages.should == {:results => ["is not valid"]}
221
+ page.errors.messages.should == {:results => [ATDIS::ErrorMessage["is not valid", nil]]}
222
+ end
223
+
224
+ it "the errors from the first errored application should be here" do
225
+ page.should_not be_valid
226
+ page.json_errors.should == [[{:response => {:dat_id => "null"}} , ["can not be empty"]]]
227
+ end
228
+
229
+ end
230
+ end
231
+
232
+ describe ".level_used?" do
233
+ describe "level 1" do
234
+ context "two applications" do
235
+ before :each do
236
+ ATDIS::Application.should_receive(:interpret).with(:description => "application1").and_return(double)
237
+ ATDIS::Application.should_receive(:interpret).with(:description => "application2").and_return(double)
238
+ end
239
+ let(:page) { ATDIS::Page.new(:results => [{:description => "application1"}, {:description => "application2"}]) }
240
+
241
+ it { page.level_used?(1).should be_true }
242
+ end
243
+
244
+ context "no L1 features used" do
245
+ let(:page) { ATDIS::Page.new }
246
+ it { page.level_used?(1).should be_false }
247
+ end
248
+ end
249
+
250
+ describe "level 2" do
251
+ context "two applications with only L1 features" do
252
+ let(:a1) { double }
253
+ let(:a2) { double }
254
+ before :each do
255
+ ATDIS::Application.should_receive(:interpret).with(:description => "application1").and_return(a1)
256
+ ATDIS::Application.should_receive(:interpret).with(:description => "application2").and_return(a2)
257
+ end
258
+ let(:page) { ATDIS::Page.new(:results => [{:description => "application1"}, {:description => "application2"}]) }
259
+
260
+ it do
261
+ a1.should_receive(:level_used?).with(2).and_return(false)
262
+ a2.should_receive(:level_used?).with(2).and_return(false)
263
+ page.level_used?(2).should be_false
264
+ end
265
+
266
+ context "L2 feature used on Page" do
267
+ before :each do
268
+ page.count = 2
269
+ end
270
+
271
+ it { page.level_used?(2).should be_true }
272
+ end
273
+ end
274
+
275
+ context "two applications with L2 features" do
276
+ let(:a1) { double }
277
+ let(:a2) { double }
278
+ before :each do
279
+ ATDIS::Application.should_receive(:interpret).with(:description => "application1").and_return(a1)
280
+ ATDIS::Application.should_receive(:interpret).with(:description => "application2").and_return(a2)
281
+ end
282
+ let(:page) { ATDIS::Page.new(:results => [{:description => "application1"}, {:description => "application2"}]) }
283
+
284
+ it "should ask the applications whether they have L2 features" do
285
+ a1.should_receive(:level_used?).with(2).and_return(true)
286
+ page.level_used?(2).should be_true
287
+ end
214
288
  end
215
289
  end
216
290
  end
217
291
 
218
292
  context "paging supported by vendor" do
293
+ context "read a from an invalid json string" do
294
+ let(:page) { ATDIS::Page.read_json(<<-EOF
295
+ {
296
+ "response": [
297
+ {
298
+ "application": {
299
+ "description": "application2"
300
+ }
301
+ }
302
+ ],
303
+ }
304
+ EOF
305
+ )}
306
+
307
+ it do
308
+ page.should_not be_valid
309
+ page.errors.messages.has_key?(:json).should be_true
310
+ page.errors.messages.count.should == 1
311
+ # The error messages returned by the library are different for different Ruby versions
312
+ ruby18_message = ATDIS::ErrorMessage['Invalid JSON: unexpected "}"', nil]
313
+ ruby19_message = ATDIS::ErrorMessage["Invalid JSON: 784: unexpected token at '{\n \"response\": [\n {\n \"application\": {\n \"description\": \"application2\"\n } \n }\n ],\n} \n'", nil]
314
+ ruby20_message = ATDIS::ErrorMessage["Invalid JSON: 795: unexpected token at '{\n \"response\": [\n {\n \"application\": {\n \"description\": \"application2\"\n } \n }\n ],\n} \n'", nil]
315
+ page.errors.messages[:json].count.should == 1
316
+ message = page.errors.messages[:json].first
317
+ (message == ruby18_message || message == ruby19_message || message == ruby20_message).should be_true
318
+ end
319
+ end
320
+
219
321
  context "read from a json string" do
220
322
  let(:page) { ATDIS::Page.read_json(<<-EOF
221
323
  {
@@ -1,6 +1,10 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
2
 
3
3
  describe ATDIS::Person do
4
+ it ".attribute_names" do
5
+ ATDIS::Person.attribute_names.should == ["name", "role", "contact"]
6
+ end
7
+
4
8
  it ".name" do
5
9
  ATDIS::Person.interpret(:name => "Tuttle").name.should == "Tuttle"
6
10
  end
metadata CHANGED
@@ -1,12 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: atdis
3
3
  version: !ruby/object:Gem::Version
4
- hash: 9
4
+ hash: 15
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 1
9
- version: "0.1"
8
+ - 2
9
+ version: "0.2"
10
10
  platform: ruby
11
11
  authors:
12
12
  - Matthew Landauer
@@ -14,12 +14,11 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2013-09-11 00:00:00 Z
17
+ date: 2013-09-20 00:00:00 Z
18
18
  dependencies:
19
19
  - !ruby/object:Gem::Dependency
20
20
  name: bundler
21
- prerelease: false
22
- requirement: &id001 !ruby/object:Gem::Requirement
21
+ version_requirements: &id001 !ruby/object:Gem::Requirement
23
22
  none: false
24
23
  requirements:
25
24
  - - ~>
@@ -29,12 +28,12 @@ dependencies:
29
28
  - 1
30
29
  - 3
31
30
  version: "1.3"
31
+ prerelease: false
32
32
  type: :development
33
- version_requirements: *id001
33
+ requirement: *id001
34
34
  - !ruby/object:Gem::Dependency
35
35
  name: rake
36
- prerelease: false
37
- requirement: &id002 !ruby/object:Gem::Requirement
36
+ version_requirements: &id002 !ruby/object:Gem::Requirement
38
37
  none: false
39
38
  requirements:
40
39
  - - ">="
@@ -43,12 +42,12 @@ dependencies:
43
42
  segments:
44
43
  - 0
45
44
  version: "0"
45
+ prerelease: false
46
46
  type: :development
47
- version_requirements: *id002
47
+ requirement: *id002
48
48
  - !ruby/object:Gem::Dependency
49
49
  name: multi_json
50
- prerelease: false
51
- requirement: &id003 !ruby/object:Gem::Requirement
50
+ version_requirements: &id003 !ruby/object:Gem::Requirement
52
51
  none: false
53
52
  requirements:
54
53
  - - ~>
@@ -58,12 +57,12 @@ dependencies:
58
57
  - 1
59
58
  - 7
60
59
  version: "1.7"
60
+ prerelease: false
61
61
  type: :runtime
62
- version_requirements: *id003
62
+ requirement: *id003
63
63
  - !ruby/object:Gem::Dependency
64
64
  name: rest-client
65
- prerelease: false
66
- requirement: &id004 !ruby/object:Gem::Requirement
65
+ version_requirements: &id004 !ruby/object:Gem::Requirement
67
66
  none: false
68
67
  requirements:
69
68
  - - ">="
@@ -72,12 +71,12 @@ dependencies:
72
71
  segments:
73
72
  - 0
74
73
  version: "0"
74
+ prerelease: false
75
75
  type: :runtime
76
- version_requirements: *id004
76
+ requirement: *id004
77
77
  - !ruby/object:Gem::Dependency
78
78
  name: rgeo-geojson
79
- prerelease: false
80
- requirement: &id005 !ruby/object:Gem::Requirement
79
+ version_requirements: &id005 !ruby/object:Gem::Requirement
81
80
  none: false
82
81
  requirements:
83
82
  - - ">="
@@ -86,12 +85,12 @@ dependencies:
86
85
  segments:
87
86
  - 0
88
87
  version: "0"
88
+ prerelease: false
89
89
  type: :runtime
90
- version_requirements: *id005
90
+ requirement: *id005
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: activemodel
93
- prerelease: false
94
- requirement: &id006 !ruby/object:Gem::Requirement
93
+ version_requirements: &id006 !ruby/object:Gem::Requirement
95
94
  none: false
96
95
  requirements:
97
96
  - - ~>
@@ -100,8 +99,9 @@ dependencies:
100
99
  segments:
101
100
  - 3
102
101
  version: "3"
102
+ prerelease: false
103
103
  type: :runtime
104
- version_requirements: *id006
104
+ requirement: *id006
105
105
  description: A ruby interface to the application tracking data interchange specification (ATDIS) API
106
106
  email:
107
107
  - matthew@openaustraliafoundation.org.au
@@ -121,8 +121,8 @@ files:
121
121
  - README.md
122
122
  - Rakefile
123
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
124
+ - docs/ATDIS-1.0.7 Application Tracking Data Interchange Specification (v1.0).doc
125
+ - docs/ATDIS-1.0.7 Application Tracking Data Interchange Specification (v1.0).pdf
126
126
  - lib/atdis.rb
127
127
  - lib/atdis/application.rb
128
128
  - lib/atdis/document.rb