Class: ApiRegulator::Param

Inherits:
Object
  • Object
show all
Defined in:
lib/api_regulator/param.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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, **options, &block)
  @name = name
  @type = type&.to_sym || (block_given? ? :object : :string)

  @item_type = options.delete(:item_type)
  @desc = options.delete(:desc) || ""
  @location = (options.delete(:location) || :body).to_sym
  @api = options.delete(:api)
  @allow_arbitrary_keys = options.delete(:allow_arbitrary_keys) || false
  @versions = Array(options.delete(:versions)).map(&:to_sym)
  @examples = options.delete(:examples) || {}

  @children = []
  @options = options

  instance_eval(&block) if block_given?
end

Instance Attribute Details

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

#apiObject (readonly)

Returns the value of attribute api.



3
4
5
# File 'lib/api_regulator/param.rb', line 3

def api
  @api
end

#childrenObject (readonly)

Returns the value of attribute children.



3
4
5
# File 'lib/api_regulator/param.rb', line 3

def children
  @children
end

#descObject (readonly)

Returns the value of attribute desc.



3
4
5
# File 'lib/api_regulator/param.rb', line 3

def desc
  @desc
end

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

#locationObject (readonly)

Returns the value of attribute location.



3
4
5
# File 'lib/api_regulator/param.rb', line 3

def location
  @location
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/api_regulator/param.rb', line 3

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



3
4
5
# File 'lib/api_regulator/param.rb', line 3

def options
  @options
end

#typeObject (readonly)

Returns the value of attribute type.



3
4
5
# File 'lib/api_regulator/param.rb', line 3

def type
  @type
end

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

Returns:

  • (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 options[:ref]
    shared_schema = ApiRegulator.shared_schema(options[: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 options[:ref]
    shared_schema = ApiRegulator.shared_schema(options[: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_nameObject



46
47
48
# File 'lib/api_regulator/param.rb', line 46

def api_action_name
  api&.action_name
end

#array?Boolean

Returns:

  • (Boolean)


144
145
146
# File 'lib/api_regulator/param.rb', line 144

def array?
  type.to_sym == :array
end

#body?Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/api_regulator/param.rb', line 124

def body?
  location == :body
end

#examplesObject



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

Returns:

  • (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

Returns:

  • (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, **options, &block)
  options[:api] ||= api
  @children << Param.new(name, type, **options, &block)
end

#parameter?Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/api_regulator/param.rb', line 136

def parameter?
  [:path, :query].include?(location)
end

#path?Boolean

Returns:

  • (Boolean)


132
133
134
# File 'lib/api_regulator/param.rb', line 132

def path?
  location == :path
end

#query?Boolean

Returns:

  • (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

Returns:

  • (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_typeObject



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