Class: Nasreddin::APIServer

Inherits:
Object
  • Object
show all
Defined in:
lib/nasreddin/api-server.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ APIServer

Returns a new instance of APIServer.



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/nasreddin/api-server.rb', line 8

def initialize(app, options = {})
  @app = app
  @threads = {}
  @resources = options[:resources]
  @route_prefix = options[:route_prefix]
  if @resources
    @resources.each do |resource|
      @threads[resource] = Thread.new { Nasreddin::APIServerResource.new(@route_prefix,resource,@app).run }
    end
  else
    $stderr.puts "WARNING: Nasreddin::APIServer is being used without any resources specified!"
  end
end

Instance Method Details

#call(env) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/nasreddin/api-server.rb', line 22

def call(env)
  params = Rack::Request.new(env).params

  if is_heartbeat?(params)
      res = params['resource'] = params['resources'].pop
      params["resources.#{res}"] = @resources
      Nasreddin::Resource(@resource).remote_call(params)
  else
    @app.call(env)
  end
end

#is_heartbeat?(params) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/nasreddin/api-server.rb', line 34

def is_heartbeat?(params)
  params.has_key? '__hearbeat__'
end