Class: Informers::Utils::Hub::FileCache

Inherits:
Object
  • Object
show all
Defined in:
lib/informers/utils/hub.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ FileCache

Returns a new instance of FileCache.



73
74
75
# File 'lib/informers/utils/hub.rb', line 73

def initialize(path)
  @path = path
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



71
72
73
# File 'lib/informers/utils/hub.rb', line 71

def path
  @path
end

Instance Method Details

#match(request) ⇒ Object



77
78
79
80
81
82
# File 'lib/informers/utils/hub.rb', line 77

def match(request)
  file_path = resolve_path(request)
  file = FileResponse.new(file_path)

  file if file.exists
end

#put(request, response) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/informers/utils/hub.rb', line 84

def put(request, response)
  output_path = resolve_path(request)

  begin
    tmp_path = "#{output_path}.incomplete"
    FileUtils.mkdir_p(File.dirname(output_path))
    File.open(tmp_path, "wb") do |f|
      while !response.eof?
        f.write(response.read(1024 * 1024))
      end
    end
    FileUtils.move(tmp_path, output_path)
  rescue => e
    warn "An error occurred while writing the file to cache: #{e}"
  end
end

#resolve_path(request) ⇒ Object



101
102
103
# File 'lib/informers/utils/hub.rb', line 101

def resolve_path(request)
  File.join(@path, request)
end