Class: OpenAPISourceTools::Helper
- Inherits:
-
Object
- Object
- OpenAPISourceTools::Helper
- Defined in:
- lib/openapi/sourcetools/helper.rb
Overview
Helper class supposed to contain helpful methods. Exposed as Gen.h if HelperTask has been run. It is automatically added as the first task but later tasks can remove it.
Constant Summary collapse
- COMPONENTS =
'#/components/'
Instance Attribute Summary collapse
-
#doc ⇒ Object
readonly
Returns the value of attribute doc.
-
#parent_parameters ⇒ Object
Returns the value of attribute parent_parameters.
-
#parents ⇒ Object
readonly
Returns the value of attribute parents.
Instance Method Summary collapse
- #basename(ref_or_obj) ⇒ Object
- #category_and_name(ref_or_obj) ⇒ Object
- #dereference(ref_or_obj) ⇒ Object
-
#initialize(doc) ⇒ Helper
constructor
A new instance of Helper.
- #parameters(operation_object, empty_unless_local = false) ⇒ Object
- #parent(object) ⇒ Object
-
#store_parents(obj, parent = nil) ⇒ Object
Stores the nearest Hash for each Hash.
Constructor Details
#initialize(doc) ⇒ Helper
Returns a new instance of Helper.
30 31 32 33 34 |
# File 'lib/openapi/sourcetools/helper.rb', line 30 def initialize(doc) @doc = doc @parents = {}.compare_by_identity store_parents(@doc) end |
Instance Attribute Details
#doc ⇒ Object (readonly)
Returns the value of attribute doc.
13 14 15 |
# File 'lib/openapi/sourcetools/helper.rb', line 13 def doc @doc end |
#parent_parameters ⇒ Object
Returns the value of attribute parent_parameters.
14 15 16 |
# File 'lib/openapi/sourcetools/helper.rb', line 14 def parent_parameters @parent_parameters end |
#parents ⇒ Object (readonly)
Returns the value of attribute parents.
13 14 15 |
# File 'lib/openapi/sourcetools/helper.rb', line 13 def parents @parents end |
Instance Method Details
#basename(ref_or_obj) ⇒ Object
59 60 61 62 63 |
# File 'lib/openapi/sourcetools/helper.rb', line 59 def basename(ref_or_obj) cn = category_and_name(ref_or_obj) return nil if cn.nil? cn.last end |
#category_and_name(ref_or_obj) ⇒ Object
42 43 44 45 46 47 48 49 50 |
# File 'lib/openapi/sourcetools/helper.rb', line 42 def category_and_name(ref_or_obj) ref = ref_or_obj.is_a?(Hash) ? ref_or_obj['$ref'] : ref_or_obj return nil unless ref.is_a?(String) return nil unless ref.start_with?(Helper::COMPONENTS) idx = ref.index('/', Helper::COMPONENTS.size) return nil if idx.nil? category = ref[Helper::COMPONENTS.size...idx] [ category, ref[(idx + 1)...ref.size] ] end |
#dereference(ref_or_obj) ⇒ Object
52 53 54 55 56 57 |
# File 'lib/openapi/sourcetools/helper.rb', line 52 def dereference(ref_or_obj) cn = category_and_name(ref_or_obj) return nil if cn.nil? cs = @doc.dig('components', cn.first) || {} cs[cn.last] end |
#parameters(operation_object, empty_unless_local = false) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/openapi/sourcetools/helper.rb', line 65 def parameters(operation_object, empty_unless_local = false) return [] if empty_unless_local && !operation_object.key?('parameters') cps = @doc.dig('components', 'parameters') || {} uniqs = {} path_item_object = parent(operation_object) [path_item_object, operation_object].each do |p| p.fetch('parameters', []).each do |param| r = basename(param) r = cps[r] if r.is_a?(String) uniqs["#{r['name']}:#{r['in']}"] = param end end uniqs.keys.sort!.map { |k| uniqs[k] } end |
#parent(object) ⇒ Object
36 37 38 |
# File 'lib/openapi/sourcetools/helper.rb', line 36 def parent(object) @parents[object] end |
#store_parents(obj, parent = nil) ⇒ Object
Stores the nearest Hash for each Hash.
17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/openapi/sourcetools/helper.rb', line 17 def store_parents(obj, parent = nil) if obj.is_a?(Hash) @parents[obj] = parent obj.each_value do |v| store_parents(v, obj) end elsif obj.is_a?(Array) obj.each do |v| store_parents(v, parent) end end end |