class Object

Constants

CMP_AT_LEAST
CMP_AT_MOST
CMP_EQUALS
CMP_LESS_THAN
CMP_MORE_THAN
FEWER_MORE_THAN_SYNONYM
FIELD_NAME_SYNONYM
GET_TYPES
HAVE_ALTERNATION
INT_AS_WORDS_SYNONYM
MAXIMAL_FIELD_NAME_SYNONYM
RESOURCE_NAME_SYNONYM

Public Instance Methods

add_to_hash(a, n) click to toggle source
# File lib/cucumber-rest-bdd/types.rb, line 163
def add_to_hash(a, n)
    result = nil    
    if (n[0] == '[' && n[-1] == ']') then
        array = Array.new(n[1..-2].to_i() + 1)
        array[n[1..-2].to_i()] = a
        result = array
    end
    result != nil ? result : { n => a };
end
get_attributes(hashes) click to toggle source
# File lib/cucumber-rest-bdd/types.rb, line 138
def get_attributes(hashes)
    attributes = hashes.each_with_object({}) do |row, hash|
      name, value, type = row["attribute"], row["value"], row["type"]
      value = resolve_functions(value)
      value = resolve(value)
      value.gsub!(/\\n/, "\n")
      names = get_fields(name)
      new_hash = names.reverse.inject(string_to_type(value, type)) { |a, n| add_to_hash(a, n) }
      hash.deep_merge!(new_hash) { |key, old, new| new.kind_of?(Array) ? merge_arrays(old, new) : new }
    end
end
get_child_data(level, data) click to toggle source
# File lib/cucumber-rest-bdd/data.rb, line 40
def get_child_data(level, data)
    if (level[:root]) then
        return data.dup
    else
        levelKey = case level[:type]
            when 'single' then get_field(level[:key])
            when 'multiple' then get_list_field(level[:key])
            when 'list' then get_list_field(level[:key])
        end
        raise %/Key not found: #{level[:key]} as #{levelKey} in #{data}/ if data.is_a?(Array) || !data[levelKey]
        return data[levelKey]
    end
end
get_field(name) click to toggle source
# File lib/cucumber-rest-bdd/types.rb, line 115
def get_field(name)
    if name[0] == '`' && name[-1] == '`'
        name = name[1..-2]
    elsif name[0] != '[' || name[-1] != ']'
        separator = ENV.has_key?('field_separator') ? ENV['field_separator'] : '_'
        name = name.parameterize(separator: separator)
        name = name.camelize(:lower) if (ENV.has_key?('field_camel') && ENV['field_camel'] == 'true')
    end
    return name
end
get_fields(names) click to toggle source
# File lib/cucumber-rest-bdd/types.rb, line 111
def get_fields(names)
    return names.split(':').map { |n| get_field(n.strip) }
end
get_json_path(names) click to toggle source
# File lib/cucumber-rest-bdd/types.rb, line 107
def get_json_path(names)
    return "#{get_root_data_key()}#{get_fields(names).join('.')}"
end
get_key(grouping) click to toggle source

gets the relevant key for the response based on the first key element

# File lib/cucumber-rest-bdd/data.rb, line 4
def get_key(grouping)
    errorKey = ENV['error_key']
    if errorKey && !errorKey.empty? && grouping.count > 1 && grouping[-2][:key].singularize == errorKey then
        return "$.#{errorKey}."
    else
        return get_root_data_key()
    end
end
get_list_field(name) click to toggle source
# File lib/cucumber-rest-bdd/types.rb, line 126
def get_list_field(name)
    if name[0] == '`' && name[-1] == '`'
        name = name[1..-2]
    elsif name[0] != '[' || name[-1] != ']'
        separator = ENV.has_key?('field_separator') ? ENV['field_separator'] : '_'
        name = name.parameterize(separator: separator)
        name = name.pluralize
        name = name.camelize(:lower) if (ENV.has_key?('field_camel') && ENV['field_camel'] == 'true')
    end
    return name
end
get_resource(name) click to toggle source
# File lib/cucumber-rest-bdd/types.rb, line 93
def get_resource(name)
    if name[0] == '`' && name[-1] == '`'
        name = name[1..-2]
    else
        name = name.parameterize
        name = (ENV.has_key?('resource_single') && ENV['resource_single'] == 'true') ? name.singularize : name.pluralize
    end
    return name
end
get_root_data_key() click to toggle source
# File lib/cucumber-rest-bdd/types.rb, line 103
def get_root_data_key()
    return ENV.has_key?('data_key') && !ENV['data_key'].empty? ? "$.#{ENV['data_key']}." : "$."
end
get_url(path) click to toggle source
# File lib/cucumber-rest-bdd/url.rb, line 1
def get_url(path)
    raise %/Please set an 'endpoint' environment variable provided with the url of the api/ if !ENV.has_key?('endpoint')
    url = ENV['endpoint']
    url = "#{url}/" unless url.end_with?("/")
    url = "#{url}#{@urlbasepath}/" unless @urlbasepath.to_s.empty?
    url = "#{url}#{path}" unless path.empty?
end
merge_arrays(a, b) click to toggle source
# File lib/cucumber-rest-bdd/types.rb, line 173
def merge_arrays(a, b)
    new_length = [a.length, b.length].max
    new_array = Array.new(new_length)
    new_length.times do |n|
        if b[n].nil? then
            new_array[n] = a[n]
        else
            if a[n].nil? then
                new_array[n] = b[n]
            else
                new_array[n] = a[n].merge(b[n])
            end
        end
    end
    return new_array
end
nest_match_attributes(data, nesting, expected, matchValue) click to toggle source

top level has 2 children with an item containing at most three fish with attributes:

nesting = [{key=fish,count=3,count_mod='<=',type=multiple},{key=item,type=single},{key=children,type=multiple,count=2,count_mod='='},{root=true,type=single}]

returns true if the expected data is contained within the data based on the nesting information

# File lib/cucumber-rest-bdd/data.rb, line 18
def nest_match_attributes(data, nesting, expected, matchValue)
    return false if !data
    return data.deep_include?(expected) if !matchValue && nesting.size == 0
    return data.include?(expected) if matchValue && nesting.size == 0

    local_nesting = nesting.dup
    level = local_nesting.pop
    child_data = get_child_data(level, data)

    case level[:type]
        when 'single' then
            return nest_match_attributes(child_data, local_nesting, expected, matchValue)
        when 'multiple' then
            matched = child_data.select { |item| nest_match_attributes(item, local_nesting, expected, matchValue) }
            return level[:comparison].compare(matched.count)
        when 'list' then
            return child_data.is_a?(Array) && (!level.has_key?(:comparison) || level[:comparison].compare(child_data.count))
        else
            raise %/Unknown nested data type: #{level[:type]}/
    end
end
parse_type(type) click to toggle source
# File lib/cucumber-rest-bdd/types.rb, line 47
def parse_type(type)
    replacements = {
        /^numeric$/i => 'numeric',
        /^int$/i => 'numeric',
        /^long$/i => 'numeric',
        /^number$/i => 'numeric',
        /^decimal$/i => 'numeric',
        /^double$/i => 'numeric',
        /^bool$/i => 'boolean',
        /^null$/i => 'nil_class',
        /^nil$/i => 'nil_class',
        /^string$/i => 'string',
        /^text$/i => 'string'
    }
    type.tr(' ', '_')
    replacements.each { |k,v| type.gsub!(k, v) }
    type
end
resolve_functions(value) click to toggle source
# File lib/cucumber-rest-bdd/types.rb, line 150
def resolve_functions(value)
    value.gsub!(/\[([a-zA-Z0-9_]+)\]/) do |s|
        s.gsub!(/[\[\]]/, '')
        case s.downcase
        when "datetime"
            Time.now.strftime("%Y%m%d%H%M%S")
        else
            raise 'Unrecognised function ' + s + '?'
        end
    end
    value
end
string_to_type(value, type) click to toggle source
# File lib/cucumber-rest-bdd/types.rb, line 66
def string_to_type(value, type)
    replacements = {
        /^numeric$/i => 'integer',
        /^int$/i => 'integer',
        /^long$/i => 'integer',
        /^number$/i => 'integer',
        /^decimal$/i => 'float',
        /^double$/i => 'float',
        /^bool$/i => 'boolean',
        /^null$/i => 'nil_class',
        /^nil$/i => 'nil_class',
        /^string$/i => 'string',
        /^text$/i => 'string'
    }
    type.tr(' ', '_')
    replacements.each { |k,v| type.gsub!(k, v) }
    type = type.camelize.constantize
    # cannot use 'case type' which checks for instances of a type rather than type equality
    if type == Boolean then !(value =~ /true|yes/i).nil?
    elsif type == Enum then value.upcase.tr(" ", "_")
    elsif type == Float then value.to_f
    elsif type == Integer then value.to_i
    elsif type == NilClass then nil
    else value
    end
end
to_compare(compare) click to toggle source

take a number modifier string (fewer than, less than, etc) and return an operator '<', etc

# File lib/cucumber-rest-bdd/list.rb, line 128
def to_compare(compare)
    return case compare
    when 'fewer than' then CMP_LESS_THAN
    when 'less than' then CMP_LESS_THAN
    when 'more than' then CMP_MORE_THAN
    when 'at least' then CMP_AT_LEAST
    when 'at most' then CMP_AT_MOST
    else CMP_EQUALS
    end
end
to_num(num) click to toggle source
# File lib/cucumber-rest-bdd/list.rb, line 139
def to_num(num)
    if /^(?:zero|one|two|three|four|five|six|seven|eight|nine|ten)$/.match(num)
        return %w(zero one two three four five six seven eight nine ten).index(num)
    end
    return num.to_i
end