lhs 11.0.0 → 11.0.1

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: 47b77a705ee21920e7c48515fdf200b60bb2be61
4
- data.tar.gz: d85c7e29b814b6ddda3c0729c1adfb405cc6e873
3
+ metadata.gz: 0b33b5484ee3a16c6d834c1520bb907c5c84cc3e
4
+ data.tar.gz: 4f179bf4a46b0e109bcd079353186e61ec08c5d4
5
5
  SHA512:
6
- metadata.gz: 6351281d1b7f3d3c5cc08fe91ac35f6a747d6ffa543a41b9a6737e2634bc714fe1a340453ab887030af652063afdd3305c4da811ef1bafaab8acb89ba0cd12bf
7
- data.tar.gz: 8aef3ee080ad5d7b6d0937138434ab2758b3941dfdfcc4fddbc38beb5a1dc16507b2929a4069dbc8ee5fbb09c4a59757a1adbfc3f1bab0cb23f8644fa43f0cf1
6
+ metadata.gz: 56558afe98484a87c27bd99d379172f6166ba9d27c32662f35e0d88cb1d010223858a60fac2ae3714d6860ee24f03c665f0388a53878193874d4120939d66b70
7
+ data.tar.gz: ce2b1ba748bd7feb39858457d36dd8e3a8f6c3dcc9db11d156279e599f83e082315e8a008127e8b7285ea393c0e8a1029271e917e77f295cae85cdc56d539fb9
@@ -11,7 +11,7 @@ class LHS::Record
11
11
  end
12
12
 
13
13
  def persisted?
14
- !href.nil?
14
+ !href.blank?
15
15
  end
16
16
 
17
17
  included do
@@ -125,13 +125,13 @@ class LHS::Record
125
125
  end
126
126
  end
127
127
 
128
- def handle_include(included, data, sub_includes = nil, references = nil)
128
+ def handle_include(included, data, sub_includes = nil, reference = nil)
129
129
  return if data.blank? || skip_loading_includes?(data, included)
130
130
  options = options_for_data(data, included)
131
- options = extend_with_references(options, references)
132
- addition = load_include(options, data, sub_includes, references)
131
+ options = extend_with_reference(options, reference)
132
+ addition = load_include(options, data, sub_includes, reference)
133
133
  extend_raw_data!(data, addition, included)
134
- expand_addition!(data, included) if no_expanded_data?(addition)
134
+ expand_addition!(data, included, options) if no_expanded_data?(addition)
135
135
  end
136
136
 
137
137
  def options_for_data(data, included = nil)
@@ -140,9 +140,10 @@ class LHS::Record
140
140
  url_option_for(data, included)
141
141
  end
142
142
 
143
- def expand_addition!(data, included)
143
+ def expand_addition!(data, included, reference)
144
144
  addition = data[included]
145
145
  options = options_for_data(addition)
146
+ options = extend_with_reference(options, reference.except(:url))
146
147
  record = record_for_options(options) || self
147
148
  options = convert_options_to_endpoints(options) if record_for_options(options)
148
149
  expanded_data = begin
@@ -163,13 +164,13 @@ class LHS::Record
163
164
  end
164
165
 
165
166
  # Extends request options with options provided for this reference
166
- def extend_with_references(options, references)
167
- return options unless references
167
+ def extend_with_reference(options, reference)
168
+ return options unless reference
168
169
  options ||= {}
169
170
  if options.is_a?(Array)
170
- options.map { |request_options| request_options.merge(references) if request_options.present? }
171
+ options.map { |request_options| request_options.merge(reference) if request_options.present? }
171
172
  elsif options.present?
172
- options.merge(references)
173
+ options.merge(reference)
173
174
  end
174
175
  end
175
176
 
@@ -1,3 +1,3 @@
1
1
  module LHS
2
- VERSION = "11.0.0"
2
+ VERSION = "11.0.1"
3
3
  end
@@ -334,32 +334,72 @@ describe LHS::Record do
334
334
  end
335
335
 
336
336
  context 'unexpanded response when requesting the included collection' do
337
- it 'loads the collection and the single items, if not already expanded' do
337
+ before(:each) do
338
338
  class Customer < LHS::Record
339
339
  endpoint ':datastore/customer/:id'
340
340
  end
341
+ end
341
342
 
343
+ let!(:customer_request) do
342
344
  stub_request(:get, "#{datastore}/customer/1")
343
345
  .to_return(body: {
344
346
  places: {
345
347
  href: "#{datastore}/places"
346
348
  }
347
349
  }.to_json)
350
+ end
348
351
 
352
+ let!(:places_request) do
349
353
  stub_request(:get, "#{datastore}/places")
350
354
  .to_return(body: {
351
355
  items: [{ href: "#{datastore}/places/1" }]
352
356
  }.to_json)
357
+ end
353
358
 
354
- item_request = stub_request(:get, "#{datastore}/places/1")
359
+ let!(:place_request) do
360
+ stub_request(:get, "#{datastore}/places/1")
355
361
  .to_return(body: {
356
362
  name: 'Casa Ferlin'
357
363
  }.to_json)
364
+ end
358
365
 
366
+ it 'loads the collection and the single items, if not already expanded' do
359
367
  place = Customer.includes(:places).find(1).places.first
360
- assert_requested(item_request)
368
+ assert_requested(place_request)
361
369
  expect(place.name).to eq 'Casa Ferlin'
362
370
  end
371
+
372
+ context 'forwarding options' do
373
+ let!(:places_request) do
374
+ stub_request(:get, "#{datastore}/places")
375
+ .with(headers: { 'Authorization' => 'Bearer 123' })
376
+ .to_return(
377
+ body: {
378
+ items: [{ href: "#{datastore}/places/1" }]
379
+ }.to_json
380
+ )
381
+ end
382
+
383
+ let!(:place_request) do
384
+ stub_request(:get, "#{datastore}/places/1")
385
+ .with(headers: { 'Authorization' => 'Bearer 123' })
386
+ .to_return(
387
+ body: {
388
+ name: 'Casa Ferlin'
389
+ }.to_json
390
+ )
391
+ end
392
+
393
+ it 'forwards options used to expand those unexpanded items' do
394
+ place = Customer
395
+ .includes(:places)
396
+ .references(places: { headers: { 'Authorization' => 'Bearer 123' } })
397
+ .find(1)
398
+ .places.first
399
+ assert_requested(place_request)
400
+ expect(place.name).to eq 'Casa Ferlin'
401
+ end
402
+ end
363
403
  end
364
404
 
365
405
  context 'includes with options' do
@@ -12,10 +12,20 @@ describe LHS::Record do
12
12
  end
13
13
 
14
14
  context 'for new record' do
15
- subject { Feedback.new }
15
+ context 'with a nil href' do
16
+ subject { Feedback.new }
16
17
 
17
- it 'is false' do
18
- expect(subject.persisted?).to be(false)
18
+ it 'is false' do
19
+ expect(subject.persisted?).to be(false)
20
+ end
21
+ end
22
+
23
+ context 'with an empty href' do
24
+ subject { Feedback.new(href: '') }
25
+
26
+ it 'is false' do
27
+ expect(subject.persisted?).to be(false)
28
+ end
19
29
  end
20
30
  end
21
31
 
@@ -1,6 +1,12 @@
1
1
  require 'rails_helper'
2
2
 
3
3
  describe LHS::Record do
4
+ before(:each) do
5
+ class Record < LHS::Record
6
+ endpoint 'http://datastore/feedbacks/:id'
7
+ end
8
+ end
9
+
4
10
  context 'url pattern' do
5
11
  let(:datastore) { 'http://local.ch/v2' }
6
12
 
@@ -18,4 +24,65 @@ describe LHS::Record do
18
24
  assert_requested(request)
19
25
  end
20
26
  end
27
+
28
+ context '#convert_options_to_endpoint' do
29
+ before(:each) do
30
+ class Record < LHS::Record
31
+ endpoint 'http://datastore/feedbacks/:id', params: { tracking: 123 }
32
+ end
33
+ end
34
+
35
+ it 'identifies endpoint by given url and merges back endpoint template parameters' do
36
+ options = LHS::Record.send(:convert_options_to_endpoints, url: 'http://datastore/feedbacks/1')
37
+ expect(options[:params][:id]).to eq '1'
38
+ expect(options[:url]).to eq 'http://datastore/feedbacks/:id'
39
+ end
40
+
41
+ it 'identifies endpoint by given url and merges back endpoint template parameters into an array, if array was given' do
42
+ options = LHS::Record.send(:convert_options_to_endpoints, [{ url: 'http://datastore/feedbacks/1' }])
43
+ expect(options[0][:params][:id]).to eq '1'
44
+ expect(options[0][:url]).to eq 'http://datastore/feedbacks/:id'
45
+ end
46
+
47
+ it 'returnes nil if endpoint was not found for the given url' do
48
+ options = LHS::Record.send(:convert_options_to_endpoints, url: 'http://datastore/reviews/1')
49
+ expect(options).to eq nil
50
+ end
51
+ end
52
+
53
+ context '#extend_with_reference' do
54
+ it 'extends given options with the one for the refernce' do
55
+ options = LHS::Record.send(:extend_with_reference, { url: 'http://datastore/feedbacks/1' }, { auth: { bearer: '123' } })
56
+ expect(options[:auth][:bearer]).to eq '123'
57
+ end
58
+
59
+ it 'extends given list of options with the one for the refernce' do
60
+ options = LHS::Record.send(:extend_with_reference, [{ url: 'http://datastore/feedbacks/1' }], auth: { bearer: '123' })
61
+ expect(options[0][:auth][:bearer]).to eq '123'
62
+ end
63
+ end
64
+
65
+ context '#options_for_data' do
66
+ it 'extract request options from raw data' do
67
+ options = LHS::Record.send(:url_option_for, LHS::Data.new({ href: 'http://datastore/feedbacks/1' }, nil, Record))
68
+ expect(options[:url]).to eq 'http://datastore/feedbacks/1'
69
+ end
70
+
71
+ it 'extract a list of request options from raw data if data is a collection' do
72
+ options = LHS::Record.send(:url_option_for, LHS::Data.new({ items: [{ href: 'http://datastore/feedbacks/1' }] }, nil, Record))
73
+ expect(options[0][:url]).to eq 'http://datastore/feedbacks/1'
74
+ end
75
+
76
+ it 'extract request options from raw nested data' do
77
+ options = LHS::Record.send(:url_option_for, LHS::Data.new({ reviews: { href: 'http://datastore/reviews/1' } }, nil, Record), :reviews)
78
+ expect(options[:url]).to eq 'http://datastore/reviews/1'
79
+ end
80
+ end
81
+
82
+ context '#record_for_options' do
83
+ it 'identifies lhs record from given options, as all request have to be done through LHS Records' do
84
+ record = LHS::Record.send(:record_for_options, url: 'http://datastore/feedbacks/1')
85
+ expect(record).to eq Record
86
+ end
87
+ end
21
88
  end
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: 11.0.0
4
+ version: 11.0.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: 2017-04-27 00:00:00.000000000 Z
11
+ date: 2017-05-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: lhc