hal-interpretation 1.8.1 → 1.9.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/hal_interpretation.rb +10 -3
- data/lib/hal_interpretation/dsl.rb +4 -4
- data/lib/hal_interpretation/version.rb +1 -1
- data/spec/hal_interpretation_spec.rb +36 -1
- metadata +3 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5d0bb8bd69d4f918747724f90a4fa6099a8902a5
|
4
|
+
data.tar.gz: a7b3dba7c022043f163e222f124867805f28d20d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 58cc7ed92a124e87edc19a071e109ff35de963a905849b11774c6e3772ba7a799131dbfd1a2872c2d9cc2d0b940e2a0bc5fbb2680f9a020596cf8c41c5b4ca50
|
7
|
+
data.tar.gz: 0ac2e4aff92dd46c3eea872a36210938e33a7900d4f3be90e9d2a56689d4002c9ccd099d1e83306cfd8e1ba91bbab7ea4dd043033080a2e803ec61c578b65057
|
data/lib/hal_interpretation.rb
CHANGED
@@ -66,9 +66,16 @@ module HalInterpretation
|
|
66
66
|
# opts
|
67
67
|
# :location - The json path of `a_representation` in the
|
68
68
|
# complete document
|
69
|
+
#
|
70
|
+
# optionally implement `#handle_initialization_opts` for custom option parsing
|
69
71
|
def initialize(a_representation, opts)
|
70
72
|
@repr = a_representation
|
71
73
|
@location = opts.fetch(:location) { raise ArgumentError, "location is required" }
|
74
|
+
handle_initialization_opts(opts)
|
75
|
+
end
|
76
|
+
|
77
|
+
# allows custom setting of options on initialization
|
78
|
+
def handle_initialization_opts(opts)
|
72
79
|
end
|
73
80
|
|
74
81
|
attr_reader :repr, :location
|
@@ -104,9 +111,9 @@ module HalInterpretation
|
|
104
111
|
#
|
105
112
|
# Raises HalInterpretation::InvalidRepresentationError if the
|
106
113
|
# provided JSON document is not parseable
|
107
|
-
def new_from_json(json)
|
108
|
-
|
109
|
-
|
114
|
+
def new_from_json(json, opts = {})
|
115
|
+
repr = HalClient::Representation.new(parsed_json: MultiJson.load(json))
|
116
|
+
self.new repr, opts.merge(location: "/")
|
110
117
|
|
111
118
|
rescue MultiJson::ParseError => err
|
112
119
|
fail InvalidRepresentationError, "Parse error: " + err.message
|
@@ -134,14 +134,14 @@ module HalInterpretation
|
|
134
134
|
#
|
135
135
|
# Examples
|
136
136
|
#
|
137
|
-
#
|
137
|
+
# extract_related :author,
|
138
138
|
# rel: "http://xmlns.com/foaf/0.1/Person"
|
139
139
|
#
|
140
140
|
# extracts the targets of the `.../Person` link and stores the
|
141
141
|
# corresponding HAL representation object in the `author`
|
142
142
|
# attribute of the model.
|
143
143
|
#
|
144
|
-
#
|
144
|
+
# extract_related :author, rel: "http://xmlns.com/foaf/0.1/Person",
|
145
145
|
# coercion: ->(person_repr) {
|
146
146
|
# MyInterpretation.new(person_repr)
|
147
147
|
# }
|
@@ -168,14 +168,14 @@ module HalInterpretation
|
|
168
168
|
#
|
169
169
|
# Examples
|
170
170
|
#
|
171
|
-
#
|
171
|
+
# extract_relateds :authors,
|
172
172
|
# rel: "http://exampe.com/authors"
|
173
173
|
#
|
174
174
|
# extracts the targets of the `.../authors` link and stores the
|
175
175
|
# corresponding HAL representation set object in the `authors`
|
176
176
|
# attribute of the model.
|
177
177
|
#
|
178
|
-
#
|
178
|
+
# extract_relateds :authors, rel: "http://example.com/authors",
|
179
179
|
# coercion: ->(person_repr_set) {
|
180
180
|
# person_repr_set.map {|repr| MyInterpretation.new(repr)}
|
181
181
|
# }
|
@@ -14,10 +14,11 @@ describe HalInterpretation do
|
|
14
14
|
extract :latitude, from: "/geo/latitude"
|
15
15
|
extract :bday, coercion: ->(val){ Time.parse(val) }
|
16
16
|
extract :seq, with: ->(_) { next_seq_num }
|
17
|
-
extract_link :up
|
17
|
+
extract_link :up, rel: "up", coercion: ->(url) { @override_up || url }
|
18
18
|
extract_links :friend_ids, rel: "http://xmlns.com/foaf/0.1/knows",
|
19
19
|
coercion: ->(urls) { urls.map{|u| u.split("/").last } }
|
20
20
|
extract_link :archives_url_tmpl, rel: "archives"
|
21
|
+
|
21
22
|
extract_related :profile, rel: "http://xmlns.com/foaf/0.1/Person"
|
22
23
|
extract_relateds :cohorts, rel: "http://xmlns.com/foaf/0.1/knows"
|
23
24
|
|
@@ -29,6 +30,10 @@ describe HalInterpretation do
|
|
29
30
|
def next_seq_num
|
30
31
|
@cur_seq_num += 1
|
31
32
|
end
|
33
|
+
|
34
|
+
def handle_initialization_opts(opts)
|
35
|
+
@override_up = opts.fetch(:override_up, nil)
|
36
|
+
end
|
32
37
|
end
|
33
38
|
}
|
34
39
|
|
@@ -116,6 +121,36 @@ describe HalInterpretation do
|
|
116
121
|
end
|
117
122
|
end
|
118
123
|
|
124
|
+
context "with initialization options" do
|
125
|
+
let(:json_doc_with_links) { <<-JSON }
|
126
|
+
{ "name": "foo"
|
127
|
+
,"bday": "2013-12-11T10:09:08Z"
|
128
|
+
,"geo": {
|
129
|
+
"latitude": 39.1
|
130
|
+
}
|
131
|
+
,"_links": {
|
132
|
+
"up": { "href": "/foo" }
|
133
|
+
}
|
134
|
+
}
|
135
|
+
JSON
|
136
|
+
|
137
|
+
let(:json_doc_without_links) { <<-JSON }
|
138
|
+
{ "name": "foo"
|
139
|
+
,"bday": "2013-12-11T10:09:08Z"
|
140
|
+
,"geo": {
|
141
|
+
"latitude": 39.1
|
142
|
+
}
|
143
|
+
}
|
144
|
+
JSON
|
145
|
+
|
146
|
+
let(:opts) { { override_up: "/new_parent"} }
|
147
|
+
let(:interpreter_with_links) { interpreter_class.new_from_json(json_doc_with_links, opts) }
|
148
|
+
let(:interpreter_without_links) { interpreter_class.new_from_json(json_doc_without_links, opts) }
|
149
|
+
|
150
|
+
specify { expect(interpreter_with_links.item.up).to eq("/new_parent") }
|
151
|
+
specify { expect(interpreter_without_links.item.up).to eq("/new_parent") }
|
152
|
+
end
|
153
|
+
|
119
154
|
context "valid collection" do
|
120
155
|
let(:json_doc) { <<-JSON }
|
121
156
|
{ "_embedded": {
|
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.9.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:
|
11
|
+
date: 2016-01-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: hal-client
|
@@ -180,7 +180,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
180
180
|
version: '0'
|
181
181
|
requirements: []
|
182
182
|
rubyforge_project:
|
183
|
-
rubygems_version: 2.2.
|
183
|
+
rubygems_version: 2.2.5
|
184
184
|
signing_key:
|
185
185
|
specification_version: 4
|
186
186
|
summary: Build models from HAL documents.
|
@@ -188,4 +188,3 @@ 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:
|