Module: Comiv

Defined in:
lib/comiv.rb,
lib/comiv/config.rb,
lib/comiv/version.rb

Defined Under Namespace

Modules: Docopt, FFmpeg, Tinify

Constant Summary collapse

CONFIG_DIRECTORY =
".comiv"
STORED_DIRECTORY =
"stored"
COMPRESS_DIRECTORY =
"compress"
DIRECTORIES =
[CONFIG_DIRECTORY, STORED_DIRECTORY, "#{STORED_DIRECTORY}/#{COMPRESS_DIRECTORY}"]
CONFIG_FILE =
".comiv/config.yml"
IMAGE_EXTENSION =
"jpg"
VIDEO_EXTENSION =
"mp4"
KEY =
"key"
COMPRESSION_COUNT =
"compression_count"
CONFIG =
<<"CONTENT"
key: nil
compression_count: 0
CONTENT
VERSION =
"1.0.1"

Class Method Summary collapse

Class Method Details

.add_key(value) ⇒ Object



68
69
70
71
72
73
# File 'lib/comiv.rb', line 68

def add_key(value)
  config_exist?
  write_config(KEY, value)
  reset_count
  puts "Add tinify api key."
end

.check_argumentObject



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

def check_argument
  args = Comiv::Docopt.docopt
  if args.is_a?(Hash)
    check_command(args)
  else
    puts args
    exit(0)
  end
end

.check_command(args) ⇒ Object



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

def check_command(args)
  command = args.find{ |key, value| value }.first
  command == "config" ? Comiv.send(command, args) : Comiv.send(command)
end

.compress_exist?Boolean

Returns:

  • (Boolean)


95
96
97
98
99
100
# File 'lib/comiv.rb', line 95

def compress_exist?
  unless File.exist?("#{STORED_DIRECTORY}/#{COMPRESS_DIRECTORY}")
    puts "Nothing #{STORED_DIRECTORY}/#{COMPRESS_DIRECTORY}. please `comiv init` command."
    exit(0)
  end
end

.config(options) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/comiv.rb', line 38

def config(options)
  options.each do |key, value|
    case key
    when "--add-key" then add_key(value) if value
    when "--delete-key" then delete_key if value
    when "--show-count" then show_count if value
    end
  end
end

.config_exist?Boolean

Returns:

  • (Boolean)


88
89
90
91
92
93
# File 'lib/comiv.rb', line 88

def config_exist?
  unless File.exist?(CONFIG_FILE)
    puts "Nothing #{CONFIG_FILE}. please `comiv init` command."
    exit(0)
  end
end

.create_configObject



59
60
61
62
63
64
65
66
# File 'lib/comiv.rb', line 59

def create_config
  if File.exist?(CONFIG_FILE)
    puts "Already #{CONFIG_FILE}"
  else
    File.write(CONFIG_FILE, CONFIG)
    puts "Create #{CONFIG_FILE}"
  end
end

.create_directoryObject



48
49
50
51
52
53
54
55
56
57
# File 'lib/comiv.rb', line 48

def create_directory
  DIRECTORIES.each do |dir|
    if File.exist?(dir)
      puts "Already #{dir}"
    else
      Dir.mkdir(dir)
      puts "Create #{dir}"
    end
  end
end

.delete_keyObject



75
76
77
78
79
80
# File 'lib/comiv.rb', line 75

def delete_key
  config_exist?
  write_config(KEY)
  reset_count
  puts "Delete tinify api key."
end

.find_image(config) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/comiv.rb', line 114

def find_image(config)
  puts "Checking image..."
  images = Dir.glob("#{STORED_DIRECTORY}/*.#{IMAGE_EXTENSION}")
  images.each.with_index(1) do |image, index|
    puts "Nothing image." if image.nil? 
    puts "#{index}: #{image}"
    index == images.size ?
      write_config(COMPRESSION_COUNT, Comiv::Tinify.compress_image(image, config["key"])) :
      Comiv::Tinify.compress_image(image, config["key"])
  end

  show_count
  puts "Completed."
end

.find_videoObject



129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/comiv.rb', line 129

def find_video
  puts "Checking video..."
  videos = Dir.glob("#{STORED_DIRECTORY}/*.#{VIDEO_EXTENSION}")
  videos.each.with_index(1) do |video, index|
    puts "Nothind video." if video.nil?
    puts "#{index}: #{video}"
    Comiv::FFmpeg.compress_video(video)
  end

  File.delete("ffmpeg2pass-0.log")
  File.delete("ffmpeg2pass-0.log.mbtree")
  puts "Complted."
end

.initObject



26
27
28
29
# File 'lib/comiv.rb', line 26

def init
  create_directory
  create_config
end

.load_configObject



110
111
112
# File 'lib/comiv.rb', line 110

def load_config
  YAML.load_file(CONFIG_FILE)
end

.reset_countObject



106
107
108
# File 'lib/comiv.rb', line 106

def reset_count
  File.write(CONFIG_FILE, File.read(CONFIG_FILE).gsub(/#{COMPRESSION_COUNT}:.*/, "#{COMPRESSION_COUNT}: 0"))
end

.runObject



31
32
33
34
35
36
# File 'lib/comiv.rb', line 31

def run
  config_exist?
  compress_exist?
  find_image(load_config)
  find_video
end

.show_countObject



82
83
84
85
86
# File 'lib/comiv.rb', line 82

def show_count
  config_exist?
  compression_count = load_config.fetch(COMPRESSION_COUNT)
  puts "Compression count is #{compression_count}"
end

.write_config(key, replacement = "nil") ⇒ Object



102
103
104
# File 'lib/comiv.rb', line 102

def write_config(key, replacement = "nil")
  File.write(CONFIG_FILE, File.read(CONFIG_FILE).gsub(/#{key}:.*/, "#{key}: #{replacement}"))
end