Class: MobME::Infrastructure::RPC::Adaptor

Inherits:
Object
  • Object
show all
Defined in:
lib/mobme/infrastructure/rpc/version.rb,
lib/mobme/infrastructure/rpc/adaptor.rb

Instance Method Summary collapse

Constructor Details

#initialize(service_object) ⇒ Adaptor

Returns a new instance of Adaptor.



3
4
5
# File 'lib/mobme/infrastructure/rpc/adaptor.rb', line 3

def initialize(service_object)
  @service_object = service_object
end

Instance Method Details

#call(env) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/mobme/infrastructure/rpc/adaptor.rb', line 11

def call(env)
  request = Rack::Request.new(env)
  command = request.body.read
  binary = server.execute(command)

  if binary.match(/NoMethodError/)
    response(404, binary)
  else
    response(200, binary)
  end
end

#response(status, body) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/mobme/infrastructure/rpc/adaptor.rb', line 23

def response(status, body)
  headers = {
    "Content-Type" => "application/json-rpc",
    "Content-Length" => body.bytesize.to_s
  }
  [status, headers, [body]]
end

#serverObject



7
8
9
# File 'lib/mobme/infrastructure/rpc/adaptor.rb', line 7

def server
  @server ||= RPC::Server.new(@service_object)
end