ur 0.0.2 → 0.2.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.
@@ -11,9 +11,20 @@ require 'minitest/reporters'
11
11
  Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new
12
12
 
13
13
  class UrSpec < Minitest::Spec
14
+ if ENV['UR_TEST_ALPHA']
15
+ # :nocov:
16
+ define_singleton_method(:test_order) { :alpha }
17
+ # :nocov:
18
+ end
19
+
14
20
  def assert_json_equal(exp, act, *a)
15
21
  assert_equal(JSI::Typelike.as_json(exp), JSI::Typelike.as_json(act), *a)
16
22
  end
23
+
24
+ def assert_equal exp, act, msg = nil
25
+ msg = message(msg, E) { diff exp, act }
26
+ assert exp == act, msg
27
+ end
17
28
  end
18
29
 
19
30
  # register this to be the base class for specs instead of Minitest::Spec
@@ -9,11 +9,11 @@ describe 'Ur faraday integration' do
9
9
  builder.use(Ur::FaradayMiddleware,
10
10
  after_response: -> (ur_) { ur = ur_ },
11
11
  )
12
- builder.use(Faraday::Adapter::Rack, -> (env) { [200, {'Content-Type' => 'text/plain'}, ['ᚒ']] })
12
+ builder.adapter(:rack, -> (env) { [200, {'Content-Type' => 'text/plain'}, ['ᚒ']] })
13
13
  end
14
14
  res = faraday_conn.get('/')
15
15
  assert_equal('ᚒ', res.body)
16
- assert_instance_of(Ur, ur)
16
+ assert_kind_of(Ur, ur)
17
17
  assert_equal('get', ur.request['method'])
18
18
  assert_equal('text/plain', ur.response.headers['Content-Type'])
19
19
  assert_equal('ᚒ', ur.response.body)
@@ -25,11 +25,11 @@ describe 'Ur faraday integration' do
25
25
  builder.use(Ur::FaradayMiddleware,
26
26
  after_response: -> (ur_) { ur = ur_ },
27
27
  )
28
- builder.use(Faraday::Adapter::Rack, -> (env) { [200, {'Content-Type' => 'text/plain'}, ['☺']] })
28
+ builder.adapter(:rack, -> (env) { [200, {'Content-Type' => 'text/plain'}, ['☺']] })
29
29
  end
30
30
  res = faraday_conn.post('/', StringIO.new('hello!'))
31
31
  assert_equal('☺', res.body)
32
- assert_instance_of(Ur, ur)
32
+ assert_kind_of(Ur, ur)
33
33
  assert_equal('post', ur.request['method'])
34
34
  assert_equal('hello!', ur.request.body)
35
35
  assert_equal('text/plain', ur.response.headers['Content-Type'])
@@ -44,11 +44,11 @@ describe 'Ur faraday integration' do
44
44
  after_response: -> (ur_) { ur = ur_ },
45
45
  )
46
46
  builder.response :json, preserve_raw: true
47
- builder.use(Faraday::Adapter::Rack, -> (env) { [200, {'Content-Type' => 'application/json'}, ['{}']] })
47
+ builder.adapter(:rack, -> (env) { [200, {'Content-Type' => 'application/json'}, ['{}']] })
48
48
  end
49
49
  res = faraday_conn.post('/', {'a' => 'b'})
50
50
  assert_equal({}, res.body)
51
- assert_instance_of(Ur, ur)
51
+ assert_kind_of(Ur, ur)
52
52
  assert_equal('post', ur.request['method'])
53
53
  assert_equal('{"a":"b"}', ur.request.body)
54
54
  assert_equal('application/json', ur.response.headers['Content-Type'])
@@ -63,11 +63,11 @@ describe 'Ur faraday integration' do
63
63
  )
64
64
  builder.request :json
65
65
  builder.response :json
66
- builder.use(Faraday::Adapter::Rack, -> (env) { [200, {'Content-Type' => 'application/json'}, ['{}']] })
66
+ builder.adapter(:rack, -> (env) { [200, {'Content-Type' => 'application/json'}, ['{}']] })
67
67
  end
68
68
  res = faraday_conn.post('/', {'a' => 'b'})
69
69
  assert_equal({}, res.body)
70
- assert_instance_of(Ur, ur)
70
+ assert_kind_of(Ur, ur)
71
71
  assert_equal('post', ur.request['method'])
72
72
  assert_nil(ur.request.body) # no good
73
73
  assert_json_equal({"a" => "b"}, ur.request['body_parsed']) # best we get here
@@ -0,0 +1,11 @@
1
+ require_relative 'test_helper'
2
+
3
+ describe 'Ur metadata' do
4
+ it 'sets duration from began_at' do
5
+ ur = Ur.new
6
+ ur.metadata.began_at = Time.now
7
+ ur.metadata.finish!
8
+ assert_instance_of(Float, ur.metadata.duration)
9
+ assert_operator(ur.metadata.duration, :>, 0)
10
+ end
11
+ end
@@ -10,8 +10,6 @@ describe 'Ur rack integration' do
10
10
  assert_equal('bar', ur.request.headers['foo'])
11
11
  assert_equal('https://ur.unth.net/', ur.request.uri)
12
12
  assert(ur.response.empty?)
13
- assert_instance_of(Time, ur.processing.began_at)
14
- assert_nil(ur.processing.duration)
15
13
  assert(ur.validate)
16
14
  end
17
15
  it 'builds from a rack request' do
@@ -22,8 +20,6 @@ describe 'Ur rack integration' do
22
20
  assert_equal('bar', ur.request.headers['foo'])
23
21
  assert_equal('https://ur.unth.net/', ur.request.uri)
24
22
  assert(ur.response.empty?)
25
- assert_instance_of(Time, ur.processing.began_at)
26
- assert_nil(ur.processing.duration)
27
23
  assert(ur.validate)
28
24
  end
29
25
  end
@@ -8,7 +8,7 @@ describe 'Ur' do
8
8
  end
9
9
 
10
10
  it 'initializes' do
11
- Ur.new({})
11
+ Ur.new_jsi({})
12
12
  end
13
13
 
14
14
  it 'would prefer not to initialize' do
@@ -37,8 +37,8 @@ describe 'Ur' do
37
37
  assert_equal('bar', ur.request.headers['foo'])
38
38
  assert_equal('https://ur.unth.net/', ur.request.uri)
39
39
  assert(ur.response.empty?)
40
- assert_instance_of(Time, ur.processing.began_at)
41
- assert_nil(ur.processing.duration)
40
+ assert_nil(ur.metadata.began_at)
41
+ assert_nil(ur.metadata.duration)
42
42
  assert(ur.validate)
43
43
  end,
44
44
  after_response: -> (ur) do
@@ -51,10 +51,10 @@ describe 'Ur' do
51
51
  assert_equal(200, ur.response.status)
52
52
  assert_equal('text/plain', ur.response.headers['Content-Type'])
53
53
  assert_equal('ᚒ', ur.response.body)
54
- assert_instance_of(Time, ur.processing.began_at)
55
- assert_instance_of(Float, ur.processing.duration)
56
- assert_operator(ur.processing.duration, :>, 0)
57
- assert_equal(['ur_test_rack'], ur.processing.tags.to_a)
54
+ assert_instance_of(Time, ur.metadata.began_at)
55
+ assert_instance_of(Float, ur.metadata.duration)
56
+ assert_operator(ur.metadata.duration, :>, 0)
57
+ assert_equal(['ur_test_rack'], ur.metadata.tags.to_a)
58
58
  assert(ur.validate)
59
59
  end,
60
60
  )
@@ -71,8 +71,8 @@ describe 'Ur' do
71
71
  assert_equal('https://ur.unth.net/', ur.request.uri)
72
72
  assert_equal(Addressable::URI.parse('https://ur.unth.net/'), ur.request.addressable_uri)
73
73
  assert(ur.response.empty?)
74
- assert_instance_of(Time, ur.processing.began_at)
75
- assert_nil(ur.processing.duration)
74
+ assert_nil(ur.metadata.began_at)
75
+ assert_nil(ur.metadata.duration)
76
76
  assert(ur.validate)
77
77
  end,
78
78
  after_response: -> (ur) do
@@ -85,14 +85,14 @@ describe 'Ur' do
85
85
  assert_equal(200, ur.response.status)
86
86
  assert_equal('text/plain', ur.response.headers['Content-Type'])
87
87
  assert_equal('ᚒ', ur.response.body)
88
- assert_instance_of(Time, ur.processing.began_at)
89
- assert_instance_of(Float, ur.processing.duration)
90
- assert_operator(ur.processing.duration, :>, 0)
91
- assert_equal(['ur_test_faraday'], ur.processing.tags.to_a)
88
+ assert_instance_of(Time, ur.metadata.began_at)
89
+ assert_instance_of(Float, ur.metadata.duration)
90
+ assert_operator(ur.metadata.duration, :>, 0)
91
+ assert_equal(['ur_test_faraday'], ur.metadata.tags.to_a)
92
92
  assert(ur.validate)
93
93
  end,
94
94
  )
95
- builder.use(Faraday::Adapter::Rack, rack_app)
95
+ builder.adapter(:rack, rack_app)
96
96
  end
97
97
  res = faraday_conn.get('/', nil, {'Foo' => 'bar'})
98
98
  assert(called_rack_before_request)
data/ur.gemspec CHANGED
@@ -1,4 +1,3 @@
1
-
2
1
  lib = File.expand_path("../lib", __FILE__)
3
2
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
3
  require "ur/version"
@@ -12,32 +11,23 @@ Gem::Specification.new do |spec|
12
11
  spec.summary = 'ur: unified request representation'
13
12
  spec.description = 'ur provides a unified representation of a request and response. it can be interpreted from rack, faraday, or potentially other sources, and provides a consistent interface to access the attributes inherent to the request and additional useful parsers and computation from the request.'
14
13
  spec.homepage = "https://github.com/notEthan/ur"
15
- spec.license = "MIT"
14
+ spec.license = "LGPL-3.0"
16
15
 
17
- ignore_files = %w(.gitignore .travis.yml Gemfile test)
16
+ ignore_files = %w(.gitignore .travis.yml Gemfile test)
18
17
  ignore_files_re = %r{\A(#{ignore_files.map { |f| Regexp.escape(f) }.join('|')})(/|\z)}
19
- # Specify which files should be added to the gem when it is released.
20
- # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
- Dir.chdir(File.expand_path('..', __FILE__)) do
22
- spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(ignore_files_re) }
23
- spec.test_files = `git ls-files -z test`.split("\x0") + [
24
- '.simplecov',
25
- ]
26
- end
27
- spec.bindir = "exe"
28
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
18
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(ignore_files_re) }
19
+ spec.test_files = `git ls-files -z test`.split("\x0") + ['.simplecov']
29
20
  spec.require_paths = ["lib"]
30
21
 
31
- spec.add_dependency "jsi", "~> 0.0.3"
22
+ spec.add_dependency "jsi", "~> 0.4"
32
23
  spec.add_dependency "addressable", "~> 2.0"
33
24
  spec.add_development_dependency "rack"
34
25
  spec.add_development_dependency "rack-test"
35
26
  spec.add_development_dependency "faraday"
36
27
  spec.add_development_dependency "faraday_middleware"
37
28
  spec.add_development_dependency "activesupport"
38
- spec.add_development_dependency "bundler", "~> 1.16"
39
- spec.add_development_dependency "rake", "~> 10.0"
40
- spec.add_development_dependency "minitest", "~> 5.0"
29
+ spec.add_development_dependency "rake"
30
+ spec.add_development_dependency "minitest"
41
31
  spec.add_development_dependency "minitest-reporters"
42
32
  spec.add_development_dependency "simplecov"
43
33
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ur
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ethan
8
8
  autorequire:
9
- bindir: exe
9
+ bindir: bin
10
10
  cert_chain: []
11
- date: 2019-01-24 00:00:00.000000000 Z
11
+ date: 2020-07-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jsi
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 0.0.3
19
+ version: '0.4'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 0.0.3
26
+ version: '0.4'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: addressable
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -108,48 +108,34 @@ dependencies:
108
108
  - - ">="
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0'
111
- - !ruby/object:Gem::Dependency
112
- name: bundler
113
- requirement: !ruby/object:Gem::Requirement
114
- requirements:
115
- - - "~>"
116
- - !ruby/object:Gem::Version
117
- version: '1.16'
118
- type: :development
119
- prerelease: false
120
- version_requirements: !ruby/object:Gem::Requirement
121
- requirements:
122
- - - "~>"
123
- - !ruby/object:Gem::Version
124
- version: '1.16'
125
111
  - !ruby/object:Gem::Dependency
126
112
  name: rake
127
113
  requirement: !ruby/object:Gem::Requirement
128
114
  requirements:
129
- - - "~>"
115
+ - - ">="
130
116
  - !ruby/object:Gem::Version
131
- version: '10.0'
117
+ version: '0'
132
118
  type: :development
133
119
  prerelease: false
134
120
  version_requirements: !ruby/object:Gem::Requirement
135
121
  requirements:
136
- - - "~>"
122
+ - - ">="
137
123
  - !ruby/object:Gem::Version
138
- version: '10.0'
124
+ version: '0'
139
125
  - !ruby/object:Gem::Dependency
140
126
  name: minitest
141
127
  requirement: !ruby/object:Gem::Requirement
142
128
  requirements:
143
- - - "~>"
129
+ - - ">="
144
130
  - !ruby/object:Gem::Version
145
- version: '5.0'
131
+ version: '0'
146
132
  type: :development
147
133
  prerelease: false
148
134
  version_requirements: !ruby/object:Gem::Requirement
149
135
  requirements:
150
- - - "~>"
136
+ - - ">="
151
137
  - !ruby/object:Gem::Version
152
- version: '5.0'
138
+ version: '0'
153
139
  - !ruby/object:Gem::Dependency
154
140
  name: minitest-reporters
155
141
  requirement: !ruby/object:Gem::Requirement
@@ -191,29 +177,32 @@ files:
191
177
  - ".simplecov"
192
178
  - ".yardopts"
193
179
  - CHANGELOG.md
194
- - LICENSE.txt
180
+ - LICENSE.md
195
181
  - README.md
196
182
  - Rakefile.rb
197
183
  - lib/ur.rb
198
- - lib/ur/content_type_attrs.rb
184
+ - lib/ur/content_type.rb
199
185
  - lib/ur/faraday.rb
200
186
  - lib/ur/faraday/yield_ur.rb
187
+ - lib/ur/metadata.rb
201
188
  - lib/ur/middleware.rb
202
- - lib/ur/processing.rb
203
189
  - lib/ur/request.rb
204
190
  - lib/ur/request_and_response.rb
205
191
  - lib/ur/response.rb
206
192
  - lib/ur/sub_ur.rb
207
193
  - lib/ur/version.rb
194
+ - resources/icons/LGPL-3.0.png
195
+ - resources/ur.schema.yml
196
+ - test/content_type_test.rb
208
197
  - test/test_helper.rb
209
198
  - test/ur_faraday_test.rb
210
- - test/ur_processing_test.rb
199
+ - test/ur_metadata_test.rb
211
200
  - test/ur_rack_test.rb
212
201
  - test/ur_test.rb
213
202
  - ur.gemspec
214
203
  homepage: https://github.com/notEthan/ur
215
204
  licenses:
216
- - MIT
205
+ - LGPL-3.0
217
206
  metadata: {}
218
207
  post_install_message:
219
208
  rdoc_options: []
@@ -230,15 +219,15 @@ required_rubygems_version: !ruby/object:Gem::Requirement
230
219
  - !ruby/object:Gem::Version
231
220
  version: '0'
232
221
  requirements: []
233
- rubyforge_project:
234
- rubygems_version: 2.7.7
222
+ rubygems_version: 3.0.6
235
223
  signing_key:
236
224
  specification_version: 4
237
225
  summary: 'ur: unified request representation'
238
226
  test_files:
227
+ - test/content_type_test.rb
239
228
  - test/test_helper.rb
240
229
  - test/ur_faraday_test.rb
241
- - test/ur_processing_test.rb
230
+ - test/ur_metadata_test.rb
242
231
  - test/ur_rack_test.rb
243
232
  - test/ur_test.rb
244
233
  - ".simplecov"
@@ -1,21 +0,0 @@
1
- The MIT License (MIT)
2
-
3
- Copyright (c) 2018 Ethan
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in
13
- all copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
- THE SOFTWARE.
@@ -1,83 +0,0 @@
1
- require 'ur' unless Object.const_defined?(:Ur)
2
-
3
- class Ur
4
- # parses attributes out of content type header
5
- class ContentTypeAttrs
6
- def initialize(content_type)
7
- raise(ArgumentError) unless content_type.nil? || content_type.respond_to?(:to_str)
8
- content_type = content_type.to_str if content_type
9
- @media_type = (content_type.split(/\s*[;]\s*/, 2).first if content_type)
10
- @media_type.strip! if @media_type
11
- @content_type = content_type
12
- @parsed = false
13
- @attributes = Hash.new { |h,k| h[k] = [] }
14
- catch(:unparseable) do
15
- throw(:unparseable) unless content_type
16
- uri_parser = URI.const_defined?(:Parser) ? URI::Parser.new : URI
17
- scanner = StringScanner.new(content_type)
18
- scanner.scan(/.*;\s*/) || throw(:unparseable)
19
- while scanner.scan(/(\w+)=("?)([^"]*)("?)\s*(,?)\s*/)
20
- key = scanner[1]
21
- quote1 = scanner[2]
22
- value = scanner[3]
23
- quote2 = scanner[4]
24
- comma_follows = !scanner[5].empty?
25
- throw(:unparseable) unless quote1 == quote2
26
- throw(:unparseable) if !comma_follows && !scanner.eos?
27
- @attributes[uri_parser.unescape(key)] << uri_parser.unescape(value)
28
- end
29
- throw(:unparseable) unless scanner.eos?
30
- @parsed = true
31
- end
32
- end
33
-
34
- attr_reader :content_type, :media_type
35
-
36
- def parsed?
37
- @parsed
38
- end
39
-
40
- def [](key)
41
- @attributes[key]
42
- end
43
-
44
- def text?
45
- # ordered hash by priority mapping types to binary or text
46
- # regexps will have \A and \z added
47
- types = {
48
- %r(image/.*) => :binary,
49
- %r(audio/.*) => :binary,
50
- %r(video/.*) => :binary,
51
- %r(model/.*) => :binary,
52
- %r(text/.*) => :text,
53
- %r(message/.*) => :text,
54
- %r(application/(.+\+)?json) => :text,
55
- %r(application/(.+\+)?xml) => :text,
56
- %r(model/(.+\+)?xml) => :text,
57
- 'application/x-www-form-urlencoded' => :text,
58
- 'application/javascript' => :text,
59
- 'application/ecmascript' => :text,
60
- 'application/octet-stream' => :binary,
61
- 'application/ogg' => :binary,
62
- 'application/pdf' => :binary,
63
- 'application/postscript' => :binary,
64
- 'application/zip' => :binary,
65
- 'application/gzip' => :binary,
66
- 'application/vnd.apple.pkpass' => :binary,
67
- }
68
- types.each do |match, type|
69
- matched = match.is_a?(Regexp) ? media_type =~ %r(\A#{match.source}\z) : media_type == match
70
- if matched
71
- return type == :text
72
- end
73
- end
74
- # fallback (unknown or not given) assume that unknown content types are binary but omitted
75
- # content-type means text
76
- if content_type && content_type =~ /\S/
77
- return false
78
- else
79
- return true
80
- end
81
- end
82
- end
83
- end