Module: ItsyBtc::Lookup

Extended by:
Lookup
Included in:
Lookup
Defined in:
lib/itsy-btc/lookup.rb

Instance Method Summary collapse

Instance Method Details

#balance(address) ⇒ Object



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
# File 'lib/itsy-btc/lookup.rb', line 43

def balance(address)
  html = open(ItsyBtc.blockexplorer_url + "/address/#{address}").read
  received_btc = nil
  sent_btc = nil
  if html =~ /Received BTC: ([0-9.]+)/
    received_btc = BigDecimal.new($1)
  end
  if html =~ /Sent BTC: ([0-9.]+)/
    sent_btc = BigDecimal.new($1)
  end
  if received_btc && sent_btc
    btc = received_btc - sent_btc
    (btc * 1e8).to_i
  else
    puts "Can't parse the balance from blockexplorer.com's HTML. (They must have changed it.)"
    false
  end
rescue OpenURI::HTTPError => e
  puts "Error looking up balance: #{e.message}"
  case e.io.status[0].to_i
  when 400
    puts "The address is invalid."
  else
    puts "Make sure you have an internet connection and can load blockexplorer.com."
  end
  false
rescue SocketError => e
  puts "Error looking up balance: #{e.message}"
  puts "Make sure you have an internet connection and can load blockexplorer.com."
  false
end

#tx(hash) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/itsy-btc/lookup.rb', line 5

def tx(hash)
  json_tx = open(ItsyBtc.blockexplorer_url + "/rawtx/#{hash}").read
  Bitcoin::Protocol::Tx.from_json(json_tx)
rescue OpenURI::HTTPError => e
  puts "Error looking up transaction: #{e.message}"
  case e.io.status[0].to_i
  when 400
    puts "The transaction hash is invalid."
  when 404
    puts "blockexplorer.com doesn't know about the transaction."
  else
    puts "Make sure you have an internet connection and can load blockexplorer.com."
  end
  false
rescue SocketError => e
  puts "Error looking up transaction: #{e.message}"
  puts "Make sure you have an internet connection and can load blockexplorer.com."
  false
end

#unspent_outputs(address) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/itsy-btc/lookup.rb', line 25

def unspent_outputs(address)
  json = open("http://blockchain.info/unspent?active=#{address}&format=json").read
  data = JSON.parse(json, symbolize_names: true)
  data[:unspent_outputs]
rescue OpenURI::HTTPError => e
  if e.io.status[0].to_i == 500
    []
  else
    puts "Error looking up unspent outputs: #{e.message}"
    puts "Make sure you have an internet connection and can load blockchain.info."
    false
  end
rescue SocketEror => e
  puts "Error looking up unspent outputs: #{e.message}"
  puts "Make sure you have an internet connection and can load blockchain.info."
  false
end