Class: OpenApi::Utils
- Inherits:
-
Object
- Object
- OpenApi::Utils
- Defined in:
- lib/open-api/utils.rb
Class Method Summary collapse
- .camelize_key(key) ⇒ Object
- .camelize_metadata(metadata, opts = {}) ⇒ Object
- .controller_class_hierarchy(controller_class) ⇒ Object
- .merge_hash(hash, merge_hash, opts = {}) ⇒ Object
- .metadata_by_string_or_regexp(metadata_map, string_or_regexp, metadata, opts = {}) ⇒ Object
- .open_api_type_and_format(type_name) ⇒ Object
- .verify_and_merge_hash(hash, merge_hash, hash_desc, opts = {}) ⇒ Object
Class Method Details
.camelize_key(key) ⇒ Object
59 60 61 |
# File 'lib/open-api/utils.rb', line 59 def camelize_key(key) key.to_s.camelize(:lower).to_sym end |
.camelize_metadata(metadata, opts = {}) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/open-api/utils.rb', line 34 def (, opts = {}) if (start_depth = opts[:start_depth].to_i) > 0 start_depth -= 1 if start_depth > 0 opts = opts.merge(start_depth: start_depth) else (opts = opts.dup).delete(:start_depth) end end if (end_depth = opts[:end_depth]).present? end_depth -= 1 return if end_depth <= 0 opts = opts.merge(end_depth: end_depth) end if .is_a?(Hash) Hash[(.map do |k, v| [start_depth > 0 ? k : camelize_key(k), (v, opts)] end)] elsif .is_a?(Array) .map { |v| (v, opts) } else end end |
.controller_class_hierarchy(controller_class) ⇒ Object
4 5 6 7 8 9 10 11 12 |
# File 'lib/open-api/utils.rb', line 4 def controller_class_hierarchy(controller_class) controller_class_hierarchy = [controller_class] loop do controller_class = controller_class.superclass break if controller_class.nil? || !controller_class.respond_to?(:open_api_controller) controller_class_hierarchy << controller_class end controller_class_hierarchy end |
.merge_hash(hash, merge_hash, opts = {}) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/open-api/utils.rb', line 20 def merge_hash(hash, merge_hash, opts = {}) hash ||= {} return hash if merge_hash.nil? fail 'Expected Hash!' unless merge_hash.is_a?(Hash) merge_hash.each do |key, value| if hash.include?(key) merge_hash_entry(hash, key, value, opts) elsif !value.nil? hash[key] = value end end hash end |
.metadata_by_string_or_regexp(metadata_map, string_or_regexp, metadata, opts = {}) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/open-api/utils.rb', line 63 def (, string_or_regexp, , opts = {}) arg_ref = "#{opts[:arg_name]} argument".strip if .blank? fail "Valid #{arg_ref} required!" unless string_or_regexp.respond_to?(:to_sym) = {} ( || {}).each do |key, | if key.is_a?(Regexp) next unless string_or_regexp =~ key else next unless string_or_regexp.casecmp(key) == 0 end OpenApi::Endpoints.(, .deep_dup) end return end unless string_or_regexp.respond_to?(:to_sym) || string_or_regexp.is_a?(Regexp) fail "Valid #{arg_ref} required!" end fail 'Expected Hash metadata_map argument!' unless .is_a?(Hash) fail 'Expected Hash metadata argument!' unless .is_a?(Hash) string_or_regexp = string_or_regexp.to_s unless string_or_regexp.is_a?(Regexp) = ([string_or_regexp] ||= {}) OpenApi::Endpoints.(, ) nil end |
.open_api_type_and_format(type_name) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/open-api/utils.rb', line 89 def open_api_type_and_format(type_name) case type_name.to_s.downcase.to_sym when :integer then [:integer, :int32] when :long then [:integer, :int] when :float then [:number, :float] when :double then [:number, :double] when :string then [:string, nil] when :byte then [:string, :byte] when :binary then [:string, :binary] when :boolean then [:boolean, nil] when :date then [:string, :date] when :datetime then [:string, :'date-time'] when :password then [:string, :password] else [nil, nil] end end |
.verify_and_merge_hash(hash, merge_hash, hash_desc, opts = {}) ⇒ Object
14 15 16 17 18 |
# File 'lib/open-api/utils.rb', line 14 def verify_and_merge_hash(hash, merge_hash, hash_desc, opts = {}) return (hash || {}) if merge_hash.nil? fail "Expected #{hash_desc} in the form of a Hash!" unless merge_hash.is_a?(Hash) merge_hash(hash, merge_hash, opts) end |