hal-interpretation 1.2.0 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 52c231b40d2b58c3c0b1a5c3256b1b3b4a9d2588
4
- data.tar.gz: ae6b29f0cb4de045685d0934d37e84c555314ab7
3
+ metadata.gz: ddd4f4d59ce42087dfde97336e8ff4c2cd32a99f
4
+ data.tar.gz: c922581f34ac914205b687a8bbabc7be87678377
5
5
  SHA512:
6
- metadata.gz: 4d8c1cf2fc169bc09f4847b41ef4a25101cbe0ded25b5f0a7d3f25d06503508ced8a4cda4b21a0db80f9f15a5fe5a6e3d8acaacfe7937b7c9f4beff7e4311e71
7
- data.tar.gz: 44f5146b86da19be5961c16c5ec4aed8ffc7ae0de5a95c576951e6805285a12b91bccdf95058fe8ac96d992efb98f432caf3618ceea8a6a9a60d38283f8629d4
6
+ metadata.gz: a52862249331ca2ebce1be637e8c44cf6d1d2073f95922b857e99400b4666a3356751849fb3a7b45d8967163b941c9be6256f679aa79488b826486177fd68319
7
+ data.tar.gz: 45c8213ca0346ea609bd3c57d767969b03590211c5396b44402ab3c09b241d6eb26db89900978768d13fec298dd7f1129e3453e5af8967f44ecc720fa75aeedd
@@ -37,6 +37,8 @@ module HalInterpretation
37
37
  context = opts.fetch(:context, self)
38
38
 
39
39
  raw_val = context.instance_exec from, &fetcher
40
+ return [] unless raw_val
41
+
40
42
  val = context.instance_exec raw_val, &value_coercion
41
43
 
42
44
  to.public_send "#{attr}=", val
@@ -54,13 +54,23 @@ module HalInterpretation
54
54
 
55
55
  def apply_validations(an_item)
56
56
  an_item.valid?
57
+
57
58
  an_item.errors.each do |attr, msg|
58
- @problems << "#{json_path_for attr} #{msg}"
59
+ leader = begin
60
+ json_path_for attr
61
+ rescue KeyError
62
+ "#{attr} [which is not directly extracted from JSON]"
63
+ end
64
+
65
+ @problems << leader + " " + msg
59
66
  end
60
67
  end
61
68
 
62
69
  def json_path_for(attr)
63
- json_pointer_join(location, extractor_for(attr).location)
70
+ extractor = extractor_for(attr)
71
+ raise KeyError unless extractor
72
+
73
+ json_pointer_join(location, extractor.location)
64
74
  end
65
75
 
66
76
  def json_pointer_join(head, tail)
@@ -71,4 +81,4 @@ module HalInterpretation
71
81
  end
72
82
  end
73
83
 
74
- end
84
+ end
@@ -1,3 +1,3 @@
1
1
  module HalInterpretation
2
- VERSION = "1.2.0"
2
+ VERSION = "1.3.0"
3
3
  end
@@ -64,9 +64,26 @@ describe HalInterpretation::Extractor do
64
64
  specify { expect(target.bday).to eq Time.utc(2013,10,10,12,13,14) }
65
65
  end
66
66
 
67
+ context "basic attr" do
68
+ subject(:extractor) { described_class.new(attr: "age") }
67
69
 
68
- let(:target) { Struct.new(:first_name, :bday, :parent, :seq).new }
70
+ before do extractor.extract(from: source, to: target) end
71
+
72
+ specify { expect(target.age).to eq 1 }
73
+ end
74
+
75
+ context "missing attr" do
76
+ subject(:extractor) { described_class.new(attr: "seq") }
77
+
78
+ before do extractor.extract(from: source, to: target) end
79
+
80
+ specify { expect(target.seq).to be_nil }
81
+ end
82
+
83
+
84
+ let(:target) { Struct.new(:first_name, :bday, :parent, :seq, :age).new }
69
85
  let(:source) { HalClient::Representation.new(parsed_json: {
86
+ "age" => 1,
70
87
  "firstName" => "Alice",
71
88
  "bday" => "2013-10-10T12:13:14Z",
72
89
  "_links" => {
@@ -13,6 +13,16 @@ describe HalInterpretation do
13
13
  extract :latitude, from: "/geo/latitude"
14
14
  extract :up, with: ->(hal_repr){hal_repr.related_hrefs("up").first}, from: "/_links/up"
15
15
  extract :bday, coercion: ->(val){ Time.parse(val) }
16
+ extract :seq, with: ->(_) { next_seq_num }
17
+
18
+ def initialize(*args)
19
+ @cur_seq_num = 0
20
+ super
21
+ end
22
+
23
+ def next_seq_num
24
+ @cur_seq_num += 1
25
+ end
16
26
  end }
17
27
 
18
28
  context "valid single item" do
@@ -33,6 +43,7 @@ describe HalInterpretation do
33
43
  specify { expect(interpreter.items.first.latitude).to eq 39.1 }
34
44
  specify { expect(interpreter.items.first.up).to eq "/foo" }
35
45
  specify { expect(interpreter.items.first.bday).to eq Time.utc(2013,12,11,10,9,8) }
46
+ specify { expect(interpreter.items.first.seq).to eq 1 }
36
47
  specify { expect(interpreter.problems).to be_empty }
37
48
  end
38
49
 
@@ -61,6 +72,8 @@ describe HalInterpretation do
61
72
  specify { expect(interpreter.items).to have(2).items }
62
73
  specify { expect(interpreter.items).to include item_named "foo" }
63
74
  specify { expect(interpreter.items).to include item_named "bar" }
75
+ specify { expect(interpreter.items[0].seq).to eq 1 }
76
+ specify { expect(interpreter.items[1].seq).to eq 2 }
64
77
 
65
78
  matcher :item_named do |expected_name|
66
79
  match do |obj|
@@ -128,6 +141,42 @@ describe HalInterpretation do
128
141
  .to include matching(%r(/geo/latitude\b)).and(match(/\bblank\b/i)) }
129
142
  end
130
143
 
144
+ context "missing non-required member" do
145
+ let(:json_doc) { <<-JSON }
146
+ { "name": "nowhere"
147
+ ,"geo": {
148
+ "latitude": 13.12
149
+ }
150
+ ,"_links": {
151
+ "up": { "href": "http://example.com/" }
152
+ }
153
+ }
154
+ JSON
155
+
156
+ specify { expect(interpreter.problems).to be_empty }
157
+ end
158
+
159
+ context "validation failure on unmapped attr" do
160
+ let(:json_doc) { <<-JSON }
161
+ { "name": "nowhere"
162
+ ,"geo": {
163
+ "latitude": 13.12
164
+ }
165
+ ,"_links": {
166
+ "up": { "href": "http://example.com/" }
167
+ }
168
+ }
169
+ JSON
170
+
171
+ before do
172
+ test_item_class.class_eval "validates :hair, presence: true"
173
+ end
174
+
175
+ specify { expect{interpreter.items}
176
+ .to raise_exception HalInterpretation::InvalidRepresentationError}
177
+ specify { expect(interpreter.problems)
178
+ .to include matching(/hair/).and(match(/\bblank\b/i)) }
179
+ end
131
180
 
132
181
  context "non-json doc" do
133
182
  let(:non_json_doc) { "what's json" }
@@ -141,7 +190,7 @@ describe HalInterpretation do
141
190
  let(:test_item_class) { Class.new do
142
191
  include ActiveModel::Validations
143
192
 
144
- attr_accessor :name, :latitude, :up, :bday
193
+ attr_accessor :name, :latitude, :up, :bday, :seq, :hair
145
194
 
146
195
  def initialize
147
196
  yield self
metadata CHANGED
@@ -1,139 +1,139 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hal-interpretation
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Williams
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-18 00:00:00.000000000 Z
11
+ date: 2014-09-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: hal-client
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>'
17
+ - - ">"
18
18
  - !ruby/object:Gem::Version
19
19
  version: '2.2'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '>'
24
+ - - ">"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '2.2'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: hana
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ~>
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
33
  version: '1.2'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ~>
38
+ - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '1.2'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: multi_json
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ~>
45
+ - - "~>"
46
46
  - !ruby/object:Gem::Version
47
47
  version: '1.9'
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ~>
52
+ - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '1.9'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: bundler
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - ~>
59
+ - - "~>"
60
60
  - !ruby/object:Gem::Version
61
61
  version: '1.5'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - ~>
66
+ - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: '1.5'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rake
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - ~>
73
+ - - "~>"
74
74
  - !ruby/object:Gem::Version
75
75
  version: '10.1'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - ~>
80
+ - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: '10.1'
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: rspec
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - ~>
87
+ - - "~>"
88
88
  - !ruby/object:Gem::Version
89
89
  version: 3.0.0.beta
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
- - - ~>
94
+ - - "~>"
95
95
  - !ruby/object:Gem::Version
96
96
  version: 3.0.0.beta
97
97
  - !ruby/object:Gem::Dependency
98
98
  name: rspec-collection_matchers
99
99
  requirement: !ruby/object:Gem::Requirement
100
100
  requirements:
101
- - - ~>
101
+ - - "~>"
102
102
  - !ruby/object:Gem::Version
103
103
  version: 0.0.3
104
104
  type: :development
105
105
  prerelease: false
106
106
  version_requirements: !ruby/object:Gem::Requirement
107
107
  requirements:
108
- - - ~>
108
+ - - "~>"
109
109
  - !ruby/object:Gem::Version
110
110
  version: 0.0.3
111
111
  - !ruby/object:Gem::Dependency
112
112
  name: activemodel
113
113
  requirement: !ruby/object:Gem::Requirement
114
114
  requirements:
115
- - - ~>
115
+ - - "~>"
116
116
  - !ruby/object:Gem::Version
117
117
  version: '4.0'
118
118
  type: :development
119
119
  prerelease: false
120
120
  version_requirements: !ruby/object:Gem::Requirement
121
121
  requirements:
122
- - - ~>
122
+ - - "~>"
123
123
  - !ruby/object:Gem::Version
124
124
  version: '4.0'
125
125
  - !ruby/object:Gem::Dependency
126
126
  name: activesupport
127
127
  requirement: !ruby/object:Gem::Requirement
128
128
  requirements:
129
- - - ~>
129
+ - - "~>"
130
130
  - !ruby/object:Gem::Version
131
131
  version: '4.0'
132
132
  type: :development
133
133
  prerelease: false
134
134
  version_requirements: !ruby/object:Gem::Requirement
135
135
  requirements:
136
- - - ~>
136
+ - - "~>"
137
137
  - !ruby/object:Gem::Version
138
138
  version: '4.0'
139
139
  description: Declarative creation of ActiveModels from HAL documents.
@@ -143,8 +143,8 @@ executables: []
143
143
  extensions: []
144
144
  extra_rdoc_files: []
145
145
  files:
146
- - .gitignore
147
- - .travis.yml
146
+ - ".gitignore"
147
+ - ".travis.yml"
148
148
  - Gemfile
149
149
  - LICENSE.txt
150
150
  - README.md
@@ -170,17 +170,17 @@ require_paths:
170
170
  - lib
171
171
  required_ruby_version: !ruby/object:Gem::Requirement
172
172
  requirements:
173
- - - '>='
173
+ - - ">="
174
174
  - !ruby/object:Gem::Version
175
175
  version: 1.9.3
176
176
  required_rubygems_version: !ruby/object:Gem::Requirement
177
177
  requirements:
178
- - - '>='
178
+ - - ">="
179
179
  - !ruby/object:Gem::Version
180
180
  version: '0'
181
181
  requirements: []
182
182
  rubyforge_project:
183
- rubygems_version: 2.0.3
183
+ rubygems_version: 2.2.2
184
184
  signing_key:
185
185
  specification_version: 4
186
186
  summary: Build models from HAL documents.