Class: Apiwork::Introspection::API

Inherits:
Object
  • Object
show all
Defined in:
lib/apiwork/introspection/api.rb,
lib/apiwork/introspection/api/info.rb,
lib/apiwork/introspection/api/resource.rb,
lib/apiwork/introspection/api/info/server.rb,
lib/apiwork/introspection/api/info/contact.rb,
lib/apiwork/introspection/api/info/license.rb

Overview

Facade for introspected API data.

Entry point for accessing all API data. Access resources via #resources, types via #types, enums via #enums.

Examples:

api = Apiwork::API.introspect('/api/v1', locale: :fr)

api.info.title # => "Mon API"
api.types[:address].description # => "Type d'adresse"
api.enums[:status].values # => ["draft", "published"]

api.resources.each_value do |resource|
  resource.actions.each_value do |action|
    # ...
  end
end

Defined Under Namespace

Classes: Info, Resource

Instance Method Summary collapse

Constructor Details

#initialize(dump) ⇒ API

Returns a new instance of API.



24
25
26
# File 'lib/apiwork/introspection/api.rb', line 24

def initialize(dump)
  @dump = dump
end

Instance Method Details

#base_pathString?

The base path for this API.

Returns:

  • (String, nil)


32
33
34
# File 'lib/apiwork/introspection/api.rb', line 32

def base_path
  @dump[:base_path]
end

#enumsHash{Symbol => Enum}

The enums for this API.

Returns:

  • (Hash{Symbol => Enum})


72
73
74
# File 'lib/apiwork/introspection/api.rb', line 72

def enums
  @enums ||= @dump[:enums].transform_values { |dump| Enum.new(dump) }
end

#error_codesHash{Symbol => ErrorCode}

The error codes for this API.

Returns:



88
89
90
# File 'lib/apiwork/introspection/api.rb', line 88

def error_codes
  @error_codes ||= @dump[:error_codes].transform_values { |dump| ErrorCode.new(dump) }
end

#fingerprintString

The fingerprint for this API.

Returns:

  • (String)


40
41
42
# File 'lib/apiwork/introspection/api.rb', line 40

def fingerprint
  @dump[:fingerprint]
end

#infoAPI::Info?

The info for this API.

Returns:



48
49
50
# File 'lib/apiwork/introspection/api.rb', line 48

def info
  @info ||= @dump[:info] ? Info.new(@dump[:info]) : nil
end

#localesArray<Symbol>

The supported locales for this API.

Returns:

  • (Array<Symbol>)


80
81
82
# File 'lib/apiwork/introspection/api.rb', line 80

def locales
  @dump[:locales]
end

#resourcesHash{Symbol => API::Resource}

The resources for this API.

Returns:



56
57
58
# File 'lib/apiwork/introspection/api.rb', line 56

def resources
  @resources ||= @dump[:resources].transform_values { |dump| Resource.new(dump) }
end

#to_hHash

Converts this API to a hash.

Returns:

  • (Hash)


96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/apiwork/introspection/api.rb', line 96

def to_h
  {
    base_path:,
    fingerprint:,
    locales:,
    enums: enums.transform_values(&:to_h),
    error_codes: error_codes.transform_values(&:to_h),
    info: info&.to_h,
    resources: resources.transform_values(&:to_h),
    types: types.transform_values(&:to_h),
  }
end

#typesHash{Symbol => Type}

The types for this API.

Returns:

  • (Hash{Symbol => Type})


64
65
66
# File 'lib/apiwork/introspection/api.rb', line 64

def types
  @types ||= @dump[:types].transform_values { |dump| Type.new(dump) }
end