crypto_market 0.1.7 → 0.1.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/crypto_market/api.rb +5 -5
- data/lib/crypto_market/coin.rb +14 -9
- data/lib/crypto_market/currencies.rb +21 -26
- data/lib/crypto_market/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fad40a350e22403bea67c5c8d4364b3d9572f7d3
|
4
|
+
data.tar.gz: 2371eaaf008f5f3189646da93d399ef3f12fcaf9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 56724ee7fff60f70d1b0b310ede0fbffaef76b2f89f5cf26608e092110bdb2d769baea1a01d8743bbc9dd9af119cd157eec6c231fa0b3f7fd06eac436fb8144b
|
7
|
+
data.tar.gz: e8fc7dbdecdd7b42c851625cbebfc93c879cce834a47cb9ab11cbdd3335b69790552945012c39aa32df291fec499f213de917b5f94b37b7589042662c63acb42
|
data/lib/crypto_market/api.rb
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
class CryptoMarket::Api
|
2
2
|
|
3
|
-
def self.
|
4
|
-
url = URI('https://coinmarketcap
|
3
|
+
def self.api_call
|
4
|
+
url = URI('https://api.coinmarketcap.com/v1/ticker/')
|
5
5
|
response = Net::HTTP.get(url)
|
6
6
|
JSON.parse(response)
|
7
7
|
end
|
8
8
|
|
9
|
-
def self.
|
10
|
-
|
11
|
-
|
9
|
+
def self.fetch_coin_data
|
10
|
+
api_call.reject do |coin, _|
|
11
|
+
coin.any? do |_, attribute_value|
|
12
12
|
attribute_value == '?' || attribute_value.nil?
|
13
13
|
end
|
14
14
|
end
|
data/lib/crypto_market/coin.rb
CHANGED
@@ -3,24 +3,29 @@
|
|
3
3
|
|
4
4
|
class CryptoMarket::Coin
|
5
5
|
include CryptoMarket
|
6
|
-
attr_accessor :name, :
|
6
|
+
attr_accessor :name, :price_usd, :price_btc, :market_cap_usd, :percent_change_1h,
|
7
|
+
:percent_change_24h, :percent_change_7d, :last_updated_unix
|
7
8
|
|
8
9
|
# Instantiate Coin with the attributes below, adds + sign for positive change numbers
|
9
|
-
def initialize(name,
|
10
|
+
def initialize(name, price_usd, price_btc, market_cap_usd, percent_change_24h, last_updated_unix)
|
10
11
|
@name = name
|
11
|
-
@
|
12
|
-
@
|
12
|
+
@price_usd = price_usd
|
13
|
+
@price_btc = price_btc
|
14
|
+
@market_cap_usd = market_cap_usd
|
15
|
+
@percent_change_24h = percent_change_24h
|
16
|
+
@last_updated_unix = last_updated_unix
|
13
17
|
end
|
14
18
|
|
15
19
|
# Prints out the coin attributes for the Coin object with terminal-table gem
|
16
20
|
def attributes
|
17
21
|
table = terminal_table do |t|
|
18
22
|
t.title = name.upcase
|
19
|
-
t.add_row ["
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
t.
|
23
|
+
t.add_row ["Price USD:", "#{price_usd}%"]
|
24
|
+
t.add_row ["Price BTC:", "#{price_btc}%"]
|
25
|
+
t.add_row ["Market Cap USD:", "#{market_cap_usd}%"]
|
26
|
+
t.add_row ["Change Last 24h:", "#{percent_change_24h}%"]
|
27
|
+
t.add_row ["Last Updated:", "#{Time.at(last_updated_unix.to_i)}"]
|
28
|
+
t.style = { all_separators: true, width: 60 }
|
24
29
|
end
|
25
30
|
puts table
|
26
31
|
end
|
@@ -5,7 +5,7 @@ class CryptoMarket::Currencies
|
|
5
5
|
attr_accessor :coins
|
6
6
|
# Store all the currencies in an Array
|
7
7
|
def initialize
|
8
|
-
@
|
8
|
+
@api_data = CryptoMarket::Api.fetch_coin_data
|
9
9
|
@coins = []
|
10
10
|
end
|
11
11
|
|
@@ -16,57 +16,52 @@ class CryptoMarket::Currencies
|
|
16
16
|
|
17
17
|
# Instantiates a new Coin based on hash that gets passed in and set it's attributes
|
18
18
|
def create_coin(coin)
|
19
|
-
name = coin['
|
20
|
-
|
21
|
-
|
22
|
-
|
19
|
+
name = coin['name']
|
20
|
+
price_usd = coin['price_usd']
|
21
|
+
price_btc = coin['price_btc']
|
22
|
+
market_cap_usd = coin['market_cap_usd']
|
23
|
+
percent_change_24h = coin['percent_change_24h']
|
24
|
+
last_updated_unix = coin['last_updated']
|
25
|
+
CryptoMarket::Coin.new(name, price_usd, price_btc, market_cap_usd,
|
26
|
+
percent_change_24h, last_updated_unix).tap do |new_coin|
|
23
27
|
coins << new_coin
|
24
28
|
end
|
25
29
|
end
|
26
30
|
|
27
31
|
# Call the create_coin method for each Coin hash
|
28
32
|
def create_coins_from_attributes
|
29
|
-
|
33
|
+
@api_data.each do |coin|
|
30
34
|
create_coin(coin)
|
31
35
|
end
|
32
36
|
end
|
33
37
|
|
34
|
-
# Returns an Array with hashes containing selected information for each Coin
|
35
|
-
def coin_attributes
|
36
|
-
@coin_data.map do |coin_name, coin_attributes|
|
37
|
-
coin_attributes.select do |attribute, attribute_value|
|
38
|
-
attribute == 'symbol' || attribute == 'price' || attribute == 'change'
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
38
|
# Returns an instance of Coin for the specific Coin that user is looking for
|
44
39
|
def find_by_number(input)
|
45
40
|
coins[input - 1]
|
46
41
|
end
|
47
42
|
|
48
43
|
# Ranks the coins from based on their USD price
|
49
|
-
def
|
50
|
-
coins.sort_by { |coin| coin.
|
44
|
+
def sort_by_price_usd
|
45
|
+
coins.sort_by { |coin| coin.price_usd }.reverse
|
51
46
|
end
|
52
47
|
|
53
48
|
# Return sorted Array based on user input (positive, negative or default)
|
54
49
|
def sort_by_change(input)
|
55
50
|
if input == 'positive'
|
56
|
-
coins.select { |coin| coin.
|
51
|
+
coins.select { |coin| coin.percent_change_24h.to_f > 0 }.sort_by { |coin| coin.percent_change_24h.to_f }.reverse
|
57
52
|
elsif input == 'negative'
|
58
|
-
coins.select { |coin| coin.
|
53
|
+
coins.select { |coin| coin.percent_change_24h.to_f < 0 }.sort_by { |coin| coin.percent_change_24h.to_f }
|
59
54
|
else
|
60
|
-
coins.sort_by { |coin| coin.
|
55
|
+
coins.sort_by { |coin| coin.percent_change_24h.to_f }.reverse
|
61
56
|
end
|
62
57
|
end
|
63
58
|
|
64
59
|
# Prints out the price for all Coin objects with terminal-table gem
|
65
60
|
def print_sorted_prices
|
66
|
-
|
61
|
+
sort_by_price_usd.each do |coin|
|
67
62
|
table = terminal_table do |t|
|
68
|
-
t.add_row [coin.name, "$#{coin.
|
69
|
-
t.style = { all_separators: true, width:
|
63
|
+
t.add_row [coin.name, "$#{coin.price_usd.to_f.round(4)}"]
|
64
|
+
t.style = { all_separators: true, width: 60 }
|
70
65
|
end
|
71
66
|
puts table
|
72
67
|
end
|
@@ -76,8 +71,8 @@ class CryptoMarket::Currencies
|
|
76
71
|
def print_sorted_changes(input = nil)
|
77
72
|
sort_by_change(input).each do |coin|
|
78
73
|
table = terminal_table do |t|
|
79
|
-
t.add_row [coin.name, "#{coin.
|
80
|
-
t.style = { all_separators: true, width:
|
74
|
+
t.add_row [coin.name, "#{coin.percent_change_24h}%"]
|
75
|
+
t.style = { all_separators: true, width: 60 }
|
81
76
|
end
|
82
77
|
puts table
|
83
78
|
end
|
@@ -88,7 +83,7 @@ class CryptoMarket::Currencies
|
|
88
83
|
coins.each_with_index do |coin, index|
|
89
84
|
table = terminal_table do |t|
|
90
85
|
t.add_row [index + 1, coin.name]
|
91
|
-
t.style = { all_separators: true, width:
|
86
|
+
t.style = { all_separators: true, width: 60 }
|
92
87
|
end
|
93
88
|
puts table
|
94
89
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: crypto_market
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sam Ojling
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-10-
|
11
|
+
date: 2017-10-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|