Browse files

initial addtion to git

  • Loading branch information...
1 parent 21c97fc commit 15391d1d769306686f26f49fec469cae227b4176 @jjeffus committed Dec 24, 2010
Showing with 126 additions and 5 deletions.
  1. +1 −1 .gitignore
  2. +3 −3 Rakefile
  3. +1 −0 VERSION
  4. +108 −0 lib/rpcjson.rb
  5. +12 −0 test.rb
  6. +1 −1 test/test_rpcjson.rb
View
2 .gitignore
@@ -39,4 +39,4 @@ pkg
#.\#*
#
# For vim:
-#*.swp
+*.swp
View
6 Rakefile
@@ -15,13 +15,13 @@ Jeweler::Tasks.new do |gem|
gem.name = "rpcjson"
gem.homepage = "http://github.com/jjeffus/rpcjson"
gem.license = "MIT"
- gem.summary = %Q{TODO: one-line summary of your gem}
- gem.description = %Q{TODO: longer description of your gem}
+ gem.summary = %Q{Modern JSON-RPC 1.1 and 2.0 implementation.}
+ gem.description = %Q{This module aims to be a complete implementation of JSON-RPC 1.1 and 2.0.}
gem.email = "jjeffus@gmail.com"
gem.authors = ["Jonathan Jeffus"]
# Include your dependencies below. Runtime dependencies are required when using your gem,
# and development dependencies are only needed for development (ie running rake tasks, tests, etc)
- # gem.add_runtime_dependency 'jabber4r', '> 0.1'
+ gem.add_runtime_dependency 'json', '> 0.0'
# gem.add_development_dependency 'rspec', '> 1.2.3'
end
Jeweler::RubygemsDotOrgTasks.new
View
1 VERSION
@@ -0,0 +1 @@
+0.0.1
View
108 lib/rpcjson.rb
@@ -0,0 +1,108 @@
+require 'net/http'
+require 'uri'
+require 'json'
+
+class RPC
+ class JSON
+ class Client
+ class Error < RuntimeError
+ attr_accessor :error
+ def initialize(error)
+ @error = error
+ end
+ end
+
+ def initialize(url, version = 2.0)
+ @id = 1
+ @uri = URI.parse(url)
+ @version = version
+ end
+
+ def method_missing(func, *args)
+ json = {
+ # jsonrpc
+ # A String specifying the version of the JSON-RPC protocol. MUST be exactly "2.0".
+ 'jsonrpc' => @version.to_s,
+ # method
+ # A String containing the name of the method to be invoked.
+ 'method' => func,
+ # id
+ # 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]
+ 'id' => @id
+ }
+
+ # 1.0/1.1:
+ # params - An Array of objects to pass as arguments to the method.
+ # 2.0:
+ # params
+ # A Structured value that holds the parameter values to be used during the invocation of the method. This member MAY be omitted.
+
+ # params are expected always in 1.0/1.1. Not required in 2.0.
+ if @version < 2.0 or (args != nil and args.length != 0)
+ json['params'] = args
+ end
+
+ body = JSON(json)
+ # puts "Sending: #{body}"
+
+ http = Net::HTTP.new(@uri.host, @uri.port)
+ request = Net::HTTP::Get.new(@uri.request_uri)
+ if @uri.user != nil
+ request.basic_auth(@uri.user, @uri.password)
+ end
+ request.body = body
+ response = http.request(request)
+ answer = JSON( response.body )
+
+ # p answer
+
+ # 1.0/1.1
+ # jsonrpc: NOT INCLUDED
+ # 2.0
+ # jsonrpc:
+ # A String specifying the version of the JSON-RPC protocol. MUST be exactly "2.0".
+ if answer['jsonrpc'] == nil
+ if @version >= 2.0
+ STDERR.puts "Error: We're configured to use JSON-RPC #{@version}, but the server appears to be using an older implementation."
+ end
+ else
+ if answer['jsonrpc'].to_f < @version
+ STDERR.puts "Error: We're configured to use JSON-RPC #{@version}, but the server appears to be using #{answer['jsonrpc']}"
+ elsif answer['jsonrpc'].to_f > @version
+ STDERR.puts "Error: We're configured to use JSON-RPC #{@version}, but the server appears to be using #{answer['jsonrpc']}"
+ end
+ end
+
+ # 1.0/1.1
+ # error - An Error object if there was an error invoking the method. It must be null if there was no error.
+ # 2.0
+ # error
+ # This member is REQUIRED on error.
+ # This member MUST NOT exist if there was no error triggered during invocation.
+ # The value for this member MUST be an Object as defined in section 5.1.
+
+ # It will be nil in either case if there was no error.
+ if answer['error'] != nil
+ if @version >= 2.0
+ raise Error.new(answer['error']), answer['error']['message']
+ else
+ # No standard for < 2.0 error objects.
+ # Bitcoind is 1.1 and seems to follow 2.0 standard, other clients?
+ raise Error.new(answer['error']), 'JSON-RPC Error'
+ end
+ end
+
+ @id += 1
+
+ # 1.0
+ # result - The Object that was returned by the invoked method. This must be null in case there was an error invoking the method.
+ # 2.0
+ # result
+ # This member is REQUIRED on success.
+ # This member MUST NOT exist if there was an error invoking the method.
+ # The value of this member is determined by the method invoked on the Server.
+ return answer['result']
+ end
+ end
+ end
+end
View
12 test.rb
@@ -0,0 +1,12 @@
+#!/opt/ruby-enterprise/bin/ruby
+
+require 'rubygems'
+require './lib/rpcjson.rb'
+
+bc = RPC::JSON::Client.new 'http://Fr0bNab1t:232js8ajdkAJdsjdiKDSieksk4@127.0.0.1:8332', 1.1
+
+begin
+ puts bc.getaccount ARGV.pop
+rescue RPC::JSON::Client::Error => e
+ puts "Got an error: #{e}: #{e.error.to_json}"
+end
View
2 test/test_rpcjson.rb
@@ -2,6 +2,6 @@
class TestRpcjson < Test::Unit::TestCase
should "probably rename this file and start testing for real" do
- flunk "hey buddy, you should probably rename this file and start testing for real"
+ # flunk "hey buddy, you should probably rename this file and start testing for real"
end
end

0 comments on commit 15391d1

Please sign in to comment.