class GraphdocRuby::Application

Constants

Semaphore

Public Class Methods

call(env) click to toggle source
# File lib/graphdoc-ruby/application.rb, line 9
def self.call(env)
  @application ||= new
  @application.call(env)
end
graphdoc() click to toggle source
# File lib/graphdoc-ruby/application.rb, line 14
def self.graphdoc
  config = GraphdocRuby.config
  config.assert_configuration!

  GraphdocRuby::Graphdoc.new(
    output: config.output_directory,
    executable: config.executable_path,
    endpoint: config.endpoint,
    overwrite: config.overwrite,
    mtime: config.mtime,
    query: config.evaluate_graphql_query,
    context: config.evaluate_graphql_context
  )
end
new() click to toggle source
# File lib/graphdoc-ruby/application.rb, line 29
def initialize
  generate_html if GraphdocRuby.config.run_time_generation

  @static = GraphdocRuby::Static.new(GraphdocRuby.config.output_directory)
end

Public Instance Methods

call(env) click to toggle source
# File lib/graphdoc-ruby/application.rb, line 35
def call(env)
  serve_static_file(env) || not_found
end

Private Instance Methods

generate_html() click to toggle source
# File lib/graphdoc-ruby/application.rb, line 41
def generate_html
  Semaphore.synchronize do
    if should_generate_schema_json?
      GraphdocRuby::GraphqlJson.write_schema_json
    end

    self.class.graphdoc.generate_document!
  end
end
html_access_without_slash?(env) click to toggle source
# File lib/graphdoc-ruby/application.rb, line 77
def html_access_without_slash?(env)
  original_path = env[Rack::REQUEST_PATH]
  File.extname(original_path).empty? && !original_path.end_with?('/')
end
not_found() click to toggle source
# File lib/graphdoc-ruby/application.rb, line 55
def not_found
  if GraphdocRuby.config.run_time_generation
    [404, { 'Content-Type' => 'text/html' }, ['Not found generated html']]
  else
    [404, { 'Content-Type' => 'text/html' }, ['Not found generated html. Please run `rake graphdoc:generate`']]
  end
end
redirect_to_slash_path(env) click to toggle source

Unfortunately, html generated by graphdoc contains relative path.

# File lib/graphdoc-ruby/application.rb, line 83
def redirect_to_slash_path(env)
  path = env[Rack::REQUEST_PATH] + '/'

  response = Rack::Response.new
  response.redirect(path)
  response.finish
end
serve_static_file(env) click to toggle source
# File lib/graphdoc-ruby/application.rb, line 63
def serve_static_file(env)
  request = Rack::Request.new(env)
  return unless request.get? || request.head?
  return redirect_to_slash_path(env) if html_access_without_slash?(env)

  path = request.path_info.chomp('/')
  match = @static.match?(path)

  if match
    request.path_info = match
    @static.serve(request)
  end
end
should_generate_schema_json?() click to toggle source
# File lib/graphdoc-ruby/application.rb, line 51
def should_generate_schema_json?
  !GraphdocRuby::Utils.valid_url?(GraphdocRuby.config.endpoint) && !GraphdocRuby::Utils.file_exist?(GraphdocRuby.config.endpoint)
end