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.



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

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

Instance Attribute Details

#debug_modeObject (readonly)

Returns the value of attribute debug_mode.



120
121
122
# File 'lib/cdnget.rb', line 120

def debug_mode
  @debug_mode
end

Class Method Details

.inherited(klass) ⇒ Object



113
114
115
# File 'lib/cdnget.rb', line 113

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

Instance Method Details

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



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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/cdnget.rb', line 141

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
  skipfile = d[:skipfile]   # ex: /\.DS_Store\z/
  d[:files].each do |file|
    filepath = File.join(target_dir, file)
    #
    if skipfile && file =~ skipfile
      puts "#{filepath} ... Skipped"   # for example, skip '.DS_Store' files
      next
    end
    #
    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)


133
134
135
# File 'lib/cdnget.rb', line 133

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

#get(library, version) ⇒ Object

Raises:

  • (NotImplementedError)


137
138
139
# File 'lib/cdnget.rb', line 137

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

#latest_version(library) ⇒ Object



192
193
194
195
196
# File 'lib/cdnget.rb', line 192

def latest_version(library)
  validate(library, nil)
  d = self.find(library)
  return d[:versions].first
end

#listObject

Raises:

  • (NotImplementedError)


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

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

#search(pattern) ⇒ Object



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

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