class ServiceContractWebmock::Field

Attributes

name[R]
type[R]
value[R]

Public Class Methods

new(name, type) click to toggle source
# File lib/service_contract_webmock/field.rb, line 5
def initialize(name, type)
  @name = name
  @type = type
  @value = build_value(type)
end

Public Instance Methods

boolean?(t = type) click to toggle source
# File lib/service_contract_webmock/field.rb, line 17
def boolean?(t = type)
  t.type_sym == :boolean ||
    (t.type_sym == :array && t.items.type_sym == :boolean) ||
    (t.type_sym == :union && t.schemas.any? {|sub| boolean?(sub)})
end
build_value(type) click to toggle source
# File lib/service_contract_webmock/field.rb, line 33
def build_value(type)
  case type.type_sym
  when :union
    subtypes = type.schemas.map {|subtype| build_value(subtype)}
    "(#{subtypes.join("|")})"
  when :null
    ""
  when :int
    "\\d+"
  when :array
    case type.items.type_sym
    when :int
      "[,\\d]+"
    when :string
      ".+"
    when :enum
      "(#{type.items.symbols.map{|symbol| "#{symbol}(\..+)?"}.join("|")})"
    else
      raise "unhandled array subtype #{type.items.type_sym}"
    end
  when :string
    ".+"
  when :boolean
    "true|false"
  when :float
    "\\d+\.\\d+"
  else
    raise "unhandled type #{type.type_sym}"
  end
end
convert(value) click to toggle source
# File lib/service_contract_webmock/field.rb, line 23
def convert(value)
  if int?
    value.to_i
  elsif boolean?
    value == "true"
  else
    value
  end
end
int?(t = type) click to toggle source
# File lib/service_contract_webmock/field.rb, line 11
def int?(t = type)
  t.type_sym == :int ||
    (t.type_sym == :array && t.items.type_sym == :int) ||
    (t.type_sym == :union && t.schemas.any? {|sub| int?(sub)})
end