trades 0.2 → 0.2.1

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.
Files changed (2) hide show
  1. data/lib/trades.rb +20 -26
  2. metadata +2 -2
data/lib/trades.rb CHANGED
@@ -6,17 +6,18 @@ require 'colored'
6
6
  class Trades
7
7
  def initialize(args = [])
8
8
  begin
9
- puts "Connecting..."
9
+ puts "Connecting to bitcoincharts.com..."
10
10
  @socket = TCPSocket.open("bitcoincharts.com", 27007)
11
- @exchanges = []
11
+ @markets = []
12
12
  @last_prices = {}
13
13
 
14
- # Convert the exchange names to lowercase
15
- args.each do |x| @exchanges.push(x.downcase) end
16
- args.clear
14
+ # Convert the market names to lowercase
15
+ # (CHANGE THIS TO CASE INSENSITIVE REGEX)
16
+ args.each do |x| @markets.push(x.downcase) end
17
+ args.clear # no need to keep them in memory
17
18
 
18
- # thUSD | 2011-06-27 23:00:55 | USD | 17.05 | 0.5 |
19
- puts "Exchange | Date and time | Currency | Price | Volume |"
19
+ # thUSD | 2011-06-27 23:00:55 | USD | 17.05 | 0.5 |
20
+ puts "Exchange | Date and time | Currency | Price | Volume |"
20
21
 
21
22
  self.loop
22
23
  rescue
@@ -26,31 +27,24 @@ class Trades
26
27
 
27
28
  def loop
28
29
  while data = @socket.gets do
29
- if data.length > 0
30
+ if data.length > 0 then
30
31
  j = JSON.parse(data.strip.tr("\x00", '')) # Delete all \x00 weirdness
31
32
 
32
- if @exchanges.empty? or @exchanges.include?(j['symbol'].downcase) then
33
- line = j['symbol'].ljust(15) + "| "
34
- line += Time.at(j['timestamp']).strftime("%Y-%m-%d %H:%M:%S").\
35
- ljust(20)
36
- line += "| " + j['currency'].ljust(9) + "| "
33
+ if @markets.empty? or @markets.include?(j['symbol'].downcase) then
34
+ line = j['symbol'].ljust(15) + "| " # market
35
+ line += Time.at(j['timestamp']).strftime("%Y-%m-%d %H:%M:%S") + " | "
36
+ line += j['currency'].ljust(9) + "| " # currency
37
37
 
38
- # Coloring: Compare the current price and the last price
38
+ # Price: display higher price in green / lower price in red
39
39
  current_price = j['price'].round(2)
40
- last_price = @last_prices[j['symbol']].to_f # Its 0 on startup
40
+ last_price = @last_prices[j['symbol']].to_f # Its nil on startup
41
+ @last_prices[j['symbol']] = current_price # store for next comparison
41
42
 
42
- if current_price > last_price then
43
- line += current_price.to_s.ljust(7).green
44
- elsif current_price < last_price then
45
- line += current_price.to_s.ljust(7).red
46
- else
47
- line += current_price.to_s.ljust(7)
48
- end
43
+ price = current_price.to_s.ljust(9)
44
+ price = price.green if current_price > last_price unless last_price.zero?
45
+ price = price.red if current_price < last_price
49
46
 
50
- @last_prices[j['symbol']] = current_price
51
-
52
-
53
- line += "| " + j['volume'].round(2).to_s.ljust(7) + "|"
47
+ line += price + "| " + j['volume'].round(2).to_s.ljust(9) + " |"
54
48
 
55
49
  puts line
56
50
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: trades
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: "0.2"
5
+ version: 0.2.1
6
6
  platform: ruby
7
7
  authors:
8
8
  - Waxjar
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-06-29 00:00:00 +02:00
13
+ date: 2011-07-04 00:00:00 +02:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency