Class: Helper
- Inherits:
-
Object
- Object
- Helper
- Defined in:
- lib/helper.rb
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.
27 28 29 30 31 |
# File 'lib/helper.rb', line 27 def initialize(doc) @doc = doc @parents = {} store_parents(@doc) end |
Instance Attribute Details
#doc ⇒ Object (readonly)
Returns the value of attribute doc.
10 11 12 |
# File 'lib/helper.rb', line 10 def doc @doc end |
#parent_parameters ⇒ Object
Returns the value of attribute parent_parameters.
11 12 13 |
# File 'lib/helper.rb', line 11 def parent_parameters @parent_parameters end |
#parents ⇒ Object (readonly)
Returns the value of attribute parents.
10 11 12 |
# File 'lib/helper.rb', line 10 def parents @parents end |
Instance Method Details
#basename(ref_or_obj) ⇒ Object
56 57 58 59 60 |
# File 'lib/helper.rb', line 56 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
39 40 41 42 43 44 45 46 47 |
# File 'lib/helper.rb', line 39 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
49 50 51 52 53 54 |
# File 'lib/helper.rb', line 49 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
62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/helper.rb', line 62 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
33 34 35 |
# File 'lib/helper.rb', line 33 def parent(object) @parents[object.object_id] end |
#store_parents(obj, parent = nil) ⇒ Object
Stores the nearest Hash for each Hash.
14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/helper.rb', line 14 def store_parents(obj, parent = nil) if obj.is_a?(Hash) @parents[obj.object_id] = parent obj.each do |k, v| store_parents(v, obj) end elsif obj.is_a?(Array) obj.each do |v| store_parents(v, parent) end end end |