reynard 0.0.5 → 0.0.9

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: 1c84ca4accb44e6e4d25e67071ade066d2fad559eedff8726915da6736e05246
4
- data.tar.gz: 1340cfef80c1464cf01d01e4509c0bdc2fa4831a3d8f6960a209fe6ee9f8b167
3
+ metadata.gz: ec98b52096f95c7c03aaa61a7f27555f5d595ab83ac15510e49fbdc600cbb3a0
4
+ data.tar.gz: e3fc7d6e0aa537ea08c727e89aec68b2e66986f5e4e1e53ebd6f74733bde6b8b
5
5
  SHA512:
6
- metadata.gz: 19e65638574e27c47310bad70a9b507aa6f5600ae7c05c3495c83ac7c58015b2f5d17d0c7d4592d2b6bf74852fe11782b796895bd4db552e96cd5ed80b85c895
7
- data.tar.gz: f63952863a80039f1735bb53e27a1a15c670e4f80e8c2c5813e1880d4c33962d791ec9ed46d5f268f924725052ed831bf6a33f31b047a9d861d8b81645fc748d
6
+ metadata.gz: 2b18bf2403a99460ded8be2c85efc08b28ffa8616d35628dbdb0806f58613e426acf48eff05d6f0c74bc0e602ed9a0bb6bc69e3604818b6b7a1ba9f1c4f38848
7
+ data.tar.gz: c566c502480ff527e5e8a6451bebd5b8629c714fb41a27faa486a311fb8a20ee85aea14d1c89c211fe20e6dd63e768027d678593341178d89177cda3f24512df
data/README.md CHANGED
@@ -66,6 +66,20 @@ employee = reynard.
66
66
  execute
67
67
  ```
68
68
 
69
+ ## Mocking
70
+
71
+ 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.
72
+
73
+ ```ruby
74
+ Reynard.http = MyMock.new
75
+
76
+ class MyMock
77
+ def request(uri, net_http_request)
78
+ Net::HTTPResponse::CODE_TO_OBJ['404'].new('HTTP/1.1', '200', 'OK')
79
+ end
80
+ end
81
+ ```
82
+
69
83
  ## Copyright and other legal
70
84
 
71
85
  See LICENCE.
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'ostruct'
4
+
3
5
  class Reynard
4
6
  # Exposes a public interface to build a request context.
5
7
  class Context
@@ -24,10 +26,10 @@ class Reynard
24
26
  copy(params: @specification.build_grouped_params(@request_context.operation.node, params))
25
27
  end
26
28
 
27
- def body(...)
29
+ def body(data)
28
30
  return unless @request_context.operation
29
31
 
30
- serialized_body = @specification.build_body(@request_context.operation.node, ...)
32
+ serialized_body = @specification.build_body(@request_context.operation.node, data)
31
33
  return unless serialized_body
32
34
 
33
35
  copy(
@@ -37,7 +39,7 @@ class Reynard
37
39
  end
38
40
 
39
41
  def headers(headers)
40
- copy(headers: @request_context.merge(headers))
42
+ copy(headers: @request_context.headers.merge(headers))
41
43
  end
42
44
 
43
45
  def execute
@@ -67,11 +69,26 @@ class Reynard
67
69
  http_response.code,
68
70
  http_response.content_type
69
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)
70
80
  ObjectBuilder.new(
71
81
  media_type: media_type,
72
82
  schema: @specification.schema(media_type.node),
73
83
  http_response: http_response
74
84
  ).call
75
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
92
+ end
76
93
  end
77
94
  end
@@ -26,6 +26,12 @@ class Reynard
26
26
  build_http_get
27
27
  when 'post'
28
28
  build_http_post
29
+ when 'put'
30
+ build_http_put
31
+ when 'patch'
32
+ build_http_patch
33
+ when 'delete'
34
+ build_http_delete
29
35
  end
30
36
  end
31
37
 
@@ -38,6 +44,22 @@ class Reynard
38
44
  post.body = @request_context.body
39
45
  post
40
46
  end
47
+
48
+ def build_http_put
49
+ put = Net::HTTP::Put.new(uri, @request_context.headers)
50
+ put.body = @request_context.body
51
+ put
52
+ end
53
+
54
+ def build_http_patch
55
+ patch = Net::HTTP::Patch.new(uri, @request_context.headers)
56
+ patch.body = @request_context.body
57
+ patch
58
+ end
59
+
60
+ def build_http_delete
61
+ Net::HTTP::Delete.new(uri, @request_context.headers)
62
+ end
41
63
  end
42
64
  end
43
65
  end
@@ -35,8 +35,8 @@ class Reynard
35
35
 
36
36
  # Returns a serialized body instance to serialize a request body and figure out the request
37
37
  # headers.
38
- def build_body(operation_node, ...)
39
- SerializedBody.new(dig(*operation_node, 'requestBody', 'content'), ...)
38
+ def build_body(operation_node, data)
39
+ SerializedBody.new(dig(*operation_node, 'requestBody', 'content'), data)
40
40
  end
41
41
 
42
42
  def operation(operation_name)
@@ -61,7 +61,10 @@ class Reynard
61
61
  end
62
62
 
63
63
  def media_type_response(responses, response_code, media_type)
64
- responses.dig(response_code, 'content').each do |expression, response|
64
+ defined_responses = responses.dig(response_code, 'content')
65
+ return unless defined_responses&.any?
66
+
67
+ defined_responses.each do |expression, response|
65
68
  return response, expression if self.class.media_type_matches?(media_type, expression)
66
69
  end
67
70
  nil
@@ -89,7 +92,7 @@ class Reynard
89
92
 
90
93
  def read
91
94
  File.open(@filename, encoding: 'UTF-8') do |file|
92
- YAML.safe_load(file)
95
+ YAML.safe_load(file, aliases: true)
93
96
  end
94
97
  end
95
98
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Reynard
4
- VERSION = '0.0.5'
4
+ VERSION = '0.0.9'
5
5
  end
data/lib/reynard.rb CHANGED
@@ -34,6 +34,14 @@ class Reynard
34
34
  @specification = Specification.new(filename: filename)
35
35
  end
36
36
 
37
+ # Assign an object that follows Reynard's internal request interface to mock requests or use a
38
+ # different HTTP client.
39
+ class << self
40
+ attr_writer :http
41
+ end
42
+
43
+ # Returns Reynard's global request interface. This is a global object to allow persistent
44
+ # connections, caching, and other features that need a persistent object in the process.
37
45
  def self.http
38
46
  @http ||= begin
39
47
  http = Net::HTTP::Persistent.new(name: 'Reynard')
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.0.5
4
+ version: 0.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Manfred Stienstra
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-09-17 00:00:00.000000000 Z
11
+ date: 2021-11-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: multi_json
@@ -140,7 +140,7 @@ homepage: https://github.com/Manfred/reynard
140
140
  licenses:
141
141
  - MIT
142
142
  metadata: {}
143
- post_install_message:
143
+ post_install_message:
144
144
  rdoc_options: []
145
145
  require_paths:
146
146
  - lib
@@ -155,8 +155,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
155
155
  - !ruby/object:Gem::Version
156
156
  version: '0'
157
157
  requirements: []
158
- rubygems_version: 3.2.22
159
- signing_key:
158
+ rubygems_version: 3.1.6
159
+ signing_key:
160
160
  specification_version: 4
161
161
  summary: Minimal OpenAPI client.
162
162
  test_files: []