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



95
96
97
98
99
100
101
102
103
# File 'lib/alephant/preview/server.rb', line 95

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



118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/alephant/preview/server.rb', line 118

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



112
113
114
115
116
# File 'lib/alephant/preview/server.rb', line 112

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

#render_previewObject



105
106
107
108
109
110
# File 'lib/alephant/preview/server.rb', line 105

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