Skip to content
This repository has been archived by the owner on Aug 28, 2024. It is now read-only.

Commit

Permalink
allow keyword-based params
Browse files Browse the repository at this point in the history
  • Loading branch information
aghull committed Jan 21, 2016
1 parent 9284584 commit 2b3fb34
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
3 changes: 3 additions & 0 deletions lib/jimson/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ def initialize(helper)
end

def method_missing(sym, *args, &block)
args = args.first if args.size == 1 && args.first.is_a?(Hash)
request = Jimson::Request.new(sym.to_s, args)
@helper.push_batch_request(request)
end
Expand All @@ -162,6 +163,7 @@ def initialize(url, opts = {}, namespace = nil)
end

def method_missing(sym, *args, &block)
args = args.first if args.size == 1 && args.first.is_a?(Hash)
@helper.process_call(sym, args)
end

Expand All @@ -171,6 +173,7 @@ def [](method, *args)
new_ns = @namespace.nil? ? "#{method}." : "#@namespace#{method}."
return Client.new(@url, @opts, new_ns)
end
args = args.first if args.size == 1 && args.first.is_a?(Hash)
@helper.process_call(method, args)
end

Expand Down
17 changes: 16 additions & 1 deletion spec/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,21 @@ module Jimson
@client[:foo][:bar].sum(1, 2, 3).should == 42
end
end

context "when sending named paramaters" do
it "sends the the params as a hash" do
expected = MultiJson.encode({
'jsonrpc' => '2.0',
'method' => 'foo.sum',
'params' => { :first_number => 5, :second_number => 6 },
'id' => 1
})
response = MultiJson.encode(BOILERPLATE.merge({'result' => 11}))
RestClient.should_receive(:post).with(SPEC_URL, expected, {:content_type => 'application/json'}).and_return(@resp_mock)
@resp_mock.should_receive(:body).at_least(:once).and_return(response)
@client[:foo].sum({:first_number => 5, :second_number => 6}).should == 11
end
end
end

context "when sending positional arguments" do
Expand Down Expand Up @@ -137,7 +152,7 @@ module Jimson
batch = MultiJson.encode([
{"jsonrpc" => "2.0", "method" => "sum", "params" => [1,2,4], "id" => "1"},
{"jsonrpc" => "2.0", "method" => "subtract", "params" => [42,23], "id" => "2"},
{"jsonrpc" => "2.0", "method" => "foo_get", "params" => [{"name" => "myself"}], "id" => "5"},
{"jsonrpc" => "2.0", "method" => "foo_get", "params" => {"name" => "myself"}, "id" => "5"},
{"jsonrpc" => "2.0", "method" => "get_data", "id" => "9"}
])

Expand Down

0 comments on commit 2b3fb34

Please sign in to comment.