lhs 7.2.3 → 7.2.4
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/lib/lhs/concerns/item/save.rb +29 -18
- data/lib/lhs/version.rb +1 -1
- data/spec/record/create_spec.rb +23 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b2d99ceddb2ae5693268943054615f83012e7915
|
4
|
+
data.tar.gz: 237d0864cfd642b7ea652646c9694e11db6df6bc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 90e6b45e7b29f894773ff861bcd5ceae5cb28d4d223e099b360da9e729e6ef27746abb5f8c97d9ed679378f2ae58a97a6a953c14082f6addb00c82f2981831c4
|
7
|
+
data.tar.gz: 6f1404569a787ef49efbc533a2e61a43eef55315387d9d32c6351d844fdde9f90a3f6556fed649558129db06dc292908dabc03a45eaa002bb2d3dd0f81806a96
|
@@ -15,30 +15,35 @@ class LHS::Item < LHS::Proxy
|
|
15
15
|
def save!(options = {})
|
16
16
|
options = options.present? ? options.dup : {}
|
17
17
|
data = _data._raw.dup
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
18
|
+
url = url_for_persistance!(options, data)
|
19
|
+
create_and_merge_data!(
|
20
|
+
apply_default_creation_options(options, url, data)
|
21
|
+
)
|
22
|
+
rescue LHC::Error => e
|
23
|
+
self.errors = LHS::Errors.new(e.response)
|
24
|
+
raise e
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
27
28
|
|
29
|
+
def apply_default_creation_options(options, url, data)
|
28
30
|
options = options.merge(method: :post, url: url, body: data.to_json)
|
29
31
|
options[:headers] ||= {}
|
30
32
|
options[:headers].merge!('Content-Type' => 'application/json')
|
33
|
+
options
|
34
|
+
end
|
31
35
|
|
32
|
-
|
33
|
-
|
36
|
+
def create_and_merge_data!(options)
|
37
|
+
direct_response_data = record_for_persistance.request(options)
|
38
|
+
_data.merge_raw!(direct_response_data)
|
39
|
+
response_headers = direct_response_data._request.response.headers
|
40
|
+
if response_headers && response_headers['Location']
|
41
|
+
location_data = record_for_persistance.request(options.merge(url: response_headers['Location'], method: :get, body: nil))
|
42
|
+
_data.merge_raw!(location_data)
|
43
|
+
end
|
34
44
|
true
|
35
|
-
rescue LHC::Error => e
|
36
|
-
self.errors = LHS::Errors.new(e.response)
|
37
|
-
raise e
|
38
45
|
end
|
39
46
|
|
40
|
-
private
|
41
|
-
|
42
47
|
def endpoint_for_persistance(data, options)
|
43
48
|
record_for_persistance
|
44
49
|
.find_endpoint(merge_data_with_options(data, options))
|
@@ -56,10 +61,16 @@ class LHS::Item < LHS::Proxy
|
|
56
61
|
_data.class
|
57
62
|
end
|
58
63
|
|
59
|
-
def url_for_persistance(
|
64
|
+
def url_for_persistance!(options, data)
|
65
|
+
return href if href.present?
|
66
|
+
endpoint = endpoint_for_persistance(data, options)
|
60
67
|
endpoint.compile(
|
61
68
|
merge_data_with_options(data, options)
|
62
|
-
)
|
69
|
+
).tap do
|
70
|
+
endpoint.remove_interpolated_params!(data)
|
71
|
+
endpoint.remove_interpolated_params!(options.fetch(:params, {}))
|
72
|
+
options.merge!(endpoint.options.merge(options)) if endpoint.options
|
73
|
+
end
|
63
74
|
end
|
64
75
|
end
|
65
76
|
end
|
data/lib/lhs/version.rb
CHANGED
data/spec/record/create_spec.rb
CHANGED
@@ -131,5 +131,28 @@ describe LHS::Record do
|
|
131
131
|
end
|
132
132
|
end
|
133
133
|
end
|
134
|
+
|
135
|
+
context 'location header' do
|
136
|
+
before(:each) do
|
137
|
+
class ContactPerson < LHS::Record
|
138
|
+
endpoint 'http://datastore/contact_persons'
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
let(:location) { 'http://datastore/contact_persons/1' }
|
143
|
+
let(:created_at) { '2017-12-21' }
|
144
|
+
let(:name) { 'Sebastian' }
|
145
|
+
|
146
|
+
it 'Loads the data from the "Location" header after creation' do
|
147
|
+
stub_request(:post, "http://datastore/contact_persons")
|
148
|
+
.to_return(status: 204, headers: { Location: location })
|
149
|
+
stub_request(:get, "http://datastore/contact_persons/1")
|
150
|
+
.to_return(body: { href: location, name: name, created_at: created_at }.to_json)
|
151
|
+
contact_person = ContactPerson.create!(name: name)
|
152
|
+
expect(contact_person.href).to eq location
|
153
|
+
expect(contact_person.created_at).to eq Date.parse(created_at)
|
154
|
+
expect(contact_person.name).to eq name
|
155
|
+
end
|
156
|
+
end
|
134
157
|
end
|
135
158
|
end
|