ur 0.0.4 → 0.2.1

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: 06e6df921036d0111ac6b57e937508d5689d30bbb702ebfc691749a9c31884ed
4
- data.tar.gz: d160635a422395081f510059b7916aa9264c3289c3cc1d9170769c32c6d1cbb3
3
+ metadata.gz: 1161e5f2e2219053fc4c54e541feb0d4c6efa1c1af930365e526a88bc0973e1b
4
+ data.tar.gz: b42e166ae62f0a651d5425147e64b1d200c09862869188b8f95e52454f7b8e97
5
5
  SHA512:
6
- metadata.gz: 41b9e773174eb1beaa88b128ebde4e75830e4c9bc9ee907bcbf24e9be7157ba0cff8a980bdc8b18a978a02d990d7933dcd3ae88fdacba81ed59f87046d36a559
7
- data.tar.gz: 42dc377e5da9acccb960e35dea7d7d874c5a90b5965b025b0dd2a8c6443b89eae282f1f034bcee450dd8962bb06470d7016cbeaebf24871e3851853db83654ff
6
+ metadata.gz: 17187f65ff16035e72fc7d7e0b9468bd970d337b1f8173fbc2dde15f420ad406aa33ecb440b4b65e0dffb921279660fdedb48b4bb320ce3e4e9a411eec3a21da
7
+ data.tar.gz: 8c8d0f0c86af8a97c18dc794e69b84634421607a7e6715120b90fd3d9f74bdd1848a5c422598dbc06fdb0ea7ce4ac474e76b761ec7e2243fdb87b2aa812af292
data/CHANGELOG.md CHANGED
@@ -1,3 +1,18 @@
1
+ # v0.2.1
2
+
3
+ - JSI v0.6.0
4
+
5
+ # v0.2.0
6
+
7
+ - Ur uses JSI schema modules instead of classes
8
+
9
+ # v0.1.1
10
+ - minor fixes
11
+
12
+ # v0.1.0
13
+ - rename processing to metadata
14
+ - Ur::ContentType
15
+
1
16
  # v0.0.4
2
17
  - bump JSI v0.2.0
3
18
 
data/LICENSE.md ADDED
@@ -0,0 +1,169 @@
1
+ Copright © [Ethan](https://github.com/notEthan/)
2
+
3
+ [<img align="right" src="https://github.com/notEthan/ur/raw/master/resources/icons/LGPL-3.0.png">](https://www.gnu.org/licenses/lgpl-3.0.html)
4
+
5
+ Ur is Open Source Software licensed under the terms of the [GNU Affero General Public License version 3](https://www.gnu.org/licenses/lgpl-3.0.html).
6
+
7
+ GNU Lesser General Public License
8
+ =================================
9
+
10
+ _Version 3, 29 June 2007_
11
+ _Copyright © 2007 Free Software Foundation, Inc. &lt;<https://fsf.org/>&gt;_
12
+
13
+ Everyone is permitted to copy and distribute verbatim copies
14
+ of this license document, but changing it is not allowed.
15
+
16
+
17
+ This version of the GNU Lesser General Public License incorporates
18
+ the terms and conditions of version 3 of the GNU General Public
19
+ License, supplemented by the additional permissions listed below.
20
+
21
+ ### 0. Additional Definitions
22
+
23
+ As used herein, “this License” refers to version 3 of the GNU Lesser
24
+ General Public License, and the “GNU GPL” refers to version 3 of the GNU
25
+ General Public License.
26
+
27
+ “The Library” refers to a covered work governed by this License,
28
+ other than an Application or a Combined Work as defined below.
29
+
30
+ An “Application” is any work that makes use of an interface provided
31
+ by the Library, but which is not otherwise based on the Library.
32
+ Defining a subclass of a class defined by the Library is deemed a mode
33
+ of using an interface provided by the Library.
34
+
35
+ A “Combined Work” is a work produced by combining or linking an
36
+ Application with the Library. The particular version of the Library
37
+ with which the Combined Work was made is also called the “Linked
38
+ Version”.
39
+
40
+ The “Minimal Corresponding Source” for a Combined Work means the
41
+ Corresponding Source for the Combined Work, excluding any source code
42
+ for portions of the Combined Work that, considered in isolation, are
43
+ based on the Application, and not on the Linked Version.
44
+
45
+ The “Corresponding Application Code” for a Combined Work means the
46
+ object code and/or source code for the Application, including any data
47
+ and utility programs needed for reproducing the Combined Work from the
48
+ Application, but excluding the System Libraries of the Combined Work.
49
+
50
+ ### 1. Exception to Section 3 of the GNU GPL
51
+
52
+ You may convey a covered work under sections 3 and 4 of this License
53
+ without being bound by section 3 of the GNU GPL.
54
+
55
+ ### 2. Conveying Modified Versions
56
+
57
+ If you modify a copy of the Library, and, in your modifications, a
58
+ facility refers to a function or data to be supplied by an Application
59
+ that uses the facility (other than as an argument passed when the
60
+ facility is invoked), then you may convey a copy of the modified
61
+ version:
62
+
63
+ * **a)** under this License, provided that you make a good faith effort to
64
+ ensure that, in the event an Application does not supply the
65
+ function or data, the facility still operates, and performs
66
+ whatever part of its purpose remains meaningful, or
67
+
68
+ * **b)** under the GNU GPL, with none of the additional permissions of
69
+ this License applicable to that copy.
70
+
71
+ ### 3. Object Code Incorporating Material from Library Header Files
72
+
73
+ The object code form of an Application may incorporate material from
74
+ a header file that is part of the Library. You may convey such object
75
+ code under terms of your choice, provided that, if the incorporated
76
+ material is not limited to numerical parameters, data structure
77
+ layouts and accessors, or small macros, inline functions and templates
78
+ (ten or fewer lines in length), you do both of the following:
79
+
80
+ * **a)** Give prominent notice with each copy of the object code that the
81
+ Library is used in it and that the Library and its use are
82
+ covered by this License.
83
+ * **b)** Accompany the object code with a copy of the GNU GPL and this license
84
+ document.
85
+
86
+ ### 4. Combined Works
87
+
88
+ You may convey a Combined Work under terms of your choice that,
89
+ taken together, effectively do not restrict modification of the
90
+ portions of the Library contained in the Combined Work and reverse
91
+ engineering for debugging such modifications, if you also do each of
92
+ the following:
93
+
94
+ * **a)** Give prominent notice with each copy of the Combined Work that
95
+ the Library is used in it and that the Library and its use are
96
+ covered by this License.
97
+
98
+ * **b)** Accompany the Combined Work with a copy of the GNU GPL and this license
99
+ document.
100
+
101
+ * **c)** For a Combined Work that displays copyright notices during
102
+ execution, include the copyright notice for the Library among
103
+ these notices, as well as a reference directing the user to the
104
+ copies of the GNU GPL and this license document.
105
+
106
+ * **d)** Do one of the following:
107
+ - **0)** Convey the Minimal Corresponding Source under the terms of this
108
+ License, and the Corresponding Application Code in a form
109
+ suitable for, and under terms that permit, the user to
110
+ recombine or relink the Application with a modified version of
111
+ the Linked Version to produce a modified Combined Work, in the
112
+ manner specified by section 6 of the GNU GPL for conveying
113
+ Corresponding Source.
114
+ - **1)** Use a suitable shared library mechanism for linking with the
115
+ Library. A suitable mechanism is one that **(a)** uses at run time
116
+ a copy of the Library already present on the user's computer
117
+ system, and **(b)** will operate properly with a modified version
118
+ of the Library that is interface-compatible with the Linked
119
+ Version.
120
+
121
+ * **e)** Provide Installation Information, but only if you would otherwise
122
+ be required to provide such information under section 6 of the
123
+ GNU GPL, and only to the extent that such information is
124
+ necessary to install and execute a modified version of the
125
+ Combined Work produced by recombining or relinking the
126
+ Application with a modified version of the Linked Version. (If
127
+ you use option **4d0**, the Installation Information must accompany
128
+ the Minimal Corresponding Source and Corresponding Application
129
+ Code. If you use option **4d1**, you must provide the Installation
130
+ Information in the manner specified by section 6 of the GNU GPL
131
+ for conveying Corresponding Source.)
132
+
133
+ ### 5. Combined Libraries
134
+
135
+ You may place library facilities that are a work based on the
136
+ Library side by side in a single library together with other library
137
+ facilities that are not Applications and are not covered by this
138
+ License, and convey such a combined library under terms of your
139
+ choice, if you do both of the following:
140
+
141
+ * **a)** Accompany the combined library with a copy of the same work based
142
+ on the Library, uncombined with any other library facilities,
143
+ conveyed under the terms of this License.
144
+ * **b)** Give prominent notice with the combined library that part of it
145
+ is a work based on the Library, and explaining where to find the
146
+ accompanying uncombined form of the same work.
147
+
148
+ ### 6. Revised Versions of the GNU Lesser General Public License
149
+
150
+ The Free Software Foundation may publish revised and/or new versions
151
+ of the GNU Lesser General Public License from time to time. Such new
152
+ versions will be similar in spirit to the present version, but may
153
+ differ in detail to address new problems or concerns.
154
+
155
+ Each version is given a distinguishing version number. If the
156
+ Library as you received it specifies that a certain numbered version
157
+ of the GNU Lesser General Public License “or any later version”
158
+ applies to it, you have the option of following the terms and
159
+ conditions either of that published version or of any later version
160
+ published by the Free Software Foundation. If the Library as you
161
+ received it does not specify a version number of the GNU Lesser
162
+ General Public License, you may choose any version of the GNU Lesser
163
+ General Public License ever published by the Free Software Foundation.
164
+
165
+ If the Library as you received it specifies that a proxy can decide
166
+ whether future versions of the GNU Lesser General Public License shall
167
+ apply, that proxy's public statement of acceptance of any version is
168
+ permanent authorization for you to choose that version for the
169
+ Library.
data/README.md CHANGED
@@ -4,13 +4,13 @@ Ur: Unified Request/Response Representation in Ruby
4
4
 
5
5
  ## Properties
6
6
 
7
- An ur primarily consists of a request, a response, and additional processing information.
7
+ An ur primarily consists of a request, a response, and additional metadata.
8
8
 
9
9
  The request consists of the request method, uri, headers, and body.
10
10
 
11
11
  The response consists of the response status, headers, and body.
12
12
 
13
- The processing information consists of the time the request began, the duration of the request, or tag strings. This is optional.
13
+ The metadata consist of the time the request began, the duration of the request, or tag strings. This is optional.
14
14
 
15
15
  Other attributes may be present, and are ignored by this library.
16
16
 
@@ -30,7 +30,7 @@ class MyRackMiddleware
30
30
  ur = Ur.from_rack_request(env)
31
31
 
32
32
  # set additional properties of the ur, for example:
33
- ur.logger = my_logger
33
+ ur.logger_tags(my_logger)
34
34
 
35
35
  rack_response = ur.with_rack_response(@app, env) do
36
36
  # do things after the response
@@ -50,7 +50,7 @@ class MyFaradayMiddleware < ::Faraday::Middleware
50
50
  ur = Ur.from_faraday_request(request_env)
51
51
 
52
52
  # set additional properties of the ur, for example:
53
- ur.logger = my_logger
53
+ ur.logger_tags(my_logger)
54
54
 
55
55
  ur.faraday_on_complete(@app, request_env) do |response_env|
56
56
  # do things after the response
@@ -61,4 +61,6 @@ end
61
61
 
62
62
  ## License
63
63
 
64
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
64
+ [<img align="right" src="https://github.com/notEthan/ur/raw/master/resources/icons/LGPL-3.0.png">](https://www.gnu.org/licenses/lgpl-3.0.html)
65
+
66
+ Ur is Open Source Software licensed under the terms of the [GNU Lesser General Public License version 3](https://www.gnu.org/licenses/lgpl-3.0.html).
@@ -0,0 +1,269 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ur' unless Object.const_defined?(:Ur)
4
+
5
+ module Ur
6
+ # Ur::ContentType represents a Content-Type header field.
7
+ # it parses the media type and its components, as well as any parameters.
8
+ #
9
+ # this class aims to be permissive in what it will parse. it will not raise any
10
+ # error when given a malformed or syntactically invalid Content-Type string.
11
+ # fields and parameters parsed from invalid Content-Type strings are undefined,
12
+ # but this class generally tries to make the most sense of what it's given.
13
+ #
14
+ # this class is based on RFCs:
15
+ # - Hypertext Transfer Protocol (HTTP/1.1): Semantics and Content
16
+ # Section 3.1.1.1. Media Type
17
+ # https://tools.ietf.org/html/rfc7231#section-3.1.1.1
18
+ # - Media Type Specifications and Registration Procedures https://tools.ietf.org/html/rfc6838
19
+ # - Multipurpose Internet Mail Extensions (MIME) Part One: Format of Internet Message Bodies.
20
+ # Section 5.1. Syntax of the Content-Type Header Field
21
+ # https://tools.ietf.org/html/rfc2045#section-5.1
22
+ # - Multipurpose Internet Mail Extensions (MIME) Part Two: Media Types
23
+ # https://tools.ietf.org/html/rfc2046
24
+ # - Additional Media Type Structured Syntax Suffixes
25
+ # https://tools.ietf.org/html/rfc6839
26
+ class ContentType < String
27
+ # the character ranges in this SHOULD be significantly more restrictive,
28
+ # and the `/<subtype>` construct should not be optional. however, we'll aim
29
+ # to match whatever media type we are given.
30
+ #
31
+ # example:
32
+ #
33
+ # MEDIA_TYPE_REGEXP.match('application/vnd.github+json').named_captures
34
+ # =>
35
+ # {
36
+ # "media_type" => "application/vnd.github+json",
37
+ # "type" => "application",
38
+ # "subtype" => "vnd.github+json",
39
+ # "facet" => "vnd",
40
+ # "suffix" => "json",
41
+ # }
42
+ #
43
+ # example of being more permissive than the spec allows:
44
+ #
45
+ # MEDIA_TYPE_REGEXP.match('where the %$*! am I').named_captures
46
+ # =>
47
+ # {
48
+ # "media_type" => "where the %$*! am I",
49
+ # "type" => "where the %$*! am I",
50
+ # "subtype" => nil,
51
+ # "facet" => nil,
52
+ # "suffix" => nil
53
+ # }
54
+ MEDIA_TYPE_REGEXP = %r{
55
+ (?<media_type> # the media type includes the type and subtype
56
+ (?<type>[^\/;\"]*) # the type precedes the first slash
57
+ (?:\/ # slash
58
+ (?<subtype> # the subtype includes the facet, the suffix, and bits in between
59
+ (?:
60
+ (?<facet>[^.+;\"]*) # the facet name comes before the first . in the subtype
61
+ \. # dot
62
+ )?
63
+ [^\+;\"]* # anything between facet and suffix
64
+ (?:\+ # plus
65
+ (?<suffix>[^;\"]*) # optional suffix
66
+ )?
67
+ )
68
+ )? # the subtype should not be optional, but we will match a type without subtype anyway
69
+ )
70
+ }x
71
+
72
+ def initialize(*a)
73
+ super
74
+
75
+ scanner = StringScanner.new(self)
76
+
77
+ if scanner.scan(MEDIA_TYPE_REGEXP)
78
+ @media_type = scanner[:media_type].strip.freeze if scanner[:media_type]
79
+ @type = scanner[:type].strip.freeze if scanner[:type]
80
+ @subtype = scanner[:subtype].strip.freeze if scanner[:subtype]
81
+ @facet = scanner[:facet].strip.freeze if scanner[:facet]
82
+ @suffix = scanner[:suffix].strip.freeze if scanner[:suffix]
83
+ end
84
+
85
+ @parameters = Hash.new do |h, k|
86
+ if k.respond_to?(:downcase) && k != k.downcase
87
+ h[k.downcase]
88
+ else
89
+ nil
90
+ end
91
+ end
92
+
93
+ while scanner.scan(/(;\s*)+/)
94
+ key = scanner.scan(/[^;=\"]*/)
95
+ if key && scanner.scan(/=/)
96
+ value = String.new
97
+ until scanner.eos? || scanner.check(/;/)
98
+ if scanner.scan(/\s+/)
99
+ ws = scanner[0]
100
+ # discard trailing whitespace.
101
+ # other whitespace isn't technically valid but we are permissive so we put it in the value.
102
+ value << ws unless scanner.eos? || scanner.check(/;/)
103
+ elsif scanner.scan(/"/)
104
+ until scanner.eos? || scanner.scan(/"/)
105
+ if scanner.scan(/\\/)
106
+ value << scanner.getch unless scanner.eos?
107
+ end
108
+ value << scanner.scan(/[^\"\\]*/)
109
+ end
110
+ else
111
+ value << scanner.scan(/[^\s;\"]*/)
112
+ end
113
+ end
114
+ @parameters[key.downcase.freeze] = value.freeze
115
+ end
116
+ end
117
+
118
+ @parameters.freeze
119
+
120
+ freeze
121
+ end
122
+
123
+ # the media type of this content type.
124
+ # e.g. `"application/vnd.github+json"` in `content-type: application/vnd.github+json; charset="utf-8"`
125
+ # @return [String, nil]
126
+ attr_reader :media_type
127
+
128
+ # the 'type' portion of our media type.
129
+ # e.g. `"application"` in `content-type: application/vnd.github+json; charset="utf-8"`
130
+ # @return [String, nil]
131
+ attr_reader :type
132
+
133
+ # the 'subtype' portion of our media type.
134
+ # e.g. `"vnd.github+json"` in `content-type: application/vnd.github+json; charset="utf-8"`
135
+ # @return [String, nil]
136
+ attr_reader :subtype
137
+
138
+ # the 'facet' portion of our media type.
139
+ # e.g. `"vnd"` in `content-type: application/vnd.github+json; charset="utf-8"`
140
+ # @return [String, nil]
141
+ attr_reader :facet
142
+
143
+ # the 'suffix' portion of our media type.
144
+ # e.g. `"json"` in `content-type: application/vnd.github+json; charset="utf-8"`
145
+ # @return [String, nil]
146
+ attr_reader :suffix
147
+
148
+ # parameters of this content type.
149
+ # e.g. `{"charset" => "utf-8"}` in `content-type: application/vnd.github+json; charset="utf-8"`
150
+ # @return [Hash<String, String>]
151
+ attr_reader :parameters
152
+
153
+ # is the 'type' portion of our media type equal (case-insensitive) to the given other_type
154
+ # @param other_type
155
+ # @return [Boolean]
156
+ def type?(other_type)
157
+ type && type.casecmp?(other_type)
158
+ end
159
+
160
+ # is the 'subtype' portion of our media type equal (case-insensitive) to the given other_subtype
161
+ # @param other_subtype
162
+ # @return [Boolean]
163
+ def subtype?(other_subtype)
164
+ subtype && subtype.casecmp?(other_subtype)
165
+ end
166
+
167
+ # is the 'suffix' portion of our media type equal (case-insensitive) to the given other_suffix
168
+ # @param other_suffix
169
+ # @return [Boolean]
170
+ def suffix?(other_suffix)
171
+ suffix && suffix.casecmp?(other_suffix)
172
+ end
173
+
174
+ SOME_TEXT_SUBTYPES = %w(
175
+ x-www-form-urlencoded
176
+ json
177
+ json-seq
178
+ jwt
179
+ jose
180
+ yaml
181
+ x-yaml
182
+ xml
183
+ html
184
+ css
185
+ javascript
186
+ ecmascript
187
+ ).map(&:freeze).freeze
188
+
189
+ # does this content type appear to be binary?
190
+ # this library makes its best guess based on a very incomplete knowledge
191
+ # of which media types indicate binary or text.
192
+ # @param unknown [Boolean] return this value when we have no idea whether
193
+ # our media type is binary or text.
194
+ # @return [Boolean]
195
+ def binary?(unknown: true)
196
+ return false if type_text?
197
+
198
+ SOME_TEXT_SUBTYPES.each do |cmpsubtype|
199
+ return false if (suffix ? suffix.casecmp?(cmpsubtype) : subtype ? subtype.casecmp?(cmpsubtype) : false)
200
+ end
201
+
202
+ # these are generally binary
203
+ return true if type_image? || type_audio? || type_video?
204
+
205
+ # we're out of ideas
206
+ return unknown
207
+ end
208
+
209
+ # is this a JSON content type?
210
+ # @return [Boolean]
211
+ def json?
212
+ suffix ? suffix.casecmp?('json') : subtype ? subtype.casecmp?('json') : false
213
+ end
214
+
215
+ # is this an XML content type?
216
+ # @return [Boolean]
217
+ def xml?
218
+ suffix ? suffix.casecmp?('xml'): subtype ? subtype.casecmp?('xml') : false
219
+ end
220
+
221
+ # is this a `x-www-form-urlencoded` content type?
222
+ # @return [Boolean]
223
+ def form_urlencoded?
224
+ suffix ? suffix.casecmp?('x-www-form-urlencoded'): subtype ? subtype.casecmp?('x-www-form-urlencoded') : false
225
+ end
226
+
227
+ # is the 'type' portion of our media type 'text'
228
+ # @return [Boolean]
229
+ def type_text?
230
+ type && type.casecmp?('text')
231
+ end
232
+
233
+ # is the 'type' portion of our media type 'image'
234
+ # @return [Boolean]
235
+ def type_image?
236
+ type && type.casecmp?('image')
237
+ end
238
+
239
+ # is the 'type' portion of our media type 'audio'
240
+ # @return [Boolean]
241
+ def type_audio?
242
+ type && type.casecmp?('audio')
243
+ end
244
+
245
+ # is the 'type' portion of our media type 'video'
246
+ # @return [Boolean]
247
+ def type_video?
248
+ type && type.casecmp?('video')
249
+ end
250
+
251
+ # is the 'type' portion of our media type 'application'
252
+ # @return [Boolean]
253
+ def type_application?
254
+ type && type.casecmp?('application')
255
+ end
256
+
257
+ # is the 'type' portion of our media type 'message'
258
+ # @return [Boolean]
259
+ def type_message?
260
+ type && type.casecmp?('message')
261
+ end
262
+
263
+ # is the 'type' portion of our media type 'multipart'
264
+ # @return [Boolean]
265
+ def type_multipart?
266
+ type && type.casecmp?('multipart')
267
+ end
268
+ end
269
+ end
@@ -1,20 +1,12 @@
1
- class Ur
1
+ # frozen_string_literal: true
2
+
3
+ module Ur
2
4
  module Faraday
3
- class YieldUr < ::Faraday::Middleware
4
- def initialize(app, options = {}, &block)
5
+ # Faraday middleware which yields an Ur to the given block
6
+ class YieldUr < ::Ur::FaradayMiddleware
7
+ def initialize(app, **options, &block)
5
8
  raise(ArgumentError, "no block given to yield ur") unless block
6
- raise(TypeError, "options must be a Hash") unless options.respond_to?(:to_hash)
7
- @app = app
8
- @options = options
9
- @yield_to = block
10
- end
11
-
12
- def call(request_env)
13
- ur = (@options[:ur_class] || Ur).from_faraday_request(request_env)
14
- ur.logger = @options[:logger] if @options[:logger]
15
- ur.faraday_on_complete(@app, request_env) do |response_env|
16
- @yield_to.call(ur)
17
- end
9
+ super(app, options.merge(after_response: block))
18
10
  end
19
11
  end
20
12
  end
data/lib/ur/faraday.rb CHANGED
@@ -1,6 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'faraday'
2
4
 
3
- class Ur
5
+ module Ur
4
6
  module Faraday
5
7
  autoload :YieldUr, 'ur/faraday/yield_ur'
6
8
  end
@@ -1,7 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'ur' unless Object.const_defined?(:Ur)
2
4
 
3
- class Ur
4
- class Processing
5
+ module Ur
6
+ module Metadata
5
7
  include SubUr
6
8
 
7
9
  def began_at
@@ -13,10 +15,13 @@ class Ur
13
15
 
14
16
  attr_accessor :began_at_ns
15
17
 
18
+ # sets began_at from the current time
16
19
  def begin!
17
- self.began_at ||= Time.now
18
- self.began_at_ns ||= Process.clock_gettime(Process::CLOCK_MONOTONIC, :nanosecond)
20
+ self.began_at = Time.now
21
+ self.began_at_ns = Process.clock_gettime(Process::CLOCK_MONOTONIC, :nanosecond)
19
22
  end
23
+
24
+ # sets the duration from the current time and began_at
20
25
  def finish!
21
26
  return if duration
22
27
  if began_at_ns
data/lib/ur/middleware.rb CHANGED
@@ -1,6 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'ur' unless Object.const_defined?(:Ur)
2
4
 
3
- class Ur
5
+ module Ur
4
6
  module Middleware
5
7
  def initialize(app, options = {})
6
8
  @app = app
@@ -9,6 +11,15 @@ class Ur
9
11
  attr_reader :app
10
12
  attr_reader :options
11
13
 
14
+ def begin_request(ur)
15
+ ur.metadata.begin!
16
+ end
17
+
18
+ def finish_request(ur)
19
+ ur.logger_tags(@options[:logger])
20
+ ur.metadata.finish!
21
+ end
22
+
12
23
  def invoke_callback(name, *a, &b)
13
24
  if @options[name]
14
25
  @options[name].call(*a, &b)
@@ -19,10 +30,11 @@ class Ur
19
30
  class FaradayMiddleware < ::Faraday::Middleware
20
31
  include Ur::Middleware
21
32
  def call(request_env)
22
- ur = Ur.from_faraday_request(request_env)
33
+ ur = Ur.from_faraday_request(request_env, @options.select { |k, _| [:schemas].include?(k) })
23
34
  invoke_callback(:before_request, ur)
24
- ur.logger = options[:logger] if options[:logger]
35
+ begin_request(ur)
25
36
  ur.faraday_on_complete(@app, request_env) do |response_env|
37
+ finish_request(ur)
26
38
  invoke_callback(:after_response, ur)
27
39
  end
28
40
  end
@@ -31,10 +43,11 @@ class Ur
31
43
  class RackMiddleware
32
44
  include Ur::Middleware
33
45
  def call(env)
34
- ur = Ur.from_rack_request(env)
46
+ ur = Ur.from_rack_request(env, @options.select { |k, _| [:schemas].include?(k) })
35
47
  invoke_callback(:before_request, ur)
36
- ur.logger = options[:logger] if options[:logger]
48
+ begin_request(ur)
37
49
  ur.with_rack_response(@app, env) do
50
+ finish_request(ur)
38
51
  invoke_callback(:after_response, ur)
39
52
  end
40
53
  end
data/lib/ur/request.rb CHANGED
@@ -1,7 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'ur' unless Object.const_defined?(:Ur)
2
4
 
3
- class Ur
4
- class Request
5
+ module Ur
6
+ module Request
5
7
  include RequestAndResponse
6
8
  include SubUr
7
9