Class: Alephant::Preview::Template::Updater

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

Instance Method Summary collapse

Instance Method Details

#apply_static_host_regex_to(string) ⇒ Object



38
39
40
# File 'lib/alephant/preview/template/updater.rb', line 38

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

#hostObject



21
22
23
# File 'lib/alephant/preview/template/updater.rb', line 21

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

#pathObject



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

def path
  uri.path
end

#static_host_regexObject

Raises:

  • (Exception)


42
43
44
45
46
47
48
49
# File 'lib/alephant/preview/template/updater.rb', line 42

def static_host_regex
  return @static_host_regex unless @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



8
9
10
11
12
13
# File 'lib/alephant/preview/template/updater.rb', line 8

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



15
16
17
18
19
# File 'lib/alephant/preview/template/updater.rb', line 15

def update(template_location)
  File.open(template_location, "w") do |file|
    file.write(template)
  end
end

#uriObject

Raises:

  • (Exception)


29
30
31
32
33
34
35
36
# File 'lib/alephant/preview/template/updater.rb', line 29

def uri
  return @uri unless @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