Class: FetchAndProcess::Base

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path) ⇒ Base

Returns a new instance of Base.



10
11
12
# File 'lib/fetch_and_process/base.rb', line 10

def initialize(file_path)
  @file_path = file_path
end

Instance Attribute Details

#file_pathObject (readonly)

Returns the value of attribute file_path.



8
9
10
# File 'lib/fetch_and_process/base.rb', line 8

def file_path
  @file_path
end

Class Method Details

.add_handler(scheme, method) ⇒ Object



56
57
58
# File 'lib/fetch_and_process/base.rb', line 56

def add_handler(scheme, method)
  handlers[scheme.to_sym] = method.to_sym
end

.handle?(uri) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/fetch_and_process/base.rb', line 48

def handle?(uri)
  handlers.key? uri.scheme.to_sym
end

.handler(uri) ⇒ Object



52
53
54
# File 'lib/fetch_and_process/base.rb', line 52

def handler(uri)
  handlers[uri.scheme.to_sym]
end

.handlersObject



61
62
63
# File 'lib/fetch_and_process/base.rb', line 61

def self.handlers
  @handlers ||= {}
end

Instance Method Details

#cache_locationObject



38
39
40
41
42
43
44
45
# File 'lib/fetch_and_process/base.rb', line 38

def cache_location
  @cache_location ||= begin
    hash = Digest::MD5.hexdigest uri.to_s
    path = "#{Dir.tmpdir}/fetch_and_process"
    Dir.mkdir(path, 0o700) unless File.exist?(path)
    "#{path}/#{hash}"
  end
end

#downloadObject

After handle is executed, the fetched file should exist on the path specified by ‘target`



24
25
26
27
28
# File 'lib/fetch_and_process/base.rb', line 24

def download
  raise FetchAndProcess::UnsupportedFileError unless uri.scheme && handle?

  send(handler)
end

#handle?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/fetch_and_process/base.rb', line 30

def handle?
  self.class.handle?(uri)
end

#handlerObject



34
35
36
# File 'lib/fetch_and_process/base.rb', line 34

def handler
  self.class.handler(uri)
end

#processObject



18
19
20
21
# File 'lib/fetch_and_process/base.rb', line 18

def process
  # No action defined by default
  raise FetchAndProcess::UnimplementedFileError
end

#uriObject



14
15
16
# File 'lib/fetch_and_process/base.rb', line 14

def uri
  @uri ||= URI.parse(file_path)
end