ropen_pi 0.1.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -35,6 +35,15 @@ module RopenPi
35
35
  .merge(opts)
36
36
  end
37
37
 
38
+ def self.param_in_header(name, schema_type, fmt: nil, desc: 'tba', opts: {})
39
+ param(name, schema_type, fmt: fmt, desc: desc, opts: opts)
40
+ .merge(
41
+ in: 'header',
42
+ required: false
43
+ )
44
+ .merge(opts)
45
+ end
46
+
38
47
  def self.param(name, schema_type, fmt: nil, desc: 'tba', opts: {})
39
48
  {
40
49
  name: name.to_s,
@@ -57,51 +66,60 @@ module RopenPi
57
66
  enum: values
58
67
  }
59
68
  end
69
+
70
+ class << self
71
+ alias boolean_param bool_param
72
+ alias integer_param int_param
73
+ end
60
74
  end
61
75
 
62
76
  module Type
63
- def self.date_time_type(opts = {})
64
- string_type(opts).merge(format: 'date-time', example: '2020-02-02')
77
+ def self.date_time_type(opts = { example: '2020-02-02' })
78
+ string_type(opts).merge(format: 'date-time')
65
79
  end
66
80
 
67
- def self.email_type(opts = {})
68
- string_type(opts).merge(format: 'email', example: 'han.solo@example.com')
81
+ def self.email_type(opts = { example: 'han.solo@example.com' })
82
+ string_type(opts).merge(format: 'email')
69
83
  end
70
84
 
71
- def self.uuid_type(opts = {})
72
- string_type(opts).merge(format: 'uuid', example: 'abcd12-1234ab-abcdef123')
85
+ def self.uuid_type(opts = { example: 'abcd12-1234ab-abcdef123' })
86
+ string_type(opts).merge(format: 'uuid')
73
87
  end
74
88
 
75
- def self.string_type(opts = {})
76
- type('string', opts).merge(example: 'Example string')
89
+ def self.string_type(opts = { example: 'Example string' })
90
+ type('string', opts)
77
91
  end
78
92
 
79
- def self.integer_type(opts = {})
80
- type('integer', opts).merge(example: 1)
93
+ def self.integer_type(opts = { example: 1 })
94
+ type('integer', opts)
81
95
  end
82
96
 
83
- def self.bool_type(opts = {})
84
- type('boolean', opts).merge(example: true)
97
+ def self.bool_type(opts = { example: true })
98
+ type('boolean', opts)
85
99
  end
86
100
 
87
101
  def self.type(thing, opts = {})
88
- { type: thing.to_s }.merge(opts)
102
+ { type: thing }.merge(opts)
89
103
  end
90
104
 
91
105
  def self.string_array_type(opts = {})
92
106
  {
93
107
  type: 'array',
94
108
  items: { type: 'string', example: 'Example string' }
95
- }.merge(opts)
109
+ }.merge(opts)
96
110
  end
97
111
 
98
112
  def self.ref_type(ref)
99
113
  { '$ref': ref }
100
114
  end
115
+
116
+ class << self
117
+ alias boolean_type bool_type
118
+ alias int_type integer_type
119
+ end
101
120
  end
102
121
 
103
122
  module Response
104
- # rubocop:disable Metrics/MethodLength
105
123
  def self.collection(ref, desc: 'tba', type: RopenPi::APP_JSON)
106
124
  {
107
125
  description: desc,
@@ -109,7 +127,7 @@ module RopenPi
109
127
  type => {
110
128
  schema: {
111
129
  type: 'object',
112
- properties: { data: { type: 'array', items: { '$ref': ref } } }
130
+ properties: { data: { type: :array, items: { '$ref': ref } } }
113
131
  }
114
132
  }
115
133
  }
@@ -129,6 +147,5 @@ module RopenPi
129
147
  }
130
148
  }
131
149
  end
132
- # rubocop:enable Metrics/MethodLength
133
150
  end
134
151
  end
@@ -1,41 +1,45 @@
1
1
  # frozen_string_literal: true
2
+ module RopenPi
3
+ module Specs
4
+ class Configuration
5
+ def initialize(rspec_config)
6
+ @rspec_config = rspec_config
7
+ end
2
8
 
3
- class RopenPi::Specs::Configuration
4
- def initialize(rspec_config)
5
- @rspec_config = rspec_config
6
- end
9
+ def root_dir
10
+ @root_dir ||= begin
11
+ raise ConfigurationError, 'No root_dir provided. See open_api_helper.rb' if @rspec_config.root_dir.nil?
7
12
 
8
- def root_dir
9
- @root_dir ||= begin
10
- raise ConfigurationError, 'No root_dir provided. See open_api_helper.rb' if @rspec_config.root_dir.nil?
13
+ @rspec_config.root_dir
14
+ end
15
+ end
11
16
 
12
- @rspec_config.root_dir
13
- end
14
- end
17
+ def open_api_output_format
18
+ @open_api_output_format ||= begin
19
+ @rspec_config.open_api_output_format || :json
20
+ end
21
+ end
15
22
 
16
- def open_api_output_format
17
- @open_api_output_format ||= begin
18
- @rspec_config.open_api_output_format || :json
19
- end
20
- end
23
+ def open_api_docs
24
+ @open_api_docs ||= begin
25
+ if @rspec_config.open_api_docs.nil? || @rspec_config.open_api_docs.empty?
26
+ raise ConfigurationError, 'No open_api_docs defined. See open_api_helper.rb'
27
+ end
21
28
 
22
- def open_api_docs
23
- @open_api_docs ||= begin
24
- if @rspec_config.open_api_docs.nil? || @rspec_config.open_api_docs.empty?
25
- raise ConfigurationError, 'No open_api_docs defined. See open_api_helper.rb'
29
+ @rspec_config.open_api_docs
30
+ end
26
31
  end
27
32
 
28
- @rspec_config.open_api_docs
29
- end
30
- end
31
-
32
- def get_doc(name)
33
- return open_api_docs.values.first if name.nil?
33
+ def get_doc(name)
34
+ return open_api_docs.values.first if name.nil?
34
35
 
35
- raise ConfigurationError, "Unknown doc '#{name}'" unless open_api_docs[name]
36
+ raise ConfigurationError, "Unknown doc '#{name}'" unless open_api_docs[name]
36
37
 
37
- open_api_docs[name]
38
+ open_api_docs[name]
39
+ end
40
+ end
41
+
42
+ class ConfigurationError < StandardError; end
38
43
  end
39
44
  end
40
45
 
41
- class ConfigurationError < StandardError; end
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  # @TODO: replace hashie!
3
4
  require 'hashie'
4
5
 
@@ -105,18 +106,19 @@ module RopenPi
105
106
  # rubocop:enable Metrics/MethodLength
106
107
 
107
108
  def request_body_text_plain(required: false, description: nil, examples: nil)
108
- content_hash = { 'test/plain' => { schema: {type: :string}, examples: examples }.compact! || {} }
109
+ content_hash = { 'test/plain' => { schema: { type: :string }, examples: examples }.compact! || {} }
109
110
  request_body(description: description, required: required, content: content_hash)
110
111
  end
111
112
 
112
113
  # TODO: add examples to this like we can for json, might be large lift
113
114
  # as many assumptions are made on content-type
114
- def request_body_xml(schema:,required: false, description: nil, examples: nil)
115
- passed_examples = Array(examples)
115
+ def request_body_xml(schema:, required: false, description: nil, examples: nil)
116
+ # passed_examples = Array(examples)
116
117
  content_hash = { 'application/xml' => { schema: schema, examples: examples }.compact! || {} }
117
118
  request_body(description: description, required: required, content: content_hash)
118
119
  end
119
120
 
121
+ # rubocop:disable Metrics/MethodLength
120
122
  def request_body_multipart(schema:, description: nil)
121
123
  content_hash = { 'multipart/form-data' => { schema: schema } }
122
124
  request_body(description: description, content: content_hash)
@@ -145,6 +147,7 @@ module RopenPi
145
147
  end
146
148
  end
147
149
  end
150
+ # rubocop:enable Metrics/MethodLength
148
151
 
149
152
  def parameter(attributes)
150
153
  attributes[:required] = true if attributes[:in] && attributes[:in].to_sym == :path
@@ -165,7 +168,7 @@ module RopenPi
165
168
  end
166
169
 
167
170
  def schema(value, content_type: 'application/json')
168
- content_hash = {content_type => {schema: value}}
171
+ content_hash = { content_type => { schema: value } }
169
172
  metadata[:response][:content] = content_hash
170
173
  end
171
174
 
@@ -173,7 +176,7 @@ module RopenPi
173
176
  metadata[:response][:headers] ||= {}
174
177
 
175
178
  if attributes[:type] && attributes[:schema].nil?
176
- attributes[:schema] = {type: attributes[:type]}
179
+ attributes[:schema] = { type: attributes[:type] }
177
180
  attributes.delete(:type)
178
181
  end
179
182
 
@@ -77,8 +77,8 @@ class RopenPi::Specs::RequestFactory
77
77
  def add_path(request, metadata, open_api_doc, parameters, example)
78
78
  template = (open_api_doc[:basePath] || '') + metadata[:path_item][:template]
79
79
 
80
- in_path = parameters.select { |p| p[:in] == :path }
81
- in_query = parameters.select { |p| p[:in] == :query }
80
+ in_path = parameters.select { |p| p[:in].to_sym == :path }
81
+ in_query = parameters.select { |p| p[:in].to_sym == :query }
82
82
 
83
83
  request[:path] = template.tap do |inner_template|
84
84
  in_path.each do |p|
@@ -161,7 +161,7 @@ class RopenPi::Specs::RequestFactory
161
161
  # Rails test infrastructure allows us to send the values directly as a hash
162
162
  # PROS: simple to implement, CONS: serialization/deserialization is bypassed in test
163
163
  tuples = parameters.select { |p| p[:in] == :formData }
164
- .map { |p| [p[:name], example.send(p[:name])] }
164
+ .map { |p| [p[:name], example.send(p[:name])] }
165
165
  Hash[tuples]
166
166
  end
167
167
 
@@ -9,10 +9,8 @@ module RopenPi::Specs
9
9
  end
10
10
 
11
11
  module Yml
12
- require 'active_support/core_ext/hash/keys'
13
-
14
12
  def self.convert(doc)
15
- doc.deep_stringify_keys.to_yaml
13
+ JSON.parse(Json.convert(doc)).to_yaml
16
14
  end
17
15
  end
18
16
 
@@ -1,4 +1,4 @@
1
1
  module RopenPi
2
- VERSION = '0.1.1'.freeze
2
+ VERSION = '0.4.0'.freeze
3
3
  APP_JSON = 'application/json'.freeze
4
4
  end