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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0733e8233d40ac86cdfca023692d567ce11944e0
4
- data.tar.gz: e679bc807e92310dc6d6bfd8a25fa718957b8e65
3
+ metadata.gz: fad40a350e22403bea67c5c8d4364b3d9572f7d3
4
+ data.tar.gz: 2371eaaf008f5f3189646da93d399ef3f12fcaf9
5
5
  SHA512:
6
- metadata.gz: 553927a28ed233019c8b0fbc64d1e53da33b3661dea17e40dfc3cf4c5a208ed6b7814a3c38706a11b712ec4a3bc27d1e86c407b2b388bf5a56cd8eb86d5ff9c8
7
- data.tar.gz: 81354db127d53a190996de414d2e3325219b91b4ec29bc72ac513eba140f1176319a0b8dbbfab9e5ac6307756155b07fbdbd41ff06c11c0a9fe1ad5aa4b9cb06
6
+ metadata.gz: 56724ee7fff60f70d1b0b310ede0fbffaef76b2f89f5cf26608e092110bdb2d769baea1a01d8743bbc9dd9af119cd157eec6c231fa0b3f7fd06eac436fb8144b
7
+ data.tar.gz: e8fc7dbdecdd7b42c851625cbebfc93c879cce834a47cb9ab11cbdd3335b69790552945012c39aa32df291fec499f213de917b5f94b37b7589042662c63acb42
@@ -1,14 +1,14 @@
1
1
  class CryptoMarket::Api
2
2
 
3
- def self.fetch_coin_data
4
- url = URI('https://coinmarketcap-nexuist.rhcloud.com/api/all')
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.validate_coin_data
10
- fetch_coin_data.reject do |coin_name, coin_attributes|
11
- coin_attributes.any? do |attribute, attribute_value|
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
@@ -3,24 +3,29 @@
3
3
 
4
4
  class CryptoMarket::Coin
5
5
  include CryptoMarket
6
- attr_accessor :name, :price, :change
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, price, change)
10
+ def initialize(name, price_usd, price_btc, market_cap_usd, percent_change_24h, last_updated_unix)
10
11
  @name = name
11
- @price = price
12
- @change = change.to_f > 0 ? "+#{change}" : change
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 ["Change:", "#{change}%"]
20
- price.each do |currency, price|
21
- t.add_row [currency.upcase, price.to_f.round(2)]
22
- end
23
- t.style = { all_separators: true, width: 30 }
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
- @coin_data = CryptoMarket::Api.validate_coin_data
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['symbol']
20
- price = coin['price']
21
- change = coin['change']
22
- CryptoMarket::Coin.new(name, price, change).tap do |new_coin|
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
- coin_attributes.each do |coin|
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 sort_by_price
50
- coins.sort_by { |coin| coin.price['usd'] }.reverse
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.change.to_f > 0 }.sort_by { |coin| coin.change.to_f }.reverse
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.change.to_f < 0 }.sort_by { |coin| coin.change.to_f }
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.change.to_f }.reverse
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
- sort_by_price.each do |coin|
61
+ sort_by_price_usd.each do |coin|
67
62
  table = terminal_table do |t|
68
- t.add_row [coin.name, "$#{coin.price['usd'].to_f.round(4)}"]
69
- t.style = { all_separators: true, width: 40 }
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.change}%"]
80
- t.style = { all_separators: true, width: 40 }
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: 15 }
86
+ t.style = { all_separators: true, width: 60 }
92
87
  end
93
88
  puts table
94
89
  end
@@ -1,3 +1,3 @@
1
1
  module CryptoMarket
2
- VERSION = "0.1.7"
2
+ VERSION = "0.1.8"
3
3
  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.7
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-03 00:00:00.000000000 Z
11
+ date: 2017-10-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler