Class: ApiRegulator::Webhook

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(event_name, desc: nil, title: nil, tags: [], &block) ⇒ Webhook



5
6
7
8
9
10
11
12
13
14
# File 'lib/api_regulator/webhook.rb', line 5

def initialize(event_name, desc: nil, title: nil, tags: [], &block)
  @event_name = event_name
  @description = desc
  @title = title
  @tags = tags
  @params = []
  @responses = {}

  instance_eval(&block) if block_given?
end

Instance Attribute Details

#descriptionObject (readonly)

Returns the value of attribute description.



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

def description
  @description
end

#event_nameObject (readonly)

Returns the value of attribute event_name.



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

def event_name
  @event_name
end

#examplesObject (readonly)

Returns the value of attribute examples.



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

def examples
  @examples
end

#paramsObject (readonly)

Returns the value of attribute params.



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

def params
  @params
end

#responsesObject (readonly)

Returns the value of attribute responses.



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

def responses
  @responses
end

#tagsObject (readonly)

Returns the value of attribute tags.



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

def tags
  @tags
end

#titleObject (readonly)

Returns the value of attribute title.



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

def title
  @title
end

Instance Method Details

#example(name, value, default: false) ⇒ Object



42
43
44
45
46
# File 'lib/api_regulator/webhook.rb', line 42

def example(name, value, default: false)
  @examples ||= {}
  @examples[name] = { summary: "#{name} Example", value: value }
  @default_example = value if default
end

#param(name, type = nil, **options, &block) ⇒ Object



16
17
18
# File 'lib/api_regulator/webhook.rb', line 16

def param(name, type = nil, **options, &block)
  @params << Param.new(name, type, **options, &block)
end

#ref(ref_name, except: [], only: []) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/api_regulator/webhook.rb', line 20

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|
    @params << shared_param
  end
end

#response(status_code, description, &block) ⇒ Object



38
39
40
# File 'lib/api_regulator/webhook.rb', line 38

def response(status_code, description, &block)
  @responses[status_code] = Param.new(:root, :object, desc: description, &block)
end