Class: KindleManager::FileStore

Inherits:
Object
  • Object
show all
Defined in:
lib/kindle_manager/file_store.rb

Constant Summary collapse

TIME_FORMAT_FOR_FILENAME =
'%Y%m%d%H%M%S'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ FileStore

Returns a new instance of FileStore.



7
8
9
10
11
12
13
14
# File 'lib/kindle_manager/file_store.rb', line 7

def initialize(options = {})
  @sub_dir = options.fetch(:sub_dir, 'books').to_s
  @dir_name = options.fetch(:dir_name) do
    tmp_dir_name = options[:create] ? nil : find_latest_dir_name
    tmp_dir_name.presence || Time.current.strftime("%Y%m%d%H%M%S")
  end
  @session = options.fetch(:session, nil)
end

Instance Attribute Details

#dir_nameObject

Returns the value of attribute dir_name.



5
6
7
# File 'lib/kindle_manager/file_store.rb', line 5

def dir_name
  @dir_name
end

#sessionObject

Returns the value of attribute session.



5
6
7
# File 'lib/kindle_manager/file_store.rb', line 5

def session
  @session
end

#sub_dirObject

Returns the value of attribute sub_dir.



5
6
7
# File 'lib/kindle_manager/file_store.rb', line 5

def sub_dir
  @sub_dir
end

Instance Method Details

#find_latest_dir_nameObject



24
25
26
# File 'lib/kindle_manager/file_store.rb', line 24

def find_latest_dir_name
  list_work_dirs.sort.last.to_s.split('/').last
end

#html_path(time) ⇒ Object



32
33
34
# File 'lib/kindle_manager/file_store.rb', line 32

def html_path(time)
  build_filepath(time, 'html')
end

#image_path(time) ⇒ Object



36
37
38
# File 'lib/kindle_manager/file_store.rb', line 36

def image_path(time)
  build_filepath(time, 'png')
end

#list_html_filesObject



28
29
30
# File 'lib/kindle_manager/file_store.rb', line 28

def list_html_files
  Dir[File.join(Capybara.save_path, target_dir,'*.html')].select{|f| File.file? f }
end

#list_work_dirsObject



20
21
22
# File 'lib/kindle_manager/file_store.rb', line 20

def list_work_dirs
  Dir[File.join(Capybara.save_path, sub_dir,'*')].select{|f| File.directory? f }
end

#record_pageObject



40
41
42
43
44
# File 'lib/kindle_manager/file_store.rb', line 40

def record_page
  time = Time.current
  @session.save_page(html_path(time))
  @session.save_screenshot(image_path(time))
end

#target_dirObject



16
17
18
# File 'lib/kindle_manager/file_store.rb', line 16

def target_dir
  File.join(sub_dir, dir_name)
end