Class: StockIndex::Component

Inherits:
Object
  • Object
show all
Defined in:
lib/stock_index/component.rb

Instance Method Summary collapse

Constructor Details

#initialize(symbol, market, wikipedia, pricing_source = :us) ⇒ Component

Returns a new instance of Component.



7
8
9
10
11
12
# File 'lib/stock_index/component.rb', line 7

def initialize(symbol, market, wikipedia, pricing_source = :us)
  @symbol = symbol
  @market = market
  @wikipedia = wikipedia
  @pricing_source = pricing_source
end

Instance Method Details

#attributesObject



14
15
16
17
18
# File 'lib/stock_index/component.rb', line 14

def attributes
  attrs = cache_lookup || attributes_lookup
  puts "---- ERROR #{attrs}" unless valid?(attrs)
  attrs
end

#attributes_lookupObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/stock_index/component.rb', line 36

def attributes_lookup
  bsym = StockIndex::BsymSearch.find(@symbol, @pricing_source)
  cik = lookup_cik
  puts "   --- #{@symbol} #{@market} #{@wikipedia} #{@pricing_source}"
  puts "   --- #{@symbol} bsym: #{bsym}"
  puts "   --- #{@symbol} cik: #{cik}"
  a = {
    market: @market,
    share: {
      symbol: @symbol,
      name: bsym ? bsym[:name] : nil,
      bbgid: bsym ? bsym[:bbgid] : nil
    },
    company: {wikipedia: @wikipedia}.merge( cik || {} )
  }
  cache_write(a)
  a
end

#cache_fileObject



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

def cache_file
  File.expand_path("../../cache/#{@market}.pstore", File.dirname(__FILE__))
end

#cache_lookupObject



20
21
22
23
# File 'lib/stock_index/component.rb', line 20

def cache_lookup
  store = PStore.new(cache_file)
  store.transaction { store[@symbol] }
end

#cache_write(a) ⇒ Object



25
26
27
28
29
30
# File 'lib/stock_index/component.rb', line 25

def cache_write(a)
  store = PStore.new(cache_file)
  store.transaction do
    store[@symbol] = a
  end
end

#lookup_cikObject



59
60
61
62
63
64
65
# File 'lib/stock_index/component.rb', line 59

def lookup_cik
  if us?
    lookup_cik_us
  else
    nil
  end
end

#lookup_cik_usObject



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/stock_index/component.rb', line 67

def lookup_cik_us
  edgar = Cik.lookup(SymbolParser.new(@symbol).bsym_to_cik)
  if edgar
    {
      cik: edgar[:cik],
      name: edgar[:name],
      sic: edgar[:sic]
    }
  else
    return nil
  end
end

#us?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/stock_index/component.rb', line 55

def us?
  @pricing_source == :us
end

#valid?(attributes) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
83
84
85
86
87
# File 'lib/stock_index/component.rb', line 80

def valid?(attributes)
  return false if attributes.nil?
  !attributes[:market].nil? &&
  !attributes[:share][:symbol].nil? &&
  !attributes[:share][:name].nil? &&
  !attributes[:share][:bbgid].nil? &&
  !attributes[:company][:wikipedia].nil?
end