lhs 18.0.3 → 19.0.0.pre.endpoint.1

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
  SHA256:
3
- metadata.gz: 88aa4428ed96338bf34f138fec43912692db335bd37ce4815606303f4746ac75
4
- data.tar.gz: f2fe0526c31d1feaa7eadbcbfcdb97d94bf870b0196e5d61d472c1755c04c409
3
+ metadata.gz: 60c1866fb8e385600c90e837f0d178521da8c7f04b503ae10f3cac3b8f58f492
4
+ data.tar.gz: be19ef5e3896150c24db1fed8c41ee25c391969aa521fb5a0838cf269f2e7cd9
5
5
  SHA512:
6
- metadata.gz: b474537163fb1aac0123d4e16a627499abdd7807953de68ac4800c71647befc7291c69a38391ce0359e6a051161a4a4dd200a41a3e5fc6f288a37f353ea048c0
7
- data.tar.gz: f0e292a2197a066f28125b1df8ef6409ce7b7686baa5ff621961917079f06663efae0f298b077576ffda3fae81f0fc6c33d18306ed71d3445e099c0f98aef4f8
6
+ metadata.gz: 29d0ad3700c65f0cba51bcd53a550f9c9af9785d3bc044f1737ed7c0ba37a4d98da5e5ed3d1be445b0aede72dba248842953f9430c4e639a80957c4f1e2aabcf
7
+ data.tar.gz: e2a57c18e8ba7e0f05c2562c44bcf2e7c6a4e411cc609021e33c913b1dca9ee7619f84f6d7812d917549fdc74384bcb0f6e08489fdadc38f05bad480cfd24741
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support'
4
+
5
+ class LHS::Item < LHS::Proxy
6
+ module EndpointLookup
7
+ extend ActiveSupport::Concern
8
+
9
+ def url_for_persistance!(data, options)
10
+ return href if href.present?
11
+ endpoint = endpoint_for_persistance!(data, options)
12
+ endpoint.compile(
13
+ merge_data_with_options(data, options)
14
+ ).tap do
15
+ endpoint.remove_interpolated_params!(data)
16
+ endpoint.remove_interpolated_params!(options.fetch(:params, {}))
17
+ options.merge!(endpoint.options.merge(options)) if endpoint.options
18
+ end
19
+ end
20
+
21
+ private
22
+
23
+ def endpoint_for_persistance!(data, options)
24
+ record.find_endpoint(merge_data_with_options(data, options))
25
+ end
26
+ end
27
+ end
@@ -3,10 +3,16 @@
3
3
  require 'active_support'
4
4
 
5
5
  class LHS::Item < LHS::Proxy
6
+ autoload :EndpointLookup,
7
+ 'lhs/concerns/item/endpoint_lookup'
6
8
 
7
9
  module Save
8
10
  extend ActiveSupport::Concern
9
11
 
12
+ included do
13
+ include EndpointLookup
14
+ end
15
+
10
16
  def save(options = nil)
11
17
  save!(options)
12
18
  rescue LHC::Error
@@ -16,7 +22,7 @@ class LHS::Item < LHS::Proxy
16
22
  def save!(options = {})
17
23
  options = options.present? ? options.dup : {}
18
24
  data = _data._raw.dup
19
- url = url_for_persistance!(options, data)
25
+ url = url_for_persistance!(data, options)
20
26
  create_and_merge_data!(
21
27
  apply_default_creation_options(options, url, data)
22
28
  )
@@ -45,21 +51,5 @@ class LHS::Item < LHS::Proxy
45
51
  end
46
52
  true
47
53
  end
48
-
49
- def endpoint_for_persistance(data, options)
50
- record.find_endpoint(merge_data_with_options(data, options))
51
- end
52
-
53
- def url_for_persistance!(options, data)
54
- return href if href.present?
55
- endpoint = endpoint_for_persistance(data, options)
56
- endpoint.compile(
57
- merge_data_with_options(data, options)
58
- ).tap do
59
- endpoint.remove_interpolated_params!(data)
60
- endpoint.remove_interpolated_params!(options.fetch(:params, {}))
61
- options.merge!(endpoint.options.merge(options)) if endpoint.options
62
- end
63
- end
64
54
  end
65
55
  end
@@ -3,10 +3,16 @@
3
3
  require 'active_support'
4
4
 
5
5
  class LHS::Item < LHS::Proxy
6
+ autoload :EndpointLookup,
7
+ 'lhs/concerns/item/endpoint_lookup'
6
8
 
7
9
  module Update
8
10
  extend ActiveSupport::Concern
9
11
 
12
+ included do
13
+ include EndpointLookup
14
+ end
15
+
10
16
  def update(params, options = nil)
11
17
  update!(params, options)
12
18
  rescue LHC::Error => e
@@ -26,11 +32,13 @@ class LHS::Item < LHS::Proxy
26
32
  end
27
33
 
28
34
  def update!(params, options = {}, partial_update = false)
29
- options ||= {}
35
+ options = options.present? ? options.dup : {}
30
36
  partial_record = _record.new(LHS::Data.new(params, _data.parent, _record))
31
37
  _data.merge_raw!(partial_record._data)
32
- data_sent = partial_update ? partial_record._data : _data
33
- url = href || record.find_endpoint(id: id).compile(id: id)
38
+ data = _data._raw.dup
39
+ partial_data = partial_record._data._raw.dup
40
+ url = url_for_persistance!(data, options)
41
+ data_sent = partial_update ? partial_data.extract!(*data.keys) : data
34
42
  response_data = record.request(
35
43
  options.merge(
36
44
  method: options.fetch(:method, :post),
data/lib/lhs/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module LHS
4
- VERSION = '18.0.3'
4
+ VERSION = '19.0.0.pre.endpoint.1'
5
5
  end
@@ -53,6 +53,48 @@ describe LHS::Item do
53
53
  end
54
54
  end
55
55
 
56
+ context 'with many placeholders' do
57
+ before do
58
+ class GrandChild < LHS::Record
59
+ endpoint 'http://host/v2/parents/{parent_id}/children/{child_id}/grand_children'
60
+ endpoint 'http://host/v2/parents/{parent_id}/children/{child_id}/grand_children/{id}'
61
+ end
62
+ end
63
+
64
+ let(:data) do
65
+ {
66
+ id: "aaa",
67
+ parent_id: "bbb",
68
+ child_id: 'ccc',
69
+ name: "Lorem"
70
+ }
71
+ end
72
+
73
+ let(:item) do
74
+ GrandChild.new(data)
75
+ end
76
+
77
+ it 'persists changes on the backend' do
78
+ stub_request(:get, 'http://host/v2/parents/bbb/children/ccc/grand_children/aaa')
79
+ .to_return(status: 200, body: data.to_json)
80
+ stub_request(:post, 'http://host/v2/parents/bbb/children/ccc/grand_children/aaa')
81
+ .with(body: { name: 'Steve' }.to_json)
82
+
83
+ grand_child = GrandChild.find(parent_id: 'bbb', child_id: 'ccc', id: 'aaa')
84
+ expect(grand_child.name).to eq('Lorem')
85
+ result = grand_child.partial_update(name: 'Steve')
86
+ expect(result).to eq true
87
+ end
88
+
89
+ it 'persists changes on the backend removing placeholder from body' do
90
+ stub_request(:post, 'http://host/v2/parents/bbb/children/ccc/grand_children/kkkk')
91
+ .with(body: { name: 'Steve' }.to_json)
92
+
93
+ result = item.partial_update(name: 'Steve', id: 'kkkk')
94
+ expect(result).to eq true
95
+ end
96
+ end
97
+
56
98
  context 'update!' do
57
99
  it 'raises if something goes wrong' do
58
100
  stub_request(:post, item.href)
@@ -63,6 +63,34 @@ describe LHS::Item do
63
63
  end
64
64
  end
65
65
 
66
+ context 'with many placeholders' do
67
+ before do
68
+ class GrandChild < LHS::Record
69
+ endpoint 'http://host/v2/parents/{parent_id}/children/{child_id}/grand_children'
70
+ endpoint 'http://host/v2/parents/{parent_id}/children/{child_id}/grand_children/{id}'
71
+ end
72
+ end
73
+
74
+ let(:data) do
75
+ {
76
+ parent_id: "bbb",
77
+ child_id: 'ccc',
78
+ name: "Lorem"
79
+ }
80
+ end
81
+
82
+ let(:item) do
83
+ GrandChild.new(data)
84
+ end
85
+
86
+ it 'persists changes on the backend' do
87
+ stub_request(:post, 'http://host/v2/parents/bbb/children/ccc/grand_children')
88
+ .with(body: { name: "Lorem" }.to_json)
89
+
90
+ expect(item.save).to eq true
91
+ end
92
+ end
93
+
66
94
  context 'save!' do
67
95
  it 'raises if something goes wrong' do
68
96
  stub_request(:post, item.href)
@@ -88,6 +88,7 @@ describe LHS::Item do
88
88
 
89
89
  class AppointmentProposal < LHS::Record
90
90
  endpoint 'http://bookings/bookings'
91
+
91
92
  def appointments_attributes=(attributes)
92
93
  self.appointments = attributes.map { |attribute| { 'date_time': attribute[:date] } }
93
94
  end
@@ -109,6 +110,40 @@ describe LHS::Item do
109
110
  end
110
111
  end
111
112
  end
113
+
114
+ context 'with many placeholders' do
115
+ before do
116
+ class GrandChild < LHS::Record
117
+ endpoint 'http://host/v2/parents/{parent_id}/children/{child_id}/grand_children'
118
+ endpoint 'http://host/v2/parents/{parent_id}/children/{child_id}/grand_children/{id}'
119
+ end
120
+ end
121
+
122
+ let(:data) do
123
+ {
124
+ id: "aaa",
125
+ parent_id: "bbb",
126
+ child_id: 'ccc',
127
+ name: "Lorem"
128
+ }
129
+ end
130
+
131
+ let(:item) do
132
+ GrandChild.new(data)
133
+ end
134
+
135
+ it 'persists changes on the backend' do
136
+ stub_request(:get, 'http://host/v2/parents/bbb/children/ccc/grand_children/aaa')
137
+ .to_return(status: 200, body: data.to_json)
138
+ stub_request(:post, 'http://host/v2/parents/bbb/children/ccc/grand_children/aaa')
139
+ .with(body: { name: 'Steve' }.to_json)
140
+
141
+ grand_child = GrandChild.find(parent_id: 'bbb', child_id: 'ccc', id: 'aaa')
142
+ expect(grand_child.name).to eq('Lorem')
143
+ result = grand_child.update(name: 'Steve')
144
+ expect(result).to eq true
145
+ end
146
+ end
112
147
  end
113
148
 
114
149
  context 'update!' do
@@ -77,4 +77,44 @@ describe LHS::Record do
77
77
  expect(listing.parent.parent._raw).to eq location._raw
78
78
  end
79
79
  end
80
+
81
+ context 'explicit association class configuration overrules href class casting' do
82
+ before do
83
+ class Place < LHS::Record
84
+ endpoint 'http://places/places/{id}'
85
+ has_many :categories, class_name: 'NewCategory'
86
+ end
87
+
88
+ class NewCategory < LHS::Record
89
+ endpoint 'http://newcategories/newcategories/{id}'
90
+
91
+ def name
92
+ self['category_name']
93
+ end
94
+ end
95
+
96
+ class Category < LHS::Record
97
+ endpoint 'http://categories/categories/{id}'
98
+ end
99
+
100
+ stub_request(:get, "http://places/places/1")
101
+ .to_return(body: {
102
+ categories: [{
103
+ href: 'https://categories/categories/1'
104
+ }]
105
+ }.to_json)
106
+
107
+ stub_request(:get, "https://categories/categories/1")
108
+ .to_return(body: {
109
+ href: 'https://categories/categories/1',
110
+ category_name: 'Pizza'
111
+ }.to_json)
112
+ end
113
+
114
+ it 'explicit association configuration overrules href class casting' do
115
+ place = Place.includes(:categories).find(1)
116
+ expect(place.categories.first).to be_kind_of NewCategory
117
+ expect(place.categories.first.name).to eq('Pizza')
118
+ end
119
+ end
80
120
  end
@@ -73,4 +73,44 @@ describe LHS::Record do
73
73
  expect(user.email).to eq 'steve@local.ch'
74
74
  end
75
75
  end
76
+
77
+ context 'explicit association class configuration overrules href class casting' do
78
+ before do
79
+ class Place < LHS::Record
80
+ endpoint 'http://places/places/{id}'
81
+ has_one :category, class_name: 'NewCategory'
82
+ end
83
+
84
+ class NewCategory < LHS::Record
85
+ endpoint 'http://newcategories/newcategories/{id}'
86
+
87
+ def name
88
+ self['category_name']
89
+ end
90
+ end
91
+
92
+ class Category < LHS::Record
93
+ endpoint 'http://categories/categories/{id}'
94
+ end
95
+
96
+ stub_request(:get, "http://places/places/1")
97
+ .to_return(body: {
98
+ category: {
99
+ href: 'https://categories/categories/1'
100
+ }
101
+ }.to_json)
102
+
103
+ stub_request(:get, "https://categories/categories/1")
104
+ .to_return(body: {
105
+ href: 'https://categories/categories/1',
106
+ category_name: 'Pizza'
107
+ }.to_json)
108
+ end
109
+
110
+ it 'explicit association configuration overrules href class casting' do
111
+ place = Place.includes(:category).find(1)
112
+ expect(place.category).to be_kind_of NewCategory
113
+ expect(place.category.name).to eq('Pizza')
114
+ end
115
+ end
76
116
  end
@@ -119,7 +119,7 @@ describe LHS::Record do
119
119
  context 'update' do
120
120
  before do
121
121
  stub_request(:post, "http://datastore/v2/records/123").to_return(body: {}.to_json)
122
- body = LHS::Data.new({ href: 'http://datastore/v2/records/123', name: 'steve' }, nil, Record)
122
+ body = { href: 'http://datastore/v2/records/123', name: 'steve' }
123
123
  expect(LHC).to receive(:request)
124
124
  .with(options.merge(method: :post, url: "http://datastore/v2/records/123", body: body))
125
125
  .and_call_original
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lhs
3
3
  version: !ruby/object:Gem::Version
4
- version: 18.0.3
4
+ version: 19.0.0.pre.endpoint.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - https://github.com/local-ch/lhs/graphs/contributors
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-02-18 00:00:00.000000000 Z
11
+ date: 2019-02-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -228,6 +228,7 @@ files:
228
228
  - lib/lhs/concerns/data/to_hash.rb
229
229
  - lib/lhs/concerns/inspect.rb
230
230
  - lib/lhs/concerns/item/destroy.rb
231
+ - lib/lhs/concerns/item/endpoint_lookup.rb
231
232
  - lib/lhs/concerns/item/save.rb
232
233
  - lib/lhs/concerns/item/update.rb
233
234
  - lib/lhs/concerns/item/validation.rb
@@ -465,13 +466,13 @@ required_ruby_version: !ruby/object:Gem::Requirement
465
466
  version: 2.3.0
466
467
  required_rubygems_version: !ruby/object:Gem::Requirement
467
468
  requirements:
468
- - - ">="
469
+ - - ">"
469
470
  - !ruby/object:Gem::Version
470
- version: '0'
471
+ version: 1.3.1
471
472
  requirements:
472
473
  - Ruby >= 2.3.0
473
474
  rubyforge_project:
474
- rubygems_version: 2.7.8
475
+ rubygems_version: 2.7.6
475
476
  signing_key:
476
477
  specification_version: 4
477
478
  summary: 'REST services accelerator: Rails gem providing an easy, active-record-like