Class: OpenAPISourceTools::Helper

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#docObject (readonly)

Returns the value of attribute doc.



13
14
15
# File 'lib/openapi/sourcetools/helper.rb', line 13

def doc
  @doc
end

#parent_parametersObject

Returns the value of attribute parent_parameters.



14
15
16
# File 'lib/openapi/sourcetools/helper.rb', line 14

def parent_parameters
  @parent_parameters
end

#parentsObject (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

#response_code_condition(code, var: 'code', op_and: '&&', op_lte: '<=', op_eq: '==') ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/openapi/sourcetools/helper.rb', line 100

def response_code_condition(code, var: 'code', op_and: '&&', op_lte: '<=', op_eq: '==')
  low = []
  high = []
  code.downcase.each_char do |c|
    if c == 'x'
      low.push('0')
      high.push('9')
    else
      low.push(c)
      high.push(c)
    end
  end
  low = low.join
  high = high.join
  if low == high
    "#{var} #{op_eq} #{low}"
  else
    "(#{low} #{op_lte} #{var}) #{op_and} (#{var} #{op_lte} #{high})"
  end
end

#response_codes(responses_object) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/openapi/sourcetools/helper.rb', line 80

def response_codes(responses_object)
  responses_object.keys.sort! do |a, b|
    ad = a.downcase
    bd = b.downcase
    if ad == 'default'
      1
    elsif bd == 'default'
      -1
    else
      ax = ad.end_with?('x')
      bx = bd.end_with?('x')
      if ax && bx || !ax && !bx
        a <=> b # Both numbers or patterns.
      else
        ax ? 1 : -1
      end
    end
  end
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