Sha256: 3809ad74862e0cf12c1ea454dd92596c47bf6cd7f784cae7b58f2a53a9dcec4d

Contents?: true

Size: 1.42 KB

Versions: 3

Compression:

Stored size: 1.42 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

    get '/javascripts/index.js' do
      @serve_static = false
      @values = @reporter.values
      erb File.read(File.expand_path('../../../app/assets/javascripts/index.js.erb', __dir__)), content_type: 'text/javascript'
    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 '/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 < Reporter
    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

3 entries across 3 versions & 1 rubygems

Version Path
attractor-0.4.2 lib/attractor/reporters/sinatra_reporter.rb
attractor-0.4.1 lib/attractor/reporters/sinatra_reporter.rb
attractor-0.4.0 lib/attractor/reporters/sinatra_reporter.rb