Sha256: 3e3f06575a597930998a9dd9310b22d54bdca5b0e93e44a99dba20b4e20b37ce

Contents?: true

Size: 1.34 KB

Versions: 2

Compression:

Stored size: 1.34 KB

Contents

require 'sinatra'

module WhippedCream
  # A server handles building a plugin/runner and starting a web server
  class Server
    attr_reader :plugin

    def initialize(plugin)
      @plugin = plugin
    end

    def start(options = {})
      ensure_routes_built
      ensure_runner_started

      start_web(options)
    end

    def runner
      @runner ||= Runner.create_instance(plugin)
    end

    def port
      8080
    end

    def web
      @web ||= Web
    end

    private

    def ensure_runner_started
      runner
    end

    def ensure_routes_built
      @routes_built ||= build_routes || true
    end

    def start_web(options = {})
      options = options.merge({ app: web, port: port })

      Rack::Server.start options
    end

    def build_routes
      build_button_routes
    end

    def build_button_routes
      plugin.buttons.each do |button|
        web.get "/#{button.id}" do
          runner.send(button.id)
          redirect to('/')
        end
      end
    end

    # A Sinatra application skeleton that is used to build up the web server
    # for this plugin.
    class Web < Sinatra::Application
      get '/' do
        erb :index
      end

      private

      def controls
        runner.plugin.controls
      end

      def runner
        Runner.instance
      end

      def title
        runner.name
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
whipped-cream-0.0.1pre3 lib/whipped-cream/server.rb
whipped-cream-0.0.1pre2 lib/whipped-cream/server.rb