reynard 0.0.4 → 0.0.8

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: 73e8e17f725539aac5739311c093ac8ff851eab5c9bc3016cdd3839b29cdcc8b
4
- data.tar.gz: a8fc3ba0731ad45a5f92324d819d84b7bf5fb418d8f4eb3751b5a7b2814b6a19
3
+ metadata.gz: fcfd5e665e0b3fcc8c593d81c15db9560e8abc0125e9a254aeb98d3f53b6fb8a
4
+ data.tar.gz: 61d7d812f5b5eacf55c9be3fb6463cf1adafeb5e7935d2d0fc0b20a33a4a27b3
5
5
  SHA512:
6
- metadata.gz: e6b03cde95bd0bf69a03f860d3eaa57bf9d39f1deb6c9abfd719d4fdbaec728f1c8d5a2485493e5b403cdd0b57a80784c3dd48c02eef5d957eaf7638cacf91b0
7
- data.tar.gz: f56a52f52d041ce58b69e1d799072510131a4f471560a908b24bb1e01630ef1e971d07e1b88845fae298e90e1d9f6f6dc1ad7990375c35d9c0ae357c69644a0c
6
+ metadata.gz: 05a42b85120cb3e519c4fcc3bad7c23a17e5c1a007072b22843421619b1460dabf378750fc83eea406a44a3ec0e08c80b309d53bdce2da0df09d6f3a67643685
7
+ data.tar.gz: d8e2ce1c77b097a4a3e4a64d8b3fd2d67bc0e8737307e9429452e4d794aedad7c34e7b44a76a1f4ac945f459a4f0497e67d354ebb37f2cb1fdece9e187981efd
data/README.md CHANGED
@@ -57,6 +57,15 @@ employee = reynard.
57
57
  execute
58
58
  ```
59
59
 
60
+ When an operation requires a body, you can add it as structured data.
61
+
62
+ ```ruby
63
+ employee = reynard.
64
+ operation('createEmployee').
65
+ body(name: 'Sam Seven').
66
+ execute
67
+ ```
68
+
60
69
  ## Copyright and other legal
61
70
 
62
71
  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,8 +26,20 @@ class Reynard
24
26
  copy(params: @specification.build_grouped_params(@request_context.operation.node, params))
25
27
  end
26
28
 
29
+ def body(data)
30
+ return unless @request_context.operation
31
+
32
+ serialized_body = @specification.build_body(@request_context.operation.node, data)
33
+ return unless serialized_body
34
+
35
+ copy(
36
+ headers: @request_context.headers.merge(serialized_body.headers),
37
+ body: serialized_body.to_s
38
+ )
39
+ end
40
+
27
41
  def headers(headers)
28
- copy(headers: headers)
42
+ copy(headers: @request_context.headers.merge(headers))
29
43
  end
30
44
 
31
45
  def execute
@@ -35,7 +49,7 @@ class Reynard
35
49
  private
36
50
 
37
51
  def build_request_context
38
- RequestContext.new(base_url: @specification.default_base_url)
52
+ RequestContext.new(base_url: @specification.default_base_url, headers: {})
39
53
  end
40
54
 
41
55
  def copy(**properties)
@@ -55,11 +69,26 @@ class Reynard
55
69
  http_response.code,
56
70
  http_response.content_type
57
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)
58
80
  ObjectBuilder.new(
59
81
  media_type: media_type,
60
82
  schema: @specification.schema(media_type.node),
61
83
  http_response: http_response
62
84
  ).call
63
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
64
93
  end
65
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
@@ -7,6 +7,7 @@ class Reynard
7
7
  :operation,
8
8
  :headers,
9
9
  :params,
10
+ :body,
10
11
  keyword_init: true
11
12
  ) do
12
13
  def verb
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Reynard
4
+ # Serializes a request body and returns headers appropriate for the request.
5
+ class SerializedBody
6
+ def initialize(content, data)
7
+ @content = content
8
+ @data = data
9
+ end
10
+
11
+ def content_type
12
+ 'application/json'
13
+ end
14
+
15
+ def headers
16
+ { 'Content-Type' => content_type }
17
+ end
18
+
19
+ def to_s
20
+ MultiJson.dump(@data)
21
+ end
22
+ end
23
+ end
@@ -33,6 +33,12 @@ class Reynard
33
33
  GroupedParameters.new(dig(*operation_node, 'parameters'), params).to_h
34
34
  end
35
35
 
36
+ # Returns a serialized body instance to serialize a request body and figure out the request
37
+ # headers.
38
+ def build_body(operation_node, data)
39
+ SerializedBody.new(dig(*operation_node, 'requestBody', 'content'), data)
40
+ end
41
+
36
42
  def operation(operation_name)
37
43
  dig('paths').each do |path, operations|
38
44
  operations.each do |verb, operation|
@@ -44,7 +50,7 @@ class Reynard
44
50
 
45
51
  def media_type(operation_node, response_code, media_type)
46
52
  responses = dig(*operation_node, 'responses')
47
- response_code = responses.key?(response_code) ? response_code : 'default'
53
+ response_code = 'default' unless responses.key?(response_code)
48
54
  response, media_type = media_type_response(responses, response_code, media_type)
49
55
  return unless response
50
56
 
@@ -55,7 +61,10 @@ class Reynard
55
61
  end
56
62
 
57
63
  def media_type_response(responses, response_code, media_type)
58
- 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|
59
68
  return response, expression if self.class.media_type_matches?(media_type, expression)
60
69
  end
61
70
  nil
@@ -83,7 +92,7 @@ class Reynard
83
92
 
84
93
  def read
85
94
  File.open(@filename, encoding: 'UTF-8') do |file|
86
- YAML.safe_load(file)
95
+ YAML.safe_load(file, aliases: true)
87
96
  end
88
97
  end
89
98
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Reynard
4
- VERSION = '0.0.4'
4
+ VERSION = '0.0.8'
5
5
  end
data/lib/reynard.rb CHANGED
@@ -15,6 +15,7 @@ class Reynard
15
15
  def_delegators :@specification, :servers
16
16
 
17
17
  autoload :Context, 'reynard/context'
18
+ autoload :GroupedParameters, 'reynard/grouped_parameters'
18
19
  autoload :Http, 'reynard/http'
19
20
  autoload :MediaType, 'reynard/media_type'
20
21
  autoload :Model, 'reynard/model'
@@ -23,10 +24,10 @@ class Reynard
23
24
  autoload :Operation, 'reynard/operation'
24
25
  autoload :RequestContext, 'reynard/request_context'
25
26
  autoload :Schema, 'reynard/schema'
27
+ autoload :SerializedBody, 'reynard/serialized_body'
26
28
  autoload :Server, 'reynard/server'
27
29
  autoload :Specification, 'reynard/specification'
28
30
  autoload :Template, 'reynard/template'
29
- autoload :GroupedParameters, 'reynard/grouped_parameters'
30
31
  autoload :VERSION, 'reynard/version'
31
32
 
32
33
  def initialize(filename:)
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.4
4
+ version: 0.0.8
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-08-06 00:00:00.000000000 Z
11
+ date: 2021-11-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: multi_json
@@ -131,6 +131,7 @@ files:
131
131
  - lib/reynard/operation.rb
132
132
  - lib/reynard/request_context.rb
133
133
  - lib/reynard/schema.rb
134
+ - lib/reynard/serialized_body.rb
134
135
  - lib/reynard/server.rb
135
136
  - lib/reynard/specification.rb
136
137
  - lib/reynard/template.rb
@@ -139,7 +140,7 @@ homepage: https://github.com/Manfred/reynard
139
140
  licenses:
140
141
  - MIT
141
142
  metadata: {}
142
- post_install_message:
143
+ post_install_message:
143
144
  rdoc_options: []
144
145
  require_paths:
145
146
  - lib
@@ -154,8 +155,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
154
155
  - !ruby/object:Gem::Version
155
156
  version: '0'
156
157
  requirements: []
157
- rubygems_version: 3.2.22
158
- signing_key:
158
+ rubygems_version: 3.1.6
159
+ signing_key:
159
160
  specification_version: 4
160
161
  summary: Minimal OpenAPI client.
161
162
  test_files: []