reynard 0.4.1 → 0.5.0

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: 7df788fc69d73d417d6b9490299d2552befb4fa4764ebc4621e9d20cd27cab49
4
- data.tar.gz: 053775ac81400f50ad28436a122fc0316c3b6037afb1fe41b4c1638a80912509
3
+ metadata.gz: d85681365ddb46b06c062b39bdea10bf46810d80d813c9737df0ee3aeb38e843
4
+ data.tar.gz: '007908692ed0a3c437befba30a29d0f694429033097b7ae47ca2aaf7050c05ea'
5
5
  SHA512:
6
- metadata.gz: 5d2bee0211247407c99e0d5fd180bca0738230c89024cdddaae96de8eaa3afdf1b41fd2e11d95ef4c8c2210ff8f269ad7b4070959065ac48063f1ba8355056c0
7
- data.tar.gz: f81ba585b5db0cea43659269ebdffc73ac4e234353f902a663e43203985a576e55874ccf2a6524eba9e029c04bf3174cf53f6ea3a35f44393ed036a37e66adc9
6
+ metadata.gz: 1e23bf2702660623a61039bb8444ea3e0f4bdd7eaceab42d22dbca2ea157e705a54af69558ce84812d4ef2f13c1330071781e8da61d3ed44c8b0d2a944816752
7
+ data.tar.gz: 9e973c551d30a6b7e6ab8ef494d270b5dac05dfd96de86c49448c5889b070a07ee8d8aa486de1f80c3d372c73ed7b11e37da49625fdeb64d21e84352130e55f3
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rack'
4
+
5
+ class Reynard
6
+ # Loads external references.
7
+ class External
8
+ def initialize(path:, ref:)
9
+ @path = path
10
+ @relative_path, @anchor = ref.split('#', 2)
11
+ end
12
+
13
+ def path
14
+ return [] unless @anchor
15
+
16
+ @anchor.split('/')[1..]
17
+ end
18
+
19
+ def data
20
+ File.open(filename, encoding: 'UTF-8') do |file|
21
+ YAML.safe_load(file, aliases: true)
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ def filename
28
+ filename = File.expand_path(@relative_path, @path)
29
+ return filename if filename.start_with?(@path)
30
+
31
+ raise 'You are only allowed to reference files relative to the specification file.'
32
+ end
33
+ end
34
+ end
@@ -46,7 +46,7 @@ class Reynard
46
46
  end
47
47
 
48
48
  def self.model_class_get(name)
49
- Reynard::Models.const_get(name)
49
+ Kernel.const_get("::Reynard::Models::#{name}")
50
50
  rescue NameError
51
51
  nil
52
52
  end
@@ -102,11 +102,14 @@ class Reynard
102
102
 
103
103
  def self.normalize_model_name(name)
104
104
  # 1. Unescape encoded characters to create an UTF-8 string
105
- # 2. Replace all non-alphabetic characters with a space (not allowed in Ruby constant)
106
- # 3. Camelcase
105
+ # 2. Remove extensions for regularly used external schema files
106
+ # 3. Replace all non-alphabetic characters with a space (not allowed in Ruby constant)
107
+ # 4. Camelcase
107
108
  Rack::Utils.unescape_path(name)
109
+ .gsub(/(.yml|.yaml|.json)\Z/, '')
108
110
  .gsub(/[^[:alpha:]]/, ' ')
109
111
  .gsub(/(\s+)([[:alpha:]])/) { Regexp.last_match(2).upcase }
112
+ .gsub(/\A(.)/) { Regexp.last_match(1).upcase }
110
113
  end
111
114
 
112
115
  private
@@ -117,6 +120,9 @@ class Reynard
117
120
  end
118
121
  end
119
122
 
123
+ # rubocop:disable Metrics/AbcSize
124
+ # rubocop:disable Metrics/CyclomaticComplexity
125
+ # rubocop:disable Metrics/MethodLength
120
126
  def dig_into(data, cursor, path)
121
127
  while path.length.positive?
122
128
  cursor = cursor[path.first]
@@ -125,12 +131,23 @@ class Reynard
125
131
  path.shift
126
132
  next unless cursor.respond_to?(:key?) && cursor&.key?('$ref')
127
133
 
128
- # We currenly only supply references inside the document starting with #/.
129
- path = Rack::Utils.unescape_path(cursor['$ref'][2..]).split('/') + path
130
- cursor = data
134
+ case cursor['$ref']
135
+ # References another element in the current specification.
136
+ when %r{\A#/}
137
+ path = Rack::Utils.unescape_path(cursor['$ref'][2..]).split('/') + path
138
+ cursor = data
139
+ # References another file, with an optional anchor to an element in the data.
140
+ when %r{\A\./}
141
+ external = External.new(path: File.dirname(@filename), ref: cursor['$ref'])
142
+ path = external.path + path
143
+ cursor = external.data
144
+ end
131
145
  end
132
146
  cursor
133
147
  end
148
+ # rubocop:enable Metrics/AbcSize
149
+ # rubocop:enable Metrics/CyclomaticComplexity
150
+ # rubocop:enable Metrics/MethodLength
134
151
 
135
152
  def schema_name(response)
136
153
  ref = response.dig('schema', '$ref')
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Reynard
4
- VERSION = '0.4.1'
4
+ VERSION = '0.5.0'
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 :External, 'reynard/external'
18
19
  autoload :GroupedParameters, 'reynard/grouped_parameters'
19
20
  autoload :Http, 'reynard/http'
20
21
  autoload :Logger, 'reynard/logger'
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.1
4
+ version: 0.5.0
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: 2022-04-05 00:00:00.000000000 Z
11
+ date: 2022-07-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: multi_json
@@ -121,6 +121,7 @@ files:
121
121
  - README.md
122
122
  - lib/reynard.rb
123
123
  - lib/reynard/context.rb
124
+ - lib/reynard/external.rb
124
125
  - lib/reynard/grouped_parameters.rb
125
126
  - lib/reynard/http.rb
126
127
  - lib/reynard/http/request.rb
@@ -142,7 +143,7 @@ licenses:
142
143
  - MIT
143
144
  metadata:
144
145
  rubygems_mfa_required: 'true'
145
- post_install_message:
146
+ post_install_message:
146
147
  rdoc_options: []
147
148
  require_paths:
148
149
  - lib
@@ -158,7 +159,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
158
159
  version: '0'
159
160
  requirements: []
160
161
  rubygems_version: 3.1.6
161
- signing_key:
162
+ signing_key:
162
163
  specification_version: 4
163
164
  summary: Minimal OpenAPI client.
164
165
  test_files: []