silkroad 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d5050de75e2288298be54325a2ff9428d98c9cde
4
+ data.tar.gz: cf687338313a99fa297a2a5d532f67e66280bb00
5
+ SHA512:
6
+ metadata.gz: aa2924a294708adde8b1e45fb25011d318d0af511b3633a61ab74d1750c30f80b067a7482233c620000a5ebf80244270933aaaa43adb40266b1fd6e8348ffffc
7
+ data.tar.gz: aac2a971cfd4e06d27fe4b0b6cd821053ba8230921c6bc1254a6c9ed038c36a96a080c803b72a80b44266c1e8a7ba2712d7b1b61de84dad13b9ff3a14dddf300
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ test.rb
19
+ .DS_Store
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in silkroad.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Kyle Drake
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,76 @@
1
+ # Silkroad
2
+
3
+ A fast, thread-safe, simple, lightweight, batchable interface to the bitcoind JSON-RPC api. Uses HTTPClient for [high performance](http://bibwild.wordpress.com/2012/04/30/ruby-http-performance-shootout-redux).
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'silkroad'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install silkroad
18
+
19
+ ## Usage
20
+
21
+ Initialize the client:
22
+
23
+ ```ruby
24
+ silkroad = Silkroad::Client.new 'rpcuser', 'rpcpass'
25
+ ```
26
+
27
+ You can set a custom url:
28
+ ```ruby
29
+ silkroad = Silkroad::Client.new 'rpcuser', 'rpcpass', url: 'https://yourbitcoinddaemon.com:31337'
30
+ ```
31
+
32
+ Now you can make RPC API calls (see the [API calls list](https://en.bitcoin.it/wiki/Original_Bitcoin_client/API_calls_list)). Pass params as per the spec, and the result will be returned as an array or hash (depending on the call):
33
+
34
+ ```ruby
35
+ silkroad.rpc 'getbalance', 'derp@example.com' # => 31337
36
+ ```
37
+
38
+ Errors throw the `Silkroad::Client::Error` exception. Catch it if you want to do something custom:
39
+
40
+ ```ruby
41
+ begin
42
+ silkroad.rpc 'failcmd', 'fail'
43
+ rescue Silkroad::Client::Error => e
44
+ puts "Error: #{e.inspect}"
45
+ end
46
+ ```
47
+
48
+ ### Batching
49
+
50
+ If you use batching, it will throw all your requests into a JSON array, send them at once, and return all of them when they are done, per the [JSON-RPC spec](http://json-rpc.org/wiki/specification). Batch is much lower level, and does not raise exceptions on errors. You will need to look for the `response[index]['error']` in the return and handle it.
51
+
52
+ ```ruby
53
+ response = @silkroad.batch do
54
+ rpc 'getbalance', 'tyler@example.com'
55
+ rpc 'notworking', 'derp'
56
+ end
57
+
58
+ # response is:
59
+ [
60
+ {result: 31337, error: nil, id: nil},
61
+ {result: nil, error: {code: -32601, message: 'Method not found'}, id: nil}
62
+ ]
63
+ ```
64
+
65
+
66
+ ## Contributing
67
+
68
+ 1. Fork it
69
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
70
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
71
+ 4. Push to the branch (`git push origin my-new-feature`)
72
+ 5. Create new Pull Request
73
+
74
+
75
+
76
+ ![Winners Don't Use Drugs - William S. Sessions, Director, FBI](http://i.imgur.com/4KdKeOK.gif)
@@ -0,0 +1,12 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require "rake/testtask"
4
+
5
+ desc "Run all tests"
6
+ Rake::TestTask.new do |t|
7
+ t.libs << "spec"
8
+ t.test_files = FileList['test/*_test.rb']
9
+ t.verbose = true
10
+ end
11
+
12
+ task :default => :test
@@ -0,0 +1,6 @@
1
+ require "httpclient"
2
+ require "addressable/uri"
3
+ require "json"
4
+ require "silkroad/batch"
5
+ require "silkroad/client"
6
+ require "silkroad/version"
@@ -0,0 +1,14 @@
1
+ module Silkroad
2
+ class Batch < BasicObject
3
+ attr_reader :requests
4
+
5
+ def initialize(&block)
6
+ @requests = []
7
+ instance_eval &block
8
+ end
9
+
10
+ def rpc(meth, *params)
11
+ @requests << {method: meth, params: params}
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,46 @@
1
+ module Silkroad
2
+ class Client
3
+ class Error < StandardError; end
4
+ DEFAULT_PORT = 8332
5
+ JSONRPC_VERSION = '2.0'
6
+
7
+ def initialize(user, pass, opts={})
8
+ @user = user
9
+ @opts = opts
10
+ @url = Addressable::URI.parse @opts[:url] || "http://localhost:#{DEFAULT_PORT}"
11
+ @url.port = DEFAULT_PORT if @url.port.nil?
12
+
13
+ @http_client = HTTPClient.new
14
+ @http_client.set_auth @url.to_s, user, pass
15
+ end
16
+
17
+ def batch(requests=nil, &block)
18
+ requests ||= Batch.new(&block).requests
19
+ requests.each {|r| r[:jsonrpc] = JSONRPC_VERSION unless r[:jsonrpc]}
20
+ JSON.parse send(requests).body
21
+ end
22
+
23
+ def rpc(meth, *params)
24
+ response = send jsonrpc: JSONRPC_VERSION, method: meth, params: params
25
+
26
+ if response.status != 200
27
+ if response.body.nil?
28
+ raise Error.new "bitcoind returned HTTP status #{response.status} with no body: #{response.http_header.reason_phrase}"
29
+ else
30
+ response_obj = JSON.parse response.body
31
+ raise Error.new "bitcoind returned error code #{response_obj['error']['code']}: #{response_obj['error']['message']}"
32
+ end
33
+ else
34
+ JSON.parse(response.body)['result']
35
+ end
36
+ end
37
+
38
+ def send(request)
39
+ @http_client.post @url, request.to_json, {'Content-Type' => 'application/json'}
40
+ end
41
+
42
+ def inspect
43
+ "#<#{self.class} user=\"#{@user}\" @url=\"#{@url.to_s}\">"
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,3 @@
1
+ module Silkroad
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'silkroad/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "silkroad"
8
+ spec.version = Silkroad::VERSION
9
+ spec.authors = ["Kyle Drake"]
10
+ spec.email = ["kyledrake@gmail.com"]
11
+ spec.description = %q{A fast, thread-safe, simple, lightweight, batchable, sober interface to the bitcoind JSON-RPC api.}
12
+ spec.summary = %q{A fast, thread-safe, simple, lightweight, batchable, sober interface to the bitcoind JSON-RPC api. Uses HTTPClient for high performance (http://bibwild.wordpress.com/2012/04/30/ruby-http-performance-shootout-redux).}
13
+ spec.homepage = "https://github.com/kyledrake/silkroad"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "addressable"
22
+ spec.add_dependency "httpclient"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.3"
25
+ spec.add_development_dependency "rake"
26
+ spec.add_development_dependency "minitest"
27
+ spec.add_development_dependency "webmock"
28
+ spec.add_development_dependency "yard"
29
+ end
@@ -0,0 +1,64 @@
1
+ # Bundler.setup
2
+ require 'rubygems'
3
+ require './lib/silkroad.rb'
4
+ require 'minitest/autorun'
5
+ require 'webmock'
6
+ include WebMock::API
7
+
8
+ def url
9
+ 'http://user:pass@localhost:8332/'
10
+ end
11
+
12
+ def stub_with_body(body, response)
13
+ stub_request(:post, url).
14
+ with(body: body,
15
+ headers: {'Authorization'=>'Basic dXNlcjpwYXNz'}).
16
+ to_return(response)
17
+ end
18
+
19
+ describe Silkroad::Client do
20
+ before do
21
+ @silkroad = Silkroad::Client.new('user', 'pass')
22
+ end
23
+
24
+ it 'makes a call' do
25
+ stub_with_body(
26
+ {jsonrpc: "2.0", method: "getbalance", params: ["tyler@example.com"]},
27
+ {status: 200, body: {result: 31337}.to_json}
28
+ )
29
+
30
+ @silkroad.rpc('getbalance', 'tyler@example.com').must_equal 31337
31
+ end
32
+
33
+ it 'fails with error' do
34
+ stub_with_body(
35
+ {jsonrpc: "2.0", method: "failbalance", params: ["tyler@example.com"]},
36
+ {status: 200, body: {result: 31337}.to_json}
37
+ )
38
+ end
39
+
40
+ it 'makes a batch call' do
41
+ stub_request(:post, url).
42
+ with(
43
+ body: [
44
+ {method: 'getbalance', params: ['tyler@example.com'], jsonrpc: '2.0'},
45
+ {method: 'notworking', params: ['derp'], jsonrpc: '2.0'}
46
+ ].to_json,
47
+ headers: {'Authorization'=>'Basic dXNlcjpwYXNz', 'Content-Type'=>'application/json'}).
48
+ to_return(
49
+ :body => [
50
+ {result: 31337, error: nil, id: nil},
51
+ {result: nil, error: {code: -32601, message: 'Method not found'}, id: nil}
52
+ ].to_json
53
+ )
54
+
55
+ response = @silkroad.batch do
56
+ rpc 'getbalance', 'tyler@example.com'
57
+ rpc 'notworking', 'derp'
58
+ end
59
+
60
+ response.length.must_equal 2
61
+ response.first['result'].must_equal 31337
62
+ response.last['error']['message'].must_equal 'Method not found'
63
+ end
64
+ end
metadata ADDED
@@ -0,0 +1,157 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: silkroad
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Kyle Drake
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-05-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: addressable
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: httpclient
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: webmock
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: yard
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: A fast, thread-safe, simple, lightweight, batchable, sober interface
112
+ to the bitcoind JSON-RPC api.
113
+ email:
114
+ - kyledrake@gmail.com
115
+ executables: []
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - .gitignore
120
+ - Gemfile
121
+ - LICENSE.txt
122
+ - README.md
123
+ - Rakefile
124
+ - lib/silkroad.rb
125
+ - lib/silkroad/batch.rb
126
+ - lib/silkroad/client.rb
127
+ - lib/silkroad/version.rb
128
+ - silkroad.gemspec
129
+ - test/client_test.rb
130
+ homepage: https://github.com/kyledrake/silkroad
131
+ licenses:
132
+ - MIT
133
+ metadata: {}
134
+ post_install_message:
135
+ rdoc_options: []
136
+ require_paths:
137
+ - lib
138
+ required_ruby_version: !ruby/object:Gem::Requirement
139
+ requirements:
140
+ - - '>='
141
+ - !ruby/object:Gem::Version
142
+ version: '0'
143
+ required_rubygems_version: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - '>='
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ requirements: []
149
+ rubyforge_project:
150
+ rubygems_version: 2.0.0
151
+ signing_key:
152
+ specification_version: 4
153
+ summary: A fast, thread-safe, simple, lightweight, batchable, sober interface to the
154
+ bitcoind JSON-RPC api. Uses HTTPClient for high performance (http://bibwild.wordpress.com/2012/04/30/ruby-http-performance-shootout-redux).
155
+ test_files:
156
+ - test/client_test.rb
157
+ has_rdoc: