Sha256: ad537c7cd93e470986f49ba6457f1362dcd8b3d0bd20d485cf1aa62b9d567817

Contents?: true

Size: 1.35 KB

Versions: 10

Compression:

Stored size: 1.35 KB

Contents

require 'faraday'
require 'uri'

module Alephant
  module Preview

    def self.path
      "#{Dir.pwd}/components/lib"
    end

    class Template

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

      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

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

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

      def path
        uri.path
      end

      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

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

      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
    end
  end
end


Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
alephant-0.0.9.9.1-java lib/alephant/preview/template.rb
alephant-0.0.9.9-java lib/alephant/preview/template.rb
alephant-0.0.9.8-java lib/alephant/preview/template.rb
alephant-0.0.9.7-java lib/alephant/preview/template.rb
alephant-0.0.9.6-java lib/alephant/preview/template.rb
alephant-0.0.9.5-java lib/alephant/preview/template.rb
alephant-0.0.9.4-java lib/alephant/preview/template.rb
alephant-0.0.9.3-java lib/alephant/preview/template.rb
alephant-0.0.9.2-java lib/alephant/preview/template.rb
alephant-0.0.9.1-java lib/alephant/preview/template.rb