Class: Alephant::Preview::Server

Inherits:
Sinatra::Base
  • Object
show all
Defined in:
lib/alephant/preview/server.rb

Constant Summary collapse

BASE_LOCATION =
"#{(ENV['BASE_LOCATION'] || Dir.pwd)}/components".freeze

Instance Method Summary collapse

Instance Method Details

#find_id_from_template(template) ⇒ Object



92
93
94
95
96
97
98
99
100
# File 'lib/alephant/preview/server.rb', line 92

def find_id_from_template(template)
  files = Dir.glob(BASE_LOCATION + "/**/models/*")
  file = files.select { |file| file.include? "/#{template}.rb" }.pop

  halt(404) if file.nil?

  result = /#{BASE_LOCATION}\/(\w+)/.match(file)
  result[1]
end

#render_batch_componentObject



115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/alephant/preview/server.rb', line 115

def render_batch_component
  content = render_component

  {
    :component    => template,
    :options      => {},
    :status       => 200,
    :body         => content,
    :content_type => get_content_type(content),
    :sequence_id  => sequence_id
  }
end

#render_componentObject



109
110
111
112
113
# File 'lib/alephant/preview/server.rb', line 109

def render_component
  view_mapper.generate(fixture_data)[template].render.tap do |content|
    response["Content-Type"] = get_content_type(content)
  end
end

#render_previewObject



102
103
104
105
106
107
# File 'lib/alephant/preview/server.rb', line 102

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