Class: DayOneKindle::Device

Inherits:
Object show all
Defined in:
lib/dayone-kindle/device.rb

Constant Summary collapse

CLIPPINGS_PATH =
'/documents/My Clippings.txt'.freeze
VERSION_PATH =
'/system/version.txt'.freeze
BACKUP_PATH =
'/clippings-backups'.freeze
BACKUP_NAME =
'%Y%m%d-%H%M%S.txt'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, name) ⇒ Device

Returns a new instance of Device.



10
11
12
13
14
15
# File 'lib/dayone-kindle/device.rb', line 10

def initialize(path, name)
  @path = path
  @name = name
  @clippings_path = File.join(path, CLIPPINGS_PATH)
  @clippings_backups_path = File.join(path, BACKUP_PATH)
end

Instance Attribute Details

#clippings_backups_pathObject (readonly)

Returns the value of attribute clippings_backups_path.



8
9
10
# File 'lib/dayone-kindle/device.rb', line 8

def clippings_backups_path
  @clippings_backups_path
end

#clippings_pathObject (readonly)

Returns the value of attribute clippings_path.



8
9
10
# File 'lib/dayone-kindle/device.rb', line 8

def clippings_path
  @clippings_path
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/dayone-kindle/device.rb', line 8

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



8
9
10
# File 'lib/dayone-kindle/device.rb', line 8

def path
  @path
end

Class Method Details

.find_at(mount_path) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/dayone-kindle/device.rb', line 33

def self.find_at(mount_path)
  volumes = Dir.entries(mount_path) - ['.', '..']

  volumes.map do |name|
    path = File.join(mount_path, name)
    version_path = File.join(path, VERSION_PATH)

    if File.exist?(version_path) && IO.read(version_path) =~ /^Kindle/
      new(path, name)
    end
  end.compact
end

Instance Method Details

#archive_highlights!Object



21
22
23
24
25
26
27
# File 'lib/dayone-kindle/device.rb', line 21

def archive_highlights!
  Dir.mkdir(clippings_backups_path) unless Dir.exist?(clippings_backups_path)
  backup_name = Time.now.strftime(BACKUP_NAME)
  backup_file_path = File.join(clippings_backups_path, backup_name)
  FileUtils.cp(clippings_path, backup_file_path)
  backup_file_path
end

#clear_highlights!Object



29
30
31
# File 'lib/dayone-kindle/device.rb', line 29

def clear_highlights!
  File.truncate(clippings_path, 0)
end

#highlightsObject



17
18
19
# File 'lib/dayone-kindle/device.rb', line 17

def highlights
  ClippingsParser.new(raw_clippings).highlights
end