lhc 9.4.0 → 9.4.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 +4 -4
- data/README.md +688 -72
- data/gh-md-toc +228 -0
- data/lhc.gemspec +3 -3
- data/lib/lhc/version.rb +1 -1
- metadata +6 -20
- data/docs/configuration.md +0 -57
- data/docs/exceptions.md +0 -68
- data/docs/interceptors.md +0 -98
- data/docs/interceptors/authentication.md +0 -54
- data/docs/interceptors/caching.md +0 -68
- data/docs/interceptors/default_timeout.md +0 -17
- data/docs/interceptors/logging.md +0 -29
- data/docs/interceptors/monitoring.md +0 -68
- data/docs/interceptors/prometheus.md +0 -18
- data/docs/interceptors/retry.md +0 -24
- data/docs/interceptors/rollbar.md +0 -19
- data/docs/interceptors/zipkin.md +0 -23
- data/docs/request.md +0 -24
- data/docs/response.md +0 -19
data/gh-md-toc
ADDED
@@ -0,0 +1,228 @@
|
|
1
|
+
#!/usr/bin/env bash
|
2
|
+
|
3
|
+
#
|
4
|
+
# Steps:
|
5
|
+
#
|
6
|
+
# 1. Download corresponding html file for some README.md:
|
7
|
+
# curl -s $1
|
8
|
+
#
|
9
|
+
# 2. Discard rows where no substring 'user-content-' (github's markup):
|
10
|
+
# awk '/user-content-/ { ...
|
11
|
+
#
|
12
|
+
# 3.1 Get last number in each row like ' ... </span></a>sitemap.js</h1'.
|
13
|
+
# It's a level of the current header:
|
14
|
+
# substr($0, length($0), 1)
|
15
|
+
#
|
16
|
+
# 3.2 Get level from 3.1 and insert corresponding number of spaces before '*':
|
17
|
+
# sprintf("%*s", substr($0, length($0), 1)*3, " ")
|
18
|
+
#
|
19
|
+
# 4. Find head's text and insert it inside "* [ ... ]":
|
20
|
+
# substr($0, match($0, /a>.*<\/h/)+2, RLENGTH-5)
|
21
|
+
#
|
22
|
+
# 5. Find anchor and insert it inside "(...)":
|
23
|
+
# substr($0, match($0, "href=\"[^\"]+?\" ")+6, RLENGTH-8)
|
24
|
+
#
|
25
|
+
|
26
|
+
gh_toc_version="0.5.0"
|
27
|
+
|
28
|
+
gh_user_agent="gh-md-toc v$gh_toc_version"
|
29
|
+
|
30
|
+
#
|
31
|
+
# Download rendered into html README.md by its url.
|
32
|
+
#
|
33
|
+
#
|
34
|
+
gh_toc_load() {
|
35
|
+
local gh_url=$1
|
36
|
+
|
37
|
+
if type curl &>/dev/null; then
|
38
|
+
curl --user-agent "$gh_user_agent" -s "$gh_url"
|
39
|
+
elif type wget &>/dev/null; then
|
40
|
+
wget --user-agent="$gh_user_agent" -qO- "$gh_url"
|
41
|
+
else
|
42
|
+
echo "Please, install 'curl' or 'wget' and try again."
|
43
|
+
exit 1
|
44
|
+
fi
|
45
|
+
}
|
46
|
+
|
47
|
+
#
|
48
|
+
# Converts local md file into html by GitHub
|
49
|
+
#
|
50
|
+
# ➥ curl -X POST --data '{"text": "Hello world github/linguist#1 **cool**, and #1!"}' https://api.github.com/markdown
|
51
|
+
# <p>Hello world github/linguist#1 <strong>cool</strong>, and #1!</p>'"
|
52
|
+
gh_toc_md2html() {
|
53
|
+
local gh_file_md=$1
|
54
|
+
URL=https://api.github.com/markdown/raw
|
55
|
+
TOKEN="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/token.txt"
|
56
|
+
if [ -f "$TOKEN" ]; then
|
57
|
+
URL="$URL?access_token=$(cat $TOKEN)"
|
58
|
+
fi
|
59
|
+
curl -s --user-agent "$gh_user_agent" \
|
60
|
+
--data-binary @"$gh_file_md" -H "Content-Type:text/plain" \
|
61
|
+
$URL
|
62
|
+
}
|
63
|
+
|
64
|
+
#
|
65
|
+
# Is passed string url
|
66
|
+
#
|
67
|
+
gh_is_url() {
|
68
|
+
case $1 in
|
69
|
+
https* | http*)
|
70
|
+
echo "yes";;
|
71
|
+
*)
|
72
|
+
echo "no";;
|
73
|
+
esac
|
74
|
+
}
|
75
|
+
|
76
|
+
#
|
77
|
+
# TOC generator
|
78
|
+
#
|
79
|
+
gh_toc(){
|
80
|
+
local gh_src=$1
|
81
|
+
local gh_src_copy=$1
|
82
|
+
local gh_ttl_docs=$2
|
83
|
+
local need_replace=$3
|
84
|
+
|
85
|
+
if [ "$gh_src" = "" ]; then
|
86
|
+
echo "Please, enter URL or local path for a README.md"
|
87
|
+
exit 1
|
88
|
+
fi
|
89
|
+
|
90
|
+
|
91
|
+
# Show "TOC" string only if working with one document
|
92
|
+
if [ "$gh_ttl_docs" = "1" ]; then
|
93
|
+
|
94
|
+
echo "Table of Contents"
|
95
|
+
echo "================="
|
96
|
+
echo ""
|
97
|
+
gh_src_copy=""
|
98
|
+
|
99
|
+
fi
|
100
|
+
|
101
|
+
if [ "$(gh_is_url "$gh_src")" == "yes" ]; then
|
102
|
+
gh_toc_load "$gh_src" | gh_toc_grab "$gh_src_copy"
|
103
|
+
if [ "$need_replace" = "yes" ]; then
|
104
|
+
echo
|
105
|
+
echo "!! '$gh_src' is not a local file"
|
106
|
+
echo "!! Can't insert the TOC into it."
|
107
|
+
echo
|
108
|
+
fi
|
109
|
+
else
|
110
|
+
local toc=`gh_toc_md2html "$gh_src" | gh_toc_grab "$gh_src_copy"`
|
111
|
+
echo "$toc"
|
112
|
+
if [ "$need_replace" = "yes" ]; then
|
113
|
+
local ts="<\!--ts-->"
|
114
|
+
local te="<\!--te-->"
|
115
|
+
local dt=`date +'%F_%H%M%S'`
|
116
|
+
local ext=".orig.${dt}"
|
117
|
+
local toc_path="${gh_src}.toc.${dt}"
|
118
|
+
local toc_footer="<!-- Added by: `whoami`, at: `date --iso-8601='minutes'` -->"
|
119
|
+
# http://fahdshariff.blogspot.ru/2012/12/sed-mutli-line-replacement-between-two.html
|
120
|
+
# clear old TOC
|
121
|
+
sed -i${ext} "/${ts}/,/${te}/{//!d;}" "$gh_src"
|
122
|
+
# create toc file
|
123
|
+
echo "${toc}" > "${toc_path}"
|
124
|
+
echo -e "\n${toc_footer}\n" >> "$toc_path"
|
125
|
+
# insert toc file
|
126
|
+
if [[ "`uname`" == "Darwin" ]]; then
|
127
|
+
sed -i "" "/${ts}/r ${toc_path}" "$gh_src"
|
128
|
+
else
|
129
|
+
sed -i "/${ts}/r ${toc_path}" "$gh_src"
|
130
|
+
fi
|
131
|
+
echo
|
132
|
+
echo "!! TOC was added into: '$gh_src'"
|
133
|
+
echo "!! Origin version of the file: '${gh_src}${ext}'"
|
134
|
+
echo "!! TOC added into a separate file: '${toc_path}'"
|
135
|
+
echo
|
136
|
+
fi
|
137
|
+
fi
|
138
|
+
}
|
139
|
+
|
140
|
+
#
|
141
|
+
# Grabber of the TOC from rendered html
|
142
|
+
#
|
143
|
+
# $1 — a source url of document.
|
144
|
+
# It's need if TOC is generated for multiple documents.
|
145
|
+
#
|
146
|
+
gh_toc_grab() {
|
147
|
+
# if closed <h[1-6]> is on the new line, then move it on the prev line
|
148
|
+
# for example:
|
149
|
+
# was: The command <code>foo1</code>
|
150
|
+
# </h1>
|
151
|
+
# became: The command <code>foo1</code></h1>
|
152
|
+
sed -e ':a' -e 'N' -e '$!ba' -e 's/\n<\/h/<\/h/g' |
|
153
|
+
# find strings that corresponds to template
|
154
|
+
grep -E -o '<a.*id="user-content-[^"]*".*</h[1-6]' |
|
155
|
+
# remove code tags
|
156
|
+
sed 's/<code>//' | sed 's/<\/code>//' |
|
157
|
+
# now all rows are like:
|
158
|
+
# <a id="user-content-..." href="..."><span ...></span></a> ... </h1
|
159
|
+
# format result line
|
160
|
+
# * $0 — whole string
|
161
|
+
echo -e "$(awk -v "gh_url=$1" '{
|
162
|
+
print sprintf("%*s", substr($0, length($0), 1)*3, " ") "* [" substr($0, match($0, /a>.*<\/h/)+2, RLENGTH-5)"](" gh_url substr($0, match($0, "href=\"[^\"]+?\" ")+6, RLENGTH-8) ")"}' | sed 'y/+/ /; s/%/\\x/g')"
|
163
|
+
}
|
164
|
+
|
165
|
+
#
|
166
|
+
# Returns filename only from full path or url
|
167
|
+
#
|
168
|
+
gh_toc_get_filename() {
|
169
|
+
echo "${1##*/}"
|
170
|
+
}
|
171
|
+
|
172
|
+
#
|
173
|
+
# Options hendlers
|
174
|
+
#
|
175
|
+
gh_toc_app() {
|
176
|
+
local app_name="gh-md-toc"
|
177
|
+
local need_replace="no"
|
178
|
+
|
179
|
+
if [ "$1" = '--help' ] || [ $# -eq 0 ] ; then
|
180
|
+
echo "GitHub TOC generator ($app_name): $gh_toc_version"
|
181
|
+
echo ""
|
182
|
+
echo "Usage:"
|
183
|
+
echo " $app_name [--insert] src [src] Create TOC for a README file (url or local path)"
|
184
|
+
echo " $app_name - Create TOC for markdown from STDIN"
|
185
|
+
echo " $app_name --help Show help"
|
186
|
+
echo " $app_name --version Show version"
|
187
|
+
return
|
188
|
+
fi
|
189
|
+
|
190
|
+
if [ "$1" = '--version' ]; then
|
191
|
+
echo "$gh_toc_version"
|
192
|
+
return
|
193
|
+
fi
|
194
|
+
|
195
|
+
if [ "$1" = "-" ]; then
|
196
|
+
if [ -z "$TMPDIR" ]; then
|
197
|
+
TMPDIR="/tmp"
|
198
|
+
elif [ -n "$TMPDIR" -a ! -d "$TMPDIR" ]; then
|
199
|
+
mkdir -p "$TMPDIR"
|
200
|
+
fi
|
201
|
+
local gh_tmp_md
|
202
|
+
gh_tmp_md=$(mktemp $TMPDIR/tmp.XXXXXX)
|
203
|
+
while read input; do
|
204
|
+
echo "$input" >> "$gh_tmp_md"
|
205
|
+
done
|
206
|
+
gh_toc_md2html "$gh_tmp_md" | gh_toc_grab ""
|
207
|
+
return
|
208
|
+
fi
|
209
|
+
|
210
|
+
if [ "$1" = '--insert' ]; then
|
211
|
+
need_replace="yes"
|
212
|
+
shift
|
213
|
+
fi
|
214
|
+
|
215
|
+
for md in "$@"
|
216
|
+
do
|
217
|
+
echo ""
|
218
|
+
gh_toc "$md" "$#" "$need_replace"
|
219
|
+
done
|
220
|
+
|
221
|
+
echo ""
|
222
|
+
echo "Created by [gh-md-toc](https://github.com/ekalinin/github-markdown-toc)"
|
223
|
+
}
|
224
|
+
|
225
|
+
#
|
226
|
+
# Entry point
|
227
|
+
#
|
228
|
+
gh_toc_app "$@"
|
data/lhc.gemspec
CHANGED
@@ -8,10 +8,10 @@ Gem::Specification.new do |s|
|
|
8
8
|
s.name = "lhc"
|
9
9
|
s.version = LHC::VERSION
|
10
10
|
s.authors = ['https://github.com/local-ch/lhc/contributors']
|
11
|
-
s.email = ['
|
11
|
+
s.email = ['web@localsearch.ch']
|
12
12
|
s.homepage = 'https://github.com/local-ch/lhc'
|
13
|
-
s.summary = '
|
14
|
-
s.description = '
|
13
|
+
s.summary = 'Advanced HTTP Client for Ruby, fueled with interceptors'
|
14
|
+
s.description = 'Advanced HTTP Client for Ruby, fueled with interceptors'
|
15
15
|
|
16
16
|
s.files = `git ls-files`.split("\n")
|
17
17
|
s.test_files = `git ls-files -- spec/*`.split("\n")
|
data/lib/lhc/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lhc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 9.4.
|
4
|
+
version: 9.4.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- https://github.com/local-ch/lhc/contributors
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-07-
|
11
|
+
date: 2018-07-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -164,10 +164,9 @@ dependencies:
|
|
164
164
|
- - ">="
|
165
165
|
- !ruby/object:Gem::Version
|
166
166
|
version: '0'
|
167
|
-
description:
|
168
|
-
(like interceptors)'
|
167
|
+
description: Advanced HTTP Client for Ruby, fueled with interceptors
|
169
168
|
email:
|
170
|
-
-
|
169
|
+
- web@localsearch.ch
|
171
170
|
executables: []
|
172
171
|
extensions: []
|
173
172
|
extra_rdoc_files: []
|
@@ -189,20 +188,7 @@ files:
|
|
189
188
|
- cider-ci/task_components/rspec.yml
|
190
189
|
- cider-ci/task_components/rubocop.yml
|
191
190
|
- cider-ci/task_components/ruby.yml
|
192
|
-
-
|
193
|
-
- docs/exceptions.md
|
194
|
-
- docs/interceptors.md
|
195
|
-
- docs/interceptors/authentication.md
|
196
|
-
- docs/interceptors/caching.md
|
197
|
-
- docs/interceptors/default_timeout.md
|
198
|
-
- docs/interceptors/logging.md
|
199
|
-
- docs/interceptors/monitoring.md
|
200
|
-
- docs/interceptors/prometheus.md
|
201
|
-
- docs/interceptors/retry.md
|
202
|
-
- docs/interceptors/rollbar.md
|
203
|
-
- docs/interceptors/zipkin.md
|
204
|
-
- docs/request.md
|
205
|
-
- docs/response.md
|
191
|
+
- gh-md-toc
|
206
192
|
- lhc.gemspec
|
207
193
|
- lib/lhc.rb
|
208
194
|
- lib/lhc/concerns/lhc/basic_methods_concern.rb
|
@@ -377,7 +363,7 @@ rubyforge_project:
|
|
377
363
|
rubygems_version: 2.7.6
|
378
364
|
signing_key:
|
379
365
|
specification_version: 4
|
380
|
-
summary:
|
366
|
+
summary: Advanced HTTP Client for Ruby, fueled with interceptors
|
381
367
|
test_files:
|
382
368
|
- spec/basic_methods/delete_spec.rb
|
383
369
|
- spec/basic_methods/get_spec.rb
|
data/docs/configuration.md
DELETED
@@ -1,57 +0,0 @@
|
|
1
|
-
Configuration
|
2
|
-
===
|
3
|
-
|
4
|
-
## Configure LHC on initialization
|
5
|
-
|
6
|
-
If you want to configure LHC on initialization (like in a Rails initializer, `environment.rb` or `application.rb`), you could run into the problem that certain configurations can only be set once.
|
7
|
-
You can use `LHC.configure` to prevent the initialization problem.
|
8
|
-
Take care that you only use `LHC.configure` once, because it is actually reseting previously made configurations and applies the new once.
|
9
|
-
|
10
|
-
```ruby
|
11
|
-
|
12
|
-
LHC.configure do |c|
|
13
|
-
c.placeholder :datastore, 'http://datastore/v2'
|
14
|
-
c.endpoint :feedbacks, '{+datastore}/feedbacks'
|
15
|
-
c.interceptors = [CachingInterceptor, MonitorInterceptor, TrackingIdInterceptor]
|
16
|
-
end
|
17
|
-
|
18
|
-
```
|
19
|
-
|
20
|
-
## Endpoints
|
21
|
-
|
22
|
-
You can configure endpoints, for later use, by giving them a name, an url and some parameters (optional).
|
23
|
-
|
24
|
-
```ruby
|
25
|
-
url = 'http://datastore/v2/feedbacks'
|
26
|
-
options = { params: { has_reviews: true } }
|
27
|
-
LHC.config.endpoint(:feedbacks, url, options)
|
28
|
-
LHC.get(:feedbacks)
|
29
|
-
```
|
30
|
-
|
31
|
-
Explicit request options override configured options.
|
32
|
-
|
33
|
-
```ruby
|
34
|
-
LHC.get(:feedbacks, params: { has_reviews: false }) # Overrides configured params
|
35
|
-
```
|
36
|
-
|
37
|
-
## Placeholders
|
38
|
-
|
39
|
-
You can configure global placeholders, that are used when generating urls from url-templates.
|
40
|
-
|
41
|
-
```ruby
|
42
|
-
LHC.config.placeholder(:datastore, 'http://datastore/v2')
|
43
|
-
options = { params: { has_reviews: true } }
|
44
|
-
LHC.config.endpoint(:feedbacks, '{+datastore}/feedbacks', options)
|
45
|
-
LHC.get(:feedbacks)
|
46
|
-
```
|
47
|
-
|
48
|
-
## Interceptors
|
49
|
-
|
50
|
-
To enable interceptors you have to configure LHC's interceptors for http communication.
|
51
|
-
The global default interceptors are processed in the order you provide them.
|
52
|
-
|
53
|
-
```ruby
|
54
|
-
LHC.config.interceptors = [CachingInterceptor, MonitorInterceptor, TrackingIdInterceptor]
|
55
|
-
```
|
56
|
-
|
57
|
-
You can only set the list of global interceptors once and you can not alter it after you set it.
|
data/docs/exceptions.md
DELETED
@@ -1,68 +0,0 @@
|
|
1
|
-
Exceptions
|
2
|
-
===
|
3
|
-
|
4
|
-
Anything but a response code indicating success (2**) raises an exception.
|
5
|
-
|
6
|
-
```ruby
|
7
|
-
|
8
|
-
LHC.get('localhost') # UnknownError: 0
|
9
|
-
LHC.get('http://localhost:3000') # LHC::Timeout: 0
|
10
|
-
|
11
|
-
```
|
12
|
-
|
13
|
-
You can access the response object that was causing the error.
|
14
|
-
|
15
|
-
```ruby
|
16
|
-
LHC.get('local.ch')
|
17
|
-
rescue => e
|
18
|
-
e.response #<LHC:Response>
|
19
|
-
e.response.code # 403
|
20
|
-
e.response.timeout? # false
|
21
|
-
Rails.logger.error e
|
22
|
-
# LHC::UnknownError: get http://local.cac
|
23
|
-
# Params: {:url=>"http://local.cac", :method=>:get}
|
24
|
-
# Response Code: 0
|
25
|
-
# <Response Body>
|
26
|
-
```
|
27
|
-
|
28
|
-
All errors that are raise by LHC inherit from `LHC::Error`.
|
29
|
-
They are divided into `LHC::ClientError`, `LHC::ServerError`, `LHC::Timeout` and `LHC::UnkownError` and mapped according to the following status code.
|
30
|
-
|
31
|
-
```ruby
|
32
|
-
400 => LHC::BadRequest
|
33
|
-
401 => LHC::Unauthorized
|
34
|
-
402 => LHC::PaymentRequired
|
35
|
-
403 => LHC::Forbidden
|
36
|
-
403 => LHC::Forbidden
|
37
|
-
404 => LHC::NotFound
|
38
|
-
405 => LHC::MethodNotAllowed
|
39
|
-
406 => LHC::NotAcceptable
|
40
|
-
407 => LHC::ProxyAuthenticationRequired
|
41
|
-
408 => LHC::RequestTimeout
|
42
|
-
409 => LHC::Conflict
|
43
|
-
410 => LHC::Gone
|
44
|
-
411 => LHC::LengthRequired
|
45
|
-
412 => LHC::PreconditionFailed
|
46
|
-
413 => LHC::RequestEntityTooLarge
|
47
|
-
414 => LHC::RequestUriToLong
|
48
|
-
415 => LHC::UnsupportedMediaType
|
49
|
-
416 => LHC::RequestedRangeNotSatisfiable
|
50
|
-
417 => LHC::ExpectationFailed
|
51
|
-
422 => LHC::UnprocessableEntity
|
52
|
-
423 => LHC::Locked
|
53
|
-
424 => LHC::FailedDependency
|
54
|
-
426 => LHC::UpgradeRequired
|
55
|
-
|
56
|
-
500 => LHC::InternalServerError
|
57
|
-
501 => LHC::NotImplemented
|
58
|
-
502 => LHC::BadGateway
|
59
|
-
503 => LHC::ServiceUnavailable
|
60
|
-
504 => LHC::GatewayTimeout
|
61
|
-
505 => LHC::HttpVersionNotSupported
|
62
|
-
507 => LHC::InsufficientStorage
|
63
|
-
510 => LHC::NotExtended
|
64
|
-
|
65
|
-
timeout? => LHC::Timeout
|
66
|
-
|
67
|
-
anything_else => LHC::UnknownError
|
68
|
-
```
|
data/docs/interceptors.md
DELETED
@@ -1,98 +0,0 @@
|
|
1
|
-
Interceptors
|
2
|
-
===
|
3
|
-
|
4
|
-
## Quick Start Guide
|
5
|
-
|
6
|
-
```ruby
|
7
|
-
class TrackingIdInterceptor < LHC::Interceptor
|
8
|
-
|
9
|
-
def before_request
|
10
|
-
request.params[:tid] = 123
|
11
|
-
end
|
12
|
-
end
|
13
|
-
```
|
14
|
-
|
15
|
-
```ruby
|
16
|
-
LHC.config.interceptors = [TrackingIdInterceptor] # global list of default interceptors
|
17
|
-
```
|
18
|
-
|
19
|
-
```ruby
|
20
|
-
LHC.request({url: 'http://local.ch', interceptors: []}) # no interceptor for this request
|
21
|
-
```
|
22
|
-
|
23
|
-
## Core Interceptors
|
24
|
-
|
25
|
-
There are some interceptors that are part of LHC already, that cover some basic usecases:
|
26
|
-
|
27
|
-
[Available Core Interceptors](/docs/interceptors)
|
28
|
-
|
29
|
-
## Callbacks
|
30
|
-
|
31
|
-
`before_raw_request` is called before the raw typhoeus request is prepared/created.
|
32
|
-
|
33
|
-
`before_request` is called when the request is prepared and about to be executed.
|
34
|
-
|
35
|
-
`after_request` is called after request was started.
|
36
|
-
|
37
|
-
`before_response` is called when response started to arrive.
|
38
|
-
|
39
|
-
`after_response` is called after the response arrived completely.
|
40
|
-
|
41
|
-
|
42
|
-
## Attributes: Request/Response
|
43
|
-
|
44
|
-
Every interceptor can directly access their instance `request` or `response`.
|
45
|
-
|
46
|
-
→ [Read more about the request object](request.md)
|
47
|
-
|
48
|
-
→ [Read more about the response object](response.md)
|
49
|
-
|
50
|
-
## Global default interceptors
|
51
|
-
|
52
|
-
Set the list of global default interceptors.
|
53
|
-
The global default interceptors are processed in the order you provide them.
|
54
|
-
|
55
|
-
```ruby
|
56
|
-
LHC.config.interceptors = [CachingInterceptor, MonitorInterceptor, TrackingIdInterceptor]
|
57
|
-
```
|
58
|
-
|
59
|
-
You can only set the list of global interceptors once and you cannot alter them later.
|
60
|
-
|
61
|
-
## Interceptors on request level
|
62
|
-
|
63
|
-
You can override the global default interceptors on request level:
|
64
|
-
|
65
|
-
```ruby
|
66
|
-
interceptors = LHC.config.interceptors
|
67
|
-
interceptors -= [CachingInterceptor] # remove caching
|
68
|
-
interceptors += [RetryInterceptor] # add retry
|
69
|
-
LHC.request({url: 'http://local.ch', retry: 2, interceptors: interceptors})
|
70
|
-
```
|
71
|
-
|
72
|
-
## Provide Response
|
73
|
-
|
74
|
-
Inside an interceptor, you are able to provide a response, rather then doing a real request.
|
75
|
-
This is useful for implementing an interceptor for caching.
|
76
|
-
|
77
|
-
```ruby
|
78
|
-
class LHC::Cache < LHC::Interceptor
|
79
|
-
|
80
|
-
def before_request(request)
|
81
|
-
cached_response = Rails.cache.fetch(request.url)
|
82
|
-
return LHC::Response.new(cached_response) if cached_response
|
83
|
-
end
|
84
|
-
end
|
85
|
-
```
|
86
|
-
|
87
|
-
Take care that having more than one interceptor trying to return a response will cause an exception.
|
88
|
-
You can access the request.response to identify if a response was already provided by another interceptor.
|
89
|
-
|
90
|
-
```ruby
|
91
|
-
class RemoteCacheInterceptor < LHC::Interceptor
|
92
|
-
|
93
|
-
def before_request(request)
|
94
|
-
return unless request.response.nil?
|
95
|
-
return LHC::Response.new(remote_cache)
|
96
|
-
end
|
97
|
-
end
|
98
|
-
```
|