Class: Device

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

Defined Under Namespace

Modules: Ibunko, Kindle, Kobo, Library, Reader Classes: UnknownDevice

Constant Summary collapse

DEVICES =
{}.tap do |h|
  Dir.glob(File.join(File.dirname(__FILE__), "device", "*.rb")).each do |name|
    name = File.basename(name, ".rb")
    require_relative "device/#{name}"
    h[name] = eval(name.capitalize)
  end
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(device_name) ⇒ Device

Returns a new instance of Device.



46
47
48
49
50
51
52
53
54
55
# File 'lib/device.rb', line 46

def initialize(device_name)
  unless Device.exists?(device_name)
    raise UnknownDevice, "#{device_name} は存在しません"
  end
  @device = DEVICES[device_name.downcase]
  @name = @device::NAME
  @display_name = @device::DISPLAY_NAME
  @ebook_file_ext = @device::EBOOK_FILE_EXT
  create_device_check_methods
end

Instance Attribute Details

#display_nameObject (readonly)

Returns the value of attribute display_name.



22
23
24
# File 'lib/device.rb', line 22

def display_name
  @display_name
end

#ebook_file_extObject (readonly)

Returns the value of attribute ebook_file_ext.



22
23
24
# File 'lib/device.rb', line 22

def ebook_file_ext
  @ebook_file_ext
end

#nameObject (readonly)

Returns the value of attribute name.



22
23
24
# File 'lib/device.rb', line 22

def name
  @name
end

Class Method Details

.create(device_name) ⇒ Object



38
39
40
41
42
# File 'lib/device.rb', line 38

def self.create(device_name)
  @@device_cache ||= {}
  name = device_name.downcase
  return @@device_cache[name] ||= new(name)
end

.exists?(device) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/device.rb', line 34

def self.exists?(device)
  DEVICES.include?(device.downcase)
end

Instance Method Details

#connecting?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/device.rb', line 57

def connecting?
  !!get_documents_path
end

#copy_to_documents(src_file) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/device.rb', line 90

def copy_to_documents(src_file)
  documents_path = get_documents_path
  if documents_path
    dst_path = File.join(documents_path, File.basename(src_file))
    case Helper.determine_os
    when :windows
      begin
        # Rubyでコピーするのは遅いのでOSのコマンドを叩く
        cmd = "copy /B " + %!"#{src_file}" "#{dst_path}"!.gsub("/", "\\").encode(Encoding::Windows_31J)
        capture = `#{cmd}`
        if $?.exitstatus > 0
          raise capture.force_encoding(Encoding::Windows_31J).rstrip
        end
      rescue Encoding::UndefinedConversionError, Encoding::InvalidByteSequenceError => e
        # Windows-31J に変換できない文字をファイル名に含むものはRubyでコピーする
        FileUtils.cp(src_file, dst_path)
      end
    else
      FileUtils.cp(src_file, dst_path)
    end
    dst_path
  else
    nil
  end
rescue Exception => e
  puts
  error $@.shift + ": #{e.message} (#{e.class})"
  exit 1
end

#ebook_file_old?(src_file) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
82
83
84
85
86
87
88
# File 'lib/device.rb', line 79

def ebook_file_old?(src_file)
  documents_path = get_documents_path
  if documents_path
    dst_path = File.join(documents_path, File.basename(src_file))
    if File.exists?(dst_path)
      return File.mtime(src_file) > File.mtime(dst_path)
    end
  end
  true
end

#find_documents_directory(device_root_dir) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/device.rb', line 61

def find_documents_directory(device_root_dir)
  @device::DOCUMENTS_PATH_LIST.each do |documents_path|
    documents_directory = File.join(device_root_dir, documents_path)
    return documents_directory if File.directory?(documents_directory)
  end
  nil
end

#get_documents_pathObject



69
70
71
72
73
74
75
76
77
# File 'lib/device.rb', line 69

def get_documents_path
  if Device.respond_to?(:get_device_root_dir)
    dir = Device.get_device_root_dir(@device::VOLUME_NAME)
    if dir
      return find_documents_directory(dir)
    end
  end
  nil
end

#physical_support?Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/device.rb', line 120

def physical_support?
  @device::PHYSICAL_SUPPORT
end