Class: Fetchers::Local

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target) ⇒ Local

Returns a new instance of Local.



42
43
44
# File 'lib/fetchers/local.rb', line 42

def initialize(target)
  @target = target
end

Class Method Details

.resolve(target) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/fetchers/local.rb', line 12

def self.resolve(target)
  local_path = if target.is_a?(String)
                 resolve_from_string(target)
               elsif target.is_a?(Hash)
                 resolve_from_hash(target)
               end

  new(local_path) if local_path
end

.resolve_from_hash(target) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/fetchers/local.rb', line 22

def self.resolve_from_hash(target)
  return unless target.key?(:path)

  local_path = target[:path]
  local_path = File.expand_path(local_path, target[:cwd]) if target.key?(:cwd)
  local_path
end

.resolve_from_string(target) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/fetchers/local.rb', line 30

def self.resolve_from_string(target)
  # Support "urls" in the form of file://
  if target.start_with?('file://')
    target = target.gsub(%r{^file://}, '')
  else
    # support for windows paths
    target = target.tr('\\', '/')
  end

  target if File.exist?(target)
end

Instance Method Details

#archive_pathObject



50
51
52
# File 'lib/fetchers/local.rb', line 50

def archive_path
  @target
end

#cache_keyObject



58
59
60
# File 'lib/fetchers/local.rb', line 58

def cache_key
  sha256.to_s
end

#fetch(_path) ⇒ Object



46
47
48
# File 'lib/fetchers/local.rb', line 46

def fetch(_path)
  archive_path
end

#resolved_sourceObject



68
69
70
71
72
# File 'lib/fetchers/local.rb', line 68

def resolved_source
  h = { path: @target }
  h[:sha256] = sha256 if sha256
  h
end

#sha256Object



62
63
64
65
66
# File 'lib/fetchers/local.rb', line 62

def sha256
  return nil if File.directory?(@target)
  @archive_shasum ||=
    OpenSSL::Digest::SHA256.digest(File.read(@target)).unpack('H*')[0]
end

#writable?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/fetchers/local.rb', line 54

def writable?
  File.directory?(@target)
end