Class: MyCoins

Inherits:
Object
  • Object
show all
Includes:
Colour
Defined in:
lib/mycoins.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, date: nil, debug: false, mycurrency: 'USD', filepath: 'mycoins', colored: true, exchangerate_key: nil) ⇒ MyCoins

Returns a new instance of MyCoins.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/mycoins.rb', line 15

def initialize(source, date: nil, debug: false, mycurrency: 'USD', 
               filepath: 'mycoins', colored: true, exchangerate_key: nil)

  @debug, @filepath, @colored = debug, filepath, colored
  @exchangerate_key = exchangerate_key
  
  @jer = JustExchangeRates.new(base: 'USD', debug: @debug, 
                               app_id: exchangerate_key)

  s = RXFHelper.read(source).first

  if s =~ /<\?dynarex / then

    @dx = Dynarex.new
    @dx.import s     

  end

  puts '@dx.to_xml: ' + @dx.to_xml if @debug

  @mycurrency = (@dx.currency || mycurrency).upcase
  puts '@mycurrency: ' + @mycurrency.inspect if @debug
  
  coin_names = @dx.all.map{ |x| x.title.gsub(/\s+\[[^\]]+\]/,'')}.uniq
  
  @cache_file = File.join(filepath, 'mycoins_lookup.yaml')
  
  h = if File.exist? @cache_file then

    puts 'reading coins symbols frome the cache' if @debug
    h2 = Psych.load(File.read(@cache_file))
    puts 'h2: ' + h2.inspect if @debug
    
    if (coin_names - h2.keys).empty? then
      h2
    else
      fetch_symbols coin_names 
    end
    
  else

    fetch_symbols coin_names
  end
  
  puts 'h: ' + h.inspect if @debug
  mycoins = h.values

  puts 'mycoins: ' + mycoins.inspect if @debug
  @ccf = CryptocoinFanboi.new(watch: mycoins, debug: debug, 
                              exchangerate_key: exchangerate_key)

end

Instance Attribute Details

#ccfObject (readonly)

Returns the value of attribute ccf.



12
13
14
# File 'lib/mycoins.rb', line 12

def ccf
  @ccf
end

#mycurrencyObject

Returns the value of attribute mycurrency.



13
14
15
# File 'lib/mycoins.rb', line 13

def mycurrency
  @mycurrency
end

Instance Method Details

#archiveObject



68
69
70
71
72
73
74
75
# File 'lib/mycoins.rb', line 68

def archive()

  filepath = File.join(@filepath, Time.now.year.to_s, 
                       Time.now.strftime("c%d%m%Y.xml"))
  FileUtils.mkdir_p File.dirname(filepath)
  File.write filepath, to_xml
  
end

#portfolio(order_by: :rank) ⇒ Object



135
136
137
138
139
140
141
# File 'lib/mycoins.rb', line 135

def portfolio(order_by: :rank)
  
  puts 'inside portfolio' if @debug
  r = build_portfolio(@dx.title)
  format_portfolio(r, order_by: order_by)

end

#price(coin_name, qty, btc: nil, date: nil) ⇒ Object

return the value of a coin given either the qty or purchase amount in BTC



122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/mycoins.rb', line 122

def price(coin_name, qty, btc: nil, date: nil)
  
  price_usd = if date then
    @ccf.price coin_name, date
  else
    coin = @ccf.find(coin_name)      
    coin.price_usd.to_f      
  end

  "%.2f %s" % [(price_usd * qty.to_f) * @jer.rate(@mycurrency), 
               @mycurrency]
end

#to_dxObject



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/mycoins.rb', line 77

def to_dx()
  
  r = build_portfolio(@dx.title)
  dx = Dynarex.new    
  dx.import r.records    
  
  h = r.to_h
  h.delete :records
  dx.summary.merge!(h)
  
  dx
  
end

#to_openstructObject



91
92
93
# File 'lib/mycoins.rb', line 91

def to_openstruct()
  build_portfolio(@dx.title)
end

#to_percentagesObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/mycoins.rb', line 95

def to_percentages()
  
  all = self.to_dx.to_a
  val = ('value_' + @mycurrency.downcase).to_sym
  
  a = all.map {|x| x[val].to_f }
  sum = a.inject(&:+)
  
  a2 = all.map do |x| 
    [x[:title].sub(/\s+\[[^\]]\]+/,''), (x[val].to_f).round(2)]
  end.group_by(&:first).to_a
  
  a3 = a2.map do |x| 
    [x.first, ((x[1].inject(0) {|r,y| r + y[1].to_f} / sum.to_f) * 100)\
     .round(2)]
  end
  
  a3.sort_by(&:last).reverse.to_h

end

#to_sObject



116
117
118
# File 'lib/mycoins.rb', line 116

def to_s()
  @ccf.to_s
end

#to_valuesObject

returns a Hash object containing the value as well as the percentage relative to the total value of the portfolio for each cryptocurrency



146
147
148
149
150
151
152
153
154
155
# File 'lib/mycoins.rb', line 146

def to_values()
  
  portfolio = build_portfolio()
  
  self.to_percentages.inject({}) do |r, x|
    currency, pct = x
    r.merge!(currency => [pct, ((portfolio.value * pct) / 100).round(2)])
  end
  
end

#to_xmlObject



157
158
159
160
161
# File 'lib/mycoins.rb', line 157

def to_xml()
  
  self.to_dx().to_xml pretty: true
  
end