lhs 19.1.0 → 19.2.0

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: fce80cb737ee0b8d0cedfb8d5ff785baf20e468fcb875dcaab3a241861df1253
4
- data.tar.gz: 7bd2a65ea111864756de1b9d74063db7865026f47e702a6ba005fa8dead64c3e
3
+ metadata.gz: bae8511bbecfd667c0139f103d932be9b15738f17ebef9c01e4205c855ba6aef
4
+ data.tar.gz: eb47255b3090beb319d4079c0a6dabc601a30b596ed50c5ee51f6d31c2222826
5
5
  SHA512:
6
- metadata.gz: 80bda8a5f669ea96f3a4922a0a0efb69f456b68431cf844fed26392fbc5b8eb0cbab68cc69972226558c05a138290dd226c6c606342685c56d0f1d417481dcb1
7
- data.tar.gz: 66bded778c0ad0ad02e71f6a81daa06ed28c640ff94064496d1dea8129200473ce11e658b9c4bcbf462ec5ea2ec5e3d4c4d11e3228218115888b79c4e20b9d19
6
+ metadata.gz: 1f46c168d58d08d7c78d7d7ec5db86ac5f0ff07ce4fb212387bce4b64f3774fa143434a31bafeea3cbebb112d673a33609858bdc74c175f9eeb37efdd0d82175
7
+ data.tar.gz: ad8ced63ed66b4697b0cf1629147bac8ca1bbc6878c8fe2bdac4c26b5e0ed0d6f658479719ebca9c06aa1d71607af352362495b08c4d700c133289e42ab09d93
@@ -0,0 +1 @@
1
+ _1.17.3_
data/README.md CHANGED
@@ -131,6 +131,7 @@ record.review # "Lunch was great
131
131
  * [Request Cycle Cache](#request-cycle-cache)
132
132
  * [Change store for LHS' request cycle cache](#change-store-for-lhs-request-cycle-cache)
133
133
  * [Disable request cycle cache](#disable-request-cycle-cache)
134
+ * [Request tracing](#request-tracing)
134
135
  * [Testing with LHS](#testing-with-lhs)
135
136
  * [Test helper for request cycle cache](#test-helper-for-request-cycle-cache)
136
137
  * [Test query chains](#test-query-chains)
@@ -2234,9 +2235,9 @@ The LHS Request Cycle Cache is opt-out, so it's enabled by default and will requ
2234
2235
  By default the LHS Request Cycle Cache will use `ActiveSupport::Cache::MemoryStore` as its cache store. Feel free to configure a cache that is better suited for your needs by:
2235
2236
 
2236
2237
  ```ruby
2237
- # config/initializers/lhc.rb
2238
+ # config/initializers/lhs.rb
2238
2239
 
2239
- LHC.configure do |config|
2240
+ LHS.configure do |config|
2240
2241
  config.request_cycle_cache = ActiveSupport::Cache::MemoryStore.new
2241
2242
  end
2242
2243
  ```
@@ -2246,12 +2247,64 @@ end
2246
2247
  If you want to disable the LHS Request Cycle Cache, simply disable it within configuration:
2247
2248
 
2248
2249
  ```ruby
2249
- # config/initializers/lhc.rb
2250
+ # config/initializers/lhs.rb
2250
2251
 
2251
- LHC.configure do |config|
2252
+ LHS.configure do |config|
2252
2253
  config.request_cycle_cache_enabled = false
2253
2254
  end
2254
2255
  ```
2256
+ ## Request tracing
2257
+
2258
+ 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!`.
2259
+
2260
+ Following links, and using `includes` are not traced (just yet).
2261
+
2262
+ In order to enable tracing you need to enable it via LHS configuration:
2263
+
2264
+ ```ruby
2265
+ # config/initializers/lhs.rb
2266
+
2267
+ LHS.configure do |config|
2268
+ config.trace = Rails.env.development? || Rails.logger.level == 0 # debug
2269
+ end
2270
+ ```
2271
+
2272
+ ```ruby
2273
+ # app/controllers/application_controller.rb
2274
+
2275
+ code = Code.find(code: params[:code])
2276
+ ```
2277
+ ```
2278
+ Called from onboarding/app/controllers/concerns/access_code_concern.rb:11:in `access_code'
2279
+ ```
2280
+
2281
+ However, following links and includes won't get traced (just yet):
2282
+
2283
+ ```ruby
2284
+ # app/controllers/application_controller.rb
2285
+
2286
+ code = Code.includes(:places).find(123)
2287
+ ```
2288
+
2289
+ ```
2290
+ # Nothing is traced
2291
+ {
2292
+ places: [...]
2293
+ }
2294
+ ```
2295
+
2296
+ ```ruby
2297
+ code.places
2298
+ ```
2299
+ ```
2300
+ {
2301
+ token: "XYZABCDEF",
2302
+ places:
2303
+ [
2304
+ { href: "http://storage-stg.preprod-local.ch/v2/places/egZelgYhdlg" }
2305
+ ]
2306
+ }
2307
+ ```
2255
2308
 
2256
2309
  ## Testing with LHS
2257
2310
 
@@ -42,10 +42,10 @@ if [ ! -f $CACHE_SIGNATURE_FILE ] ; then
42
42
  fi
43
43
  else
44
44
  echo "bundle install"
45
- bundle install
45
+ bundle $BUNDLER install
46
46
  fi
47
47
  touch $CACHE_SIGNATURE_FILE
48
48
  fi
49
49
 
50
50
  echo "bundle install"
51
- bundle install
51
+ bundle $BUNDLER install
@@ -7,6 +7,10 @@ trial_attachments:
7
7
  include_match: Gemfile
8
8
  content_type: text/plain
9
9
 
10
+ environment_variables:
11
+ BUNDLER:
12
+ read_and_replace_with: .bundler-version
13
+
10
14
  scripts:
11
15
 
12
16
  bundle:
@@ -9,6 +9,8 @@ ports:
9
9
  environment_variables:
10
10
  RUBY:
11
11
  read_and_replace_with: .ruby-version
12
+ BUNDLER:
13
+ read_and_replace_with: .bundler-version
12
14
 
13
15
  scripts:
14
16
  rspec:
@@ -17,7 +19,7 @@ scripts:
17
19
  set -eux
18
20
  mkdir -p tmp/cache
19
21
  export PATH=~/.rubies/$RUBY/bin:$PATH
20
- if [ ! -z ${ACTIVESUPPORT:-} ]; then BUNDLE_GEMFILE=Gemfile.activesupport$ACTIVESUPPORT bundle exec rspec; else bundle exec rspec; fi
22
+ if [ ! -z ${ACTIVESUPPORT:-} ]; then BUNDLE_GEMFILE=Gemfile.activesupport$ACTIVESUPPORT bundle $BUNDLER exec rspec; else bundle exec rspec; fi
21
23
 
22
24
  start_when:
23
25
  'bundled':
@@ -24,7 +24,7 @@ Gem::Specification.new do |s|
24
24
 
25
25
  s.add_dependency 'activemodel'
26
26
  s.add_dependency 'activesupport', '>= 4.2.11'
27
- s.add_dependency 'lhc', '~> 10.1.1'
27
+ s.add_dependency 'lhc', '~> 10.2'
28
28
 
29
29
  s.add_development_dependency 'capybara'
30
30
  s.add_development_dependency 'json', '>= 1.8.2'
@@ -11,6 +11,7 @@ class LHS::Record
11
11
  # Find a single uniqe record
12
12
  def find(*args)
13
13
  args, options = process_args(args)
14
+ options = trace!(options)
14
15
  raise(LHS::Unprocessable.new, 'Cannot find Record without an ID') if args.blank? && !args.is_a?(Array)
15
16
  data =
16
17
  if args.present? && args.is_a?(Array)
@@ -10,14 +10,14 @@ class LHS::Record
10
10
  module ClassMethods
11
11
  # Fetch some record by parameters
12
12
  def find_by(params = {}, options = nil)
13
- _find_by(params, options)
13
+ _find_by(params, trace!(options))
14
14
  rescue LHC::NotFound
15
15
  nil
16
16
  end
17
17
 
18
18
  # Raise if no record was found
19
19
  def find_by!(params = {}, options = nil)
20
- _find_by(params, options)
20
+ _find_by(params, trace!(options))
21
21
  end
22
22
 
23
23
  private
@@ -9,11 +9,11 @@ class LHS::Record
9
9
 
10
10
  module ClassMethods
11
11
  def first(options = nil)
12
- find_by({}, options)
12
+ find_by({}, trace!(options))
13
13
  end
14
14
 
15
15
  def first!(options = nil)
16
- find_by!({}, options)
16
+ find_by!({}, trace!(options))
17
17
  end
18
18
  end
19
19
  end
@@ -9,6 +9,7 @@ class LHS::Record
9
9
 
10
10
  module ClassMethods
11
11
  def last(options = nil)
12
+ options = trace!(options)
12
13
  first_batch = find_by({}, options).parent
13
14
  if first_batch.paginated?
14
15
  pagination = first_batch._pagination
@@ -19,7 +20,7 @@ class LHS::Record
19
20
  end
20
21
 
21
22
  def last!(options = nil)
22
- find_by!({}, options)
23
+ find_by!({}, trace!(options))
23
24
  end
24
25
  end
25
26
  end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support'
4
+
5
+ class LHS::Record
6
+
7
+ module Tracing
8
+ extend ActiveSupport::Concern
9
+
10
+ module ClassMethods
11
+ # Needs to be called directly from the first method (level) within LHS
12
+ def trace!(options = {})
13
+ return options unless LHS.config.trace
14
+
15
+ (options || {}).tap do |options|
16
+ source = caller.detect do |source|
17
+ !source.match?(%r{/lib/lhs})
18
+ end
19
+ options[:source] = source
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -5,12 +5,20 @@ require 'singleton'
5
5
  class LHS::Config
6
6
  include Singleton
7
7
 
8
- attr_accessor :request_cycle_cache_enabled, :request_cycle_cache
8
+ attr_accessor :request_cycle_cache_enabled, :request_cycle_cache, :trace
9
9
 
10
10
  def initialize
11
11
  self.request_cycle_cache_enabled ||= true
12
+ self.trace ||= false
12
13
  if defined?(ActiveSupport::Cache::MemoryStore)
13
14
  self.request_cycle_cache ||= ActiveSupport::Cache::MemoryStore.new
14
15
  end
15
16
  end
17
+
18
+ def reset
19
+ self.request_cycle_cache_enabled = nil
20
+ self.trace = nil
21
+ self.request_cycle_cache = nil
22
+ initialize
23
+ end
16
24
  end
@@ -37,6 +37,8 @@ class LHS::Record
37
37
  'lhs/concerns/record/relations'
38
38
  autoload :Scope,
39
39
  'lhs/concerns/record/scope'
40
+ autoload :Tracing,
41
+ 'lhs/concerns/record/tracing'
40
42
 
41
43
  module RequestCycleCache
42
44
  autoload :RequestCycleThreadRegistry,
@@ -65,6 +67,7 @@ class LHS::Record
65
67
  include Relations
66
68
  include RequestCycleCache
67
69
  include Scope
70
+ include Tracing
68
71
 
69
72
  delegate :_proxy, :_endpoint, :merge_raw!, :select, :becomes, :respond_to?, to: :_data
70
73
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module LHS
4
- VERSION = '19.1.0'
4
+ VERSION = '19.2.0'
5
5
  end
@@ -19,7 +19,7 @@ Rails.application.configure do
19
19
  config.action_controller.perform_caching = false
20
20
 
21
21
  # Raise exceptions instead of rendering exception templates.
22
- config.action_dispatch.show_exceptions = false
22
+ config.action_dispatch.show_exceptions = true
23
23
 
24
24
  # Disable request forgery protection in test environment.
25
25
  config.action_controller.allow_forgery_protection = false
@@ -34,4 +34,7 @@ Rails.application.configure do
34
34
 
35
35
  # Raises error for missing translations
36
36
  # config.action_view.raise_on_missing_translations = true
37
+
38
+ # Use the log level higher than debug to make sure LHS tracing is disabled globally. Then enable it in tests as needed
39
+ config.log_level = :warn
37
40
  end
@@ -1,11 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ ENV["RAILS_ENV"] ||= 'test'
4
+
3
5
  require 'spec_helper'
4
6
  require File.expand_path("../dummy/config/environment", __FILE__)
5
7
  require 'rspec/rails'
6
8
 
7
- ENV["RAILS_ENV"] ||= 'test'
8
-
9
9
  RSpec.configure do |config|
10
10
  config.infer_spec_type_from_file_location!
11
11
  end
@@ -15,7 +15,14 @@ describe LHS::Record do
15
15
  end
16
16
 
17
17
  it 'uses the options that are configured for an endpoint' do
18
- expect(LHC).to receive(:request).with(cache_expires_in: 1.day, retry: 2, cache: true, url: 'backend/v2/feedbacks/1').and_call_original
18
+ expect(LHC).to receive(:request)
19
+ .with(
20
+ cache_expires_in: 1.day,
21
+ retry: 2,
22
+ cache: true,
23
+ url: 'backend/v2/feedbacks/1'
24
+ ).and_call_original
25
+
19
26
  stub_request(:get, "http://backend/v2/feedbacks/1").to_return(status: 200)
20
27
  Record.find(1)
21
28
  end
@@ -0,0 +1,155 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails_helper'
4
+
5
+ describe LHS::Record do
6
+
7
+ context 'tracing' do
8
+ context 'with tracing enabled' do
9
+
10
+ before do
11
+ allow(LHS.config).to receive(:trace).and_return(true)
12
+ end
13
+
14
+ context 'with non-paginated methods' do
15
+
16
+ let(:request) do
17
+ stub_request(:get, "https://records/3jg781")
18
+ .to_return(status: 204)
19
+ end
20
+
21
+ before do
22
+ class Record < LHS::Record
23
+ endpoint 'https://records'
24
+ end
25
+
26
+ expect(LHC).to receive(:request).with(anything) do |arguments|
27
+ expect(arguments[:source]).to include(__FILE__)
28
+ spy(:response)
29
+ end
30
+ end
31
+
32
+ %w[find find_by find_by! first first! last!].each do |method|
33
+ context method do
34
+ it 'forwards tracing options to lhc' do
35
+ Record.public_send(method, color: :blue)
36
+ end
37
+ end
38
+ end
39
+ end
40
+
41
+ context 'with paginated method last' do
42
+
43
+ before do
44
+ class Place < LHS::Record
45
+ endpoint 'http://datastore/places'
46
+ end
47
+
48
+ stub_request(:get, "http://datastore/places?limit=1")
49
+ .to_return(
50
+ body: {
51
+ items: [
52
+ { id: 'first-1', company_name: 'Localsearch AG' }
53
+ ],
54
+ total: 500,
55
+ limit: 1,
56
+ offset: 0
57
+ }.to_json
58
+ )
59
+
60
+ stub_request(:get, "http://datastore/places?limit=1&offset=499")
61
+ .to_return(
62
+ body: {
63
+ items: [
64
+ { id: 'last-500', company_name: 'Curious GmbH' }
65
+ ],
66
+ total: 500,
67
+ limit: 1,
68
+ offset: 0
69
+ }.to_json
70
+ )
71
+
72
+ expect(LHC).to receive(:request).and_call_original
73
+ expect(LHC).to receive(:request).with(hash_including(params: { offset: 499, limit: 1 })) do |arguments|
74
+ expect(arguments[:source]).to include(__FILE__)
75
+ spy(:response)
76
+ end
77
+ end
78
+
79
+ it 'forwards tracing options to lhc' do
80
+ # binding.pry
81
+ Place.last
82
+ end
83
+ end
84
+ end
85
+
86
+ context 'tracing disabled (default)' do
87
+ context 'non-paginated methods' do
88
+
89
+ before do
90
+ class Record < LHS::Record
91
+ endpoint 'https://records'
92
+ end
93
+
94
+ expect(LHC).to receive(:request).with(anything) do |arguments|
95
+ expect(arguments).not_to include(:source)
96
+ spy(:response)
97
+ end
98
+ end
99
+
100
+ %w[find find_by find_by! first first! last!].each do |method|
101
+ context method do
102
+ it 'does not forward tracing options to lhc' do
103
+ Record.public_send(method, color: :blue)
104
+ end
105
+ end
106
+ end
107
+ end
108
+
109
+ context 'with paginated method last' do
110
+ before do
111
+ class Place < LHS::Record
112
+ endpoint 'http://datastore/places'
113
+ end
114
+
115
+ stub_request(:get, "http://datastore/places?limit=1")
116
+ .to_return(
117
+ body: {
118
+ items: [
119
+ { id: 'first-1', company_name: 'Localsearch AG' }
120
+ ],
121
+ total: 500,
122
+ limit: 1,
123
+ offset: 0
124
+ }.to_json
125
+ )
126
+
127
+ stub_request(:get, "http://datastore/places?limit=1&offset=499")
128
+ .to_return(
129
+ body: {
130
+ items: [
131
+ { id: 'last-500', company_name: 'Curious GmbH' }
132
+ ],
133
+ total: 500,
134
+ limit: 1,
135
+ offset: 0
136
+ }.to_json
137
+ )
138
+ end
139
+
140
+ it 'does not forward tracing options to lhc' do
141
+ # for first pagination requets (first-1)
142
+ expect(LHC).to receive(:request).and_call_original
143
+
144
+ # for second reques (last-500)
145
+ expect(LHC).to receive(:request).with(hash_including(params: { offset: 499, limit: 1 })) do |arguments|
146
+ expect(arguments).not_to include(:source)
147
+ spy(:response)
148
+ end
149
+
150
+ Place.last
151
+ end
152
+ end
153
+ end
154
+ end
155
+ 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: 19.1.0
4
+ version: 19.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - https://github.com/local-ch/lhs/graphs/contributors
@@ -44,14 +44,14 @@ dependencies:
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: 10.1.1
47
+ version: '10.2'
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: 10.1.1
54
+ version: '10.2'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: capybara
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -192,6 +192,7 @@ executables: []
192
192
  extensions: []
193
193
  extra_rdoc_files: []
194
194
  files:
195
+ - ".bundler-version"
195
196
  - ".gitignore"
196
197
  - ".rubocop.localch.yml"
197
198
  - ".rubocop.yml"
@@ -256,6 +257,7 @@ files:
256
257
  - lib/lhs/concerns/record/request_cycle_cache/interceptor.rb
257
258
  - lib/lhs/concerns/record/request_cycle_cache/request_cycle_thread_registry.rb
258
259
  - lib/lhs/concerns/record/scope.rb
260
+ - lib/lhs/concerns/record/tracing.rb
259
261
  - lib/lhs/config.rb
260
262
  - lib/lhs/data.rb
261
263
  - lib/lhs/endpoint.rb
@@ -439,6 +441,7 @@ files:
439
441
  - spec/record/to_ary_spec.rb
440
442
  - spec/record/to_hash_spec.rb
441
443
  - spec/record/to_json_spec.rb
444
+ - spec/record/tracing_spec.rb
442
445
  - spec/record/where_chains_spec.rb
443
446
  - spec/record/where_spec.rb
444
447
  - spec/record/where_values_hash_spec.rb
@@ -640,6 +643,7 @@ test_files:
640
643
  - spec/record/to_ary_spec.rb
641
644
  - spec/record/to_hash_spec.rb
642
645
  - spec/record/to_json_spec.rb
646
+ - spec/record/tracing_spec.rb
643
647
  - spec/record/where_chains_spec.rb
644
648
  - spec/record/where_spec.rb
645
649
  - spec/record/where_values_hash_spec.rb