Module: Tinypass::Utils

Extended by:
Utils
Included in:
Utils
Defined in:
lib/tinypass/utils.rb

Instance Method Summary collapse

Instance Method Details

#parse_loose_period_in_msecs(period) ⇒ Object

Raises:

  • (ArgumentError)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/tinypass/utils.rb', line 9

def parse_loose_period_in_msecs(period)
  period = period.to_s
  return period.to_i if period.to_i.to_s == period

  if matches = /(\d+)\s*(\w+)/.match(period)
    number = matches[1].to_i
    string = matches[2]

    return number if string.start_with?("ms")
    return number * 1000 if string.start_with?("s")
    return number * 1000 * 60 if string.start_with?("mi")
    return number * 1000 * 60 * 60 if string.start_with?("h")
    return number * 1000 * 60 * 60 * 24 if string.start_with?("d")
    return number * 1000 * 60 * 60 * 24 * 7 if string.start_with?("w")
    return number * 1000 * 60 * 60 * 24 * 30 if string.start_with?("mo")
    return number * 1000 * 60 * 60 * 24 * 365 if string.start_with?("y")
  end

  raise ArgumentError.new("Cannot parse the specified period: #{ period }")
end

#parse_loose_period_in_secs(period) ⇒ Object



30
31
32
# File 'lib/tinypass/utils.rb', line 30

def parse_loose_period_in_secs(period)
  parse_loose_period_in_msecs(period) / 1000
end

#valid_ip?(ip) ⇒ Boolean

Returns:

  • (Boolean)


5
6
7
# File 'lib/tinypass/utils.rb', line 5

def valid_ip?(ip)
  ip && ip =~ /\d{1,3}\.\d{1,3}\.\d{1,3}.\d{1,3}/
end