Module: OpenAPIParser::Parseable

Included in:
Schemas::Base
Defined in:
lib/openapi_parser/concerns/parseable.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



2
3
4
# File 'lib/openapi_parser/concerns/parseable.rb', line 2

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#_add_child_object(object) ⇒ Object



68
69
70
71
# File 'lib/openapi_parser/concerns/parseable.rb', line 68

def _add_child_object(object)
  return unless object.is_a?(OpenAPIParser::Parseable)
  _openapi_all_child_objects[object.object_reference] = object
end

#_openapi_all_child_objectsObject



64
65
66
# File 'lib/openapi_parser/concerns/parseable.rb', line 64

def _openapi_all_child_objects
  @_openapi_all_child_objects ||= {}
end

#_update_child_object(old, new) ⇒ Object



73
74
75
# File 'lib/openapi_parser/concerns/parseable.rb', line 73

def _update_child_object(old, new)
  _openapi_all_child_objects[old.object_reference] = new
end

#build_object_reference(names) ⇒ String

Returns:

  • (String)


232
233
234
235
236
237
# File 'lib/openapi_parser/concerns/parseable.rb', line 232

def build_object_reference(names)
  names = [names] unless names.is_a?(Array)
  ref = names.map{ |n| escape_reference(n)}.join('/')

  "#{object_reference}/#{ref}"
end

#build_openapi_object(ref_names, schema, klass, allow_reference, allow_data_type) ⇒ Object

return nil, klass, Reference



206
207
208
209
210
211
212
213
214
215
216
# File 'lib/openapi_parser/concerns/parseable.rb', line 206

def build_openapi_object(ref_names, schema, klass, allow_reference, allow_data_type)
  return nil unless schema

  if allow_data_type && !check_object_schema?(schema)
    schema
  elsif allow_reference && check_reference_schema?(schema)
    OpenAPIParser::Schemas::Reference.new(build_object_reference(ref_names), self, self.root, schema)
  else
    klass.new(build_object_reference(ref_names), self, self.root, schema)
  end
end

#check_object_schema?(check_schema) ⇒ Boolean

Returns:

  • (Boolean)


223
224
225
# File 'lib/openapi_parser/concerns/parseable.rb', line 223

def check_object_schema?(check_schema)
  check_schema.is_a?(Hash)
end

#check_reference_schema?(check_schema) ⇒ Boolean

Returns Boolean.

Returns:

  • (Boolean)

    Boolean



219
220
221
# File 'lib/openapi_parser/concerns/parseable.rb', line 219

def check_reference_schema?(check_schema)
  check_object_schema?(check_schema) && !check_schema['$ref'].nil?
end

#child_from_reference(reference) ⇒ Object



60
61
62
# File 'lib/openapi_parser/concerns/parseable.rb', line 60

def child_from_reference(reference)
  _openapi_all_child_objects[reference]
end

#create_attr_hash_body_objects(values) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/openapi_parser/concerns/parseable.rb', line 126

def create_attr_hash_body_objects(values)
  return unless values

  values.each do |name, options|
    klass = options[:klass]
    allow_reference = options[:reference] || false
    allow_data_type = options[:allow_data_type]
    reject_keys = options[:reject_keys]

    create_hash_body_objects(name, raw_schema, klass, allow_reference, allow_data_type, reject_keys)
  end
end

#create_attr_hash_object(variable_name, hash_schema, ref_name_base, klass, allow_reference, allow_data_type) ⇒ Object



175
176
177
178
179
180
181
182
183
184
# File 'lib/openapi_parser/concerns/parseable.rb', line 175

def create_attr_hash_object(variable_name, hash_schema, ref_name_base, klass, allow_reference, allow_data_type)
  data = if hash_schema
           hash_schema.map { |key, s| [key, build_openapi_object([ref_name_base, key], s, klass, allow_reference, allow_data_type) ]}.to_h
         else
           nil
         end

  create_variable(variable_name, data)
  data.values.each { |obj| _add_child_object(obj) } if data
end

#create_attr_hash_objects(settings) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/openapi_parser/concerns/parseable.rb', line 99

def create_attr_hash_objects(settings)
  return unless settings

  settings.each do |variable_name, options|
    ref_name_base = options[:schema_key] ? options[:schema_key] : variable_name
    klass = options[:klass]
    allow_reference = options[:reference] || false
    allow_data_type = options[:allow_data_type]
    
    create_attr_hash_object(variable_name, raw_schema[ref_name_base .to_s], ref_name_base, klass, allow_reference, allow_data_type)
  end
end

#create_attr_list_object(variable_name, array_schema, ref_name_base, klass, allow_reference, allow_data_type) ⇒ Object



186
187
188
189
190
191
192
193
194
195
# File 'lib/openapi_parser/concerns/parseable.rb', line 186

def create_attr_list_object(variable_name, array_schema, ref_name_base, klass, allow_reference, allow_data_type)
  data = if array_schema
           array_schema.map.with_index { |s, idx| build_openapi_object([ref_name_base, idx], s, klass, allow_reference, allow_data_type) }
         else
           nil
         end

  create_variable(variable_name, data)
  data.each { |obj| _add_child_object(obj) } if data
end

#create_attr_list_objects(settings) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/openapi_parser/concerns/parseable.rb', line 86

def create_attr_list_objects(settings)
  return unless settings

  settings.each do |variable_name, options|
    ref_name_base = options[:schema_key] ? options[:schema_key] : variable_name
    klass = options[:klass]
    allow_reference = options[:reference] || false
    allow_data_type = options[:allow_data_type]
    
    create_attr_list_object(variable_name, raw_schema[ref_name_base.to_s], ref_name_base, klass, allow_reference, allow_data_type)
  end
end

#create_attr_object(variable_name, ref_name, schema, klass, allow_reference, allow_data_type) ⇒ OpenAPIParser::Schemas::Base



169
170
171
172
173
# File 'lib/openapi_parser/concerns/parseable.rb', line 169

def create_attr_object(variable_name, ref_name, schema, klass, allow_reference, allow_data_type)
  data = build_openapi_object(ref_name, schema, klass, allow_reference, allow_data_type)
  create_variable(variable_name, data)
  data
end

#create_attr_objects(values) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/openapi_parser/concerns/parseable.rb', line 112

def create_attr_objects(values)
  return unless values

  values.each do |name, options|
    key = options[:schema_key] ? options[:schema_key] : name
    klass = options[:klass]
    allow_reference = options[:reference] || false
    allow_data_type = options[:allow_data_type]
    
    obj = create_attr_object(name, key, raw_schema[key.to_s], klass, allow_reference, allow_data_type)
    _add_child_object(obj)
  end
end

#create_attr_values(values) ⇒ Object



139
140
141
142
143
144
145
146
147
# File 'lib/openapi_parser/concerns/parseable.rb', line 139

def create_attr_values(values)
  return unless values

  values.each do |variable_name, options|
    key = options[:schema_key] ? options[:schema_key] : variable_name

    create_variable(variable_name, raw_schema[key.to_s])
  end
end

#create_hash_body_objects(name, schema, klass, allow_reference, allow_data_type, reject_keys) ⇒ Object

for responses and paths object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/openapi_parser/concerns/parseable.rb', line 151

def create_hash_body_objects(name, schema, klass, allow_reference, allow_data_type, reject_keys)
  unless schema
    create_variable(name, nil)
    return
  end

  objects = schema.reject{|k, _| reject_keys.include?(k)}.map do |name, child_schema|
    [
        name.to_s, # support string key only in OpenAPI3
        build_openapi_object(escape_reference(name), child_schema, klass, allow_reference, allow_data_type)
    ]
  end.to_h

  create_variable(name, objects)
  objects.values.each { |o| _add_child_object(o) }
end

#create_variable(variable_name, data) ⇒ Object

create instance variable @variable_name using data

Parameters:

  • variable_name (String)
  • data (Object)


201
202
203
# File 'lib/openapi_parser/concerns/parseable.rb', line 201

def create_variable(variable_name, data)
  instance_variable_set("@#{variable_name}", data)
end

#escape_reference(str) ⇒ Object



227
228
229
# File 'lib/openapi_parser/concerns/parseable.rb', line 227

def escape_reference(str)
  str.to_s.gsub('/', '~1')
end

#load_dataObject



77
78
79
80
81
82
83
84
# File 'lib/openapi_parser/concerns/parseable.rb', line 77

def load_data
  create_attr_values(self.class._openapi_attr_values)

  create_attr_objects(self.class._openapi_attr_objects)
  create_attr_list_objects(self.class._openapi_attr_list_objects)
  create_attr_hash_objects(self.class._openapi_attr_hash_objects)
  create_attr_hash_body_objects(self.class._openapi_attr_hash_body_objects)
end