Skip to content

Commit

Permalink
D / rewrite done
Browse files Browse the repository at this point in the history
  • Loading branch information
hikari-desu committed Feb 12, 2019
1 parent 85951a7 commit 2e60868
Show file tree
Hide file tree
Showing 9 changed files with 233 additions and 323 deletions.
434 changes: 214 additions & 220 deletions README.md

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -359,11 +359,11 @@


# method signature
# `exp_by` (select_example_by): choose the example fields.
examples(exp_by = :all, examples_hash)
# `exp_params` (select_example_by): choose the example fields.
examples(exp_params = :all, examples_hash)
# usage
# it defines 2 examples by using parameter :id and :name
# if pass :all to `exp_by`, keys will be all the parameter's names.
# if pass :all to `exp_params`, keys will be all the parameter's names.
examples [:id, :name], {
:right_input => [ 1, 'user'], # == { id: 1, name: 'user' }
:wrong_input => [ -1, '' ]
Expand Down Expand Up @@ -425,7 +425,7 @@
:password! => { type: String, pattern: /[0-9]{6,10}/, desc: 'password' },
# optional
:remarks => { type: String, desc: 'remarks' },
}, exp_by: %i[ name password ],
}, exp_params: %i[ name password ],
examples: { # ↓ ↓
:right_input => [ 'user1', '123456' ],
:wrong_input => [ 'user2', 'abc' ]
Expand All @@ -448,7 +448,7 @@

1. `media_type`: we provide some [mapping](lib/oas_objs/media_type_obj.rb) from symbols to real media-types.
2. `schema_info`: as above (see param).
3. `exp_by` and `examples`: for the above example, the following has the same effect:
3. `exp_params` and `examples`: for the above example, the following has the same effect:
```
examples: {
:right_input => { name: 'user1', password: '123456' },
Expand Down
55 changes: 0 additions & 55 deletions documentation/parameter.md

This file was deleted.

29 changes: 0 additions & 29 deletions examples/auto_gen_desc.rb

This file was deleted.

4 changes: 2 additions & 2 deletions lib/oas_objs/media_type_obj.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ class MediaTypeObj < Hash

def initialize(media_type, hash)
examples_hash = hash.delete(:examples)
exp_by = schema_type.keys if (exp_by = hash.delete(:exp_by)) == :all
self.examples = ExampleObj.new(examples_hash, exp_by, multiple: true) if examples_hash.present?
exp_params = schema_type.keys if (exp_params = hash.delete(:exp_params)) == :all
self.examples = ExampleObj.new(examples_hash, exp_params, multiple: true) if examples_hash.present?
self.media_type = media_type_mapping(media_type)
self.schema = SchemaObj.new(hash.values_at(:type, :data).compact.first,
hash.except(:type, :data))
Expand Down
2 changes: 1 addition & 1 deletion lib/oas_objs/schema_obj.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def other
{
pattern: _pattern.is_a?(String) ? _pattern : _pattern&.inspect&.delete('/'),
example: ExampleObj.new(self[:example]).process,
examples: ExampleObj.new(self[:examples], self[:exp_by], multiple: true).process
examples: ExampleObj.new(self[:examples], self[:exp_params], multiple: true).process
}
end

Expand Down
12 changes: 6 additions & 6 deletions lib/open_api/dsl/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def param_ref component_key, *keys
self[:parameters] += [component_key, *keys].map { |key| RefObj.new(:parameter, key) }
end

# options: `exp_by` and `examples`
# options: `exp_params` and `examples`
def request_body required, media_type, data: { }, desc: '', **options
(self[:requestBody] ||= RequestBodyObj.new(required, desc)).absorb(media_type, { data: data , **options })
end
Expand All @@ -90,13 +90,13 @@ def form! data:, **options
body! :form, data: data, **options
end

def data name, type = nil, schema_info = { }
schema_info[:type] = type if type.present?
form data: { name => schema_info }
def data name, type = nil, schema = { }
schema[:type] = type if type.present?
form data: { name => schema }
end

def response code, desc, media_type = nil, data: { }, type: nil
(self[:responses][code] ||= ResponseObj.new(desc)).absorb(desc, media_type, { data: type || data })
def response code, desc, media_type = nil, data: { }
(self[:responses][code] ||= ResponseObj.new(desc)).absorb(desc, media_type, { data: data })
end

alias_method :resp, :response
Expand Down
8 changes: 4 additions & 4 deletions lib/open_api/dsl/components.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ def initialize
merge!(%i[ schemas responses parameters examples requestBodies securitySchemes ].map { |k| [ k, { } ] }.to_h)
end

def schema component_key, type = nil, **schema_info
return unless schema = process_schema_input(type, schema_info, component_key, model: component_key)
def schema component_key, type = nil, **schema
return unless schema = process_schema_input(type, schema, component_key, model: component_key)
self[:schemas][component_key.to_s.to_sym] = schema.process
end

Expand Down Expand Up @@ -49,8 +49,8 @@ def request_body component_key, required, media_type, data: { }, desc: '', **opt
arrow_enable :body
arrow_enable :body!

def response component_key, desc, media_type = nil, data: { }, type: nil
(self[:responses][component_key] ||= ResponseObj.new(desc)).absorb(desc, media_type, { data: type || data })
def response component_key, desc, media_type = nil, data: { }
(self[:responses][component_key] ||= ResponseObj.new(desc)).absorb(desc, media_type, { data: data })
end

alias_method :resp, :response
Expand Down
2 changes: 1 addition & 1 deletion spec/oas_objs/schema_obj_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@
end

describe ':examples' do
api -> { query :info, { name: String, age: Integer }, examples: { input1: ['a', 1], input2: ['b, 2'] }, exp_by: %i[ name age ] },
api -> { query :info, { name: String, age: Integer }, examples: { input1: ['a', 1], input2: ['b, 2'] }, exp_params: %i[ name age ] },
has_key!: :examples
focus_on :examples
expect_its 0, eq: { input1: { value: { name: 'a', age: 1 } } }
Expand Down

0 comments on commit 2e60868

Please sign in to comment.