class Alephant::Preview::Server
Constants
- BASE_LOCATION
Public Instance Methods
find_id_from_template(template)
click to toggle source
# 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_component()
click to toggle source
# 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_component()
click to toggle source
# 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_preview()
click to toggle source
# File lib/alephant/preview/server.rb, line 105 def render_preview Template::Base.new( { region => render_component }, preview_template_location ).render end
Private Instance Methods
base_path()
click to toggle source
# File lib/alephant/preview/server.rb, line 169 def base_path File.join(BASE_LOCATION, id) end
fixture()
click to toggle source
# File lib/alephant/preview/server.rb, line 189 def fixture params['fixture'] || id end
fixture_data()
click to toggle source
# File lib/alephant/preview/server.rb, line 193 def fixture_data if File.exist? "#{base_path}/mapper.rb" loader = Alephant::Preview::FixtureLoader.new(base_path) data_mapper_factory = Alephant::Publisher::Request::DataMapperFactory.new(loader, BASE_LOCATION) begin data_mapper_factory.create(id, params).data rescue Alephant::Publisher::Request::InvalidApiResponse raise "The JSON passed to the data mapper isn't valid" rescue StandardError => e puts e.backtrace raise "There was an issue with the data mapper class: #{e.message}" end else msg = Struct.new(:body) .new(raw_fixture_data) parser.parse msg end end
fixture_location()
click to toggle source
# File lib/alephant/preview/server.rb, line 220 def fixture_location "#{base_path}/fixtures/#{fixture}.json" end
get_batched_components()
click to toggle source
# File lib/alephant/preview/server.rb, line 160 def get_batched_components query_string.fetch('components', []) end
get_content_type(content)
click to toggle source
# File lib/alephant/preview/server.rb, line 137 def get_content_type(content) return 'application/json' if is_json?(content) 'text/html' end
id()
click to toggle source
# File lib/alephant/preview/server.rb, line 185 def id params['id'] end
is_json?(content)
click to toggle source
# File lib/alephant/preview/server.rb, line 142 def is_json?(content) JSON.parse(content) && true rescue Exception false end
model()
click to toggle source
# File lib/alephant/preview/server.rb, line 164 def model require model_location Alephant::Renderer::Views.get_registered_class(template).new(fixture_data) end
model_location()
click to toggle source
# File lib/alephant/preview/server.rb, line 173 def model_location File.join(base_path, 'models', "#{template}.rb") end
parser()
click to toggle source
# File lib/alephant/preview/server.rb, line 216 def parser @parser ||= ::Alephant::Support::Parser.new end
post_batched_components()
click to toggle source
# File lib/alephant/preview/server.rb, line 156 def post_batched_components request_body.fetch(:components, []) end
preview_template_location()
click to toggle source
# File lib/alephant/preview/server.rb, line 224 def preview_template_location "#{Template.path}/templates/preview.mustache" end
query_string()
click to toggle source
# File lib/alephant/preview/server.rb, line 152 def query_string Rack::Utils.parse_nested_query(request.query_string) end
raw_fixture_data()
click to toggle source
# File lib/alephant/preview/server.rb, line 212 def raw_fixture_data File.open(fixture_location).read end
region()
click to toggle source
# File lib/alephant/preview/server.rb, line 181 def region params['region'] end
request_body()
click to toggle source
# File lib/alephant/preview/server.rb, line 148 def request_body JSON.parse(request.body.read, symbolize_names: true) || {} end
sequence_id()
click to toggle source
# File lib/alephant/preview/server.rb, line 133 def sequence_id Time.now.to_i.to_s end
symbolize(hash)
click to toggle source
# File lib/alephant/preview/server.rb, line 232 def symbolize(hash) Hash[hash.map { |k, v| [k.to_sym, v] }] end
template()
click to toggle source
# File lib/alephant/preview/server.rb, line 177 def template params['template'] end
view_mapper()
click to toggle source
# File lib/alephant/preview/server.rb, line 228 def view_mapper Alephant::Renderer::ViewMapper.new(id, BASE_LOCATION) end