Class: Apia::API

Inherits:
Object
  • Object
show all
Extended by:
Defineable
Defined in:
lib/apia/api.rb

Class Method Summary collapse

Methods included from Defineable

create, inspect, method_missing, name, respond_to_missing?

Class Method Details

.definitionApia::Definitions::API

Return the definition for this API



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

def definition
  @definition ||= Definitions::API.new(Helpers.class_name_to_id(name))
end

.objectsApia::ObjectSet

Return all objects which are referenced by the API. This list is used for the purposes of validating all objects and generating schemas.

Parameters:

  • include_apia_controller (Boolean)

    whether the schema/internal API should be included

Returns:



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/apia/api.rb', line 30

def objects
  set = ObjectSet.new([self])
  if definition.authenticator
    set.add_object(definition.authenticator)
  end

  definition.route_set.controllers.each do |con|
    set.add_object(con)
  end

  definition.route_set.endpoints.each do |endpoint|
    set.add_object(endpoint)
  end

  set
end

.schema(host:, namespace:) ⇒ Hash

Return the schema hash for this API

Parameters:

  • host (String)
  • namespace (String)

Returns:

  • (Hash)


65
66
67
68
69
70
71
72
73
74
# File 'lib/apia/api.rb', line 65

def schema(host:, namespace:)
  require 'apia/schema/controller'
  Schema::Controller.definition.endpoints[:schema].definition.fields.generate_hash({
    schema_version: 1,
    host: host,
    namespace: namespace,
    api: definition.id,
    objects: objects.map(&:definition).select(&:schema?)
  })
end

.test_endpoint(endpoint, controller: nil) ⇒ Apia::Response

Execute a request for a given controller & endpoint

Parameters:

Returns:



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/apia/api.rb', line 81

def test_endpoint(endpoint, controller: nil)
  if controller && endpoint.is_a?(Symbol) || endpoint.is_a?(String)
    endpoint_name = endpoint
    endpoint = controller.definition.endpoints[endpoint.to_sym]
    if endpoint.nil?
      raise Apia::StandardError, "Invalid endpoint name '#{endpoint_name}' for '#{controller.name}'"
    end
  end

  endpoint.test do |r|
    r.api = self
    r.controller = controller
    yield r if block_given?
  end
end

.validate_allApia::ManifestErrors

Validate all objects in the API and return details of any issues encountered



50
51
52
53
54
55
56
57
58
# File 'lib/apia/api.rb', line 50

def validate_all
  errors = ManifestErrors.new
  objects.each do |object|
    next unless object.respond_to?(:definition)

    object.definition.validate(errors)
  end
  errors
end