Class: Apiwork::Introspection::Contract

Inherits:
Object
  • Object
show all
Defined in:
lib/apiwork/introspection/contract.rb

Overview

Facade for introspected contract data.

Provides access to actions, types, and enums defined on this contract.

Examples:

contract = InvoiceContract.introspect(expand: true)

contract.actions[:show].response # => Action::Response
contract.types[:address].shape # => { street: ..., city: ... }
contract.enums[:status].values # => ["draft", "published"]

contract.actions.each_value do |action|
  action.request # => Action::Request
  action.response # => Action::Response
end

Instance Method Summary collapse

Constructor Details

#initialize(dump) ⇒ Contract

Returns a new instance of Contract.



22
23
24
# File 'lib/apiwork/introspection/contract.rb', line 22

def initialize(dump)
  @dump = dump
end

Instance Method Details

#actionsHash{Symbol => Introspection::Action}

The actions for this contract.

Returns:



30
31
32
# File 'lib/apiwork/introspection/contract.rb', line 30

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

#enumsHash{Symbol => Enum}

The enums for this contract.

Returns:

  • (Hash{Symbol => Enum})


46
47
48
# File 'lib/apiwork/introspection/contract.rb', line 46

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

#to_hHash

Converts this contract to a hash.

Returns:

  • (Hash)


54
55
56
57
58
59
60
# File 'lib/apiwork/introspection/contract.rb', line 54

def to_h
  {
    actions: actions.transform_values(&:to_h),
    enums: enums.transform_values(&:to_h),
    types: types.transform_values(&:to_h),
  }
end

#typesHash{Symbol => Type}

The types for this contract.

Returns:

  • (Hash{Symbol => Type})


38
39
40
# File 'lib/apiwork/introspection/contract.rb', line 38

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