rpcjson 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ gem "json", ">= 0"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "shoulda", ">= 0"
10
+ gem "bundler", "~> 1.0.0"
11
+ gem "jeweler", "~> 1.5.2"
12
+ gem "rcov", ">= 0"
13
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Jonathan Jeffus
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,66 @@
1
+ = rpcjson
2
+
3
+ This is my implementation of JSON-RPC as detailed in the original 2007 1.1 specification found here:
4
+ http://json-rpc.org/wiki/specification
5
+
6
+ And the newer non-standardized 2.0 specification:
7
+ http://groups.google.com/group/json-rpc/web/json-rpc-2-0
8
+
9
+ I wrote this because it didn't appear that any of the JSON-RPC implementations worked anymore for modern Ruby. There were numerous errors, missing gems and nothing worked.
10
+
11
+ == Example usage
12
+
13
+ Here's an example which connects to bitcoind and asks it to getinfo.
14
+
15
+ require 'rubygems'
16
+ require 'rpcjson'
17
+
18
+ # 1.1 is required because bitcoind does not speak 2.0
19
+ bc = RPC::JSON::Client.new 'http://username:password@127.0.0.1:8332', 1.1
20
+
21
+ # JSON-RPC error objects are raised as Ruby objects of the type
22
+ # RPC::JSON::Client::Error. The original object is available at
23
+ # e.error
24
+
25
+ begin
26
+
27
+ # First get server info
28
+ puts "Getting server information..."
29
+ info = bc.getinfo
30
+ info.each_key do |key|
31
+ puts "#{key}: #{info[key]}"
32
+ end
33
+
34
+ # Will raise error
35
+ puts "Shutting down the server..."
36
+ bc.st0p
37
+ rescue RPC::JSON::Client::Error => e
38
+ puts "Got an error: #{e}: #{e.error.to_json}"
39
+ end
40
+
41
+ This software has been tested on Ruby EE 2010.02. Let me know how it works elsewhere.
42
+
43
+ == Contributing to rpcjson
44
+
45
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
46
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
47
+ * Fork the project
48
+ * Start a feature/bugfix branch
49
+ * Commit and push until you are happy with your contribution
50
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
51
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
52
+
53
+ == Donations
54
+
55
+ I wrote this mainly to provide support in Ruby for Bitcoin. If you happen to have a bitcoin account and you feel like supporting this project - please send a few coins my way.
56
+
57
+ Receiving address: 14SjyYcHakwmqu8QQYh6emjy47fW8rhUKk
58
+
59
+ If you haven't heard about Bitcoin, well, check out the system that makes PayPal obsolete.
60
+ http://www.bitcoin.org
61
+
62
+ == Copyright
63
+
64
+ Copyright (c) 2010 Jonathan Jeffus. See LICENSE.txt for
65
+ further details.
66
+
data/Rakefile ADDED
@@ -0,0 +1,53 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "rpcjson"
16
+ gem.homepage = "http://github.com/jjeffus/rpcjson"
17
+ gem.license = "MIT"
18
+ gem.summary = %Q{Modern JSON-RPC 1.1 and 2.0 implementation.}
19
+ gem.description = %Q{This module aims to be a complete implementation of JSON-RPC 1.1 and 2.0.}
20
+ gem.email = "jjeffus@gmail.com"
21
+ gem.authors = ["Jonathan Jeffus"]
22
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
23
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
24
+ gem.add_runtime_dependency 'json', '> 0.0'
25
+ # gem.add_development_dependency 'rspec', '> 1.2.3'
26
+ end
27
+ Jeweler::RubygemsDotOrgTasks.new
28
+
29
+ require 'rake/testtask'
30
+ Rake::TestTask.new(:test) do |test|
31
+ test.libs << 'lib' << 'test'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.verbose = true
34
+ end
35
+
36
+ require 'rcov/rcovtask'
37
+ Rcov::RcovTask.new do |test|
38
+ test.libs << 'test'
39
+ test.pattern = 'test/**/test_*.rb'
40
+ test.verbose = true
41
+ end
42
+
43
+ task :default => :test
44
+
45
+ require 'rake/rdoctask'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "rpcjson #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
data/lib/rpcjson.rb ADDED
@@ -0,0 +1,108 @@
1
+ require 'net/http'
2
+ require 'uri'
3
+ require 'json'
4
+
5
+ class RPC
6
+ class JSON
7
+ class Client
8
+ class Error < RuntimeError
9
+ attr_accessor :error
10
+ def initialize(error)
11
+ @error = error
12
+ end
13
+ end
14
+
15
+ def initialize(url, version = 2.0)
16
+ @id = 1
17
+ @uri = URI.parse(url)
18
+ @version = version
19
+ end
20
+
21
+ def method_missing(func, *args)
22
+ json = {
23
+ # jsonrpc
24
+ # A String specifying the version of the JSON-RPC protocol. MUST be exactly "2.0".
25
+ 'jsonrpc' => @version.to_s,
26
+ # method
27
+ # A String containing the name of the method to be invoked.
28
+ 'method' => func,
29
+ # id
30
+ # An identifier established by the Client that MUST contain a String, Number, or NULL value if included. If it is not included it is assumed to be a notification. The value SHOULD normally not be Null [1] and Numbers SHOULD NOT contain fractional parts [2]
31
+ 'id' => @id
32
+ }
33
+
34
+ # 1.0/1.1:
35
+ # params - An Array of objects to pass as arguments to the method.
36
+ # 2.0:
37
+ # params
38
+ # A Structured value that holds the parameter values to be used during the invocation of the method. This member MAY be omitted.
39
+
40
+ # params are expected always in 1.0/1.1. Not required in 2.0.
41
+ if @version < 2.0 or (args != nil and args.length != 0)
42
+ json['params'] = args
43
+ end
44
+
45
+ body = JSON(json)
46
+ # puts "Sending: #{body}"
47
+
48
+ http = Net::HTTP.new(@uri.host, @uri.port)
49
+ request = Net::HTTP::Get.new(@uri.request_uri)
50
+ if @uri.user != nil
51
+ request.basic_auth(@uri.user, @uri.password)
52
+ end
53
+ request.body = body
54
+ response = http.request(request)
55
+ answer = JSON( response.body )
56
+
57
+ # p answer
58
+
59
+ # 1.0/1.1
60
+ # jsonrpc: NOT INCLUDED
61
+ # 2.0
62
+ # jsonrpc:
63
+ # A String specifying the version of the JSON-RPC protocol. MUST be exactly "2.0".
64
+ if answer['jsonrpc'] == nil
65
+ if @version >= 2.0
66
+ STDERR.puts "Error: We're configured to use JSON-RPC #{@version}, but the server appears to be using an older implementation."
67
+ end
68
+ else
69
+ if answer['jsonrpc'].to_f < @version
70
+ STDERR.puts "Error: We're configured to use JSON-RPC #{@version}, but the server appears to be using #{answer['jsonrpc']}"
71
+ elsif answer['jsonrpc'].to_f > @version
72
+ STDERR.puts "Error: We're configured to use JSON-RPC #{@version}, but the server appears to be using #{answer['jsonrpc']}"
73
+ end
74
+ end
75
+
76
+ # 1.0/1.1
77
+ # error - An Error object if there was an error invoking the method. It must be null if there was no error.
78
+ # 2.0
79
+ # error
80
+ # This member is REQUIRED on error.
81
+ # This member MUST NOT exist if there was no error triggered during invocation.
82
+ # The value for this member MUST be an Object as defined in section 5.1.
83
+
84
+ # It will be nil in either case if there was no error.
85
+ if answer['error'] != nil
86
+ if @version >= 2.0
87
+ raise Error.new(answer['error']), answer['error']['message']
88
+ else
89
+ # No standard for < 2.0 error objects.
90
+ # Bitcoind is 1.1 and seems to follow 2.0 standard, other clients?
91
+ raise Error.new(answer['error']), 'JSON-RPC Error'
92
+ end
93
+ end
94
+
95
+ @id += 1
96
+
97
+ # 1.0
98
+ # result - The Object that was returned by the invoked method. This must be null in case there was an error invoking the method.
99
+ # 2.0
100
+ # result
101
+ # This member is REQUIRED on success.
102
+ # This member MUST NOT exist if there was an error invoking the method.
103
+ # The value of this member is determined by the method invoked on the Server.
104
+ return answer['result']
105
+ end
106
+ end
107
+ end
108
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'rpcjson'
16
+
17
+ class Test::Unit::TestCase
18
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestRpcjson < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ # flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,165 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rpcjson
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Jonathan Jeffus
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-12-24 00:00:00 -06:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ version_requirements: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ hash: 3
28
+ segments:
29
+ - 0
30
+ version: "0"
31
+ requirement: *id001
32
+ prerelease: false
33
+ type: :runtime
34
+ name: json
35
+ - !ruby/object:Gem::Dependency
36
+ version_requirements: &id002 !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ hash: 3
42
+ segments:
43
+ - 0
44
+ version: "0"
45
+ requirement: *id002
46
+ prerelease: false
47
+ type: :development
48
+ name: shoulda
49
+ - !ruby/object:Gem::Dependency
50
+ version_requirements: &id003 !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ~>
54
+ - !ruby/object:Gem::Version
55
+ hash: 23
56
+ segments:
57
+ - 1
58
+ - 0
59
+ - 0
60
+ version: 1.0.0
61
+ requirement: *id003
62
+ prerelease: false
63
+ type: :development
64
+ name: bundler
65
+ - !ruby/object:Gem::Dependency
66
+ version_requirements: &id004 !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ~>
70
+ - !ruby/object:Gem::Version
71
+ hash: 7
72
+ segments:
73
+ - 1
74
+ - 5
75
+ - 2
76
+ version: 1.5.2
77
+ requirement: *id004
78
+ prerelease: false
79
+ type: :development
80
+ name: jeweler
81
+ - !ruby/object:Gem::Dependency
82
+ version_requirements: &id005 !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ hash: 3
88
+ segments:
89
+ - 0
90
+ version: "0"
91
+ requirement: *id005
92
+ prerelease: false
93
+ type: :development
94
+ name: rcov
95
+ - !ruby/object:Gem::Dependency
96
+ version_requirements: &id006 !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ">"
100
+ - !ruby/object:Gem::Version
101
+ hash: 11
102
+ segments:
103
+ - 0
104
+ - 0
105
+ version: "0.0"
106
+ requirement: *id006
107
+ prerelease: false
108
+ type: :runtime
109
+ name: json
110
+ description: This module aims to be a complete implementation of JSON-RPC 1.1 and 2.0.
111
+ email: jjeffus@gmail.com
112
+ executables: []
113
+
114
+ extensions: []
115
+
116
+ extra_rdoc_files:
117
+ - LICENSE.txt
118
+ - README.rdoc
119
+ files:
120
+ - .document
121
+ - Gemfile
122
+ - LICENSE.txt
123
+ - README.rdoc
124
+ - Rakefile
125
+ - VERSION
126
+ - lib/rpcjson.rb
127
+ - test/helper.rb
128
+ - test/test_rpcjson.rb
129
+ has_rdoc: true
130
+ homepage: http://github.com/jjeffus/rpcjson
131
+ licenses:
132
+ - MIT
133
+ post_install_message:
134
+ rdoc_options: []
135
+
136
+ require_paths:
137
+ - lib
138
+ required_ruby_version: !ruby/object:Gem::Requirement
139
+ none: false
140
+ requirements:
141
+ - - ">="
142
+ - !ruby/object:Gem::Version
143
+ hash: 3
144
+ segments:
145
+ - 0
146
+ version: "0"
147
+ required_rubygems_version: !ruby/object:Gem::Requirement
148
+ none: false
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ hash: 3
153
+ segments:
154
+ - 0
155
+ version: "0"
156
+ requirements: []
157
+
158
+ rubyforge_project:
159
+ rubygems_version: 1.3.7
160
+ signing_key:
161
+ specification_version: 3
162
+ summary: Modern JSON-RPC 1.1 and 2.0 implementation.
163
+ test_files:
164
+ - test/helper.rb
165
+ - test/test_rpcjson.rb