Class: JSONBroker::Broker

Inherits:
Object
  • Object
show all
Defined in:
lib/jiji/util/json_broker.rb

Overview

JSONリクエストを元にサービスのAPIを実行して結果をJSONで返す。

リクエスト: “params”:[<引数1>, <引数2>]

レスポンス(正常時):

“result”:<実行結果>

レスポンス(エラー時):

“result”:null

Instance Method Summary collapse

Constructor Details

#initialize(service) ⇒ Broker

Returns a new instance of Broker.



32
33
34
# File 'lib/jiji/util/json_broker.rb', line 32

def initialize ( service )
  @service = service
end

Instance Method Details

#invoke(request) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/jiji/util/json_broker.rb', line 36

def invoke ( request )
  begin
    json = JSON::Lexer.new(request).nextvalue
    method   = json["method"]
    args       = json["params"]

    result = @service.send( method.to_sym, *args )
    return '[{"error":null, "detail":null,"result":' << result.to_json << "}]"
  rescue Exception
    error =  $!.to_s.gsub(/'/, "")
    detail = $!.respond_to?(:detail) ? $!.detail : {}
    detail[:backtrace] =  $!.backtrace.join("\n").gsub(/'/, "")
    return '[{"error":' << error.to_json << ', "detail":' + detail.to_json + ', "result":null}]'
  end
end