Module: Bitbot::Common

Included in:
Plugin
Defined in:
lib/bitbot/plugin/common.rb

Instance Method Summary collapse

Instance Method Details

#blockchainObject

Returns a Blockchain API helper. Everyone uses the same one, but it doesn’t keep any state so it’s fine.



39
40
41
42
43
44
# File 'lib/bitbot/plugin/common.rb', line 39

def blockchain
  @@cached_blockchain ||=
    Bitbot::Blockchain.new(config['blockchain']['wallet_id'],
                           config['blockchain']['password1'],
                           config['blockchain']['password2'])
end

#cached_addressesObject



8
9
10
# File 'lib/bitbot/plugin/common.rb', line 8

def cached_addresses
  @cached_addresses
end

#cached_addresses=(new) ⇒ Object



12
13
14
# File 'lib/bitbot/plugin/common.rb', line 12

def cached_addresses=(new)
  @cached_addresses = new
end

#dbObject

Returns a database handle for use in the current thread.



30
31
32
33
# File 'lib/bitbot/plugin/common.rb', line 30

def db
  Thread.current[:bitbot_db] ||=
    Bitbot::Database.new(File.join(config['data']['path'], "bitbot.db"))
end

#exchange_rate(currency) ⇒ Object



16
17
18
19
20
21
# File 'lib/bitbot/plugin/common.rb', line 16

def exchange_rate(currency)
  if @cached_exchange_rates
    return @cached_exchange_rates[currency]["15m"]
  end
  nil
end

#exchange_rates=(new) ⇒ Object



23
24
25
# File 'lib/bitbot/plugin/common.rb', line 23

def exchange_rates=(new)
  @cached_exchange_rates = new
end

#satoshi_to_str(satoshi) ⇒ Object

Some number formatting helpers



60
61
62
63
64
# File 'lib/bitbot/plugin/common.rb', line 60

def satoshi_to_str(satoshi)
  str = "฿%.8f" % (satoshi.to_f / 10**8)
  # strip trailing 0s
  str.gsub(/0*$/, '')
end

#satoshi_to_usd(satoshi) ⇒ Object



66
67
68
69
70
71
72
73
# File 'lib/bitbot/plugin/common.rb', line 66

def satoshi_to_usd(satoshi)
  rate = exchange_rate("USD")
  if rate
    "$%.2f" % (satoshi.to_f / 10**8 * rate)
  else
    "$?"
  end
end

#satoshi_with_usd(satoshi) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/bitbot/plugin/common.rb', line 75

def satoshi_with_usd(satoshi)
  btc_str = satoshi_to_str(satoshi)
  if satoshi < 0
    btc_str = btc_str.irc(:red)
  else
    btc_str = btc_str.irc(:green)
  end

  usd_str = "[".irc(:grey) + satoshi_to_usd(satoshi).irc(:blue) + "]".irc(:grey)

  "#{btc_str} #{usd_str}"
end

#user_with_username(username) ⇒ Object

Returns the User for the given username. If you want to get a User based on nick, User(nick) is easier.



50
51
52
53
54
55
# File 'lib/bitbot/plugin/common.rb', line 50

def user_with_username(username)
  bot.user_list.each do |user|
    return user if user.user == username
  end
  nil
end