summarylogtreecommitdiffstats
path: root/crypto-cli
blob: f5220839d49ec157d4e3f64383060d6a2ea1522e (plain)
1
2
3
4
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
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/usr/bin/env ruby
# This file is part of crypto-cli, https://github.com/elisaado/crypto-cli
# The prices are provided by Moonstats, an amazing website

require 'faraday'
require 'json'

currencies = []

# I love long lines
JSON.parse(Faraday.get('https://www.moonstats.com/cache/json/coins.json').body).sort_by {|currency| currency['name'].downcase}.each { |currency| currencies.push({slug: currency['slug'], name: currency['name']})}

if ARGV[0].to_s.strip.size > 0 && (ARGV[1].to_s.strip.size > 0 || ARGV[0].to_s.downcase == 'list')
  case ARGV[0].downcase
  when 'list'
    currencies.each do |currency|
      puts "Name: #{currency[:name]}"
      puts "Slug: #{currency[:slug].upcase}"
      puts
    end
  when 'search'
    term = ARGV[1].downcase
    results = currencies.select {|currency| currency[:name].match(/#{term}/i) || currency[:slug].match(/#{term}/i) }
    
    if results.size > 0
      results.each do |currency|
        puts "Name: #{currency[:name]}"
        puts "Slug: #{currency[:slug].upcase}"
        puts
      end
    else
      puts 'No currencies were found using that term...'
    end
  when 'vearch'
    term = ARGV[1].downcase
    results = currencies.select {|currency| currency[:name].match(/#{term}/i) || currency[:slug].match(/#{term}/i) }
    
    if results.size > 0
      results.each do |currency|
        puts "Name: #{currency[:name]}"
        puts "Slug: #{currency[:slug].upcase}"
        puts
        puts 'Price:'
        prices = JSON.parse(Faraday.get("https://www.moonstats.com/cache/json/#{currency[:slug]}/latest.json").body)
        if ARGV[2].to_s.strip.size > 0
          price = prices[ARGV[2].strip.downcase]
          if price.nil?
            puts 'Unkown currency provided as third argument, using default...'
            puts "  BTC: Ƀ#{prices['btc']}"
            puts "  EUR: €#{prices['eur']}"
            puts "  USD: $#{prices['usd']}"
          else
            puts "  #{ARGV[2].upcase}: #{price}#{ARGV[2]}"
          end
        else
          puts "  BTC: Ƀ#{prices['btc']}"
          puts "  EUR: €#{prices['eur']}"
          puts "  USD: $#{prices['usd']}"
        end

        puts
        puts 'Change:'
        puts "  #{JSON.parse(Faraday.get("https://www.moonstats.com/cache/json/#{currency[:slug]}/cmc.json").body)[2]}%"
        puts

        sleep 0.5 if results.size > 1 # Don't wanna get rate-limited or DOS the website
      end
    else
      puts 'No currencies were found using that term...'
      puts
    end
  when 'view'
    term = ARGV[1].downcase
    currency = currencies.find {|currency| currency[:name].downcase == term.downcase || currency[:slug].downcase == term}

    if !currency.nil?
      puts "Name: #{currency[:name]}"
      puts "Slug: #{currency[:slug].upcase}"
      puts
      puts 'Price:'
      prices = JSON.parse(Faraday.get("https://www.moonstats.com/cache/json/#{currency[:slug]}/latest.json").body)
      if ARGV[2].to_s.strip.size > 0
        price = prices[ARGV[2].strip.downcase]
        if price.nil?
          puts 'Unkown currency provided as third argument, using default...'
          puts "  BTC: Ƀ#{prices['btc']}"
          puts "  EUR: €#{prices['eur']}"
          puts "  USD: $#{prices['usd']}"
        else
          puts "  #{ARGV[2].upcase}: #{price}#{ARGV[2]}"
        end
      else
        puts "  BTC: Ƀ#{prices['btc']}"
        puts "  EUR: €#{prices['eur']}"
        puts "  USD: $#{prices['usd']}"
      end

      puts
      puts 'Change:'
      puts "  #{JSON.parse(Faraday.get("https://www.moonstats.com/cache/json/#{currency[:slug]}/cmc.json").body)[2]}%"
      puts
    else
      puts 'Unkown currency provided as second argument...'
      puts
    end
  end 
  puts 'Everything provided by Moonstats'
  puts 'Crypto-CLI is made by Eli Saado and contributors'
else
  puts 'Usage: crypto-cli [command] [command arguments]'
  puts '  list -- List all cryptocurrencies this program supports (sorted alphabetically)'
  puts '  search [term] -- Search through the list mentioned earlier'
  puts '  vearch [term] [currency to show price in (optional)] -- Search and view the matched cryptocurrencies (BTC, EUR, and USD)'
  puts '  view [slug or name] [currency to show price in (optional)] -- View cryptocurrency price (BTC, EUR, and USD)'
  puts
  puts 'Everything provided by Moonstats'
  puts 'Crypto-CLI is made by Eli Saado and contributors'
  exit
end