Class: Capucine::Incloudr

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

Instance Method Summary collapse

Constructor Details

#initialize(capucine) ⇒ Incloudr

Returns a new instance of Incloudr.



11
12
13
# File 'lib/incloudr.rb', line 11

def initialize(capucine)
  @cap = capucine
end

Instance Method Details

#cdnjsObject



74
75
76
77
78
79
80
81
82
# File 'lib/incloudr.rb', line 74

def cdnjs
  api = Capucine::CDNJS.new(@file['name'], @file['version'], content = false)

  if not api.result[:error]
    self.url api.result[:content]
  else
    puts "Error on #{@file['name']}"
  end
end

#npmObject



84
85
86
87
# File 'lib/incloudr.rb', line 84

def npm
  # TODO !

end

#pack(file) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/incloudr.rb', line 29

def pack file

  @file = file

  @file['version'] = @file['version'].to_s if @file['version']
  @file['type'] = @file['type'] || 'cdnjs'
  @file['type'] = 'url' if @file['source']

  name = @file['file_name'] || @file['name']

  @dir = @cap.settings.working_dir
  @conf = @cap.settings.conf

  @output = File.join(@dir, @conf['incloudr_output_dir'], name.gsub(/$/, '.js'))
  @output_min = File.join(@dir, @conf['incloudr_output_dir'], name.gsub(/$/, '.min.js'))

  self.cdnjs if @file['type'] == 'cdnjs'
  self.url if @file['type'] == 'url'
  # self.npm if @file['type'] == 'npm'
end

#run_onceObject



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/incloudr.rb', line 15

def run_once
  files = @cap.settings.conf['incloudr_libs']
  return false if files.length == 0

  dir = File.join(@cap.settings.working_dir, @cap.settings.conf['incloudr_output_dir'])
  FileUtils.rm_r dir if File.exist?(dir)
  FileUtils.mkdir_p dir

  @file = nil

  files.each {|file| self.pack file}
  puts "[incloudr] - Packaged"
end

#url(raw_content = nil) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/incloudr.rb', line 50

def url raw_content = nil
  if not raw_content
    begin
      content = open(@file['source']).read
    rescue => e
      puts "Error"
      return e
    end
  else
    content = raw_content
  end

  content_min = ""
  content_min << Packr.pack(content)

  f1 = File.open(@output, 'w+')
  f1.write(content)
  f1.close

  f2 = File.open(@output_min, 'w+')
  f2.write(content_min)
  f2.close
end