Class: Alphavcli::Cli

Inherits:
Object
  • Object
show all
Defined in:
lib/alphavcli/cli.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCli

Returns a new instance of Cli.



2
3
4
5
6
7
8
9
10
11
# File 'lib/alphavcli/cli.rb', line 2

def initialize

    if File.exist?(File.expand_path('../../userfiles/apikey.json', File.dirname(__FILE__)))
        @@apikey= JSON.parse(File.read(File.expand_path('../../userfiles/apikey.json', File.dirname(__FILE__))))["apikey"]
    else
        puts "No api Key found."
        getkey
    end
    #
end

Class Method Details

.returnKeyObject



105
106
107
# File 'lib/alphavcli/cli.rb', line 105

def self.returnKey
    @@apikey
end

Instance Method Details

#callObject



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
# File 'lib/alphavcli/cli.rb', line 22

def call

    loop {
        puts '
Please enter the number cooresponding to the action you would like to take.
    
"Search" - Search to find security by keyword.
"Returns - Get data directly from a ticker you know"
"exit" quit program'

        select = gets.chomp
        case select
        
        when "Search"
            searchDisplay
            #Alphavcli::Search.new
        when "Returns"
            returnsFromTicker
        when "exit"
            break
        else
            puts "#{select} is not a valid action"
        end
        }
end

#dataLanding(sec) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/alphavcli/cli.rb', line 64

def dataLanding(sec)
    
    loop {
        puts "What would you like to know about #{sec.name}?"
        puts '"Daily" - Display return from the past day
"Weekly" - Display returns from the past week
"Back" - Go Back to main menu'
        select=gets.chomp
        case select
        when "Daily"
            
            sec.getTimeSeries
            diplay(sec.daily[0..10])
            #display(sec.getTimeSeries)
        when "Weekly"
            sec.getTimeSeries
            diplay(sec.daily[0..7*10],7)
        when "back"
            break
        else
            puts "#{select} is not a valid action!"
        end
    }
end

#diplay(data, interval = 1) ⇒ Object



96
97
98
99
100
101
102
103
# File 'lib/alphavcli/cli.rb', line 96

def diplay(data,interval=1)
    
    data[0,data.length-2].each_with_index{|day,i|
    if i % interval == 0
        puts day[:day]+"  "+ "%.2f" % day[:price] + "  "+ "%.4f" % (((day[:price]-data[i+interval][:price])/data[i+interval][:price])*100) +'%'
    end
    }
end

#getkeyObject



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

def getkey
    puts "Please go to https://www.alphavantage.co/ to claim your free API key and enter it."
    @@apikey=gets.chomp

    open(File.expand_path('../../userfiles/apikey.json', File.dirname(__FILE__)),'w'){
        |f| f.puts JSON.generate(:apikey=>@apikey)
    }
end

#invalid_keyObject



109
110
111
112
# File 'lib/alphavcli/cli.rb', line 109

def invalid_key
    puts "that is an invalid key"
    getkey
end

#returnsFromTickerObject



89
90
91
92
93
94
# File 'lib/alphavcli/cli.rb', line 89

def returnsFromTicker
    puts "Please enter the ticker you are intersted in."
    ticker = gets.chomp
    new_result=Result.new_from_ticker(ticker)
    dataLanding(new_result)
end

#searchDisplayObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/alphavcli/cli.rb', line 48

def searchDisplay
    puts 'Enter a search term'
    query=gets.chomp
    if Alphavcli::Search.history.find{|search| search.query==query} != nil
        new_search = Alphavcli::Search.history.find{|search| search.query==query}
    else
        new_search = Alphavcli::Search.new(query)
    end
    puts 'Please select a security'
    new_search.results.each_with_index{|res,i|
    puts "#{i+1}. #{res.ticker} - #{res.name}"}
    puts "Select the number that cooresponds to the secutity you are interested in"
    select = gets.chomp.to_i
    dataLanding(new_search.results[select-1])
end