Class: Alphavantage::Sector

Inherits:
Object
  • Object
show all
Includes:
HelperFunctions
Defined in:
lib/Sector.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HelperFunctions

#check_argument, #method_missing, #recreate_metadata_key, #return_client, #return_int_val, #return_matype, #return_series, #return_value

Constructor Details

#initialize(key:, verbose: false) ⇒ Sector

Returns a new instance of Sector.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/Sector.rb', line 5

def initialize key:, verbose: false
  check_argument([true, false], verbose, "verbose")
  @client = return_client(key, verbose)
  @hash = @client.request("function=SECTOR")
   = @hash.dig("Meta Data") || {}
  .each do |key, val|
    key_sym = key.downcase.gsub(/[0-9.]/, "").lstrip.gsub(" ", "_").to_sym
    define_singleton_method(key_sym) do
      return val
    end
  end
  @hash.each do |key, val|
    next if key == "Meta Data"
    key = key.split(":")[1].lstrip
    key = key.split(" ")
    if key[0].to_i != 0
      key[0] = key[0].to_i.humanize
    end
    key.delete_if{|k| k.include?("(")}
    key = key.join("_")
    key_sym = key.downcase.gsub("-", "_").to_sym
    define_singleton_method(key_sym) do
      return val
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class HelperFunctions

Instance Attribute Details

#hashObject (readonly)

Returns the value of attribute hash.



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

def hash
  @hash
end