Class: CDNGet::Base

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

Direct Known Subclasses

CDNJS, GoogleCDN, JSDelivr, Unpkg

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(debug_mode: false) ⇒ Base

Returns a new instance of Base.



106
107
108
# File 'lib/cdnget.rb', line 106

def initialize(debug_mode: false)
  @debug_mode = debug_mode
end

Instance Attribute Details

#debug_modeObject (readonly)

Returns the value of attribute debug_mode.



109
110
111
# File 'lib/cdnget.rb', line 109

def debug_mode
  @debug_mode
end

Class Method Details

.inherited(klass) ⇒ Object



102
103
104
# File 'lib/cdnget.rb', line 102

def self.inherited(klass)
  CLASSES << klass
end

Instance Method Details

#download(library, version, basedir = ".", quiet: false) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/cdnget.rb', line 130

def download(library, version, basedir=".", quiet: false)
  validate(library, version)
  File.exist?(basedir)  or
    raise CommandError.new("#{basedir}: not exist.")
  File.directory?(basedir)  or
    raise CommandError.new("#{basedir}: not a directory.")
  d = get(library, version)
  target_dir = d[:destdir] ? File.join(basedir, d[:destdir]) \
                           : File.join(basedir, library, version)
  http = nil
  d[:files].each do |file|
    filepath = File.join(target_dir, file)
    if filepath.end_with?('/')
      if File.exist?(filepath)
        puts "#{filepath} ... Done (Already exists)" unless quiet
      else
        print "#{filepath} ..." unless quiet
        FileUtils.mkdir_p(filepath)
        puts " Done (Created)" unless quiet
      end
      next
    end
    dirpath  = File.dirname(filepath)
    print "#{filepath} ..." unless quiet
    url = File.join(d[:baseurl], file)   # not use URI.join!
    uri = URI.parse(url)
    http ||= HttpConnection.new(uri)
    content = http.get(uri)
    content = content.force_encoding('ascii-8bit')
    print " Done (#{format_integer(content.bytesize)} byte)" unless quiet
    FileUtils.mkdir_p(dirpath) unless File.exist?(dirpath)
    unchanged = File.exist?(filepath) && File.read(filepath, mode: 'rb') == content
    if unchanged
      print " (Unchanged)" unless quiet
    else
      File.open(filepath, 'wb') {|f| f.write(content) }
    end
    puts() unless quiet
  end
  http.close() if http
  nil
end

#find(library) ⇒ Object

Raises:

  • (NotImplementedError)


122
123
124
# File 'lib/cdnget.rb', line 122

def find(library)
  raise NotImplementedError.new("#{self.class.name}#find(): not implemented yet.")
end

#get(library, version) ⇒ Object

Raises:

  • (NotImplementedError)


126
127
128
# File 'lib/cdnget.rb', line 126

def get(library, version)
  raise NotImplementedError.new("#{self.class.name}#get(): not implemented yet.")
end

#listObject

Raises:

  • (NotImplementedError)


111
112
113
# File 'lib/cdnget.rb', line 111

def list()
  raise NotImplementedError.new("#{self.class.name}#list(): not implemented yet.")
end

#search(pattern) ⇒ Object



115
116
117
118
119
120
# File 'lib/cdnget.rb', line 115

def search(pattern)
  return list().select {|d| File.fnmatch(pattern, d[:name], File::FNM_CASEFOLD) }
  #rexp_str = pattern.split('*', -1).collect {|x| Regexp.escape(x) }.join('.*')
  #rexp = Regexp.compile("\\A#{rexp_str}\\z", Regexp::IGNORECASE)
  #return list().select {|d| d[:name] =~ rexp }
end