Class: GRI::API

Inherits:
Object show all
Defined in:
lib/gri/api.rb

Instance Method Summary collapse

Instance Method Details

#call(env) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/gri/api.rb', line 6

def call env
  if env['PATH_INFO'] =~ %r{^/api/(\w+)/(\w+)/(\w+)\b}
    service_name, section_name, graph_name = $1, $2, $3
    req = Rack::Request.new env
    root_dir = Config['root-dir'] || Config::ROOT_PATH
    cast_dir = root_dir + '/cast'
    service_dir = "#{cast_dir}/#{service_name}"
    host = section_name
    key = "num_#{graph_name}"
    FileUtils.mkdir_p service_dir

    records = [{'_host'=>host, '_key'=>key, 'num'=>req['number']}]
    writer = Writer.create 'rrd', :gra_dir=>service_dir, :interval=>60
    writer.write records
    writer.finalize
    res = "OK\n"
  else
    res = "NG\n"
  end
  [200, {}, [res]]
end