Module: TwelvedataRuby::Utils

Defined in:
lib/twelvedata_ruby/utils.rb

Class Method Summary collapse

Class Method Details

.blank?(value) ⇒ Boolean

Checks if a value is blank (nil, empty, or whitespace-only)

Parameters:

  • value (Object)

    Value to check

Returns:

  • (Boolean)

    True if value is blank



104
105
106
107
108
109
110
# File 'lib/twelvedata_ruby/utils.rb', line 104

def blank?(value)
  return true if value.nil?
  return true if value.respond_to?(:empty?) && value.empty?
  return true if value.is_a?(String) && value.strip.empty?

  false
end

.camelize(str) ⇒ String

Converts snake_case to CamelCase

Examples:

Utils.camelize("snake_case") #=> "SnakeCase"

Parameters:

  • str (String)

    String to convert

Returns:

  • (String)

    CamelCase string



40
41
42
# File 'lib/twelvedata_ruby/utils.rb', line 40

def camelize(str)
  str.to_s.split("_").map(&:capitalize).join
end

.demodulize(obj) ⇒ String

Removes module namespace from class name

Examples:

Utils.demodulize(TwelvedataRuby::Error) #=> "Error"

Parameters:

  • obj (Object)

    Object to extract class name from

Returns:

  • (String)

    Class name without module namespace



14
15
16
# File 'lib/twelvedata_ruby/utils.rb', line 14

def demodulize(obj)
  obj.to_s.gsub(/^.+::/, "")
end

.empty_to_nil(obj) ⇒ Object?

Converts empty values to nil

Examples:

Utils.empty_to_nil("") #=> nil
Utils.empty_to_nil("test") #=> "test"

Parameters:

  • obj (Object)

    Object to check

Returns:

  • (Object, nil)

    Original object or nil if empty



52
53
54
55
56
57
# File 'lib/twelvedata_ruby/utils.rb', line 52

def empty_to_nil(obj)
  return nil if obj.nil?
  return nil if obj.respond_to?(:empty?) && obj.empty?

  obj
end

.execute_if_true(condition) { ... } ⇒ Object?

Executes block only if condition is exactly true

Parameters:

  • condition (Object)

    Condition to evaluate

Yields:

  • Block to execute if condition is true

Returns:

  • (Object, nil)

    Block result or nil



88
89
90
# File 'lib/twelvedata_ruby/utils.rb', line 88

def execute_if_true(condition, &block)
  execute_if_truthy(condition == true, &block)
end

.execute_if_truthy(condition, default_return = nil) { ... } ⇒ Object

Executes block if condition is truthy

Parameters:

  • condition (Object)

    Condition to evaluate

  • default_return (Object) (defaults to: nil)

    Default return value

Yields:

  • Block to execute if condition is truthy

Returns:

  • (Object)

    Block result or default value



77
78
79
80
81
# File 'lib/twelvedata_ruby/utils.rb', line 77

def execute_if_truthy(condition, default_return = nil)
  return default_return unless condition && block_given?

  yield
end

.present?(value) ⇒ Boolean

Validates that a value is not blank

Parameters:

  • value (Object)

    Value to validate

Returns:

  • (Boolean)

    True if value is present



96
97
98
# File 'lib/twelvedata_ruby/utils.rb', line 96

def present?(value)
  !blank?(value)
end

.to_array(objects) ⇒ Array

Ensures return value is an array

Examples:

Utils.to_array("test") #=> ["test"]
Utils.to_array(["a", "b"]) #=> ["a", "b"]

Parameters:

  • objects (Object)

    Single object or array

Returns:

  • (Array)

    Array containing the objects



67
68
69
# File 'lib/twelvedata_ruby/utils.rb', line 67

def to_array(objects)
  objects.is_a?(Array) ? objects : [objects]
end

.to_integer(obj, default_value = nil) ⇒ Integer?

Converts string to integer with default fallback

Examples:

Utils.to_integer("123") #=> 123
Utils.to_integer("abc", 0) #=> 0

Parameters:

  • obj (Object)

    Object to convert

  • default_value (Integer, nil) (defaults to: nil)

    Default value if conversion fails

Returns:

  • (Integer, nil)

    Converted integer or default value



27
28
29
30
31
# File 'lib/twelvedata_ruby/utils.rb', line 27

def to_integer(obj, default_value = nil)
  obj.is_a?(Integer) ? obj : Integer(obj.to_s)
rescue ArgumentError
  default_value
end