hal-interpretation 1.3.0 → 1.4.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.
- checksums.yaml +4 -4
- data/README.md +25 -0
- data/lib/hal_interpretation.rb +31 -1
- data/lib/hal_interpretation/item_interpreter.rb +2 -2
- data/lib/hal_interpretation/version.rb +1 -1
- data/spec/hal_interpretation_spec.rb +25 -5
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7c73d5eadd5be34552f2a0b5d2677410b55da858
|
4
|
+
data.tar.gz: 8f612d0a43581eb0aa0293d1da2b91a0530d6db7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 257068790c8689c780904f9da8762bae1706e5adc32a2458c0873155b3d15feb32afeb63cbf5712c72ff5ae00ec1ffcc21227245b1be700a52493cc316e0ebaa
|
7
|
+
data.tar.gz: 991b7e1ff8582dc1341bbcdbf8821043518b3c9f3f4538f764917ed4087d56979c4bb8fc6546cb1f57d12e6e9549066ff804dad5fd2e444ca7876abcc5b54150
|
data/README.md
CHANGED
@@ -34,6 +34,8 @@ class UserHalInterpreter
|
|
34
34
|
end
|
35
35
|
```
|
36
36
|
|
37
|
+
#### Create
|
38
|
+
|
37
39
|
To interpret a HAL document simply create a new interpreter from the
|
38
40
|
JSON document to interpret and then call its `#items` method.
|
39
41
|
|
@@ -42,6 +44,8 @@ class Users < ApplicationController
|
|
42
44
|
def create
|
43
45
|
@users = UserHalInterpreter.new_from_json(request.raw_post).items
|
44
46
|
|
47
|
+
@users.each(&:save!)
|
48
|
+
|
45
49
|
rescue HalInterpretation::InvalidRepresentationError => err
|
46
50
|
render template: "shared/error", status: 422, locals: { problems: err.problems }
|
47
51
|
end
|
@@ -50,6 +54,27 @@ end
|
|
50
54
|
|
51
55
|
The `items` method returns an `Enumerable` of valid `item_class` objects.
|
52
56
|
|
57
|
+
#### Update
|
58
|
+
|
59
|
+
To update and existing record
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
|
63
|
+
class Users < ApplicationController
|
64
|
+
def update
|
65
|
+
existing_user = User.find(params[:id])
|
66
|
+
|
67
|
+
@user = UserHalInterpreter.new_from_json(request.raw_post).only_update(existing_user)
|
68
|
+
.item
|
69
|
+
|
70
|
+
@user.save!
|
71
|
+
|
72
|
+
rescue HalInterpretation::InvalidRepresentationError => err
|
73
|
+
render template: "shared/error", status: 422, locals: { problems: err.problems }
|
74
|
+
end
|
75
|
+
end
|
76
|
+
```
|
77
|
+
|
53
78
|
### Errors
|
54
79
|
|
55
80
|
If the JSON being interpreted is invalid or malformed
|
data/lib/hal_interpretation.rb
CHANGED
@@ -6,6 +6,11 @@ require "hal-client"
|
|
6
6
|
# objects.
|
7
7
|
module HalInterpretation
|
8
8
|
|
9
|
+
# Declares that this interpreter should only update `an_item`.
|
10
|
+
def only_update(an_item)
|
11
|
+
@item_to_update = an_item
|
12
|
+
end
|
13
|
+
|
9
14
|
# Returns array of models created from the HAL representation we are
|
10
15
|
# interpreting.
|
11
16
|
#
|
@@ -14,7 +19,17 @@ module HalInterpretation
|
|
14
19
|
def items
|
15
20
|
(fail InvalidRepresentationError.new(problems)) if problems.any?
|
16
21
|
|
17
|
-
@
|
22
|
+
@items ||= interpreters.flat_map(&:items)
|
23
|
+
end
|
24
|
+
|
25
|
+
# Returns the single item interpreted.
|
26
|
+
#
|
27
|
+
# Raises InvalidRepresentationError if more than one item was found.
|
28
|
+
def item
|
29
|
+
(fail InvalidRepresentationError, "More than one representation found") if
|
30
|
+
items.size > 1
|
31
|
+
|
32
|
+
items.first
|
18
33
|
end
|
19
34
|
|
20
35
|
# Returns array of problems messages, or empty array if there are
|
@@ -27,6 +42,17 @@ module HalInterpretation
|
|
27
42
|
extend Forwardable
|
28
43
|
def_delegators "self.class", :extractors, :extractor_for
|
29
44
|
|
45
|
+
# Internal: builds and returns items that should be use interpreted
|
46
|
+
# into.
|
47
|
+
def new_item(&blk)
|
48
|
+
if item_to_update
|
49
|
+
yield item_to_update
|
50
|
+
item_to_update
|
51
|
+
else
|
52
|
+
item_class.new(&blk)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
30
56
|
protected
|
31
57
|
|
32
58
|
# opts
|
@@ -54,6 +80,8 @@ module HalInterpretation
|
|
54
80
|
[ItemInterpreter.new(repr, location: location, interpreter: self)]
|
55
81
|
end
|
56
82
|
end
|
83
|
+
.tap {|is| raise(HalInterpretation::InvalidRepresentationError,
|
84
|
+
"Too many representations") if item_to_update && is.size > 1 }
|
57
85
|
end
|
58
86
|
|
59
87
|
# Back stop method to be overridden by individual interpreters.
|
@@ -61,6 +89,8 @@ module HalInterpretation
|
|
61
89
|
fail NotImplementedError, "interpreter classes must call `item_class <model class>` in the class defintion"
|
62
90
|
end
|
63
91
|
|
92
|
+
attr_reader :item_to_update
|
93
|
+
|
64
94
|
module ClassMethods
|
65
95
|
# Returns new interpreter for the provided JSON document.
|
66
96
|
#
|
@@ -28,7 +28,7 @@ module HalInterpretation
|
|
28
28
|
|
29
29
|
extend Forwardable
|
30
30
|
|
31
|
-
def_delegators :interpreter, :extractors, :extractor_for
|
31
|
+
def_delegators :interpreter, :extractors, :extractor_for
|
32
32
|
|
33
33
|
attr_reader :repr, :location, :interpreter
|
34
34
|
|
@@ -37,7 +37,7 @@ module HalInterpretation
|
|
37
37
|
end
|
38
38
|
|
39
39
|
def interpret
|
40
|
-
new_item =
|
40
|
+
new_item = interpreter.new_item do |it|
|
41
41
|
e = extractors
|
42
42
|
e.each do |an_extractor|
|
43
43
|
@problems += an_extractor.extract(from: repr, to: it, context: interpreter)
|
@@ -39,12 +39,29 @@ describe HalInterpretation do
|
|
39
39
|
JSON
|
40
40
|
|
41
41
|
specify { expect(interpreter.items).to have(1).item }
|
42
|
-
specify { expect(interpreter.
|
43
|
-
specify { expect(interpreter.
|
44
|
-
specify { expect(interpreter.
|
45
|
-
specify { expect(interpreter.
|
46
|
-
specify { expect(interpreter.
|
42
|
+
specify { expect(interpreter.item).to be interpreter.items.first }
|
43
|
+
specify { expect(interpreter.item.name).to eq "foo" }
|
44
|
+
specify { expect(interpreter.item.latitude).to eq 39.1 }
|
45
|
+
specify { expect(interpreter.item.up).to eq "/foo" }
|
46
|
+
specify { expect(interpreter.item.bday).to eq Time.utc(2013,12,11,10,9,8) }
|
47
|
+
specify { expect(interpreter.item.seq).to eq 1 }
|
47
48
|
specify { expect(interpreter.problems).to be_empty }
|
49
|
+
|
50
|
+
context "for update" do
|
51
|
+
let(:existing) { test_item_class.new do |it|
|
52
|
+
it.name = "foo"
|
53
|
+
it.latitude = 40
|
54
|
+
end }
|
55
|
+
|
56
|
+
before do interpreter.only_update(existing) end
|
57
|
+
|
58
|
+
specify { expect(interpreter.items).to have(1).item }
|
59
|
+
specify { expect(interpreter.item).to be interpreter.items.first }
|
60
|
+
specify { expect(interpreter.item).to eq existing }
|
61
|
+
specify { expect(interpreter.item.name).to eq "foo" }
|
62
|
+
specify { expect(interpreter.item.latitude).to eq 39.1 }
|
63
|
+
specify { expect(interpreter.item.bday).to eq Time.utc(2013,12,11,10,9,8) }
|
64
|
+
end
|
48
65
|
end
|
49
66
|
|
50
67
|
context "valid collection" do
|
@@ -75,6 +92,9 @@ describe HalInterpretation do
|
|
75
92
|
specify { expect(interpreter.items[0].seq).to eq 1 }
|
76
93
|
specify { expect(interpreter.items[1].seq).to eq 2 }
|
77
94
|
|
95
|
+
specify { expect{interpreter.item}
|
96
|
+
.to raise_error HalInterpretation::InvalidRepresentationError }
|
97
|
+
|
78
98
|
matcher :item_named do |expected_name|
|
79
99
|
match do |obj|
|
80
100
|
obj.name == expected_name
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hal-interpretation
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.4.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-
|
11
|
+
date: 2014-12-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: hal-client
|
@@ -188,3 +188,4 @@ test_files:
|
|
188
188
|
- spec/hal_interpretation/extractor_spec.rb
|
189
189
|
- spec/hal_interpretation_spec.rb
|
190
190
|
- spec/spec_helper.rb
|
191
|
+
has_rdoc:
|