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 +4 -4
- data/CHANGELOG.md +15 -0
- data/LICENSE.md +169 -0
- data/README.md +7 -5
- data/lib/ur/content_type.rb +269 -0
- data/lib/ur/faraday/yield_ur.rb +7 -15
- data/lib/ur/faraday.rb +3 -1
- data/lib/ur/{processing.rb → metadata.rb} +9 -4
- data/lib/ur/middleware.rb +18 -5
- data/lib/ur/request.rb +4 -2
- data/lib/ur/request_and_response.rb +31 -8
- data/lib/ur/response.rb +4 -2
- data/lib/ur/sub_ur.rb +4 -2
- data/lib/ur/version.rb +1 -1
- data/lib/ur.rb +34 -64
- data/resources/ur.schema.yml +51 -0
- metadata +13 -154
- data/.simplecov +0 -1
- data/.yardopts +0 -1
- data/LICENSE.txt +0 -21
- data/Rakefile.rb +0 -9
- data/lib/ur/content_type_attrs.rb +0 -83
- data/test/test_helper.rb +0 -22
- data/test/ur_faraday_test.rb +0 -79
- data/test/ur_processing_test.rb +0 -11
- data/test/ur_rack_test.rb +0 -29
- data/test/ur_test.rb +0 -105
- data/ur.gemspec +0 -42
data/test/ur_faraday_test.rb
DELETED
@@ -1,79 +0,0 @@
|
|
1
|
-
require_relative 'test_helper'
|
2
|
-
require 'faraday'
|
3
|
-
require 'faraday_middleware'
|
4
|
-
|
5
|
-
describe 'Ur faraday integration' do
|
6
|
-
it 'integrates, basic usage' do
|
7
|
-
ur = nil
|
8
|
-
faraday_conn = ::Faraday.new('https://ur.unth.net/') do |builder|
|
9
|
-
builder.use(Ur::FaradayMiddleware,
|
10
|
-
after_response: -> (ur_) { ur = ur_ },
|
11
|
-
)
|
12
|
-
builder.use(Faraday::Adapter::Rack, -> (env) { [200, {'Content-Type' => 'text/plain'}, ['ᚒ']] })
|
13
|
-
end
|
14
|
-
res = faraday_conn.get('/')
|
15
|
-
assert_equal('ᚒ', res.body)
|
16
|
-
assert_instance_of(Ur, ur)
|
17
|
-
assert_equal('get', ur.request['method'])
|
18
|
-
assert_equal('text/plain', ur.response.headers['Content-Type'])
|
19
|
-
assert_equal('ᚒ', ur.response.body)
|
20
|
-
assert(ur.validate)
|
21
|
-
end
|
22
|
-
it 'integrates, IO body' do
|
23
|
-
ur = nil
|
24
|
-
faraday_conn = ::Faraday.new('https://ur.unth.net/') do |builder|
|
25
|
-
builder.use(Ur::FaradayMiddleware,
|
26
|
-
after_response: -> (ur_) { ur = ur_ },
|
27
|
-
)
|
28
|
-
builder.use(Faraday::Adapter::Rack, -> (env) { [200, {'Content-Type' => 'text/plain'}, ['☺']] })
|
29
|
-
end
|
30
|
-
res = faraday_conn.post('/', StringIO.new('hello!'))
|
31
|
-
assert_equal('☺', res.body)
|
32
|
-
assert_instance_of(Ur, ur)
|
33
|
-
assert_equal('post', ur.request['method'])
|
34
|
-
assert_equal('hello!', ur.request.body)
|
35
|
-
assert_equal('text/plain', ur.response.headers['Content-Type'])
|
36
|
-
assert_equal('☺', ur.response.body)
|
37
|
-
assert(ur.validate)
|
38
|
-
end
|
39
|
-
it 'integrates, faraday middleware munges the json bodies but uses preserve_raw' do
|
40
|
-
ur = nil
|
41
|
-
faraday_conn = ::Faraday.new('https://ur.unth.net/') do |builder|
|
42
|
-
builder.request :json
|
43
|
-
builder.use(Ur::FaradayMiddleware,
|
44
|
-
after_response: -> (ur_) { ur = ur_ },
|
45
|
-
)
|
46
|
-
builder.response :json, preserve_raw: true
|
47
|
-
builder.use(Faraday::Adapter::Rack, -> (env) { [200, {'Content-Type' => 'application/json'}, ['{}']] })
|
48
|
-
end
|
49
|
-
res = faraday_conn.post('/', {'a' => 'b'})
|
50
|
-
assert_equal({}, res.body)
|
51
|
-
assert_instance_of(Ur, ur)
|
52
|
-
assert_equal('post', ur.request['method'])
|
53
|
-
assert_equal('{"a":"b"}', ur.request.body)
|
54
|
-
assert_equal('application/json', ur.response.headers['Content-Type'])
|
55
|
-
assert_equal('{}', ur.response.body)
|
56
|
-
assert(ur.validate)
|
57
|
-
end
|
58
|
-
it 'integrates, faraday middleware munges the json bodies and does not preserve_raw' do
|
59
|
-
ur = nil
|
60
|
-
faraday_conn = ::Faraday.new('https://ur.unth.net/') do |builder|
|
61
|
-
builder.use(Ur::FaradayMiddleware,
|
62
|
-
after_response: -> (ur_) { ur = ur_ },
|
63
|
-
)
|
64
|
-
builder.request :json
|
65
|
-
builder.response :json
|
66
|
-
builder.use(Faraday::Adapter::Rack, -> (env) { [200, {'Content-Type' => 'application/json'}, ['{}']] })
|
67
|
-
end
|
68
|
-
res = faraday_conn.post('/', {'a' => 'b'})
|
69
|
-
assert_equal({}, res.body)
|
70
|
-
assert_instance_of(Ur, ur)
|
71
|
-
assert_equal('post', ur.request['method'])
|
72
|
-
assert_nil(ur.request.body) # no good
|
73
|
-
assert_json_equal({"a" => "b"}, ur.request['body_parsed']) # best we get here
|
74
|
-
assert_equal('application/json', ur.response.headers['Content-Type'])
|
75
|
-
assert_nil(ur.response.body) # no good
|
76
|
-
assert_json_equal({}, ur.response['body_parsed']) # best we get here
|
77
|
-
assert(ur.validate)
|
78
|
-
end
|
79
|
-
end
|
data/test/ur_processing_test.rb
DELETED
@@ -1,11 +0,0 @@
|
|
1
|
-
require_relative 'test_helper'
|
2
|
-
|
3
|
-
describe 'Ur processing' do
|
4
|
-
it 'sets duration from began_at' do
|
5
|
-
ur = Ur.new
|
6
|
-
ur.processing.began_at = Time.now
|
7
|
-
ur.processing.finish!
|
8
|
-
assert_instance_of(Float, ur.processing.duration)
|
9
|
-
assert_operator(ur.processing.duration, :>, 0)
|
10
|
-
end
|
11
|
-
end
|
data/test/ur_rack_test.rb
DELETED
@@ -1,29 +0,0 @@
|
|
1
|
-
require_relative 'test_helper'
|
2
|
-
require 'rack'
|
3
|
-
|
4
|
-
describe 'Ur rack integration' do
|
5
|
-
it 'builds from a rack env' do
|
6
|
-
env = Rack::MockRequest.env_for('https://ur.unth.net/', {'HTTP_FOO' => 'bar'})
|
7
|
-
ur = Ur.from_rack_request(env)
|
8
|
-
assert_equal('inbound', ur.bound)
|
9
|
-
assert_equal('GET', ur.request['method'])
|
10
|
-
assert_equal('bar', ur.request.headers['foo'])
|
11
|
-
assert_equal('https://ur.unth.net/', ur.request.uri)
|
12
|
-
assert(ur.response.empty?)
|
13
|
-
assert_instance_of(Time, ur.processing.began_at)
|
14
|
-
assert_nil(ur.processing.duration)
|
15
|
-
assert(ur.validate)
|
16
|
-
end
|
17
|
-
it 'builds from a rack request' do
|
18
|
-
env = Rack::Request.new(Rack::MockRequest.env_for('https://ur.unth.net/', {'HTTP_FOO' => 'bar'}))
|
19
|
-
ur = Ur.from_rack_request(env)
|
20
|
-
assert_equal('inbound', ur.bound)
|
21
|
-
assert_equal('GET', ur.request['method'])
|
22
|
-
assert_equal('bar', ur.request.headers['foo'])
|
23
|
-
assert_equal('https://ur.unth.net/', ur.request.uri)
|
24
|
-
assert(ur.response.empty?)
|
25
|
-
assert_instance_of(Time, ur.processing.began_at)
|
26
|
-
assert_nil(ur.processing.duration)
|
27
|
-
assert(ur.validate)
|
28
|
-
end
|
29
|
-
end
|
data/test/ur_test.rb
DELETED
@@ -1,105 +0,0 @@
|
|
1
|
-
require_relative 'test_helper'
|
2
|
-
require 'faraday'
|
3
|
-
require 'active_support/tagged_logging'
|
4
|
-
|
5
|
-
describe 'Ur' do
|
6
|
-
it 'has a valid schema' do
|
7
|
-
Ur.schema.validate_schema!
|
8
|
-
end
|
9
|
-
|
10
|
-
it 'initializes' do
|
11
|
-
Ur.new({})
|
12
|
-
end
|
13
|
-
|
14
|
-
it 'would prefer not to initialize' do
|
15
|
-
assert_raises(TypeError) { Ur.new("hello!") }
|
16
|
-
end
|
17
|
-
|
18
|
-
it 'integrates with rack and faraday middlewares' do
|
19
|
-
rack_app = proc do |env|
|
20
|
-
[200, {'Content-Type' => 'text/plain'}, ['ᚒ']]
|
21
|
-
end
|
22
|
-
client_logger = ActiveSupport::TaggedLogging.new(Logger.new(StringIO.new))
|
23
|
-
server_logger = ActiveSupport::TaggedLogging.new(Logger.new(StringIO.new))
|
24
|
-
called_rack_before_request = false
|
25
|
-
called_rack_after_response = false
|
26
|
-
called_faraday_before_request = false
|
27
|
-
called_faraday_after_response = false
|
28
|
-
rack_app = Ur::RackMiddleware.new(rack_app, logger: server_logger,
|
29
|
-
before_request: -> (ur) do
|
30
|
-
called_rack_before_request = true
|
31
|
-
|
32
|
-
server_logger.push_tags 'ur_test_rack'
|
33
|
-
|
34
|
-
assert_equal('inbound', ur.bound)
|
35
|
-
assert_equal('GET', ur.request['method'])
|
36
|
-
assert_equal('ur.unth.net', ur.request.headers['host'])
|
37
|
-
assert_equal('bar', ur.request.headers['foo'])
|
38
|
-
assert_equal('https://ur.unth.net/', ur.request.uri)
|
39
|
-
assert(ur.response.empty?)
|
40
|
-
assert_instance_of(Time, ur.processing.began_at)
|
41
|
-
assert_nil(ur.processing.duration)
|
42
|
-
assert(ur.validate)
|
43
|
-
end,
|
44
|
-
after_response: -> (ur) do
|
45
|
-
called_rack_after_response = true
|
46
|
-
|
47
|
-
server_logger.pop_tags
|
48
|
-
|
49
|
-
assert_equal('inbound', ur.bound)
|
50
|
-
assert_equal('GET', ur.request['method'])
|
51
|
-
assert_equal(200, ur.response.status)
|
52
|
-
assert_equal('text/plain', ur.response.headers['Content-Type'])
|
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)
|
58
|
-
assert(ur.validate)
|
59
|
-
end,
|
60
|
-
)
|
61
|
-
faraday_conn = ::Faraday.new('https://ur.unth.net/') do |builder|
|
62
|
-
builder.use(Ur::FaradayMiddleware, logger: client_logger,
|
63
|
-
before_request: -> (ur) do
|
64
|
-
called_faraday_before_request = true
|
65
|
-
|
66
|
-
client_logger.push_tags 'ur_test_faraday'
|
67
|
-
|
68
|
-
assert_equal('outbound', ur.bound)
|
69
|
-
assert_equal('get', ur.request['method'])
|
70
|
-
assert_equal('bar', ur.request.headers['foo'])
|
71
|
-
assert_equal('https://ur.unth.net/', ur.request.uri)
|
72
|
-
assert_equal(Addressable::URI.parse('https://ur.unth.net/'), ur.request.addressable_uri)
|
73
|
-
assert(ur.response.empty?)
|
74
|
-
assert_instance_of(Time, ur.processing.began_at)
|
75
|
-
assert_nil(ur.processing.duration)
|
76
|
-
assert(ur.validate)
|
77
|
-
end,
|
78
|
-
after_response: -> (ur) do
|
79
|
-
called_faraday_after_response = true
|
80
|
-
|
81
|
-
client_logger.pop_tags
|
82
|
-
|
83
|
-
assert_equal('outbound', ur.bound)
|
84
|
-
assert_equal('get', ur.request['method'])
|
85
|
-
assert_equal(200, ur.response.status)
|
86
|
-
assert_equal('text/plain', ur.response.headers['Content-Type'])
|
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)
|
92
|
-
assert(ur.validate)
|
93
|
-
end,
|
94
|
-
)
|
95
|
-
builder.use(Faraday::Adapter::Rack, rack_app)
|
96
|
-
end
|
97
|
-
res = faraday_conn.get('/', nil, {'Foo' => 'bar'})
|
98
|
-
assert(called_rack_before_request)
|
99
|
-
assert(called_rack_after_response)
|
100
|
-
assert(called_faraday_before_request)
|
101
|
-
assert(called_faraday_after_response)
|
102
|
-
assert_equal(200, res.status)
|
103
|
-
assert_equal('ᚒ', res.body)
|
104
|
-
end
|
105
|
-
end
|
data/ur.gemspec
DELETED
@@ -1,42 +0,0 @@
|
|
1
|
-
|
2
|
-
lib = File.expand_path("../lib", __FILE__)
|
3
|
-
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require "ur/version"
|
5
|
-
|
6
|
-
Gem::Specification.new do |spec|
|
7
|
-
spec.name = "ur"
|
8
|
-
spec.version = UR_VERSION
|
9
|
-
spec.authors = ["Ethan"]
|
10
|
-
spec.email = ["ethan@unth"]
|
11
|
-
|
12
|
-
spec.summary = 'ur: unified request representation'
|
13
|
-
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
|
-
spec.homepage = "https://github.com/notEthan/ur"
|
15
|
-
spec.license = "MIT"
|
16
|
-
|
17
|
-
ignore_files = %w(.gitignore .travis.yml Gemfile test)
|
18
|
-
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) }
|
29
|
-
spec.require_paths = ["lib"]
|
30
|
-
|
31
|
-
spec.add_dependency "jsi", "~> 0.2.0"
|
32
|
-
spec.add_dependency "addressable", "~> 2.0"
|
33
|
-
spec.add_development_dependency "rack"
|
34
|
-
spec.add_development_dependency "rack-test"
|
35
|
-
spec.add_development_dependency "faraday"
|
36
|
-
spec.add_development_dependency "faraday_middleware"
|
37
|
-
spec.add_development_dependency "activesupport"
|
38
|
-
spec.add_development_dependency "rake", "~> 10.0"
|
39
|
-
spec.add_development_dependency "minitest", "~> 5.0"
|
40
|
-
spec.add_development_dependency "minitest-reporters"
|
41
|
-
spec.add_development_dependency "simplecov"
|
42
|
-
end
|