class WADL::Param

Constants

BOOLEAN_RE

cf. <www.w3.org/TR/xmlschema-2/#boolean>

Public Class Methods

default() click to toggle source

A default Param object to use for a path parameter that is only specified as a name in the path of a resource.

# File lib/wadl/param.rb, line 46
def self.default
  @default ||= begin
    default = Param.new

    default.required = 'true'
    default.style    = 'plain'
    default.type     = 'xsd:string'

    default
  end
end

Public Instance Methods

%(value, name = nil, style = nil)
Alias for: format
format(value, name = nil, style = nil) click to toggle source

Validates and formats a proposed value for this parameter. Returns the formatted value. Raises an ArgumentError if the value is invalid.

The 'name' and 'style' arguments are used in conjunction with the default Param object.

# File lib/wadl/param.rb, line 76
def format(value, name = nil, style = nil)
  name  ||= self.name
  style ||= self.style

  value = fixed if fixed
  value ||= default if default

  unless value
    if required?
      raise ArgumentError, %Q{No value provided for required param "#{name}"!}
    else
      return '' # No value provided and none required.
    end
  end

  if value.respond_to?(:each) && !value.respond_to?(:to_str)
    if repeating?
      values = value
    else
      raise ArgumentError, %Q{Multiple values provided for single-value param "#{name}"}
    end
  else
    values = [value]
  end

  # If the param lists acceptable values in option tags, make sure that
  # all values are found in those tags.
  if options && !options.empty?
    values.each { |_value|
      unless find_option(_value)
        acceptable = options.map { |o| o.value }.join('", "')
        raise ArgumentError, %Q{"#{_value}" is not among the acceptable parameter values ("#{acceptable}")}
      end
    }
  end

  if style == 'query' || parent.is_a?(RequestFormat) || (
    parent.respond_to?(:is_form_representation?) && parent.is_form_representation?
  )
    values.map { |v| "#{uri_escape(name)}=#{uri_escape(v.to_s)}" }.join('&')
  elsif style == 'matrix'
    if type == 'xsd:boolean'
      values.map { |v| ";#{name}" if v =~ BOOLEAN_RE }.compact.join
    else
      values.map { |v| ";#{uri_escape(name)}=#{uri_escape(v.to_s)}" if v }.compact.join
    end
  elsif style == 'header'
    values.join(',')
  else
    # All other cases: plain text representation.
    values.map { |v| uri_escape(v.to_s) }.join(',')
  end
end
Also aliased as: %
inspect() click to toggle source
# File lib/wadl/param.rb, line 66
def inspect
  %Q{Param "#{name}"}
end
repeating?() click to toggle source
# File lib/wadl/param.rb, line 62
def repeating?
  repeating =~ BOOLEAN_RE
end
required?() click to toggle source
# File lib/wadl/param.rb, line 58
def required?
  required =~ BOOLEAN_RE
end

Private Instance Methods

uri_escape(v) click to toggle source
# File lib/wadl/param.rb, line 134
def uri_escape(v)
  ERB::Util.url_encode(v)
end