Created
May 10, 2013 03:15
-
-
Save yrral86/5552183 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Bitcoin | |
class Client | |
include Singleton | |
def initialize | |
config_file = File.open(File.join(Rails.root, "config", "bitcoin.yml")) | |
config = YAML::load(config_file)[Rails.env].symbolize_keys | |
@client = JsonWrapper.new(config[:url], | |
config[:username], | |
config[:password] | |
) | |
end | |
def method_missing(method, *args) | |
@client.request({ | |
:method => method.to_s.gsub(/\_/, ""), | |
:params => args | |
} | |
) | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'net/http' | |
require 'addressable/uri' | |
require 'json' | |
module Bitcoin | |
class JsonWrapper | |
def initialize(url, username, password) | |
@address = Addressable::URI.parse(url) | |
@username = username | |
@password = password | |
end | |
def request(params) | |
result = nil | |
full_params = params.merge({ | |
:jsonrpc => "2.0", | |
:id => (rand * 10 ** 12).to_i.to_s | |
}) | |
request_body = full_params.to_json | |
Net::HTTP.start(@address.host, @address.port) do |connection| | |
post = Net::HTTP::Post.new(@address.path) | |
post.body = request_body | |
post.basic_auth(@username, @password) | |
result = connection.request(post) | |
result = JSON.parse(result.body) | |
end | |
if error = result["error"] | |
raise "#{error["message"]}, request was #{request_body}" | |
end | |
result = result["result"] | |
result | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Bitcoin | |
module Util | |
def self.valid_bitcoin_address?(address) | |
# We don't want leading/trailing spaces to pollute addresses | |
(address == address.strip) and Bitcoin::Client.instance.validate_address(address)['isvalid'] | |
end | |
def self.my_bitcoin_address?(address) | |
Bitcoin::Client.instance.validate_address(address)['ismine'] | |
end | |
def self.get_account(address) | |
Bitcoin::Client.instance.get_account(address) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment