Class: Actn::Api::Public

Inherits:
Goliath::API
  • Object
show all
Defined in:
lib/actn/api/public.rb

Constant Summary collapse

CT_JS =
{ 'Content-Type' => 'application/javascript' }
CT_JSON =
{'Content-Type' => 'application/json'}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.inherited(base) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/actn/api/public.rb', line 18

def self.inherited base
  
  super
  
  base.use Goliath::Rack::Params
  base.use Goliath::Rack::Heartbeat

  base.use Mw::NoXSS
  base.use Rack::Session::Cookie, secret: ENV['SECRET']
  base.use Rack::Csrf, skip: ['OPTIONS:/.*'], skip_if: proc { |r| ENV['RACK_ENV'] == "test" }
  base.use Mw::Cors
  base.use Goliath::Rack::BarrierAroundwareFactory, Mw::Auth, exclude: /^\/connect$/
  
  super
end

Instance Method Details

#process(table, path) ⇒ Object

Raises:

  • (NotImplementedError)


35
36
37
# File 'lib/actn/api/public.rb', line 35

def process table, path
  raise NotImplementedError
end

#response(env) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/actn/api/public.rb', line 41

def response env

  path = env['REQUEST_PATH'] || "/"

  unless table = path[1..-1].split("/").first
    raise Goliath::Validation::Error.new(400, "model identifier missing")
  end
  
  begin
    json = process(table,path)
  rescue PG::InternalError => e
    if e.message =~ /does not exist/
      raise Goliath::Validation::NotFoundError.new("resource not found")        
    else
      raise e
    end
  end

  status = json =~ /errors/ ? 406 : 200

  [status, CT_JSON, json]

end