Module: SevenApi::Util

Defined in:
lib/seven_api/util.rb

Overview

This module exposes general utilities used throughout the library.

Class Method Summary collapse

Class Method Details

.boolean?(val) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/seven_api/util.rb', line 58

def self.boolean?(val)
  [true, false].include? val
end

.get_module_constant_values(mod) ⇒ Object



23
24
25
# File 'lib/seven_api/util.rb', line 23

def self.get_module_constant_values(mod)
  mod.constants(false).map &mod.method(:const_get)
end

.get_namespace_classes(ns) ⇒ Object



19
20
21
# File 'lib/seven_api/util.rb', line 19

def self.get_namespace_classes(ns)
  return self.get_namespace_members_by_type(ns, Class)
end

.get_namespace_members_by_type(ns, type) ⇒ Object



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

def self.get_namespace_members_by_type(ns, type)
  ns.constants.map(&ns.method(:const_get)).grep(type)
end

.in_module_constants?(needle, mod) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/seven_api/util.rb', line 44

def self.in_module_constants?(needle, mod)
  get_module_constant_values(mod).include?(needle)
end

.is_positive_integer?(val) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/seven_api/util.rb', line 40

def self.is_positive_integer?(val)
  /\A\d+\z/.match?(val.to_s)
end

.is_valid_datetime?(str) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
34
35
36
37
38
# File 'lib/seven_api/util.rb', line 31

def self.is_valid_datetime?(str)
  begin
    DateTime.parse(str)
    true
  rescue ArgumentError
    false
  end
end

.is_valid_url?(str) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/seven_api/util.rb', line 27

def self.is_valid_url?(str)
  str =~ URI::regexp
end

.lengthy_string?(val) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/seven_api/util.rb', line 66

def self.lengthy_string?(val)
  return val.is_a?(String) && !val.empty?
end

.nil_or_lengthy_string?(val) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/seven_api/util.rb', line 62

def self.nil_or_lengthy_string?(val)
  val.nil? || (val.is_a?(String) && val.length)
end

.numeric?(val) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
55
56
# File 'lib/seven_api/util.rb', line 52

def self.numeric?(val)
  return true if val.is_a?(Integer)

  val.scan(/\D/).empty?
end

.to_numbered_bool(val) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/seven_api/util.rb', line 5

def self.to_numbered_bool(val)
  if true == val
    return 1
  elsif false == val
    return 0
  end

  return val
end

.valid_float?(str) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/seven_api/util.rb', line 48

def self.valid_float?(str)
  !!Float(str) rescue false
end