class Skeletor::Includes

The Includes class contains methods for dealing with loading and parsing required include files.

Constants

PROTOCOL_PATTERN

Internal regular expression to match for includes that should be loaded from a remote destination

SUPPORTED_PROTOCOLS

Specifies a list of supported protocols that Skeletor can load from.

Public Class Methods

copy_include(include,path) click to toggle source

Reads the required include from either the remote url or the local path and writes it to the required location in the skeleton.

# File lib/skeletor/includes.rb, line 19
def self.copy_include(include,path)
  
  #if include path includes a protocol. Load from that
  matches = PROTOCOL_PATTERN.match(include).to_a
  if !matches.empty?
    protocol = matches[1].to_s.downcase
    if !SUPPORTED_PROTOCOLS.find_index(protocol).nil?
      case protocol
        when 'http'
          content = HTTP.get URI.parse(include)
        when 'https'
          uri = URI.parse(include)
          http = HTTPS.new uri.host,443
          http.use_ssl = true
          req = HTTPS::Get.new uri.path
          request = http.request(req)
          content = request.body
        else
          raise TypeError, 'Unsupported protocol ' + protocol + ' for remote file. Only the following are supported: ' + SUPPORTED_PROTOCOLS.join(', ') 
       end
       puts 'Reading remote file ' + include
    else
      raise TypeError, 'Unsupported protocol for remote file. Only the following are supported: ' + SUPPORTED_PROTOCOLS.join(', ') 
    end
  else
    puts 'Reading ' + include +  ' from template directory'
    file = File.open(File.join(path,include))
    content = file.gets nil
  end
  
  return content
  
end