Sha256: 6269cbe2427f69d1134559a6f7cbf9eaf8801723344d4396d3880dc2ad13c1dc

Contents?: true

Size: 1.71 KB

Versions: 1

Compression:

Stored size: 1.71 KB

Contents

require 'alephant/renderer'
require 'alephant/views'

require 'alephant/models/parser'

require 'alephant/preview/template/base'

require 'sinatra/base'
require 'faraday'
require 'json'
require 'uri'

module Alephant
  module Preview
    class Server < Sinatra::Base
      DEFAULT_LOCATION = 'components'

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

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

      def render_preview
        Template::Base.new(
          { region => render_component },
          preview_template_location
        ).render
      end

      def render_component
        ::Alephant::Renderer.create(template, base_path, model).render
      end

      private
      def model
        require model_location
        ::Alephant::Views.get_registered_class(template).new(fixture_data)
      end

      def base_path
        File.join(Dir.pwd, DEFAULT_LOCATION, id)
      end

      def model_location
        File.join(base_path, 'models', "#{template}.rb")
      end

      def template
        params['template']
      end

      def region
        params['region']
      end

      def id
        params['id']
      end

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

      def fixture_data
        msg = Struct.new(:body)
          .new(raw_fixture_data)
        parser.parse msg
      end

      def raw_fixture_data
        File.open(fixture_location).read
      end

      def parser
        @parser ||= Parser.new
      end

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

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
alephant-preview-0.0.5.1 lib/alephant/preview/server.rb