Class: Skeletor::Includes

Inherits:
Object
  • Object
show all
Defined in:
lib/skeletor/includes.rb

Overview

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

Constant Summary collapse

PROTOCOL_PATTERN =

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

/(?:([a-z][\w-]+):(?:\/{1,3}|[a-z0-9%]))/
SUPPORTED_PROTOCOLS =

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

['http','https']

Class Method Summary collapse

Class Method Details

.copy_include(include, path) ⇒ Object

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



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# 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