Class: CDNGet::CDNJS

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

Overview

TODO: use jsdelivr api

Constant Summary collapse

CODE =
"cdnjs"
SITE_URL =
'https://cdnjs.com/'

Instance Method Summary collapse

Methods inherited from Base

#download, inherited

Instance Method Details

#find(library) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/cdnget.rb', line 143

def find(library)
  validate(library, nil)
  html = fetch("https://cdnjs.com/libraries/#{library}", library)
  flagment = html.split(/<select class=".*?version-selector.*?"/, 2).last
  flagment = flagment.split(/<\/select>/, 2).first
  versions = []
  flagment.scan(/<option value="([^"]+)" *(?:selected)?>/) do |ver,|
    versions << ver
  end
  desc = tags = nil
  if html =~ /<\/p>\s*<p>(.*?)<\/p>\s*<em>(.*?)<\/em>/
    desc = $1
    tags = $2
  end
  return {
    name: library,
    desc: desc,
    tags: tags,
    versions: versions,
  }
end

#get(library, version) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/cdnget.rb', line 165

def get(library, version)
  validate(library, version)
  html = fetch("https://cdnjs.com/libraries/#{library}/#{version}", library)
  baseurl = "https://cdnjs.cloudflare.com/ajax/libs/#{library}/#{version}/"
  basepat = Regexp.escape("#{library}/#{version}")
  files = []
  html.scan(%r`>#{basepat}/([^<]+)<\/p>`) do |file,|
    files << file.gsub(/&#x2F;/, '/')
  end
  urls = files.collect {|s| baseurl + s }
  return {
    name:     library,
    version:  version,
    urls:     urls,
    files:    files,
    baseurl:  baseurl,
  }
end

#listObject



131
132
133
134
135
136
137
138
139
140
141
# File 'lib/cdnget.rb', line 131

def list
  libs = []
  html = fetch("https://cdnjs.com/libraries")
  html.scan(/<td\b(.*?)<\/td>/m) do |str,|
    name = desc = nil
    name = $1 if str =~ /href="\/libraries\/([-.\w]+)">\s*\1\s*<\/a>/
    desc = $1.strip if str =~ /<div style="display: none;">(.*?)<\/div>/m
    libs << {name: name, desc: desc} if name
  end
  return libs.sort_by {|d| d[:name] }.uniq
end