Class: DaimonSkycrawlers::Storage::File

Inherits:
Base
  • Object
show all
Defined in:
lib/daimon_skycrawlers/storage/file.rb

Overview

Storage for files

Defined Under Namespace

Classes: Page

Instance Method Summary collapse

Methods included from LoggerMixin

included

Constructor Details

#initialize(base_dir) ⇒ File

Returns a new instance of File.



9
10
11
12
# File 'lib/daimon_skycrawlers/storage/file.rb', line 9

def initialize(base_dir)
  super()
  @base_dir = Pathname(base_dir)
end

Instance Method Details

#read(url) ⇒ Object



25
26
27
28
29
# File 'lib/daimon_skycrawlers/storage/file.rb', line 25

def read(url)
  headers = JSON.parse(headers_path(url).read)
  body = body_path(url).read
  Page.new(url, headers, body, headers["last-modified"], headers["etag"])
end

#save(url, headers, body) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/daimon_skycrawlers/storage/file.rb', line 14

def save(url, headers, body)
  @base_dir.mkpath
  body_path(url).dirname.mkpath
  body_path(url).open("wb+") do |file|
    file.write(body)
  end
  headers_path(url).open("wb+") do |file|
    file.write(JSON.generate(headers))
  end
end