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



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/apia/dsls/endpoint.rb', line 47

def field(name, *args, type: nil, **options, &block)
  if @definition.fields_overriden?
    raise Apia::StandardError, 'Cannot add fields to an endpoint that has a separate fieldset'
  end

  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_one_hundred) { |o| o <= (pagination_options[:maximum_per_page]&.to_i || 200) }
    end

    field :pagination, type: PaginationObject
  end
  super
end

#fields(fieldset) ⇒ Object



75
76
77
# File 'lib/apia/dsls/endpoint.rb', line 75

def fields(fieldset)
  @definition.fields = fieldset
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

#response_type(type) ⇒ Object



43
44
45
# File 'lib/apia/dsls/endpoint.rb', line 43

def response_type(type)
  @definition.response_type = type
end

#scope(name) ⇒ Object



83
84
85
86
87
# File 'lib/apia/dsls/endpoint.rb', line 83

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

  @definition.scopes << name.to_s
end

#scopes(*names) ⇒ Object



79
80
81
# File 'lib/apia/dsls/endpoint.rb', line 79

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