Module: Nearmiss::Util

Included in:
Nearmiss, Client
Defined in:
lib/nearmiss-ruby/util.rb

Instance Method Summary collapse

Instance Method Details

#get_string(obj) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/nearmiss-ruby/util.rb', line 7

def get_string(obj)
  if obj.respond_to?(:strftime)
    obj.strftime('%Y-%m-%d')
  else
    obj.to_s
  end
end

#url_encode(hash) ⇒ Object



3
4
5
# File 'lib/nearmiss-ruby/util.rb', line 3

def url_encode(hash)
  hash.to_a.map {|p| p.map {|e| CGI.escape get_string(e)}.join '='}.join '&'
end

#uuid?(uuid) ⇒ Boolean

Validate if argument is a UUID

Examples:

Nearmiss.uuid?("31817811-dce4-48c4-aa5f-f49603c5abee") => true
Nearmiss.uuid?("[email protected]") => false

Parameters:

  • uuid (String)

    the string to test against

Returns:

  • (Boolean)


23
24
25
26
27
28
# File 'lib/nearmiss-ruby/util.rb', line 23

def uuid?(uuid)
  return true if uuid =~ /\A[\da-f]{32}\z/i
  return true if
    uuid =~ /\A(urn:uuid:)?[\da-f]{8}-([\da-f]{4}-){3}[\da-f]{12}\z/i
  return false
end