Module: HelperFunctions

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



2
3
4
# File 'lib/helper_function.rb', line 2

def method_missing(method, *args, &block)
  raise Alphavantage::Error.new message: "#{method} is undefined for #{self.class}"
end

Instance Method Details

#check_argument(list, value, attribute) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/helper_function.rb', line 6

def check_argument(list, value, attribute)
  unless list.include? value
    list.each{|l| l = "nil" if l.nil?}
    raise Alphavantage::Error.new message: "Only #{list.join(", ")} are supported for #{attribute}",
      data: {"list_valid" => list, "wrong_value" => value, "wrong_attribute" => attribute}
  end
end

#recreate_metadata_key(key) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/helper_function.rb', line 59

def (key)
  key_sym = key.split(" ")
  key_sym.shift if key_sym.size > 1
  if key_sym[-1] == "(USD)"
    key_sym[-1] = "USD"
  elsif key_sym[-1].include?("(") && key_sym[-1].include?(")")
    key_sym.pop
  end
  key_sym = key_sym.join("_")
  key_sym = key_sym.downcase.lstrip.gsub(" ", "_")
  key_sym = key_sym.to_sym
  return key_sym
end

#return_client(key, verbose = false) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/helper_function.rb', line 37

def return_client(key, verbose=false)
  if key.is_a?(String)
    client = Alphavantage::Client.new key: key, verbose: verbose
  elsif key.is_a?(Alphavantage::Client)
    client = key
  else
    raise Alphavantage::Error.new message: "Key should be a string"
  end
  return client
end

#return_int_val(val, val_string, type = "integer", check_positivity = true) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/helper_function.rb', line 22

def return_int_val(val, val_string, type="integer", check_positivity=true)
  value = case type
  when "integer"
    val.to_i
  when "float"
    val.to_f
  end
  if value.to_s != val
    Alphavantage::Error.new message: "Error: #{val_string} is not a correct number"
  elsif check_positivity && value <= 0
    Alphavantage::Error.new message: "Error: #{val_string} is not a correct positive #{type}"
  end
  return "&#{val_string}=#{val}"
end

#return_matype(val, val_string) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/helper_function.rb', line 14

def return_matype(val, val_string)
  check_argument(["0", "1", "2", "3", "4", "5", "6", "7", "8", "SMA", "EMA", "WMA", "DEMA", "TEMA", "TRIMA", "T3", "KAMA", "MAMA"], val, "ma_type")
  hash = {"SMA" => "0", "EMA" => "1", "WMA" => "2", "DEMA" => "3",
    "TEMA" => "4", "TRIMA" => "5", "T3" => "6", "KAMA" => "7", "MAMA" => "8"}
  val = hash[val] unless hash[val].nil?
  return "&#{val_string}=#{val}"
end

#return_series(series, order) ⇒ Object



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

def return_series(series, order)
  order ||= "desc"
  check_argument(["asc", "desc"], order, "order")
  return series.sort_by{ |hsh| hsh[0]} if order == "asc"
  return series
end

#return_value(hash, val) ⇒ Object



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

def return_value(hash, val)
  return hash.find{|key, value| key.include?(val)}&.dig(1)
end