Module: Eazypi::Api::ClassMethods

Defined in:
lib/eazypi/api.rb

Overview

ClassMethods that can be used in your API definition Todo: Split this up

Instance Method Summary collapse

Instance Method Details

#componentsObject



59
60
61
# File 'lib/eazypi/api.rb', line 59

def components
  @components ||= Components.new
end

#info(&block) ⇒ Object

rubocop:todo Metrics/ModuleLength



13
14
15
16
17
18
19
# File 'lib/eazypi/api.rb', line 13

def info(&block)
  @info ||= Info.new

  @info.load(&block) if block_given?

  @info
end

#load_controller(controller_klass) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/eazypi/api.rb', line 47

def load_controller(controller_klass)
  controller_klass.operations.each do |operation|
    paths[operation.path] ||= PathItem.new

    paths[operation.path].send(operation.method, operation)

    operation.collect_components(
      schemas: method(:reference_schema)
    )
  end
end

#mount(router) ⇒ Object

rubocop:todo Metrics/AbcSize, Metrics/MethodLength



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/eazypi/api.rb', line 87

def mount(router) # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
  prepare_controller_class

  router.get "openapi.:format", to: "#{ancestors[0].name.underscore}/eazypi#show"

  paths.each do |path_name, path_item|
    %i[get post patch put delete].each do |http_method|
      operation = path_item.send(http_method)
      next unless operation

      controller_router_name = operation.controller_klass.name[...-"Controller".length]
      router.send(
        http_method,
        path_name,
        to: "#{controller_router_name.underscore}##{operation.controller_method}"
      )
    end
  end
end

#pathsObject



21
22
23
# File 'lib/eazypi/api.rb', line 21

def paths
  @paths ||= {}
end

#security(name, scopes = []) ⇒ Object



41
42
43
44
45
# File 'lib/eazypi/api.rb', line 41

def security(name, scopes = [])
  @security ||= []

  @security.append(SecurityRequirement.new(name, scopes))
end

#security_scheme(name, &block) ⇒ Object



34
35
36
37
38
39
# File 'lib/eazypi/api.rb', line 34

def security_scheme(name, &block)
  scheme = SecurityScheme.new
  scheme.load(&block)

  components.add_security_scheme(name, scheme)
end

#serversObject



63
64
65
# File 'lib/eazypi/api.rb', line 63

def servers
  []
end

#tag(&block) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/eazypi/api.rb', line 25

def tag(&block)
  @tags ||= []

  tag = Tag.new
  tag.load(&block)

  @tags.append(tag)
end

#to_openapi_specObject



67
68
69
# File 'lib/eazypi/api.rb', line 67

def to_openapi_spec
  to_openapi_spec_with_servers(servers)
end

#to_openapi_spec_with_servers(override_servers) ⇒ Object

rubocop:disable Metrics/MethodLength,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/eazypi/api.rb', line 71

def to_openapi_spec_with_servers(override_servers) # rubocop:disable Metrics/MethodLength,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
  actual_servers = override_servers || servers

  {
    "openapi" => "3.0.3", # Future improvement allow different version
    "info" => info.to_openapi_spec,
    "paths" => paths.transform_keys do |path|
                 Operation.normalized_path(path)
               end.transform_values(&:to_openapi_spec),
    "servers" => actual_servers.map(&:to_openapi_spec),
    "components" => components.to_openapi_spec,
    "security" => @security&.map(&:to_openapi_spec),
    "tags" => @tags&.map(&:to_openapi_spec)
  }.compact
end