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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 472c0308bceb0d57beb2999ddf90b98881ee9b2c
4
- data.tar.gz: 3b100b77107bafa8eec978370e98ece0b0103dc5
3
+ metadata.gz: b2d99ceddb2ae5693268943054615f83012e7915
4
+ data.tar.gz: 237d0864cfd642b7ea652646c9694e11db6df6bc
5
5
  SHA512:
6
- metadata.gz: 00d83acc6b319b7e5d7b32a27eb58674f135f7afba5e9cdfde405b3e7b4f5555456583b3bae6f5cf7bc7e7cf25c27407f0e9c919a46e31984946ed2db84c60c7
7
- data.tar.gz: c54dcd9cb6a0dbc785166b6ab3185a985767142232ff20e0cb5459ff56eb2aab3394729445d93a6b453d2f0430c1d040bf150e7fd6e95f62b867180f76c8a8c4
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
- if href.present?
19
- url = href
20
- else
21
- endpoint = endpoint_for_persistance(data, options)
22
- url = url_for_persistance(endpoint, data, options)
23
- endpoint.remove_interpolated_params!(data)
24
- endpoint.remove_interpolated_params!(options.fetch(:params, {}))
25
- options.merge!(endpoint.options.merge(options)) if endpoint.options
26
- end
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
- data = record_for_persistance.request(options)
33
- _data.merge_raw!(data)
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(endpoint, data, options)
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
@@ -1,3 +1,3 @@
1
1
  module LHS
2
- VERSION = "7.2.3"
2
+ VERSION = "7.2.4"
3
3
  end
@@ -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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lhs
3
3
  version: !ruby/object:Gem::Version
4
- version: 7.2.3
4
+ version: 7.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - https://github.com/local-ch/lhs/graphs/contributors