Class: Nasreddin::APIServerResource

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

Constant Summary collapse

DEFAULT_ENV =
{
    'rack.errors'       => $stderr,
    'rack.input'        => StringIO.new,
    'rack.version'      => [1, 1],
    'rack.multithread'  => true,
    'rack.multiprocess' => true,
    'rack.run_once'     => false,
    'HTTP_ACCEPT'       => 'application/json',
    'HTTP_USER_AGENT'   => 'NasreddinAPI'
}

Instance Method Summary collapse

Constructor Details

#initialize(route_prefix, resource, app) ⇒ APIServerResource

Returns a new instance of APIServerResource.



52
53
54
55
56
# File 'lib/nasreddin/api-server.rb', line 52

def initialize(route_prefix,resource,app)
  @resource = resource
  @route_prefix = route_prefix
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



80
81
82
# File 'lib/nasreddin/api-server.rb', line 80

def call(env)
    @app.call(env)
end

#env(msg) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/nasreddin/api-server.rb', line 105

def env(msg)
  env = DEFAULT_ENV.clone
  env['rack.url_scheme'] = (msg.delete(:secure) ? 'https' : 'http')
  method = msg.delete(:method) || 'GET'
  env['REQUEST_METHOD'] = method.to_s.upcase
  env['QUERY_STRING'] = queryize(msg.delete(:params)) || ''
  env['SCRIPT_NAME'] = "/#{@route_prefix}" || ''
  env['PATH_INFO'] = "/#{@resource}"
  if ((id = msg.delete(:id)))
    env['PATH_INFO'] += "/#{id}"
  end
  if ((path = msg.delete(:path)))
    env['PATH_INFO'] += "/#{path}"
  end
  env['REQUEST_URI'] = "#{env['SCRIPT_NAME']}#{env['PATH_INFO']}"
  env.merge!(Hash[msg.map { |k, v| [k.to_s, v] }])
  env
end

#heartbeat_okObject



101
102
103
# File 'lib/nasreddin/api-server.rb', line 101

def heartbeat_ok
    [200, nil, "OK"]
end

#is_heartbeat?(msg) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/nasreddin/api-server.rb', line 76

def is_heartbeat?(msg)
    msg[:params] && msg[:params].has_key?('__heartbeat__')
end

#process_incoming_message(msg) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/nasreddin/api-server.rb', line 84

def process_incoming_message(msg)
    return heartbeat_ok  if is_heartbeat?(msg)

    begin
      status, headers, body = call(env(msg))

      resp = ''
      body.each { |d| resp += d.to_s }
      body.close if body.respond_to?(:close)

    rescue Exception => err
      resp = "#{err.message}\n\n#{err.backtrace.join("\n")}"
    end

    [status, headers, resp]
end

#queueObject



58
59
60
# File 'lib/nasreddin/api-server.rb', line 58

def queue
  @queue ||= TorqueBox::Messaging::Queue.start("/queues/#{@resource}", durable: false)
end

#runObject



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/nasreddin/api-server.rb', line 62

def run
  loop do
    begin
      queue.receive_and_publish &method(:process_incoming_message)
    rescue Exception => err
      $stderr.puts "Error processing request: #{err.class} - #{err.message}"
      if err.kind_of?(Java::JavaxJms::JMSException)
        break
      end
    end
  end
  queue.stop
end