Class: Skeletor::Includes

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

Constant Summary collapse

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

Class Method Summary collapse

Class Method Details

.copy_include(include, target, path) ⇒ Object



13
14
15
16
17
18
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
# File 'lib/skeletor/includes.rb', line 13

def self.copy_include(include,target,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 'Copying remote file ' + include + ' to ' + target
    else
      raise TypeError, 'Unsupported protocol for remote file. Only the following are supported: ' + SUPPORTED_PROTOCOLS.join(', ') 
    end
  else
    puts 'Copying ' + include +  ' from template directory to ' + target
    file = File.open(File.join(path,include))
    content = file.gets 
  end
  
  File.open(target,'w'){|f| f.write(content)}
  
end