Class: OasGrape::Web::View

Inherits:
Object
  • Object
show all
Defined in:
lib/oas_grape/web/view.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.call(env) ⇒ Object



6
7
8
# File 'lib/oas_grape/web/view.rb', line 6

def self.call(env)
  new.call(env)
end

Instance Method Details

#call(env) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/oas_grape/web/view.rb', line 10

def call(env)
  @request = Rack::Request.new(env)
  oas = OasGrape.build.to_json

  if @request.path.end_with?(".json")
    [200, { "content-type" => "application/json" }, [oas]]
  elsif @request.path.end_with?(".js")
    [200, { "content-type" => "application/javascript" }, [render_js]]
  else
    [200, { "content-type" => "text/html" }, [render_view]]
  end
end

#render_jsObject



29
30
31
32
# File 'lib/oas_grape/web/view.rb', line 29

def render_js
  template_path = File.expand_path("assets/rapidoc-min.js", __dir__)
  File.read(template_path)
end

#render_viewObject



23
24
25
26
27
# File 'lib/oas_grape/web/view.rb', line 23

def render_view
  template_path = File.expand_path("views/index.html.erb", __dir__)
  template = File.read(template_path)
  ERB.new(template).result(binding)
end