Sha256: 4870875c682e0f4686cd87aba9c2e88615f5bedef2ad760a927e7278ef1d34ac

Contents?: true

Size: 1.28 KB

Versions: 5

Compression:

Stored size: 1.28 KB

Contents

# frozen_string_literal: true

require 'pry'
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__)

    get '/' do
      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
      @reporter.values.to_json
    end

    get '/suggestions' do
      threshold = params[:t] || 95
      @reporter.suggestions(threshold).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'
      Launchy.open('http://localhost:7890')

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

    def watch
      @suggestions = @suggester.suggest

      app = AttractorApp.new(self)

      puts 'Serving attractor at http://localhost:7890'
      Launchy.open('http://localhost:7890')

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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
attractor-0.6.1 lib/attractor/reporters/sinatra_reporter.rb
attractor-0.6.0 lib/attractor/reporters/sinatra_reporter.rb
attractor-0.5.1 lib/attractor/reporters/sinatra_reporter.rb
attractor-0.5.0 lib/attractor/reporters/sinatra_reporter.rb
attractor-0.4.3 lib/attractor/reporters/sinatra_reporter.rb