Sha256: 599fe628243f33a2efceb522848a7c3b4d5c32017cef801a2817286fee45ef8f

Contents?: true

Size: 1.27 KB

Versions: 1

Compression:

Stored size: 1.27 KB

Contents

require 'sinatra/base'
require 'alephant/models/parser'
require 'alephant/models/renderer'
require 'alephant/views/preview'
require 'faraday'
require 'json'
require 'uri'

module Alephant
  module Preview
    class Server < Sinatra::Base

      get '/preview/:id/:region/?:fixture?' do
        render_preview
      end

      get '/component/:id/?:fixture?' do
        render_component
      end

      def render_preview
        ::Alephant::Views::Preview.new(
          { region => render_component },
          preview_template_location
        ).render
      end

      def render_component
        Renderer.new(id, base_path).render(fixture_data)
      end

      private
      def region
        params['region']
      end

      def id
        params['id']
      end

      def fixture
        params['fixture'] || id
      end

      def fixture_data
        parser.parse raw_fixture_data
      end

      def raw_fixture_data
        File.open(fixture_location).read
      end

      def parser
        @parser ||= Parser.new
      end

      def base_path
        "#{Dir.pwd}/views"
      end

      def fixture_location
        "#{base_path}/fixtures/#{fixture}.json"
      end

      def preview_template_location
        "#{base_path}/templates/preview.mustache"
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
alephant-0.0.8.2-java lib/alephant/preview/server.rb