Class: Bitbot::Blockchain

Inherits:
Object
  • Object
show all
Defined in:
lib/bitbot/blockchain.rb

Instance Method Summary collapse

Constructor Details

#initialize(id, password1, password2) ⇒ Blockchain

Returns a new instance of Blockchain.



7
8
9
10
11
# File 'lib/bitbot/blockchain.rb', line 7

def initialize(id, password1, password2)
  @id = id
  @password1 = password1
  @password2 = password2
end

Instance Method Details

#create_deposit_address_for_user_id(user_id) ⇒ Object



32
33
34
35
36
37
# File 'lib/bitbot/blockchain.rb', line 32

def create_deposit_address_for_user_id(user_id)
  self.request(:merchant, :new_address,
               :password => @password1,
               :second_password => @password2,
               :label => user_id)
end

#create_payment(address, amount, fee) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/bitbot/blockchain.rb', line 56

def create_payment(address, amount, fee)
  response = self.request(:merchant, :payment,
                          :password => @password1,
                          :second_password => @password2,
                          :to => address,
                          :amount => amount,
                          :fee => fee)
  response
end

#get_addresses_in_walletObject



43
44
45
46
# File 'lib/bitbot/blockchain.rb', line 43

def get_addresses_in_wallet
  response = self.request(:merchant, :list, :password => @password1)
  response["addresses"]
end

#get_balance_for_address(address, confirmations = 1) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/bitbot/blockchain.rb', line 48

def get_balance_for_address(address, confirmations = 1)
  response = self.request(:merchant, :address_balance,
                          :password => @password1,
                          :address => address,
                          :confirmations => confirmations)
  response["balance"]
end

#get_details_for_address(address) ⇒ Object



39
40
41
# File 'lib/bitbot/blockchain.rb', line 39

def get_details_for_address(address)
  self.request(:address, address, :format => :json)
end

#get_exchange_ratesObject



66
67
68
# File 'lib/bitbot/blockchain.rb', line 66

def get_exchange_rates
  self.request(:ticker)
end

#request(api, action = nil, params = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/bitbot/blockchain.rb', line 13

def request(api, action = nil, params = {})
  path = if api == :merchant
           "merchant/#{@id}/#{action}"
         elsif api == :ticker
           "ticker"
         else
           "#{api}/#{action}"
         end
  url = "https://blockchain.info/#{path}?"
  params.each do |key, value|
    url += "#{key}=#{CGI::escape value.to_s}&"
  end

  response = HTTParty.get(url)
  raise "HTTP Error: #{response}" unless response.code == 200

  JSON.parse(response.body)
end