class Atum::Core::Schema::LinkSchema

Constants

PARAMETER_REGEX

Match parameters in definition strings.

Attributes

resource_schema[R]

Public Class Methods

new(api_schema, resource_schema, link_schema_hash) click to toggle source

@param api_schema [ApiSchema] The schema for the whole API @param resource_schema [ResourceSchema] The schema of the resource this link belongs to. @param link_schema [Hash] The link’s schema

# File lib/atum/core/schema/link_schema.rb, line 11
def initialize(api_schema, resource_schema, link_schema_hash)
  @api_schema = api_schema
  @resource_schema = resource_schema
  @link_schema_hash = link_schema_hash
end

Public Instance Methods

construct_path(*params) click to toggle source

Construct the URL and body for a call to this link

@param params [Array] The list of parameters to inject into the

path.

@raise [ArgumentError] Raised if either too many or too few parameters

were provided.

@return [String,Object] A path and request body pair. The body value is

nil if a payload wasn't included in the list of parameters.
# File lib/atum/core/schema/link_schema.rb, line 55
def construct_path(*params)
  if expected_params.count != params.count
    raise ArgumentError, "Wrong number of arguments: #{params.count} " \
                         "for #{expected_params.count}"
  end

  params.map! { |param| URI.escape(param) }

  href.gsub(PARAMETER_REGEX) { |_match| format_parameter(params.shift) }
end
description() click to toggle source
# File lib/atum/core/schema/link_schema.rb, line 21
def description
  @link_schema_hash['description']
end
method() click to toggle source
# File lib/atum/core/schema/link_schema.rb, line 25
def method
  @link_schema_hash['method'].downcase.to_sym
end
name() click to toggle source
# File lib/atum/core/schema/link_schema.rb, line 17
def name
  @link_schema_hash['title'].downcase.gsub(' ', '_')
end
needs_request_body?() click to toggle source
# File lib/atum/core/schema/link_schema.rb, line 29
def needs_request_body?
  @link_schema_hash.key?('schema')
end
parameters() click to toggle source

Get the parameters this link expects.

@param parameters [Array] The names of the parameter definitions to

convert to parameter names.

@return [Array<Parameter|ParameterChoice>] A list of parameter instances

that represent parameters to be injected into the link URL.
# File lib/atum/core/schema/link_schema.rb, line 39
def parameters
  expected_params.map do |parameter|
    # URI decode parameters and strip the leading '{(' and trailing ')}'.
    parameter = URI.unescape(parameter[2..-3])
    generate_parameter(parameter)
  end
end

Private Instance Methods

expected_params() click to toggle source
# File lib/atum/core/schema/link_schema.rb, line 75
def expected_params
  href.scan(PARAMETER_REGEX)
end
format_parameter(parameter) click to toggle source

Convert a path parameter to a format suitable for use in a path.

@param [Fixnum,String,TrueClass,FalseClass,Time] The parameter to format. @return [String] The formatted parameter.

# File lib/atum/core/schema/link_schema.rb, line 109
def format_parameter(parameter)
  parameter.instance_of?(Time) ? iso_format(parameter) : parameter.to_s
end
generate_parameter(param) click to toggle source
# File lib/atum/core/schema/link_schema.rb, line 89
def generate_parameter(param)
  path = param.split('/')[1..-1]
  name = path[-1]
  resource_name = path.size > 2 ? path[1] : nil
  info = @api_schema.lookup_path(*path)
  description = info['description']

  if info.key?('anyOf')
    ParameterChoice.new(resource_name, unpack_multi_params(info['anyOf']))
  elsif info.key?('oneOf')
    ParameterChoice.new(resource_name, unpack_multi_params(info['oneOf']))
  else
    Parameter.new(resource_name, name, description)
  end
end
href() click to toggle source
# File lib/atum/core/schema/link_schema.rb, line 71
def href
  @link_schema_hash['href']
end
iso_format(time) click to toggle source

Convert a time to an ISO 8601 combined data and time format.

@param time [Time] The time to convert to ISO 8601 format. @return [String] An ISO 8601 date in ‘YYYY-MM-DDTHH:MM:SSZ` format.

# File lib/atum/core/schema/link_schema.rb, line 117
def iso_format(time)
  time.getutc.iso8601
end
unpack_multi_params(parameters) click to toggle source

Unpack an ‘anyOf’ or ‘oneOf’ multi-parameter blob.

@param parameters [Array<Hash>] An array of hashes containing ‘$ref’

keys and definition values.

@return [Array<Parameter>] An array of parameters extracted from the

blob.
# File lib/atum/core/schema/link_schema.rb, line 85
def unpack_multi_params(parameters)
  parameters.map { |info| generate_parameter(info['$ref']) }
end