Module: Softcover::Commands::Publisher

Extended by:
Publisher
Includes:
Output, Utils
Included in:
Publisher
Defined in:
lib/softcover/commands/publisher.rb

Constant Summary

Constants included from Utils

Utils::UNITS

Instance Method Summary collapse

Methods included from Output

should_output?, silence!, silent?, #system, unsilence!

Methods included from Utils

#add_highlight_class!, #article?, #as_size, #book_file_lines, #chapter_label, #commands, #current_book, #dependency_filename, #digest, #executable, #execute, #filename_or_default, #first_path, #get_filename, #html_extension, #in_book_directory?, #language_labels, #linux?, #logged_in?, #master_content, #master_filename, #master_latex_header, #mkdir, #non_comment_lines, #os_x?, #path, #polytexnic_html, #raw_lines, #reset_current_book!, #rm, #rm_r, #silence, #silence_stream, #source, #template_dir, #tmpify, #unpublish_slug, #write_master_latex_file, #write_pygments_file

Instance Method Details

#exit_with_messageObject



89
90
91
92
93
# File 'lib/softcover/commands/publisher.rb', line 89

def exit_with_message
  number = current_book.processed_media.size
  dir = number == 1 ? "directory" : "directories"
  puts "Processed #{number} #{dir}"
end

#process_media(options = {}) ⇒ Object



85
86
87
# File 'lib/softcover/commands/publisher.rb', line 85

def process_media(options={})
  current_book.process_media(options)
end

#publish!(options = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/softcover/commands/publisher.rb', line 7

def publish!(options={})
  return false unless current_book

  if current_book.create_or_update(options)
    require 'ruby-progressbar'
    require 'curb'
    unless options[:quiet] || options[:silent]
      puts "Uploading #{current_book.uploader.file_count} files " \
           "(#{as_size current_book.uploader.total_size}):"
    end

    url = current_book.upload!(options)
    current_book.process_media_directory "ebooks"

    unless options[:quiet] || options[:silent]
      puts "Published! #{url}"
    end
  else
    puts "Errors: #{current_book.errors}"
    return false
  end

  true
rescue Softcover::BookManifest::NotFound => e
  puts e.message
  false
rescue Softcover::Book::UploadError => e
  puts e.message
  false
end

#publish_media!(options = {}) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/softcover/commands/publisher.rb', line 38

def publish_media!(options={})
  return false unless current_book

  require 'ruby-progressbar'
  require 'curb'

  current_book.media_dir = options[:dir] || Softcover::Book::DEFAULT_MEDIA_DIR

  @watch = options[:watch]

  if options[:daemon]
    pid = fork do
      run_publish_media(options)
    end

    puts "Daemonized, pid: #{pid}"
  else
    run_publish_media(options)
  end

  current_book
end

#run_publish_media(options = {}) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/softcover/commands/publisher.rb', line 61

def run_publish_media(options={})
  if @watch
    puts "Watching..."

    Signal.trap("TERM") do
      puts "SIGTERM received."
      exit_with_message
    end

    begin
      loop do
        process_media(options)
        sleep 1
      end
    rescue Interrupt
      puts " Interrupt Received."
      exit_with_message
    end
  else
    process_media(options)
    exit_with_message
  end
end

#unpublish!(slug = nil) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/softcover/commands/publisher.rb', line 95

def unpublish!(slug=nil)
  require "rest_client"
  require "softcover/client"

  if slug.present?
    begin
      res = Softcover::Client.new.destroy_book_by_slug(slug)
      if res["errors"]
        puts "Errors: #{res.errors}"
        return false
      else
        puts "Done!"
        return true
      end
    rescue RestClient::ResourceNotFound
      puts "Book with slug='#{slug}' not found under this account."
      return false
    end
  end

  return false unless current_book
  if current_book.destroy
    Softcover::BookConfig.remove
    puts "Done!"
    return true
  else
    puts "Errors: #{current_book.errors}"
    return false
  end
rescue RestClient::ResourceNotFound
  puts "Book with ID=#{current_book.id} not found under this account."
  false
rescue Softcover::BookManifest::NotFound => e
  puts e.message
  false
end