Sha256: f9eca3cbce1bd14b7a3fff4c88018bc3aabf640d7b06ed84249e8ab81645d082

Contents?: true

Size: 1.8 KB

Versions: 6

Compression:

Stored size: 1.8 KB

Contents

# frozen_string_literal: true

require 'rack/livereload'
require 'rack'
require 'sinatra/base'

module Attractor
  # skeleton sinatra app
  class AttractorApp < Sinatra::Base
    def initialize(reporter)
      super
      @reporter = reporter
    end

    enable :static
    set :public_folder, File.expand_path('../../../app/assets', __dir__)
    set :show_exceptions, :after_handler

    get '/' do
      @types = @reporter.types
      erb File.read(File.expand_path('../../../app/views/index.html.erb', __dir__))
    end

    get '/file_prefix' do
      { file_prefix: @reporter.file_prefix }.to_json
    end

    get '/values' do
      type = params[:type] || 'rb'
      @reporter.values(type: type).to_json
    end

    get '/suggestions' do
      threshold = params[:t] || 95
      @reporter.suggestions(threshold).to_json
    end

    error NoMethodError do
      { error: env['sinatra.error'].message }.to_json
    end
  end

  # serving the HTML locally
  class SinatraReporter < BaseReporter
    def report
      super

      app = AttractorApp.new(self)

      puts 'Serving attractor at http://localhost:7890'

      if @open_browser
        Launchy.open('http://localhost:7890') if @open_browser
        puts "Opening browser window..."
      end

      Rack::Handler::WEBrick.run app, Port: 7890
    end

    def watch
      @suggestions = @suggester.suggest

      app = AttractorApp.new(self)

      puts 'Serving attractor at http://localhost:7890'
      if @open_browser
        Launchy.open('http://localhost:7890')
        puts "Opening browser window..."
      end

      Rack::Handler::WEBrick.run Rack::LiveReload.new(app), Port: 7890
    end

    def values(type: 'rb')
      @values = @calculators[type].calculate
      @values
    rescue NoMethodError => e
      puts "No calculator for type #{type}"
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
attractor-2.0.3 lib/attractor/reporters/sinatra_reporter.rb
attractor-2.0.2 lib/attractor/reporters/sinatra_reporter.rb
attractor-2.0.1 lib/attractor/reporters/sinatra_reporter.rb
attractor-2.0.0 lib/attractor/reporters/sinatra_reporter.rb
attractor-1.2.0 lib/attractor/reporters/sinatra_reporter.rb
attractor-1.1.1 lib/attractor/reporters/sinatra_reporter.rb