autoparse 0.1.0 → 0.2.0

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.
@@ -26,6 +26,10 @@ describe AutoParse::Instance, 'with an empty schema' do
26
26
  @parser = AutoParse::EMPTY_SCHEMA
27
27
  end
28
28
 
29
+ it 'should have a nil URI' do
30
+ @parser.uri.should be_nil
31
+ end
32
+
29
33
  it 'should accept all inputs' do
30
34
  instance = @parser.new({
31
35
  "this" => "doesn't",
@@ -76,6 +80,10 @@ describe AutoParse::Instance, 'with the geo schema' do
76
80
  @parser = AutoParse.generate(@schema_data, @uri)
77
81
  end
78
82
 
83
+ it 'should have the correct URI' do
84
+ @parser.uri.should === @uri
85
+ end
86
+
79
87
  it 'should accept a valid geographic coordinate input' do
80
88
  instance = @parser.new({
81
89
  "latitude" => 37.422,
@@ -110,6 +118,16 @@ describe AutoParse::Instance, 'with the geo schema' do
110
118
  instance.longitude.should == -122.084
111
119
  end
112
120
 
121
+ it 'should alter output structure via generated mutators' do
122
+ instance = @parser.new
123
+ instance.latitude = 37.422
124
+ instance.longitude = -122.084
125
+ instance.to_hash.should == {
126
+ "latitude" => 37.422,
127
+ "longitude" => -122.084
128
+ }
129
+ end
130
+
113
131
  it 'should be coerceable to a Hash value' do
114
132
  instance = @parser.new({
115
133
  "latitude" => 37.422,
@@ -130,7 +148,6 @@ describe AutoParse::Instance, 'with the geo schema' do
130
148
  end
131
149
  end
132
150
 
133
-
134
151
  describe AutoParse::Instance, 'with the address schema' do
135
152
  before do
136
153
  @uri = Addressable::URI.new(
@@ -142,6 +159,10 @@ describe AutoParse::Instance, 'with the address schema' do
142
159
  @parser = AutoParse.generate(@schema_data, @uri)
143
160
  end
144
161
 
162
+ it 'should have the correct URI' do
163
+ @parser.uri.should === @uri
164
+ end
165
+
145
166
  it 'should accept a valid address input' do
146
167
  instance = @parser.new({
147
168
  "post-office-box" => "PO Box 3.14159",
@@ -204,6 +225,24 @@ describe AutoParse::Instance, 'with the address schema' do
204
225
  instance.country_name.should == "United States"
205
226
  end
206
227
 
228
+ it 'should alter output structure via generated mutators' do
229
+ instance = @parser.new
230
+ instance.post_office_box = "PO Box 3.14159"
231
+ instance.street_address = "1600 Amphitheatre Parkway"
232
+ instance.locality = "Mountain View"
233
+ instance.region = "CA"
234
+ instance.postal_code = "94043"
235
+ instance.country_name = "United States"
236
+ instance.to_hash.should == {
237
+ "post-office-box" => "PO Box 3.14159",
238
+ "street-address" => "1600 Amphitheatre Parkway",
239
+ "locality" => "Mountain View",
240
+ "region" => "CA",
241
+ "postal-code" => "94043",
242
+ "country-name" => "United States"
243
+ }
244
+ end
245
+
207
246
  it 'should be coerceable to a Hash value' do
208
247
  instance = @parser.new({
209
248
  "post-office-box" => "PO Box 3.14159",
@@ -235,6 +274,10 @@ describe AutoParse::Instance, 'with the person schema' do
235
274
  @parser = AutoParse.generate(@schema_data, @uri)
236
275
  end
237
276
 
277
+ it 'should have the correct URI' do
278
+ @parser.uri.should === @uri
279
+ end
280
+
238
281
  it 'should accept a valid person input' do
239
282
  instance = @parser.new({
240
283
  "name" => "Bob Aman",
@@ -293,6 +336,16 @@ describe AutoParse::Instance, 'with the person schema' do
293
336
  instance.age.should == 29
294
337
  end
295
338
 
339
+ it 'should alter output structure via generated mutators' do
340
+ instance = @parser.new
341
+ instance.name = "Bob Aman"
342
+ instance.age = 29
343
+ instance.to_hash.should == {
344
+ "name" => "Bob Aman",
345
+ "age" => 29
346
+ }
347
+ end
348
+
296
349
  it 'should be coerceable to a Hash value' do
297
350
  instance = @parser.new({
298
351
  "name" => "Bob Aman",
@@ -334,6 +387,11 @@ describe AutoParse::Instance, 'with the adult schema' do
334
387
  @adult_parser = AutoParse.generate(@adult_schema_data, @adult_uri)
335
388
  end
336
389
 
390
+ it 'should have the correct URI' do
391
+ @person_parser.uri.should === @person_uri
392
+ @adult_parser.uri.should === @adult_uri
393
+ end
394
+
337
395
  it 'should accept a valid person input' do
338
396
  instance = @adult_parser.new({
339
397
  "name" => "Bob Aman",
@@ -392,6 +450,16 @@ describe AutoParse::Instance, 'with the adult schema' do
392
450
  instance.age.should == 29
393
451
  end
394
452
 
453
+ it 'should alter output structure via generated mutators' do
454
+ instance = @adult_parser.new
455
+ instance.name = "Bob Aman"
456
+ instance.age = 29
457
+ instance.to_hash.should == {
458
+ "name" => "Bob Aman",
459
+ "age" => 29
460
+ }
461
+ end
462
+
395
463
  it 'should be coerceable to a Hash value' do
396
464
  instance = @adult_parser.new({
397
465
  "name" => "Bob Aman",
@@ -424,6 +492,10 @@ describe AutoParse::Instance, 'with the positive schema' do
424
492
  @positive_parser = AutoParse.generate(@positive_schema_data, @positive_uri)
425
493
  end
426
494
 
495
+ it 'should have the correct URI' do
496
+ @positive_parser.uri.should === @positive_uri
497
+ end
498
+
427
499
  it 'should not allow instantiation' do
428
500
  (lambda do
429
501
  instance = @positive_parser.new(-1000)
@@ -458,6 +530,11 @@ describe AutoParse::Instance, 'with the account schema' do
458
530
  @account_parser = AutoParse.generate(@account_schema_data, @account_uri)
459
531
  end
460
532
 
533
+ it 'should have the correct URI' do
534
+ @positive_parser.uri.should === @positive_uri
535
+ @account_parser.uri.should === @account_uri
536
+ end
537
+
461
538
  it 'should accept a valid account input' do
462
539
  instance = @account_parser.new({
463
540
  "accountNumber" => "12345",
@@ -508,6 +585,16 @@ describe AutoParse::Instance, 'with the account schema' do
508
585
  instance.balance.should == 1000
509
586
  end
510
587
 
588
+ it 'should alter output structure via generated mutators' do
589
+ instance = @account_parser.new
590
+ instance.account_number = "12345"
591
+ instance.balance = 1000
592
+ instance.to_hash.should == {
593
+ "accountNumber" => "12345",
594
+ "balance" => 1000
595
+ }
596
+ end
597
+
511
598
  it 'should be coerceable to a Hash value' do
512
599
  instance = @account_parser.new({
513
600
  "accountNumber" => "12345",
@@ -558,6 +645,39 @@ describe AutoParse::Instance, 'with the card schema' do
558
645
  @card_parser = AutoParse.generate(@card_schema_data, @card_uri)
559
646
  end
560
647
 
648
+ it 'should have the correct URI' do
649
+ @address_parser.uri.should === @address_uri
650
+ @geo_parser.uri.should === @geo_uri
651
+ @card_parser.uri.should === @card_uri
652
+ end
653
+
654
+ it 'should have the correct URI for anonymous nested objects' do
655
+ instance = @card_parser.new({
656
+ "givenName" => "Robert",
657
+ "familyName" => "Aman",
658
+ "org" => {
659
+ "organizationName" => "Google, Inc.",
660
+ "organizationUnit" => "Developer Relations"
661
+ }
662
+ })
663
+ # Anonymous schemas inherit the parent schema's URI.
664
+ instance.org.class.uri.should === @card_uri
665
+ end
666
+
667
+ it 'should have the correct URI for external nested objects' do
668
+ instance = @card_parser.new({
669
+ "givenName" => "Robert",
670
+ "familyName" => "Aman",
671
+ "adr" => {
672
+ "locality" => "Lavington",
673
+ "region" => "Nairobi",
674
+ "country-name" => "Kenya"
675
+ }
676
+ })
677
+ # External schemas have their own URI.
678
+ instance.adr.class.uri.should === @address_uri
679
+ end
680
+
561
681
  it 'should accept a valid card input' do
562
682
  instance = @card_parser.new({
563
683
  "givenName" => "Robert",
@@ -703,6 +823,58 @@ describe AutoParse::Instance, 'with the card schema' do
703
823
  instance.org.organization_unit.should == "Developer Relations"
704
824
  end
705
825
 
826
+ it 'should return nil for undefined object values' do
827
+ instance = @card_parser.new
828
+ instance.email.should be_nil
829
+ instance.tel.should be_nil
830
+ instance.org.should be_nil
831
+ instance.adr.should be_nil
832
+ instance.geo.should be_nil
833
+ end
834
+
835
+ it 'should alter output structure via generated mutators' do
836
+ instance = @card_parser.new
837
+ instance.given_name = "Robert"
838
+ instance.family_name = "Aman"
839
+ instance.additional_name = ["Danger"]
840
+ instance.nickname = "Bob"
841
+ instance.url = "https://plus.google.com/116452824309856782163"
842
+ instance.email = {}
843
+ instance.email.type = "work"
844
+ instance.email.value = "bobaman@google.com"
845
+ instance.tel = {}
846
+ instance.tel.type = "fake"
847
+ instance.tel.value = "867-5309"
848
+ instance.tz = "+03:00"
849
+ instance.logo =
850
+ "https://secure.gravatar.com/avatar/56ee28134dd0776825445e3551979b14"
851
+ instance.org = {}
852
+ instance.org.organization_name = "Google, Inc."
853
+ instance.org.organization_unit = "Developer Relations"
854
+ instance.to_hash.should == {
855
+ "givenName" => "Robert",
856
+ "familyName" => "Aman",
857
+ "additionalName" => ["Danger"],
858
+ "nickname" => "Bob",
859
+ "url" => "https://plus.google.com/116452824309856782163",
860
+ "email" => {
861
+ "type" => "work",
862
+ "value" => "bobaman@google.com"
863
+ },
864
+ "tel" => {
865
+ "type" => "fake",
866
+ "value" => "867-5309"
867
+ },
868
+ "tz" => "+03:00",
869
+ "logo" =>
870
+ "https://secure.gravatar.com/avatar/56ee28134dd0776825445e3551979b14",
871
+ "org" => {
872
+ "organizationName" => "Google, Inc.",
873
+ "organizationUnit" => "Developer Relations"
874
+ }
875
+ }
876
+ end
877
+
706
878
  it 'should be coerceable to a Hash value' do
707
879
  instance = @card_parser.new({
708
880
  "givenName" => "Robert",
@@ -780,6 +952,11 @@ describe AutoParse::Instance, 'with the calendar schema' do
780
952
  @calendar_parser = AutoParse.generate(@calendar_schema_data, @calendar_uri)
781
953
  end
782
954
 
955
+ it 'should have the correct URI' do
956
+ @geo_parser.uri.should === @geo_uri
957
+ @calendar_parser.uri.should === @calendar_uri
958
+ end
959
+
783
960
  it 'should accept a valid calendar input' do
784
961
  instance = @calendar_parser.new({
785
962
  "dtstart" => "1592-03-14T00:00:00Z",
@@ -862,6 +1039,36 @@ describe AutoParse::Instance, 'with the calendar schema' do
862
1039
  instance.geo.longitude.should == -122.084
863
1040
  end
864
1041
 
1042
+ it 'should return nil for undefined object values' do
1043
+ instance = @calendar_parser.new
1044
+ instance.geo.should be_nil
1045
+ end
1046
+
1047
+ it 'should alter output structure via generated mutators' do
1048
+ instance = @calendar_parser.new
1049
+ instance.dtstart = Time.utc(1592, 3, 14)
1050
+ instance.dtend = Time.utc(1592, 3, 14, 23, 59, 59)
1051
+ instance.summary = "Pi Day"
1052
+ instance.location = "Googleplex"
1053
+ instance.url = Addressable::URI.parse("http://www.piday.org/")
1054
+ instance.rrule = "FREQ=YEARLY"
1055
+ instance.geo = @geo_parser.new
1056
+ instance.geo.latitude = 37.422
1057
+ instance.geo.longitude = -122.084
1058
+ instance.to_hash.should == {
1059
+ "dtstart" => "1592-03-14T00:00:00Z",
1060
+ "dtend" => "1592-03-14T23:59:59Z",
1061
+ "summary" => "Pi Day",
1062
+ "location" => "Googleplex",
1063
+ "url" => "http://www.piday.org/",
1064
+ "rrule" => "FREQ=YEARLY",
1065
+ "geo" => {
1066
+ "latitude" => 37.422,
1067
+ "longitude" => -122.084
1068
+ }
1069
+ }
1070
+ end
1071
+
865
1072
  it 'should be coerceable to a Hash value' do
866
1073
  instance = @calendar_parser.new({
867
1074
  "dtstart" => "1592-03-14T00:00:00Z",
@@ -902,3 +1109,142 @@ describe AutoParse::Instance, 'with the calendar schema' do
902
1109
  )
903
1110
  end
904
1111
  end
1112
+
1113
+ describe AutoParse::Instance, 'with the node schema' do
1114
+ before do
1115
+ @uri = Addressable::URI.new(
1116
+ :scheme => 'file',
1117
+ :host => '',
1118
+ :path => File.expand_path(File.join(spec_dir, './data/node.json'))
1119
+ )
1120
+ @schema_data = JSON.parse(File.open(@uri.path, 'r') { |f| f.read })
1121
+ @parser = AutoParse.generate(@schema_data, @uri)
1122
+ end
1123
+
1124
+ it 'should have the correct URI' do
1125
+ @parser.uri.should === @uri
1126
+ end
1127
+
1128
+ it 'should accept a valid node input' do
1129
+ instance = @parser.new({
1130
+ "value" => 42,
1131
+ "left" => nil,
1132
+ "right" => nil
1133
+ })
1134
+ instance.should be_valid
1135
+ end
1136
+
1137
+ it 'should accept extra fields' do
1138
+ instance = @parser.new({
1139
+ "value" => "1",
1140
+ "left" => nil,
1141
+ "right" => nil,
1142
+ "extra" => "bonus!"
1143
+ })
1144
+ instance.should be_valid
1145
+ end
1146
+
1147
+ it 'should not accept an invalid node input' do
1148
+ instance = @parser.new({
1149
+ "value" => 42,
1150
+ "left" => 3.14,
1151
+ "right" => 2.71
1152
+ })
1153
+ instance.should_not be_valid
1154
+ end
1155
+
1156
+ it 'should accept a valid recursive node input' do
1157
+ instance = @parser.new({
1158
+ "value" => 42,
1159
+ "left" => {
1160
+ "value" => 3.14,
1161
+ "left" => nil,
1162
+ "right" => nil
1163
+ },
1164
+ "right" => {
1165
+ "value" => 2.71,
1166
+ "left" => nil,
1167
+ "right" => nil
1168
+ }
1169
+ })
1170
+ instance.should be_valid
1171
+ end
1172
+
1173
+ it 'should expose values via generated accessors' do
1174
+ instance = @parser.new({
1175
+ "value" => 42,
1176
+ "left" => {
1177
+ "value" => 3.14,
1178
+ "left" => nil,
1179
+ "right" => nil
1180
+ },
1181
+ "right" => {
1182
+ "value" => 2.71,
1183
+ "left" => nil,
1184
+ "right" => nil
1185
+ }
1186
+ })
1187
+ instance.value.should == 42
1188
+ instance.left.value.should == 3.14
1189
+ instance.right.value.should == 2.71
1190
+
1191
+ instance.left.left.should == nil
1192
+ instance.right.left.should == nil
1193
+ instance.left.right.should == nil
1194
+ instance.right.right.should == nil
1195
+ end
1196
+
1197
+ it 'should return nil for undefined object values' do
1198
+ instance = @parser.new
1199
+ instance.left.should be_nil
1200
+ instance.right.should be_nil
1201
+ end
1202
+
1203
+ it 'should alter output structure via generated mutators' do
1204
+ instance = @parser.new
1205
+ instance.value = 42
1206
+ instance.left = @parser.new
1207
+ instance.left.value = 3.14
1208
+ instance.left.left = nil
1209
+ instance.left.right = nil
1210
+ instance.right = @parser.new
1211
+ instance.right.value = 2.71
1212
+ instance.right.left = nil
1213
+ instance.right.right = nil
1214
+ instance.to_hash.should == {
1215
+ "value" => 42,
1216
+ "left" => {
1217
+ "value" => 3.14,
1218
+ "left" => nil,
1219
+ "right" => nil
1220
+ },
1221
+ "right" => {
1222
+ "value" => 2.71,
1223
+ "left" => nil,
1224
+ "right" => nil
1225
+ }
1226
+ }
1227
+ end
1228
+
1229
+ it 'should be coerceable to a Hash value' do
1230
+ instance = @parser.new({
1231
+ "value" => 42,
1232
+ "left" => nil,
1233
+ "right" => nil
1234
+ })
1235
+ instance.to_hash.should == {
1236
+ "value" => 42,
1237
+ "left" => nil,
1238
+ "right" => nil
1239
+ }
1240
+ end
1241
+
1242
+ it 'should convert to a JSON string' do
1243
+ instance = @parser.new({
1244
+ "value" => 42,
1245
+ "left" => nil,
1246
+ "right" => nil
1247
+ })
1248
+ instance.to_json.should == '{"left":null,"value":42,"right":null}'
1249
+ end
1250
+ end
@@ -0,0 +1,22 @@
1
+ {
2
+ "description" : "A union type",
3
+ "type" : ["null", {
4
+ "properties" : {
5
+ "chaos": {
6
+ "type":"string",
7
+ "format": "url"
8
+ }
9
+ }
10
+ }, {
11
+ "properties" : {
12
+ "chaos": {
13
+ "type":"string",
14
+ "format": "date-time"
15
+ }
16
+ }
17
+ }, {
18
+ "properties" : {
19
+ "chaos": {"type":["boolean", "number"]}
20
+ }
21
+ }]
22
+ }
@@ -0,0 +1,9 @@
1
+ {
2
+ "description" : "A binary tree node",
3
+ "type" : ["object", "null"],
4
+ "properties" : {
5
+ "left" : { "$ref" : "node.json" },
6
+ "right" : { "$ref" : "node.json" },
7
+ "value": { "type" : "any" }
8
+ }
9
+ }
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: autoparse
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 1
8
+ - 2
9
9
  - 0
10
- version: 0.1.0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Bob Aman
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-09-22 00:00:00 Z
18
+ date: 2011-10-05 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: addressable
@@ -151,11 +151,13 @@ files:
151
151
  - spec/data/adult.json
152
152
  - spec/data/calendar.json
153
153
  - spec/data/card.json
154
+ - spec/data/chaos.json
154
155
  - spec/data/geo.json
155
156
  - spec/data/hyper-schema.json
156
157
  - spec/data/interfaces.json
157
158
  - spec/data/json-ref.json
158
159
  - spec/data/links.json
160
+ - spec/data/node.json
159
161
  - spec/data/person.json
160
162
  - spec/data/positive.json
161
163
  - spec/data/schema.json