Class: Apia::DSLs::Endpoint

Inherits:
Apia::DSL show all
Includes:
Concerns::HasFields
Defined in:
lib/apia/dsls/endpoint.rb

Instance Method Summary collapse

Methods inherited from Apia::DSL

#description, #initialize, #name, #no_schema

Constructor Details

This class inherits a constructor from Apia::DSL

Instance Method Details

#action(&block) ⇒ Object



35
36
37
# File 'lib/apia/dsls/endpoint.rb', line 35

def action(&block)
  @definition.action = block
end

#argument(*args, **kwargs, &block) ⇒ Object



31
32
33
# File 'lib/apia/dsls/endpoint.rb', line 31

def argument(*args, **kwargs, &block)
  @definition.argument_set.argument(*args, **kwargs, &block)
end

#authenticator(klass = nil, &block) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/apia/dsls/endpoint.rb', line 13

def authenticator(klass = nil, &block)
  if block_given?
    id = "#{@definition.id}/#{Helpers.camelize(klass) || 'Authenticator'}"
    klass = Apia::Authenticator.create(id, &block)
  end

  @definition.authenticator = klass
end

#field(name, *args, type: nil, **options, &block) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/apia/dsls/endpoint.rb', line 43

def field(name, *args, type: nil, **options, &block)
  if pagination_options = options.delete(:paginate)

    unless @definition.paginated_field.nil?
      raise Apia::RuntimeError, 'Cannot define more than one paginated field per endpoint'
    end

    pagination_options = {} if pagination_options == true
    @definition.paginated_field = name

    argument :page, type: Scalars::Integer, default: 1 do
      validation(:greater_than_zero) { |o| o.positive? }
    end

    argument :per_page, type: Scalars::Integer, default: 30 do
      validation(:greater_than_zero) { |o| o.positive? }
      validation(:less_than_or_equal_to_100) { |o| o <= (pagination_options[:maximum_per_page]&.to_i || 200) }
    end

    field :pagination, type: PaginationObject
  end
  super(name, *args, type: type, **options, &block)
end

#http_status(status) ⇒ Object



39
40
41
# File 'lib/apia/dsls/endpoint.rb', line 39

def http_status(status)
  @definition.http_status = status
end

#potential_error(klass, &block) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/apia/dsls/endpoint.rb', line 22

def potential_error(klass, &block)
  if block_given? && klass.is_a?(String)
    id = "#{@definition.id}/#{Helpers.camelize(klass)}"
    klass = Apia::Error.create(id, &block)
  end

  @definition.potential_errors << klass
end

#scope(name) ⇒ Object



71
72
73
74
75
# File 'lib/apia/dsls/endpoint.rb', line 71

def scope(name)
  return if @definition.scopes.include?(name.to_s)

  @definition.scopes << name.to_s
end

#scopes(*names) ⇒ Object



67
68
69
# File 'lib/apia/dsls/endpoint.rb', line 67

def scopes(*names)
  names.each { |name| scope(name) }
end