Class: ApiRegulator::Param
- Inherits:
-
Object
- Object
- ApiRegulator::Param
- Defined in:
- lib/api_regulator/param.rb
Instance Attribute Summary collapse
-
#allow_arbitrary_keys ⇒ Object
readonly
Returns the value of attribute allow_arbitrary_keys.
-
#api ⇒ Object
readonly
Returns the value of attribute api.
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#desc ⇒ Object
readonly
Returns the value of attribute desc.
-
#item_type ⇒ Object
readonly
Returns the value of attribute item_type.
-
#location ⇒ Object
readonly
Returns the value of attribute location.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#versions ⇒ Object
readonly
Returns the value of attribute versions.
Instance Method Summary collapse
- #allow_nil? ⇒ Boolean
- #allowed_arbitrary_keys(parent_key = nil) ⇒ Object
- #allowed_keys(parent_key = nil) ⇒ Object
- #api_action_name ⇒ Object
- #array? ⇒ Boolean
- #body? ⇒ Boolean
- #examples ⇒ Object
- #for_version?(version) ⇒ Boolean
-
#initialize(name, type = nil, **options, &block) ⇒ Param
constructor
A new instance of Param.
- #object? ⇒ Boolean
- #param(name, type = nil, **options, &block) ⇒ Object
- #parameter? ⇒ Boolean
- #path? ⇒ Boolean
- #query? ⇒ Boolean
- #ref(ref_name, except: [], only: []) ⇒ Object
- #required?(context = api_action_name) ⇒ Boolean
- #schema_type ⇒ Object
Constructor Details
#initialize(name, type = nil, **options, &block) ⇒ Param
Returns a new instance of Param.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/api_regulator/param.rb', line 5 def initialize(name, type = nil, **, &block) @name = name @type = type&.to_sym || (block_given? ? :object : :string) @item_type = .delete(:item_type) @desc = .delete(:desc) || "" @location = (.delete(:location) || :body).to_sym @api = .delete(:api) @allow_arbitrary_keys = .delete(:allow_arbitrary_keys) || false @versions = Array(.delete(:versions)).map(&:to_sym) @examples = .delete(:examples) || {} @children = [] @options = instance_eval(&block) if block_given? end |
Instance Attribute Details
#allow_arbitrary_keys ⇒ Object (readonly)
Returns the value of attribute allow_arbitrary_keys.
3 4 5 |
# File 'lib/api_regulator/param.rb', line 3 def allow_arbitrary_keys @allow_arbitrary_keys end |
#api ⇒ Object (readonly)
Returns the value of attribute api.
3 4 5 |
# File 'lib/api_regulator/param.rb', line 3 def api @api end |
#children ⇒ Object (readonly)
Returns the value of attribute children.
3 4 5 |
# File 'lib/api_regulator/param.rb', line 3 def children @children end |
#desc ⇒ Object (readonly)
Returns the value of attribute desc.
3 4 5 |
# File 'lib/api_regulator/param.rb', line 3 def desc @desc end |
#item_type ⇒ Object (readonly)
Returns the value of attribute item_type.
3 4 5 |
# File 'lib/api_regulator/param.rb', line 3 def item_type @item_type end |
#location ⇒ Object (readonly)
Returns the value of attribute location.
3 4 5 |
# File 'lib/api_regulator/param.rb', line 3 def location @location end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/api_regulator/param.rb', line 3 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
3 4 5 |
# File 'lib/api_regulator/param.rb', line 3 def @options end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
3 4 5 |
# File 'lib/api_regulator/param.rb', line 3 def type @type end |
#versions ⇒ Object (readonly)
Returns the value of attribute versions.
3 4 5 |
# File 'lib/api_regulator/param.rb', line 3 def versions @versions end |
Instance Method Details
#allow_nil? ⇒ Boolean
70 71 72 73 74 |
# File 'lib/api_regulator/param.rb', line 70 def allow_nil? @options.any? do |_, opts| opts.is_a?(Hash) && opts[:allow_nil] end end |
#allowed_arbitrary_keys(parent_key = nil) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/api_regulator/param.rb', line 97 def allowed_arbitrary_keys(parent_key = nil) key = parent_key.present? ? "#{parent_key}.#{name}" : name.to_s key += "[]" if array? if [:ref] shared_schema = ApiRegulator.shared_schema([:ref]) shared_schema.params.flat_map do |param| param.allowed_arbitrary_keys end.compact elsif children.any? key = nil if key == "root" child_keys = children.flat_map do |child| child.allowed_arbitrary_keys(key) end.compact child_keys << key if allow_nil? && allow_arbitrary_keys child_keys else key if allow_arbitrary_keys end end |
#allowed_keys(parent_key = nil) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/api_regulator/param.rb', line 76 def allowed_keys(parent_key = nil) key = parent_key.present? ? "#{parent_key}.#{name}" : name.to_s key += "[]" if array? if [:ref] shared_schema = ApiRegulator.shared_schema([:ref]) shared_schema.params.flat_map do |param| param.allowed_keys end elsif children.any? key = nil if key == "root" child_keys = children.flat_map do |child| child.allowed_keys(key) end child_keys << key if allow_nil? child_keys else key end end |
#api_action_name ⇒ Object
46 47 48 |
# File 'lib/api_regulator/param.rb', line 46 def api_action_name api&.action_name end |
#array? ⇒ Boolean
144 145 146 |
# File 'lib/api_regulator/param.rb', line 144 def array? type.to_sym == :array end |
#body? ⇒ Boolean
124 125 126 |
# File 'lib/api_regulator/param.rb', line 124 def body? location == :body end |
#examples ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/api_regulator/param.rb', line 156 def examples return nil if @examples.blank? @examples.each_with_object({}) do |(key, value), formatted_examples| formatted_key = key.to_s.parameterize.underscore formatted_examples[formatted_key] = { summary: key.to_s, value: value } end end |
#for_version?(version) ⇒ Boolean
118 119 120 121 122 |
# File 'lib/api_regulator/param.rb', line 118 def for_version?(version) return true unless versions.present? && version.present? versions.include?(version.to_sym) end |
#object? ⇒ Boolean
140 141 142 |
# File 'lib/api_regulator/param.rb', line 140 def object? type.to_sym == :object end |
#param(name, type = nil, **options, &block) ⇒ Object
23 24 25 26 |
# File 'lib/api_regulator/param.rb', line 23 def param(name, type = nil, **, &block) [:api] ||= api @children << Param.new(name, type, **, &block) end |
#parameter? ⇒ Boolean
136 137 138 |
# File 'lib/api_regulator/param.rb', line 136 def parameter? [:path, :query].include?(location) end |
#path? ⇒ Boolean
132 133 134 |
# File 'lib/api_regulator/param.rb', line 132 def path? location == :path end |
#query? ⇒ Boolean
128 129 130 |
# File 'lib/api_regulator/param.rb', line 128 def query? location == :query end |
#ref(ref_name, except: [], only: []) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/api_regulator/param.rb', line 28 def ref(ref_name, except: [], only: []) shared_schema = ApiRegulator.shared_schema(ref_name) raise "Shared schema #{ref_name} not found" unless shared_schema # Filter parameters based on `only` or `except` options filtered_params = shared_schema.params if only.any? filtered_params = filtered_params.select { |param| only.include?(param.name) } elsif except.any? filtered_params = filtered_params.reject { |param| except.include?(param.name) } end filtered_params.each do |shared_param| @children << shared_param end end |
#required?(context = api_action_name) ⇒ Boolean
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/api_regulator/param.rb', line 50 def required?(context = api_action_name) return false if @options[:presence].nil? if @options[:presence].is_a?(Hash) if @options[:presence].key?(:allow_nil) !@options[:presence][:allow_nil] elsif context.nil? true # No context provided elsif @options[:presence][:required_on].present? Array(@options[:presence][:required_on]).map(&:to_sym).include?(context.to_sym) elsif @options[:presence][:required_except_on].present? Array(@options[:presence][:required_except_on]).map(&:to_sym).exclude?(context.to_sym) else true # TODO: should we try to handle :if or :unless procs? end else !!@options[:presence] end end |
#schema_type ⇒ Object
148 149 150 151 152 153 154 |
# File 'lib/api_regulator/param.rb', line 148 def schema_type if allow_nil? [type.to_s.downcase, "null"] else type.to_s.downcase end end |