Module: CdnjsCommand::Fetcher

Extended by:
Helpers
Defined in:
lib/cdnjs_command/fetcher.rb

Constant Summary collapse

CACHE_PATH =
"~/.cache/cdnjs"

Class Method Summary collapse

Methods included from Helpers

get_package, invalid_usage, show_help, tip

Class Method Details

.cache_for(url) ⇒ Object



32
33
34
# File 'lib/cdnjs_command/fetcher.rb', line 32

def self.cache_for(url)
  File.open(cache_path_for(url)) { |f| f.read }
end

.cache_path_for(url) ⇒ Object



28
29
30
# File 'lib/cdnjs_command/fetcher.rb', line 28

def self.cache_path_for(url)
  File.expand_path slug(url), CACHE_PATH
end

.cached?(url) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/cdnjs_command/fetcher.rb', line 24

def self.cached?(url)
  File.file? cache_path_for(url)
end

.fetch(url) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/cdnjs_command/fetcher.rb', line 6

def self.fetch(url)
  return cache_for(url)  if cached?(url)

  tip "Fetching #{url}..."
  output = open(url).read

  FileUtils.mkdir_p File.expand_path(CACHE_PATH)
  File.open(cache_path_for(url), 'w') { |f| f.write output }
  output
rescue => e
  tip "Unable to fetch (#{e.class.name}: #{e.message})."
  exit
end

.purgeObject



20
21
22
# File 'lib/cdnjs_command/fetcher.rb', line 20

def self.purge
  FileUtils.rm_rf File.expand_path(CACHE_PATH)
end

.slug(url) ⇒ Object



36
37
38
# File 'lib/cdnjs_command/fetcher.rb', line 36

def self.slug(url)
  url.gsub(/[\/:]/, '_')
end