Sha256: b4dfa62e42f8ff0ee0341ebf1d0a11bfe6e9a50dde2bc3c973722d9b1b8b176e

Contents?: true

Size: 1.37 KB

Versions: 2

Compression:

Stored size: 1.37 KB

Contents

module KindleManager
  class FileStore
    attr_accessor :dir_name

    def initialize(session, options = {})
      @dir_name = options.fetch(:dir_name) do
        options[:latest] ? find_latest_dir_name : Time.current.strftime("%Y%m%d%H%M%S")
      end
      @session = session
    end

    def base_dir
      File.join(self.class.downloads_dir, @dir_name)
    end

    def self.downloads_dir
      'downloads'
    end

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

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

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

    def self.list_download_dirs
      Dir["#{downloads_dir}/*"].select{|f| File.directory? f }
    end

    def self.list_html_files(dir = nil)
      if dir
        Dir[File.join(downloads_dir, dir,'*.html')].select{|f| File.file? f }
      else
        Dir["#{downloads_dir}/*/*.html"].select{|f| File.file? f }
      end
    end

    def list_html_files
      self.class.list_html_files(@dir_name)
    end

    def find_latest_dir_name
      self.class.list_download_dirs.sort.last.split('/').last
    end

    private

      def build_filepath(time, ext)
        File.join(base_dir, "#{time.strftime('%Y%m%d%H%M%S')}#{(time.usec / 1000.0).round.to_s.rjust(3,'0')}.#{ext}")
      end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
kindle_manager-0.1.1 lib/kindle_manager/file_store.rb
kindle_manager-0.1.0 lib/kindle_manager/file_store.rb