class APIGatewayDSL::Operation::Collection

Constants

ACCESS_CONTROL_ALLOW_HEADERS

Public Instance Methods

as_json() click to toggle source

Indexes a flat array of operarations by path and HTTP method:

Given:

- operationA ( path = '/path1', method = 'get'  )
- operationB ( path = '/path1', method = 'post' )
- operationC ( path = '/path2', method = 'get'  )

Result:

/path1:
  get:
    operationA
  post:
    operationB
/path2:
  get:
    operationC
# File lib/api_gateway_dsl/operation/collection.rb, line 32
def as_json
  group_by(&:path).transform_values do |operations|
    append_cors_operation!(operations)
    operations.index_by(&:method).transform_values(&:as_json)
  end
end

Private Instance Methods

append_cors_operation!(operations) click to toggle source

All passed operations must have the same path

# File lib/api_gateway_dsl/operation/collection.rb, line 42
def append_cors_operation!(operations)
  cors_enabled_ops = operations.select(&:cors)
  return if cors_enabled_ops.none?
  operations << cors_operation(operations.first.path, (cors_enabled_ops.map(&:cors_method) << 'OPTIONS').sort)
end
cors_operation(path, methods) click to toggle source
# File lib/api_gateway_dsl/operation/collection.rb, line 48
def cors_operation(path, methods)
  Operation.new(Context.new, 'OPTIONS', path) do
    MOCK 200

    RESPONSE 200 do
      header 'Access-Control-Allow-Headers', ACCESS_CONTROL_ALLOW_HEADERS.join(',')
      header 'Access-Control-Allow-Methods', methods.join(',')
      header 'Access-Control-Allow-Origin',  '*'
    end
  end
end