Class: Alephant::Preview::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/alephant/preview/template.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.update(template_location) ⇒ Object



13
14
15
# File 'lib/alephant/preview/template.rb', line 13

def self.update(template_location)
  self.new.update(template_location)
end

Instance Method Details

#apply_static_host_regex_to(string) ⇒ Object



47
48
49
# File 'lib/alephant/preview/template.rb', line 47

def apply_static_host_regex_to(string)
  string.gsub(static_host_regex, '{{{static_host}}}')
end

#hostObject



30
31
32
# File 'lib/alephant/preview/template.rb', line 30

def host
  "#{uri.scheme}://#{uri.host}"
end

#pathObject



34
35
36
# File 'lib/alephant/preview/template.rb', line 34

def path
  uri.path
end

#static_host_regexObject

Raises:

  • (Exception)


51
52
53
54
55
56
57
58
# File 'lib/alephant/preview/template.rb', line 51

def static_host_regex
  return @static_host_regex if not @static_host_regex.nil?

  static_host_regex_from_env = ENV['STATIC_HOST_REGEX']
  raise Exception.new("STATIC_HOST_REGEX is unset!") if static_host_regex_from_env.nil?

  @static_host_regex = Regexp.new(static_host_regex_from_env)
end

#templateObject



17
18
19
20
21
22
# File 'lib/alephant/preview/template.rb', line 17

def template
  response = Faraday.new(:url => host).get(path)
  raise "Can't get template" if response.status != 200

  apply_static_host_regex_to response.body
end

#update(template_location) ⇒ Object



24
25
26
27
28
# File 'lib/alephant/preview/template.rb', line 24

def update(template_location)
  File.open(template_location, 'w') { |file|
    file.write(template)
  }
end

#uriObject

Raises:

  • (Exception)


38
39
40
41
42
43
44
45
# File 'lib/alephant/preview/template.rb', line 38

def uri
  return @uri if not @uri.nil?

  uri_from_env = ENV['PREVIEW_TEMPLATE_URL']
  raise Exception.new("PREVIEW_TEMPLATE_URL is unset!") if uri_from_env.nil?

  @uri = URI(uri_from_env)
end