reynard 0.1.0 → 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.
- checksums.yaml +4 -4
- data/README.md +17 -2
- data/lib/reynard/context.rb +6 -26
- data/lib/reynard/http/response.rb +55 -0
- data/lib/reynard/http.rb +1 -0
- data/lib/reynard/version.rb +1 -1
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 95ca8eb7744c1550bfcf705a92d97331d9c558b08dec5f2369b166b285b38edc
|
4
|
+
data.tar.gz: a140a0dbe5f74c95f43d2aa785dd3c96a322eab2893b2d8a629abd3b9859724e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 77148ca6897e1e7b0479348cfe536ff94afc5c1741b6fe59afea7940b442d7c46a71914868f572f3588b7d85d95f38446bd8d28885a22a032c91a86e71e3da28
|
7
|
+
data.tar.gz: eab59f1738c99af0f2c8fba6a4fb09f15a5a26f31f86de91125d9f299d2d50844060bd3c13db33b260f267a82e269175e0c69d2cdeb4f32d397e99bca74e9694
|
data/README.md
CHANGED
@@ -51,7 +51,7 @@ reynard.base_url(base_url)
|
|
51
51
|
Assuming there is an operation called `employeeByUuid` you can it as shown below.
|
52
52
|
|
53
53
|
```ruby
|
54
|
-
|
54
|
+
response = reynard.
|
55
55
|
operation('employeeByUuid').
|
56
56
|
params(uuid: uuid).
|
57
57
|
execute
|
@@ -60,12 +60,27 @@ employee = reynard.
|
|
60
60
|
When an operation requires a body, you can add it as structured data.
|
61
61
|
|
62
62
|
```ruby
|
63
|
-
|
63
|
+
response = reynard.
|
64
64
|
operation('createEmployee').
|
65
65
|
body(name: 'Sam Seven').
|
66
66
|
execute
|
67
67
|
```
|
68
68
|
|
69
|
+
In case the response matches a response in the specification it will attempt to build an object using the specified schema.
|
70
|
+
|
71
|
+
```ruby
|
72
|
+
response.object.name #=> 'Sam Seven'
|
73
|
+
```
|
74
|
+
|
75
|
+
The response object shared much of its interface with `Net::HTTP::Response`.
|
76
|
+
|
77
|
+
```ruby
|
78
|
+
response.code #=> '200'
|
79
|
+
response.content_type #=> 'application/json'
|
80
|
+
response['Content-Type'] #=> 'application/json'
|
81
|
+
response.body #=> '{"name":"Sam Seven"}'
|
82
|
+
```
|
83
|
+
|
69
84
|
## Mocking
|
70
85
|
|
71
86
|
You can mock Reynard requests by changing the HTTP implementation. The class **must** implement a single `request` method that accepts an URI and net/http request object. It **must** return a net/http response object or an object with the exact same interface.
|
data/lib/reynard/context.rb
CHANGED
@@ -43,7 +43,7 @@ class Reynard
|
|
43
43
|
end
|
44
44
|
|
45
45
|
def execute
|
46
|
-
|
46
|
+
build_response(build_request.perform)
|
47
47
|
end
|
48
48
|
|
49
49
|
private
|
@@ -63,32 +63,12 @@ class Reynard
|
|
63
63
|
Reynard::Http::Request.new(request_context: @request_context)
|
64
64
|
end
|
65
65
|
|
66
|
-
def
|
67
|
-
|
68
|
-
@
|
69
|
-
|
70
|
-
http_response.content_type
|
71
|
-
)
|
72
|
-
if media_type
|
73
|
-
build_object_with_media_type(http_response, media_type)
|
74
|
-
else
|
75
|
-
build_object_without_media_type(http_response)
|
76
|
-
end
|
77
|
-
end
|
78
|
-
|
79
|
-
def build_object_with_media_type(http_response, media_type)
|
80
|
-
ObjectBuilder.new(
|
81
|
-
media_type: media_type,
|
82
|
-
schema: @specification.schema(media_type.node),
|
66
|
+
def build_response(http_response)
|
67
|
+
Reynard::Http::Response.new(
|
68
|
+
specification: @specification,
|
69
|
+
request_context: @request_context,
|
83
70
|
http_response: http_response
|
84
|
-
)
|
85
|
-
end
|
86
|
-
|
87
|
-
def build_object_without_media_type(http_response)
|
88
|
-
# Try to parse the response as JSON and give up otherwise.
|
89
|
-
OpenStruct.new(MultiJson.load(http_response.body))
|
90
|
-
rescue StandardError
|
91
|
-
http_response.body
|
71
|
+
)
|
92
72
|
end
|
93
73
|
end
|
94
74
|
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Reynard
|
4
|
+
class Http
|
5
|
+
# Wraps an HTTP response and returns an object when it can find a definition for the response
|
6
|
+
# in the specification.
|
7
|
+
class Response
|
8
|
+
extend Forwardable
|
9
|
+
def_delegators :@http_response, :code, :content_type, :[], :body
|
10
|
+
|
11
|
+
def initialize(specification:, request_context:, http_response:)
|
12
|
+
@specification = specification
|
13
|
+
@request_context = request_context
|
14
|
+
@http_response = http_response
|
15
|
+
end
|
16
|
+
|
17
|
+
# Instantiates an object based on the schema that fits the response.
|
18
|
+
def object
|
19
|
+
return @object if defined?(@object)
|
20
|
+
|
21
|
+
@object = build_object
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def build_object
|
27
|
+
media_type = @specification.media_type(
|
28
|
+
@request_context.operation.node,
|
29
|
+
@http_response.code,
|
30
|
+
@http_response.content_type
|
31
|
+
)
|
32
|
+
if media_type
|
33
|
+
build_object_with_media_type(media_type)
|
34
|
+
else
|
35
|
+
build_object_without_media_type
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def build_object_with_media_type(media_type)
|
40
|
+
ObjectBuilder.new(
|
41
|
+
media_type: media_type,
|
42
|
+
schema: @specification.schema(media_type.node),
|
43
|
+
http_response: @http_response
|
44
|
+
).call
|
45
|
+
end
|
46
|
+
|
47
|
+
def build_object_without_media_type
|
48
|
+
# Try to parse the response as JSON and give up otherwise.
|
49
|
+
OpenStruct.new(MultiJson.load(@http_response.body))
|
50
|
+
rescue StandardError
|
51
|
+
@http_response.body
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
data/lib/reynard/http.rb
CHANGED
data/lib/reynard/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: reynard
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Manfred Stienstra
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-11-
|
11
|
+
date: 2021-11-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: multi_json
|
@@ -124,6 +124,7 @@ files:
|
|
124
124
|
- lib/reynard/grouped_parameters.rb
|
125
125
|
- lib/reynard/http.rb
|
126
126
|
- lib/reynard/http/request.rb
|
127
|
+
- lib/reynard/http/response.rb
|
127
128
|
- lib/reynard/media_type.rb
|
128
129
|
- lib/reynard/model.rb
|
129
130
|
- lib/reynard/models.rb
|
@@ -139,7 +140,8 @@ files:
|
|
139
140
|
homepage: https://github.com/Manfred/reynard
|
140
141
|
licenses:
|
141
142
|
- MIT
|
142
|
-
metadata:
|
143
|
+
metadata:
|
144
|
+
rubygems_mfa_required: 'true'
|
143
145
|
post_install_message:
|
144
146
|
rdoc_options: []
|
145
147
|
require_paths:
|