Module: TwelvedataRuby::Utils

Defined in:
lib/twelvedata_ruby/utils.rb

Class Method Summary collapse

Class Method Details

.call_block_if_truthy(truthy_val, return_this = nil, &block) ⇒ Object



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

def self.call_block_if_truthy(truthy_val, return_this=nil, &block)
  truthy_val ? block.call : return_this
end

.camelize(str) ⇒ Object



16
17
18
# File 'lib/twelvedata_ruby/utils.rb', line 16

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

.demodulize(obj) ⇒ Object



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

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

.empty_to_nil(obj) ⇒ Object



20
21
22
# File 'lib/twelvedata_ruby/utils.rb', line 20

def self.empty_to_nil(obj)
  !obj.nil? && obj.empty? ? nil : obj
end

.return_nil_unless_true(is_true, &block) ⇒ Object



32
33
34
# File 'lib/twelvedata_ruby/utils.rb', line 32

def self.return_nil_unless_true(is_true, &block)
  call_block_if_truthy(is_true == true) { block.call }
end

.to_a(objects) ⇒ Object



24
25
26
# File 'lib/twelvedata_ruby/utils.rb', line 24

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

.to_d(obj, default_value = nil) ⇒ Object

Converts a string to integer an all integer or nothing



10
11
12
13
14
# File 'lib/twelvedata_ruby/utils.rb', line 10

def self.to_d(obj, default_value=nil)
  return obj if obj.is_a?(Integer)

  obj.to_s.match(/^\d+$/) {|m| Integer(m[0]) } || default_value
end