Class: Softcover::Book

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/softcover/book.rb

Defined Under Namespace

Classes: BookFile, UploadError

Constant Summary collapse

DEFAULT_SCREENCASTS_DIR =
"screencasts"

Constants included from Utils

Utils::UNITS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#add_highlight_class!, #as_size, #current_book, #digest, #executable, #execute, #in_book_directory?, #logged_in?, #mkdir, #path, #reset_current_book!, #rm, #silence, #source, #tmpify, #write_pygments_file

Constructor Details

#initialize(options = {}) ⇒ Book

Returns a new instance of Book.



11
12
13
14
15
16
17
18
19
# File 'lib/softcover/book.rb', line 11

def initialize(options={})
  require "softcover/client"
  @manifest = Softcover::BookManifest.new(options)
  @client = Softcover::Client.new_with_book self

  @screencasts_dir = DEFAULT_SCREENCASTS_DIR

  @processed_screencasts = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (private)



187
188
189
# File 'lib/softcover/book.rb', line 187

def method_missing(name, *args, &block)
  @manifest.send(name) || super
end

Instance Attribute Details

#errorsObject

Returns the value of attribute errors.



6
7
8
# File 'lib/softcover/book.rb', line 6

def errors
  @errors
end

#manifestObject

Returns the value of attribute manifest.



6
7
8
# File 'lib/softcover/book.rb', line 6

def manifest
  @manifest
end

#processed_screencastsObject

Returns the value of attribute processed_screencasts.



6
7
8
# File 'lib/softcover/book.rb', line 6

def processed_screencasts
  @processed_screencasts
end

#screencasts_dirObject

Returns the value of attribute screencasts_dir.



6
7
8
# File 'lib/softcover/book.rb', line 6

def screencasts_dir
  @screencasts_dir
end

#signaturesObject

Returns the value of attribute signatures.



6
7
8
# File 'lib/softcover/book.rb', line 6

def signatures
  @signatures
end

#uploaderObject

Returns the value of attribute uploader.



6
7
8
# File 'lib/softcover/book.rb', line 6

def uploader
  @uploader
end

Instance Method Details

#chapter_attributesObject



70
71
72
# File 'lib/softcover/book.rb', line 70

def chapter_attributes
  chapters.map(&:to_hash)
end

#create_or_updateObject



84
85
86
87
88
89
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
# File 'lib/softcover/book.rb', line 84

def create_or_update
  raise "HTML not built!" if Dir['html/*'].empty?

  res = @client.create_or_update_book id: id,
                                      files: files,
                                      title: title,
                                      slug: slug,
                                      subtitle: subtitle,
                                      description: description,
                                      cover: cover,
                                      chapters: chapter_attributes

  if res['errors']
    @errors = res['errors']
    return false
  end

  # is this needed?
  @attrs = res['book']

  self.id = @attrs['id']
  Softcover::BookConfig['last_uploaded_at'] = Time.now

  @uploader = Softcover::Uploader.new res

  true

rescue Exception => e
  @errors = [e.message]
  raise e
  false
end

#destroyObject



141
142
143
144
145
146
147
148
# File 'lib/softcover/book.rb', line 141

def destroy
  res = @client.destroy
  if res['errors']
    @errors = res['errors']
    return false
  end
  true
end

#filenamesObject



66
67
68
# File 'lib/softcover/book.rb', line 66

def filenames
  files.map &:path
end

#filesObject

get array of paths and checksums



57
58
59
60
61
62
63
64
# File 'lib/softcover/book.rb', line 57

def files
  # question: should we use `git ls-files` instead?
  # TODO: only use pertinent files
  paths = %w{html/*_fragment.html images/**/* ebooks/*}
  Dir[*paths].reject { |path| File.directory?(path) }.map do |path|
    BookFile.new path
  end
end

#find_screencastsObject



166
167
168
# File 'lib/softcover/book.rb', line 166

def find_screencasts
  Dir["#{@screencasts_dir}/**/*.mov"].map{ |path| BookFile.new path }
end

#idObject

TODO: extract pattern to config helper:

has_config_for :id, :last_uploaded_at, path: ".polytex-book"


48
49
50
# File 'lib/softcover/book.rb', line 48

def id
  Softcover::BookConfig['id']
end

#id=(n) ⇒ Object



52
53
54
# File 'lib/softcover/book.rb', line 52

def id=(n)
  Softcover::BookConfig['id'] = n
end

#notify_file_upload(path) ⇒ Object



133
134
135
136
137
138
139
# File 'lib/softcover/book.rb', line 133

def notify_file_upload(path)
  book_file = BookFile.find path

  # this could spin off new thread:
  @client.notify_file_upload path: book_file.path,
    checksum: book_file.checksum
end

#open_in_browserObject

Opens the book in the browser (OS X only).



80
81
82
# File 'lib/softcover/book.rb', line 80

def open_in_browser
  `open #{url}`
end

#process_screencastsObject

Screencast handling



154
155
156
157
158
159
160
161
162
163
164
# File 'lib/softcover/book.rb', line 154

def process_screencasts
  files_to_upload = find_screencasts.select do |file|
    next false if @processed_screencasts.include?(file)

    file.ready?# && upload_screencast!(file)
  end

  upload_screencasts! files_to_upload

  @processed_screencasts += files_to_upload
end

#upload!(options = {}) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/softcover/book.rb', line 117

def upload!(options={})
  @uploader.after_each do |params|
    notify_file_upload params['path']
  end

  @uploader.upload!(options)

  res = @client.notify_upload_complete

  if res['errors'].nil?
    return url
  else
    raise UploadError, "Couldn't verify upload: #{res['errors']}"
  end
end

#upload_screencasts!(files) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/softcover/book.rb', line 170

def upload_screencasts!(files)
  return if files.empty?

  res = @client.get_screencast_upload_params files

  if res['upload_params']
    screencast_uploader = Softcover::Uploader.new res
    screencast_uploader.after_each do |params|
      notify_file_upload params['path']
    end
    screencast_uploader.upload!
  else
    raise 'server error'
  end
end

#urlObject



74
75
76
77
# File 'lib/softcover/book.rb', line 74

def url
  # TODO: append api_token to auto-login?
  "#{@client.host}/books/#{id}/redirect"
end