Module: Sinatra::JsonRpc
- Defined in:
- lib/sinatra/json_rpc.rb,
lib/sinatra/json_rpc/errors.rb,
lib/sinatra/json_rpc/request.rb,
lib/sinatra/json_rpc/version.rb,
lib/sinatra/json_rpc/response.rb
Defined Under Namespace
Modules: Helpers Classes: InvalidParams, ParseError, Request, RequestError, Response, ResponseError, TypeError
Constant Summary collapse
- VERSION =
"0.0.1"
Class Method Summary collapse
Class Method Details
.registered(app) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/sinatra/json_rpc.rb', line 31 def self.registered(app) app.helpers Sinatra::JsonRpc::Helpers, Sinatra::Param # Create a Sinatra::JsonRpc::Request from request body app.before do raise Sinatra::JsonRpc::ParseError unless request.media_type == 'application/json' @rpc_req = Sinatra::JsonRpc::Request.new.from_json(request.body.read) @rpc_req.valid? if @rpc_req.params if @rpc_req.params.is_a?(Array) @params[:splat] = *@rpc_req.params else @rpc_req.params.each { |k,v| params[k] = v } end end end # Test whether or not the conditional route matches the JSON-RPC method contained in the request app.set(:method) { |value| condition { @rpc_req.method == value } } app.not_found do status 400 send_error -32601 end [ Sinatra::JsonRpc::ParseError, MultiJson::LoadError ].each do |err| app.error err do status 400 send_error -32700 end end app.error Sinatra::JsonRpc::RequestError do status 400 send_error -32600 end app.error Sinatra::JsonRpc::ResponseError do status 500 send_error -32603 end app.error Sinatra::JsonRpc::InvalidParams do status 400 send_error -32602 end app.error 400 do send_error -32602 if body.first.start_with?("Invalid parameter") end end |