Sha256: 6d23114464a5a524d7d690b3b7db699f9dcbb0079b1077f10f6b57f105963559

Contents?: true

Size: 1.96 KB

Versions: 14

Compression:

Stored size: 1.96 KB

Contents

require 'httparty'
require 'cgi'
require 'json'

module Bitbot
  class Blockchain
    def initialize(id, password1, password2)
      @id = id
      @password1 = password1
      @password2 = password2
    end

    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

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

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

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

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

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

    def get_exchange_rates
      self.request(:ticker)
    end
  end
end

Version data entries

14 entries across 14 versions & 2 rubygems

Version Path
tipjar-0.1.196 lib/bitbot/blockchain.rb
tipjar-0.1.195 lib/bitbot/blockchain.rb
tipjar-0.1.194 lib/bitbot/blockchain.rb
tipjar-0.1.193 lib/bitbot/blockchain.rb
tipjar-0.1.192 lib/bitbot/blockchain.rb
tipjar-0.1.191 lib/bitbot/blockchain.rb
tipjar-0.1.19 lib/bitbot/blockchain.rb
tipjar-0.1.18 lib/bitbot/blockchain.rb
tipjar-0.1.171 lib/bitbot/blockchain.rb
tipjar-0.1.16 lib/bitbot/blockchain.rb
bitbot-0.0.4 lib/bitbot/blockchain.rb
bitbot-0.0.3 lib/bitbot/blockchain.rb
bitbot-0.0.2 lib/bitbot/blockchain.rb
bitbot-0.0.1 lib/bitbot/blockchain.rb