Class: Application::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/gris/generators/templates/scaffold/app.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeService

Returns a new instance of Service.



24
25
26
27
28
29
30
31
# File 'lib/gris/generators/templates/scaffold/app.rb', line 24

def initialize
  @filenames = ['', '.html', 'index.html', '/index.html']
  @rack_static = ::Rack::Static.new(
    -> { [404, {}, []] },
    root: File.expand_path('../public', __FILE__),
    urls: ['/']
    )
end

Class Method Details

.instanceObject



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/gris/generators/templates/scaffold/app.rb', line 33

def self.instance
  @instance ||= Rack::Builder.new do
    use Rack::Cors do
      allow do
        origins '*'
        resource '*', headers: :any, methods: :get
      end
    end
    run Application::Service.new
  end.to_app
end

Instance Method Details

#call(env) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/gris/generators/templates/scaffold/app.rb', line 45

def call(env)
  response = ApplicationEndpoint.call(env)
  # Render error pages or return API response
  case response[0]
  when 404, 500
    content = @rack_static.call(env.merge('PATH_INFO' => "/errors/#{response[0]}.html"))
    [response[0], content[1], content[2]]
  else
    response
  end
end