Top Level Namespace

Defined Under Namespace

Modules: Colours, Klipbook Classes: InvalidSourceError, NilClass, String, Struct

Constant Summary collapse

CONFIG =

Go time

Klipbook::Config.new.read
DEFAULT_MAXBOOKS =
5
SOURCE_HELP =
"You must specify either `--credentials` or `--infile` as an input."
CONFIG_HELP =
"Note that credentials, outdir, and outfile defaults can be stored in a file called ~/.klipbookrc\n\n" +
"This file is YAML formatted e.g.\n\n" +
":credentials: [email protected]:my-kindle-password\n" +
":outdir: my/default/output/directory\n" +
":outfile: my/default/output/file.json\n"

Instance Method Summary collapse

Instance Method Details



144
145
146
# File 'bin/klipbook', line 144

def banner_for_command(command, description)
  "Usage: klipbook #{command} [options]\n\n#{description}\n\n#{SOURCE_HELP}\n"
end

#book_file(file_path) ⇒ Object



74
75
76
# File 'bin/klipbook', line 74

def book_file(file_path)
  Klipbook::ToJson::BookFile.from_json(raw_json_from_file(file_path))
end

#credentials(opts) ⇒ Object



86
87
88
89
90
# File 'bin/klipbook', line 86

def credentials(opts)
  puts "Using credentials from ~/.klipbookrc" if !opts[:credentials] && CONFIG[:credentials]

  opts[:credentials] || CONFIG[:credentials]
end

#ensure_outdir(opts) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'bin/klipbook', line 107

def ensure_outdir(opts)
  unless opts[:outdir] || CONFIG[:outdir]
    $stderr.puts("Error: Please specify an outdir.")
    exit(127)
  end

  outdir = outdir_path(opts)

  unless File.exists?(outdir)
    $stderr.puts("Error: Outdir does not exist.")
    exit(127)
  end

  outdir
end

#ensure_outfile(opts) ⇒ Object



92
93
94
95
96
97
98
99
# File 'bin/klipbook', line 92

def ensure_outfile(opts)
  unless opts[:outfile] || CONFIG[:outfile]
    $stderr.puts("Error: Please specify an outfile.")
    exit(127)
  end

  outfile_path(opts)
end

#exit_unless_valid_source(opts) ⇒ Object



25
26
27
28
29
30
# File 'bin/klipbook', line 25

def exit_unless_valid_source(opts)
  unless opts[:infile] || opts[:credentials] || CONFIG[:credentials]
    $stderr.puts "Error: #{SOURCE_HELP}"
    exit 127
  end
end

#fetch_books(opts) ⇒ Object



51
52
53
# File 'bin/klipbook', line 51

def fetch_books(opts)
  get_book_source(opts).books
end

#get_book_source(opts) ⇒ Object

Helpers



12
13
14
15
16
17
18
19
20
21
22
23
# File 'bin/klipbook', line 12

def get_book_source(opts)
  exit_unless_valid_source(opts)

  max_books   = opts[:number].to_i

  unless max_books > 0
    $stderr.puts "Error: Specify a number of books greater than 0"
    exit 127
  end

  get_file_source(infile(opts), max_books) || get_site_source(credentials(opts), max_books)
end

#get_file_source(file, max_books) ⇒ Object



45
46
47
48
49
# File 'bin/klipbook', line 45

def get_file_source(file, max_books)
  return nil unless file

  Klipbook::Sources::KindleDevice::File.new(file, max_books)
end

#get_site_source(creds, max_books) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'bin/klipbook', line 32

def get_site_source(creds, max_books)
  return nil unless creds

  unless creds =~ /(.+):(.+)/
    $stderr.puts "Error: your credentials need to be in username:password format."
    exit 127
  end

  username = $1
  password = $2
  Klipbook::Sources::AmazonSite::SiteScraper.new(username, password, max_books)
end

#infile(opts) ⇒ Object

Params



82
83
84
# File 'bin/klipbook', line 82

def infile(opts)
  open_infile(opts[:infile])
end

#open_infile(file_path) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'bin/klipbook', line 55

def open_infile(file_path)
  return nil unless file_path

  unless File.exists? file_path
    $stderr.puts "Error: could not open #{file_path}"
    exit 127
  end

  File.open(file_path, "r")
end

#outdir_path(opts) ⇒ Object



123
124
125
126
127
# File 'bin/klipbook', line 123

def outdir_path(opts)
  puts "Using outdir from ~/.klipbookrc" if !opts[:outdir] && CONFIG[:outdir]

  opts[:outdir] || CONFIG[:outdir] || Dir.pwd
end

#outfile_path(opts) ⇒ Object



101
102
103
104
105
# File 'bin/klipbook', line 101

def outfile_path(opts)
  puts "Using outfile from ~/.klipbookrc" if !opts[:outfile] && CONFIG[:outfile]

  opts[:outfile] || CONFIG[:outfile]
end

#raw_json_from_file(file_path) ⇒ Object



66
67
68
69
70
71
72
# File 'bin/klipbook', line 66

def raw_json_from_file(file_path)
  File.open(file_path, 'r') do |f|
    f.read
  end
rescue
  ""
end