Class: ShipHero::Util::Helpers

Inherits:
Object
  • Object
show all
Defined in:
lib/ship_hero/util/helpers.rb

Class Method Summary collapse

Class Method Details

.encode(str) ⇒ Object

Escape special characters

Parameters:

  • str (String)


21
22
23
# File 'lib/ship_hero/util/helpers.rb', line 21

def encode(str)
  CGI.escape(str).gsub('.', '%2E').gsub('-', '%2D')
end

.http_build_query(params) ⇒ String

Build the HTTP query from the given parameters

Parameters:

  • params (Hash)

Returns:

  • (String)

    query string



15
16
17
# File 'lib/ship_hero/util/helpers.rb', line 15

def http_build_query(params)
  params.collect{ |k,v| "#{k}=#{encode(v)}" }.reverse.join('&')
end

.to_bool(str) ⇒ Object

String to bool

Parameters:

  • str (String)


27
28
29
30
# File 'lib/ship_hero/util/helpers.rb', line 27

def to_bool(str)
  return true if str == true || str =~ (/(true|t|yes|y|1)$/i)
  return false if str == false || str == nil || str == '' || str =~ (/(false|f|no|n|0)$/i)
end

.to_date_time(str) ⇒ Object

String to DateTime

Parameters:

  • str (String)


34
35
36
37
38
39
40
# File 'lib/ship_hero/util/helpers.rb', line 34

def to_date_time(str)
  begin 
    Time.parse(str)
  rescue
    str
  end
end