Module: Saorin::Server::Base
- Includes:
- Formatter
- Included in:
- Rack
- Defined in:
- lib/saorin/server/base.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Formatter
#default_formatter, #formatter
Instance Attribute Details
#allowed_methods ⇒ Object
Returns the value of attribute allowed_methods.
12
13
14
|
# File 'lib/saorin/server/base.rb', line 12
def allowed_methods
@allowed_methods
end
|
#handler ⇒ Object
Returns the value of attribute handler.
12
13
14
|
# File 'lib/saorin/server/base.rb', line 12
def handler
@handler
end
|
#options ⇒ Object
Returns the value of attribute options.
13
14
15
|
# File 'lib/saorin/server/base.rb', line 13
def options
@options
end
|
Instance Method Details
#dispatch_request(request) ⇒ Object
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
# File 'lib/saorin/server/base.rb', line 76
def dispatch_request(request)
method = request.method.to_s
params = request.params || []
unless @allowed_methods.include?(method) && @handler.respond_to?(method)
raise Saorin::MethodNotFound
end
begin
if params.is_a?(::Hash)
@handler.__send__ method, params
else
@handler.__send__ method, *params
end
rescue Saorin::RPCError
raise
rescue ArgumentError
raise Saorin::InvalidParams
rescue Exception => e
raise Saorin::ServerError, e
end
end
|
#dispatch_request_with_trap_notification(request) ⇒ Object
69
70
71
72
73
74
|
# File 'lib/saorin/server/base.rb', line 69
def dispatch_request_with_trap_notification(request)
result = dispatch_request request
Response.new(:result => result, :id => request.id)
ensure
return nil if request.notify?
end
|
#dump_response(response) ⇒ Object
49
50
51
|
# File 'lib/saorin/server/base.rb', line 49
def dump_response(response)
formatter.dump response
end
|
#handle_request(hash) ⇒ Object
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
# File 'lib/saorin/server/base.rb', line 53
def handle_request(hash)
begin
request = Request.from_hash(hash)
request.validate
dispatch_request_with_trap_notification request
rescue Saorin::InvalidRequest => e
Response.new(:error => e)
rescue Saorin::Error => e
Response.new(:error => e, :id => request.id)
rescue Exception => e
options = {:error => Saorin::InternalError.new}
options[:id] = request.id if request
Response.new(options)
end
end
|
#initialize(handler, options = {}) ⇒ Object
15
16
17
18
19
20
|
# File 'lib/saorin/server/base.rb', line 15
def initialize(handler, options = {})
@handler = handler
@allowed_methods = options[:allowed_methods] || handler.public_methods(false)
@allowed_methods.map! { |m| m.to_s }
@options = options
end
|
#parse_request(content) ⇒ Object
43
44
45
46
47
|
# File 'lib/saorin/server/base.rb', line 43
def parse_request(content)
formatter.load content
rescue
raise Saorin::ParseError
end
|
#process_request(content) ⇒ Object
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
# File 'lib/saorin/server/base.rb', line 25
def process_request(content)
response = begin
request = parse_request content
if request.is_a?(::Array)
raise Saorin::InvalidRequest if request.empty?
responses = request.map { |req| handle_request(req) }
responses.compact!
responses.empty? ? nil : responses
else
handle_request(request)
end
rescue Saorin::Error => e
Response.new(:error => e)
end
dump_response response if response
end
|
#shutdown ⇒ Object
22
23
|
# File 'lib/saorin/server/base.rb', line 22
def shutdown
end
|