lhs 19.6.0 → 19.7.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: 27484e8b67d86a110c1e880e53d42f605646754cd0fe7b52369a5dcc381f3d97
4
- data.tar.gz: b359ed2464317011e78586fbc1804812b269fa6da8f053028d8bbca946acac18
3
+ metadata.gz: 267b853caadad4c6709bd01b799f4679fcc0a7efda0d9aba1241a2ec52948628
4
+ data.tar.gz: b627506faa83289f4b13b714fd459207ed81706236148a8dd98cca263f118e8e
5
5
  SHA512:
6
- metadata.gz: 779e482a39d72b249c30ed2ab82ab9c26d5fcd922c5d8149bfc1d0bd2838daec87ad1ff170503f532962f6bb07e9806c932c00c630ecae77d4af600aa45f2e6c
7
- data.tar.gz: 85ba8a79e1e6b0c8f66fd7c4592636140f61d8a12b1b2d86333bb5ef86f2a2f48a44aeeae51732e1fd54752bcd90456f6951539a59cc5c4f94f7d2a7a875bb95
6
+ metadata.gz: 80341435042e936ecc1d6cb3e80ab240836c5a4f885c75c0519823b697aa852c5c82ac9d355cd05a49964b8748926b70a4c0c159c2b978cac8917e77343b4dcd
7
+ data.tar.gz: c667486950b86fb685e9b58ac224d7125d448e92ba61a8341b53325089831773e2331c8d3a7ca45224fe4d2ec5c2b3a79581416b9eb26bcedc7f10cca7f56c99
data/README.md CHANGED
@@ -45,7 +45,7 @@ record.review # "Lunch was great
45
45
  * [Record](#record)
46
46
  * [Endpoints](#endpoints)
47
47
  * [Configure endpoint hosts](#configure-endpoint-hosts)
48
- * [Endpoint priorities](#endpoint-priorities)
48
+ * [Endpoint Priorities](#endpoint-priorities)
49
49
  * [Record inheritance](#record-inheritance)
50
50
  * [Find multiple records](#find-multiple-records)
51
51
  * [fetch](#fetch)
@@ -81,6 +81,7 @@ record.review # "Lunch was great
81
81
  * [Pagination strategy: offset (default)](#pagination-strategy-offset-default)
82
82
  * [Pagination strategy: page](#pagination-strategy-page)
83
83
  * [Pagination strategy: start](#pagination-strategy-start)
84
+ * [Pagination strategy: link](#pagination-strategy-link)
84
85
  * [Pagination keys](#pagination-keys)
85
86
  * [limit_key](#limit_key)
86
87
  * [pagination_key](#pagination_key)
@@ -123,7 +124,7 @@ record.review # "Lunch was great
123
124
  * [Identify and cast known records when including records](#identify-and-cast-known-records-when-including-records)
124
125
  * [Apply options for requests performed to fetch included records](#apply-options-for-requests-performed-to-fetch-included-records)
125
126
  * [Record batch processing](#record-batch-processing)
126
- * [all](#all)
127
+ * [all](#all-1)
127
128
  * [Using all, when endpoint does not implement response pagination meta data](#using-all-when-endpoint-does-not-implement-response-pagination-meta-data)
128
129
  * [find_each](#find_each)
129
130
  * [find_in_batches](#find_in_batches)
@@ -132,6 +133,7 @@ record.review # "Lunch was great
132
133
  * [Request Cycle Cache](#request-cycle-cache)
133
134
  * [Change store for LHS' request cycle cache](#change-store-for-lhs-request-cycle-cache)
134
135
  * [Disable request cycle cache](#disable-request-cycle-cache)
136
+ * [Option Blocks](#option-blocks)
135
137
  * [Request tracing](#request-tracing)
136
138
  * [Testing with LHS](#testing-with-lhs)
137
139
  * [Test helper for request cycle cache](#test-helper-for-request-cycle-cache)
@@ -2315,6 +2317,25 @@ LHS.configure do |config|
2315
2317
  config.request_cycle_cache_enabled = false
2316
2318
  end
2317
2319
  ```
2320
+
2321
+ ## Option Blocks
2322
+
2323
+ In order to apply options to all requests performed in a give block, LHS provides option blocks.
2324
+
2325
+ ```ruby
2326
+ # app/controllers/records_controller.rb
2327
+
2328
+ LHS.options(headers: { 'Tracking-Id' => 123 }) do
2329
+ Record.find(1)
2330
+ end
2331
+
2332
+ Record.find(2)
2333
+ ```
2334
+ ```
2335
+ GET https://records/1 { headers: { 'Tracking-Id' => '123' } }
2336
+ GET https://records/2 { headers: { } }
2337
+ ```
2338
+
2318
2339
  ## Request tracing
2319
2340
 
2320
2341
  LHS supports tracing the source (in your application code) of http requests being made with methods like `find find_by find_by! first first! last last!`.
data/lib/lhs.rb CHANGED
@@ -23,6 +23,8 @@ module LHS
23
23
  'lhs/concerns/is_href'
24
24
  autoload :Item,
25
25
  'lhs/item'
26
+ autoload :OptionBlocks,
27
+ 'lhs/concerns/option_blocks'
26
28
  autoload :Pagination,
27
29
  'lhs/pagination/base'
28
30
  module Pagination
@@ -59,12 +61,12 @@ module LHS
59
61
  'lhs/proxy'
60
62
  autoload :Record,
61
63
  'lhs/record'
62
-
63
64
  autoload :Unprocessable,
64
65
  'lhs/unprocessable'
65
66
 
66
67
  include Configuration
67
68
  include AutoloadRecords if defined?(Rails)
69
+ include OptionBlocks
68
70
 
69
71
  require 'lhs/record' # as lhs records in an application are directly inheriting it
70
72
 
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support'
4
+
5
+ module LHS
6
+ module OptionBlocks
7
+ extend ActiveSupport::Concern
8
+
9
+ class CurrentOptionBlock
10
+ # Using ActiveSupports PerThreadRegistry to be able to support Active Support v4.
11
+ # Will switch to thread_mattr_accessor (which comes with Activesupport) when we dropping support for Active Support v5.
12
+ extend ActiveSupport::PerThreadRegistry
13
+ attr_accessor :options
14
+ end
15
+
16
+ module ClassMethods
17
+ def options(options, &block)
18
+ CurrentOptionBlock.options = options
19
+ block.call
20
+ ensure
21
+ CurrentOptionBlock.options = nil
22
+ end
23
+ end
24
+ end
25
+ end
@@ -11,6 +11,7 @@ class LHS::Record
11
11
  module ClassMethods
12
12
  def request(options)
13
13
  options ||= {}
14
+ options = deep_merge_with_option_blocks(options)
14
15
  options = options.freeze
15
16
  if options.is_a?(Array)
16
17
  multiple_requests(
@@ -23,6 +24,11 @@ class LHS::Record
23
24
 
24
25
  private
25
26
 
27
+ def deep_merge_with_option_blocks(options)
28
+ return options if LHS::OptionBlocks::CurrentOptionBlock.options.blank?
29
+ options.deep_merge(LHS::OptionBlocks::CurrentOptionBlock.options)
30
+ end
31
+
26
32
  def single_request_load_and_merge_remaining_objects!(data, options, endpoint)
27
33
  return if options[:all].blank? || !paginated
28
34
  load_and_merge_remaining_objects!(
@@ -7,6 +7,7 @@ module LHS
7
7
 
8
8
  def initialize
9
9
  prepare_lhs_request_cycle_cache
10
+ reset_option_blocks
10
11
  super
11
12
  end
12
13
 
@@ -16,6 +17,10 @@ module LHS
16
17
  return unless LHS.config.request_cycle_cache_enabled
17
18
  LHS::Record::RequestCycleCache::RequestCycleThreadRegistry.request_id = [Time.now.to_f, request.object_id].join('#')
18
19
  end
20
+
21
+ def reset_option_blocks
22
+ LHS::OptionBlocks::CurrentOptionBlock.options = nil
23
+ end
19
24
  end
20
25
  end
21
26
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module LHS
4
- VERSION = '19.6.0'
4
+ VERSION = '19.7.0'
5
5
  end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ class OptionBlocksController < ApplicationController
4
+
5
+ def first
6
+ LHS::OptionBlocks::CurrentOptionBlock.options = { params: { request: 'first' } }
7
+ Record.where(request: 'second').fetch
8
+ render text: 'ok'
9
+ end
10
+
11
+ def second
12
+ Record.where(request: 'second').fetch
13
+ render text: 'ok'
14
+ end
15
+ end
@@ -12,4 +12,8 @@ Rails.application.routes.draw do
12
12
  # Error handling with chains
13
13
  get 'error_handling_with_chains/fetch_in_controller' => 'error_handling_with_chains#fetch_in_controller'
14
14
  get 'error_handling_with_chains/fetch_in_view' => 'error_handling_with_chains#fetch_in_view'
15
+
16
+ # Option Blocks
17
+ get 'option_blocks/first' => 'option_blocks#first'
18
+ get 'option_blocks/second' => 'option_blocks#second'
15
19
  end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails_helper'
4
+
5
+ describe 'Option Blocks', type: :request do
6
+ let!(:first_request) do
7
+ stub_request(:get, "http://datastore/v2/records?request=first")
8
+ .to_return(status: 200)
9
+ end
10
+
11
+ let!(:second_request) do
12
+ stub_request(:get, "http://datastore/v2/records?request=second")
13
+ .to_return(status: 200)
14
+ end
15
+
16
+ it 'always ensures option blocks are always reset for new requests', dummy_models: true do
17
+ get '/option_blocks/first'
18
+ expect(first_request).to have_been_made.once
19
+ get '/option_blocks/second'
20
+ expect(second_request).to have_been_made.once
21
+ end
22
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails_helper'
4
+
5
+ describe LHS::OptionBlocks do
6
+ let(:status) { 200 }
7
+
8
+ before do
9
+ class Record < LHS::Record
10
+ endpoint 'http://records'
11
+ end
12
+
13
+ stub_request(:get, 'http://records/?id=1234')
14
+ .with(headers: { 'Tracking-Id' => 1 })
15
+ .to_return(status: status)
16
+ end
17
+
18
+ it 'allows to apply options to all requests made within a certain block' do
19
+ LHS.options(headers: { 'Tracking-Id': 1 }) do
20
+ Record.find(1234)
21
+ end
22
+ end
23
+
24
+ it 'ensures that option blocks are reset after the block has been executed' do
25
+ expect(LHS::OptionBlocks::CurrentOptionBlock.options).to eq nil
26
+ LHS.options(headers: { 'Tracking-Id': 1 }) do
27
+ Record.find(1234)
28
+ end
29
+ expect(LHS::OptionBlocks::CurrentOptionBlock.options).to eq nil
30
+ end
31
+
32
+ context 'failing request' do
33
+ let(:status) { 400 }
34
+
35
+ it 'ensures that option blocks are reset when an exception occures in the block' do
36
+ expect(LHS::OptionBlocks::CurrentOptionBlock.options).to eq nil
37
+ LHS.options(headers: { 'Tracking-Id': 1 }) do
38
+ begin
39
+ Record.find(1234)
40
+ rescue LHC::Error
41
+ end
42
+ end
43
+ expect(LHS::OptionBlocks::CurrentOptionBlock.options).to eq nil
44
+ end
45
+ end
46
+ 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: 19.6.0
4
+ version: 19.7.0
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-07-12 00:00:00.000000000 Z
11
+ date: 2019-07-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -235,6 +235,7 @@ files:
235
235
  - lib/lhs/concerns/item/save.rb
236
236
  - lib/lhs/concerns/item/update.rb
237
237
  - lib/lhs/concerns/item/validation.rb
238
+ - lib/lhs/concerns/option_blocks.rb
238
239
  - lib/lhs/concerns/proxy/accessors.rb
239
240
  - lib/lhs/concerns/proxy/create.rb
240
241
  - lib/lhs/concerns/proxy/link.rb
@@ -319,6 +320,7 @@ files:
319
320
  - spec/dummy/app/controllers/application_controller.rb
320
321
  - spec/dummy/app/controllers/concerns/.keep
321
322
  - spec/dummy/app/controllers/error_handling_with_chains_controller.rb
323
+ - spec/dummy/app/controllers/option_blocks_controller.rb
322
324
  - spec/dummy/app/controllers/request_cycle_cache_controller.rb
323
325
  - spec/dummy/app/helpers/application_helper.rb
324
326
  - spec/dummy/app/mailers/.keep
@@ -382,6 +384,8 @@ files:
382
384
  - spec/item/validation_spec.rb
383
385
  - spec/item/warning_codes_spec.rb
384
386
  - spec/item/warnings_spec.rb
387
+ - spec/option_blocks/ensure_reset_between_requests_spec.rb
388
+ - spec/option_blocks/main_spec.rb
385
389
  - spec/pagination/link/current_page_spec.rb
386
390
  - spec/pagination/link/pages_left_spec.rb
387
391
  - spec/pagination/link/parallel_spec.rb
@@ -527,6 +531,7 @@ test_files:
527
531
  - spec/dummy/app/controllers/application_controller.rb
528
532
  - spec/dummy/app/controllers/concerns/.keep
529
533
  - spec/dummy/app/controllers/error_handling_with_chains_controller.rb
534
+ - spec/dummy/app/controllers/option_blocks_controller.rb
530
535
  - spec/dummy/app/controllers/request_cycle_cache_controller.rb
531
536
  - spec/dummy/app/helpers/application_helper.rb
532
537
  - spec/dummy/app/mailers/.keep
@@ -590,6 +595,8 @@ test_files:
590
595
  - spec/item/validation_spec.rb
591
596
  - spec/item/warning_codes_spec.rb
592
597
  - spec/item/warnings_spec.rb
598
+ - spec/option_blocks/ensure_reset_between_requests_spec.rb
599
+ - spec/option_blocks/main_spec.rb
593
600
  - spec/pagination/link/current_page_spec.rb
594
601
  - spec/pagination/link/pages_left_spec.rb
595
602
  - spec/pagination/link/parallel_spec.rb