lhs 20.1.4 → 21.0.0

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
  SHA256:
3
- metadata.gz: 90bb5680db8b584ee9597cf6e3029014f05a56704de09cecb8f4b9788ff0bbdf
4
- data.tar.gz: 1403e9844367ef3cc13c2987368d219b12db90700178b26bfacba1f3690346f7
3
+ metadata.gz: 80228cca0e4b14a7a21cf408db24114f85648afccc58fbc658fbe7ef579b69a9
4
+ data.tar.gz: 774070abf3a426ef7f68c9ee707c540f53c91499f8576220fcae2e610baca1bd
5
5
  SHA512:
6
- metadata.gz: 5c8c37a17da7d96fb7f397425dcf74440e3a3d3a562eecf142992546b58c07cefa7527166b8ba26131e3e025ca823b3d78c3f98a1d8cb0c974394e9887b623c6
7
- data.tar.gz: 187cf1be11baf4d8c32cb9521e4eeb283152b5b026b06e67d2e548deb649c36957fc6fa91b2d5d577e45de90382faf4fed80dd999cdfad161ae647753df28a94
6
+ metadata.gz: 33f14c2fe5cd5e6cc9a8bdbee060d247bc3f62fe9a3dfde38ff87aadb4370de371996455a4ea55865c2ee13aa7b3728e405c73ad7185e29ec48a8f2a657b750d
7
+ data.tar.gz: 8aebc91f90fe095c4dc7d0d10dd8c389c0b1935928299421fce9df6c6245caa69a4991ce9f9a73758b5e316ea871a4437b7128243413634e4e7949ce96ac9405
data/README.md CHANGED
@@ -2537,17 +2537,21 @@ It will initialize a MemoryStore cache for LHC::Caching interceptor and resets t
2537
2537
 
2538
2538
  #### Stub
2539
2539
 
2540
- LHS offers stub helpers that simplify stubbing https request to your apis.
2540
+ LHS offers stub helpers that simplify stubbing https request to your apis through your defined Records.
2541
2541
 
2542
- ##### Stub All
2542
+ ##### stub_all
2543
2543
 
2544
- `LHS.stub.all(url, items, additional_options)`
2544
+ `Record.stub_all(url, items, additional_options)`
2545
2545
 
2546
2546
  ```ruby
2547
2547
  # your_spec.rb
2548
2548
 
2549
2549
  before do
2550
- LHS.stub.all(
2550
+ class Record < LHS::Record
2551
+ endpoint 'https://records'
2552
+ end
2553
+
2554
+ Record.stub_all(
2551
2555
  'https://records',
2552
2556
  200.times.map{ |index| { name: "Item #{index}" } },
2553
2557
  headers: {
@@ -2561,6 +2565,28 @@ GET https://records?limit=100
2561
2565
  GET https://records?limit=100&offset=100
2562
2566
  ```
2563
2567
 
2568
+ LHS also uses Record configuration when stubbing all.
2569
+ ```ruby
2570
+ # your_spec.rb
2571
+
2572
+ before do
2573
+ class Record < LHS::Record
2574
+ configuration limit_key: :per_page, pagination_strategy: :page, pagination_key: :page
2575
+
2576
+ endpoint 'https://records'
2577
+ end
2578
+
2579
+ Record.stub_all(
2580
+ 'https://records',
2581
+ 200.times.map{ |index| { name: "Item #{index}" } }
2582
+ )
2583
+ end
2584
+ ```
2585
+ ```
2586
+ GET https://records?per_page=100
2587
+ GET https://records?per_page=100&page=2
2588
+ ```
2589
+
2564
2590
  ### Test query chains
2565
2591
 
2566
2592
  #### By explicitly resolving the chain: fetch
@@ -1,19 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'lhs'
4
- require 'lhs/test/stub'
4
+ require 'lhs/test/stubbable_records'
5
5
 
6
6
  RSpec.configure do |config|
7
7
  config.before(:each) do
8
8
  LHS.config.request_cycle_cache.clear
9
9
  end
10
-
11
- config.before(:all) do
12
-
13
- module LHS
14
- def self.stub
15
- LHS::Test::Stub
16
- end
17
- end
18
- end
19
10
  end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'local_uri'
4
+ require 'webmock'
5
+
6
+ class LHS::Record
7
+ DEFAULT_LIMIT = LHS::Pagination::Base::DEFAULT_LIMIT
8
+
9
+ def self.stub_all(url, items, options = {})
10
+ extend WebMock::API
11
+
12
+ items.each_slice(DEFAULT_LIMIT).with_index do |(*batch), index|
13
+ uri = LocalUri::URI.new(url)
14
+ uri.query.merge!(
15
+ limit_key(:parameter) => DEFAULT_LIMIT
16
+ )
17
+ offset = pagination_class.page_to_offset(index + 1, DEFAULT_LIMIT)
18
+ unless index.zero?
19
+ uri.query.merge!(
20
+ pagination_key(:parameter) => offset
21
+ )
22
+ end
23
+ request_stub = stub_request(:get, uri.to_s)
24
+ request_stub.with(options) if options.present?
25
+ request_stub.to_return(
26
+ body: {
27
+ items: batch,
28
+ offset: index.zero? ? 0 : offset,
29
+ total: items.length
30
+ }.to_json
31
+ )
32
+ end
33
+ end
34
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module LHS
4
- VERSION = '20.1.4'
4
+ VERSION = '21.0.0'
5
5
  end
@@ -10,7 +10,7 @@ describe LHS do
10
10
  endpoint 'https://records'
11
11
  end
12
12
 
13
- LHS.stub.all(
13
+ Record.stub_all(
14
14
  'https://records',
15
15
  200.times.map { |index| { name: "Item #{index}" } },
16
16
  headers: {
@@ -33,7 +33,7 @@ describe LHS do
33
33
  endpoint 'https://records'
34
34
  end
35
35
 
36
- LHS.stub.all(
36
+ Record.stub_all(
37
37
  'https://records',
38
38
  200.times.map { |index| { name: "Item #{index}" } }
39
39
  )
@@ -46,4 +46,27 @@ describe LHS do
46
46
  expect(records.first.name).to eq 'Item 0'
47
47
  end
48
48
  end
49
+
50
+ context 'with configured record' do
51
+
52
+ before do
53
+ class Record < LHS::Record
54
+ configuration limit_key: :per_page, pagination_strategy: :page, pagination_key: :page
55
+
56
+ endpoint 'https://records'
57
+ end
58
+
59
+ Record.stub_all(
60
+ 'https://records',
61
+ 200.times.map { |index| { name: "Item #{index}" } }
62
+ )
63
+ end
64
+
65
+ it 'stubs all requests with record configurations for pagination' do
66
+ records = Record.all.fetch
67
+ expect(records.count).to eq 200
68
+ expect(records.length).to eq 200
69
+ expect(records.first.name).to eq 'Item 0'
70
+ end
71
+ end
49
72
  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: 20.1.4
4
+ version: 21.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - https://github.com/local-ch/lhs/graphs/contributors
@@ -329,7 +329,7 @@ files:
329
329
  - lib/lhs/railtie.rb
330
330
  - lib/lhs/record.rb
331
331
  - lib/lhs/rspec.rb
332
- - lib/lhs/test/stub.rb
332
+ - lib/lhs/test/stubbable_records.rb
333
333
  - lib/lhs/unprocessable.rb
334
334
  - lib/lhs/version.rb
335
335
  - script/ci/build.sh
@@ -1,29 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'local_uri'
4
- require 'webmock'
5
- module LHS
6
- module Test
7
- class Stub
8
- extend WebMock::API
9
- DEFAULT_LIMIT = LHS::Pagination::Base::DEFAULT_LIMIT
10
-
11
- def self.all(url, items, options = {})
12
- items.each_slice(DEFAULT_LIMIT).with_index do |(*batch), index|
13
- uri = LocalUri::URI.new(url)
14
- uri.query.merge!(limit: DEFAULT_LIMIT)
15
- uri.query.merge!(offset: DEFAULT_LIMIT * index) unless index.zero?
16
- request_stub = stub_request(:get, uri.to_s)
17
- request_stub.with(options) if options.present?
18
- request_stub.to_return(
19
- body: {
20
- items: batch,
21
- offset: index * DEFAULT_LIMIT,
22
- total: items.length
23
- }.to_json
24
- )
25
- end
26
- end
27
- end
28
- end
29
- end