class Softcover::Book

Constants

DEFAULT_MEDIA_DIR

Attributes

errors[RW]
manifest[RW]
media_dir[RW]
processed_media[RW]
signatures[RW]
uploader[RW]

Public Class Methods

new(options={}) click to toggle source
# File lib/softcover/book.rb, line 12
def initialize(options={})
  require "softcover/client"
  @manifest = Softcover::BookManifest.new(options)
  @marketing = Softcover::MarketingManifest.new

  @client = Softcover::Client.new_with_book self

  @media_dir = DEFAULT_MEDIA_DIR

  @processed_media = []
end

Public Instance Methods

chapter_attributes() click to toggle source
# File lib/softcover/book.rb, line 72
def chapter_attributes
  chapters.map(&:to_hash)
end
create_or_update(options={}) click to toggle source
# File lib/softcover/book.rb, line 98
def create_or_update(options={})
  raise "HTML not built!" if Dir['html/*'].empty?

  params = {
    id: id,
    files: files,
    title: title,
    slug: slug,
    subtitle: subtitle,
    description: description,
    chapters: chapter_attributes,
    prices: prices,
    faq: faq,
    testimonials: testimonials,
    marketing_content: marketing_content,
    contact_email: contact_email,
    hide_softcover_footer: hide_softcover_footer,
    author_name: author,
    authors: authors,
    ga_account: ga_account,
    repo_url: repo_url,
    remove_unused_media_bundles: options[:remove_unused_media_bundles],
    custom_math: custom_math,
    convert_kit_follow_tag_id: convert_kit_follow_tag_id
  }

  res = @client.create_or_update_book params

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

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

  self.id = @attrs['id']

  # Not needed for now:
  # Softcover::BookConfig['last_uploaded_at'] = Time.now

  # res contains the S3 upload signatures needed
  @uploader = Softcover::Uploader.new res

  true

rescue Exception => e
  @errors = [e.message]
  raise e
  false
end
custom_math() click to toggle source
# File lib/softcover/book.rb, line 242
def custom_math
  Softcover::Mathjax.custom_macros
end
destroy() click to toggle source
# File lib/softcover/book.rb, line 177
def destroy
  res = @client.destroy
  if res['errors']
    @errors = res['errors']
    return false
  end
  true
end
filenames() click to toggle source
# File lib/softcover/book.rb, line 68
def filenames
  files.map &:path
end
files() click to toggle source

get array of paths and checksums

# File lib/softcover/book.rb, line 60
def files
  paths = %W{html/#{slug}.html html/*_fragment.html images/**/* config/*
             html/stylesheets/custom.css}
  Dir[*paths].map do |path|
    BookFile.new(path) unless File.directory?(path)
  end.compact
end
get_book_files(dir) click to toggle source
# File lib/softcover/book.rb, line 216
def get_book_files(dir)
  Dir["#{dir}/**/*"].map do |path|
    BookFile.new(path) unless File.directory?(path)
  end.compact
end
id() click to toggle source

TODO: extract pattern to config helper:

has_config_for :id, :last_uploaded_at, path: ".polytex-book"
# File lib/softcover/book.rb, line 51
def id
  Softcover::BookConfig['id']
end
id=(n) click to toggle source
# File lib/softcover/book.rb, line 55
def id=(n)
  Softcover::BookConfig['id'] = n
end
notify_file_upload(path) click to toggle source
# File lib/softcover/book.rb, line 169
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
notify_upload_complete() click to toggle source
# File lib/softcover/book.rb, line 159
def notify_upload_complete
  res = @client.notify_upload_complete

  if res['errors'].nil?
    return url
  else
    raise UploadError, "Couldn't verify upload: #{res['errors']}"
  end
end
open() click to toggle source

Returns the system-dependent `open` command.

# File lib/softcover/book.rb, line 88
def open
  if os_x?
    'open'
  elsif linux?
    'xdg-open'
  else
    raise "Platform #{RUBY_PLATFORM} not supported"
  end
end
open_in_browser() click to toggle source

Opens the book in the browser (OS X & Linux).

# File lib/softcover/book.rb, line 82
def open_in_browser
  puts url
  `#{open} #{url}`
end
process_media(options={}) click to toggle source
  1. iterate over /media/*

> use directory name as path parameter

> get checksums for all included files

> send each to /media API endpoint and then upload

# File lib/softcover/book.rb, line 195
def process_media(options={})
  Dir["media/*"].each do |media_dir|
    next unless File.directory?(media_dir) && !(media_dir =~ /^\./)
    process_media_directory media_dir, options
  end
end
process_media_directory(dir, options={}) click to toggle source
# File lib/softcover/book.rb, line 202
def process_media_directory(dir, options={})
  return false if @processed_media.include?(dir)

  puts "Processing #{dir} directory..."

  files_to_upload = get_book_files(dir).select do |file|
    file.ready?
  end

  upload_media! dir, files_to_upload, options

  @processed_media.push dir
end
upload!(options={}) click to toggle source
# File lib/softcover/book.rb, line 150
def upload!(options={})
  @uploader.after_each do |params|
    notify_file_upload params['path']
  end

  @uploader.upload!(options)
  notify_upload_complete
end
upload_media!(path, files, options={}) click to toggle source
# File lib/softcover/book.rb, line 222
def upload_media!(path, files, options={})
  return if files.empty?

  manifest_path = File.join(path, "manifest.yml")
  manifest = File.exists?(manifest_path) ? File.read(manifest_path) : nil

  res = @client.get_media_upload_params path, files, manifest, options

  if res['upload_params']
    media_uploader = Softcover::Uploader.new res
    media_uploader.after_each do |params|
      notify_file_upload params['path']
    end
    media_uploader.upload!
    notify_upload_complete
  else
    raise 'server error'
  end
end
url() click to toggle source
# File lib/softcover/book.rb, line 76
def url
  # TODO: append api_token to auto-login?
  "#{@client.host}/books/#{id}/redirect"
end

Private Instance Methods

method_missing(name, *args, &block) click to toggle source
# File lib/softcover/book.rb, line 247
def method_missing(name, *args, &block)
  @manifest.send(name) || @marketing.send(name) || nil
end