module HomeAway::API::Util::Validators

@private

Public Class Methods

array(input) click to toggle source

@private

# File lib/homeaway/api/util/validators.rb, line 111
def self.array(input)
  return nil if input == nil
  [input].flatten
end
boolean(input) click to toggle source

@private

# File lib/homeaway/api/util/validators.rb, line 76
def self.boolean(input)
  input_s = input.to_s
  return true if input_s =~ (/^(true)$/i)
  return false if input_s.empty? || input_s =~ (/^(false)$/i)
  raise ArgumentError.new("Supplied argument #{input} must be a valid boolean value")
end
camel_case(input) click to toggle source

@private

# File lib/homeaway/api/util/validators.rb, line 84
def self.camel_case(input)
  input.to_s.camelize(:lower)
end
date(input) click to toggle source

@private

# File lib/homeaway/api/util/validators.rb, line 31
def self.date(input)
  if input.is_a? String
    input = Chronic.parse(input, :ambiguous_time_range => :none)
  end
  raise ArgumentError.new('dates must be a parseable date string or a Ruby Time object') unless input.is_a? Time
  input.to_date.to_s
end
float(input, min=nil, max=nil) click to toggle source

@private

# File lib/homeaway/api/util/validators.rb, line 40
def self.float(input, min=nil, max=nil)
  begin
    input_s = input.to_s
    !!Float(input_s)
    f = input_s.to_f
    unless min == nil
      raise ArgumentError.new("Supplied argument #{f} must be greater than #{min}") if f < min
    end
    unless max == nil
      raise ArgumentError.new("Supplied argument #{f} must be less than #{max}") if f > max
    end
    return f
  rescue
    raise ArgumentError.new("Supplied argument #{input} must be a valid float value")
  end
end
integer(input, min=nil, max=nil) click to toggle source

@private

# File lib/homeaway/api/util/validators.rb, line 58
def self.integer(input, min=nil, max=nil)
  begin
    input_s = input.to_s
    !!Integer(input_s)
    i = input_s.to_i
    unless min == nil
      raise ArgumentError.new("Supplied argument #{i} must be greater than #{min}") if i < min
    end
    unless max == nil
      raise ArgumentError.new("Supplied argument #{i} must be less than #{max}") if i > max
    end
    return i
  rescue
    raise ArgumentError.new("Supplied argument #{input} must be a valid integer value")
  end
end
query_keys(hash) click to toggle source

@private

# File lib/homeaway/api/util/validators.rb, line 94
def self.query_keys(hash)
  raise ArgumentError.new('arguments must be a hash') unless hash.is_a? Hash
  transformed = {}
  hash.each_key do |key|
    transformed[self.camel_case(key)] = hash[key]
  end
  transformed
end
snake_case(input) click to toggle source

@private

# File lib/homeaway/api/util/validators.rb, line 89
def self.snake_case(input)
  input.to_s.underscore
end
uri_encoded(input) click to toggle source

@private

# File lib/homeaway/api/util/validators.rb, line 26
def self.uri_encoded(input)
  CGI.escape input
end
uuid(input) click to toggle source

@private

# File lib/homeaway/api/util/validators.rb, line 104
def self.uuid(input)
  uuid_regex = /\A([0-9a-fA-F]{32}|[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})\z/
  raise ArgumentError.new("#{uuid.to_s} must be a valid uuid") unless input.to_s =~ uuid_regex
  input.to_s
end