Class: Faceapp::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/faceapp/cli.rb

Constant Summary collapse

OPTION_REGEXP =
/\A--(?<name>[^=]+)(?:=(?<value>.+))?\Z/
PARAMS =
[:filter, :input, :output].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ CLI

Returns a new instance of CLI.



12
13
14
# File 'lib/faceapp/cli.rb', line 12

def initialize(args)
  @params, @options = parse_arguments(args)
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/faceapp/cli.rb', line 6

def options
  @options
end

#paramsObject (readonly)

Returns the value of attribute params.



6
7
8
# File 'lib/faceapp/cli.rb', line 6

def params
  @params
end

#silentObject (readonly)

Returns the value of attribute silent.



6
7
8
# File 'lib/faceapp/cli.rb', line 6

def silent
  @silent
end

Instance Method Details



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/faceapp/cli.rb', line 46

def print_usage
  info <<TEXT
faceapp [options] <filter> <input> [ouput]

  <filter> - Faceapp filter name
Possible values: #{Faceapp::KNOWN_FILTERS.join(', ')}

  <input> - Input file name
Use '-' for STDIN

  [output] - Optinal, output file name
Do not specify or use '-' for STDOUT

  Options:
--help - Display this message
--cropped[=true|false] - Crop output image to face region. Enabled by default.
--api_host=<api_host> - Faceapp API host
--device_id=<device_id> - DeviceId for Faceapp
--user_agent=<user_agent> - User-Agent header for Faceapp API requests
--silent - Keep quiet, it will override `debug`
--debug - Print HTTP requests/responses to STDERR.

TEXT
  exit(-1)
end

#run!Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/faceapp/cli.rb', line 16

def run!
  print_usage if params.empty? || options[:help]

  options[:logger] = Logger.new(STDERR) if debug?

  filter = params[:filter]
  input = parse_input(params[:input])
  output = parse_output(params[:output])

  client = Faceapp::Client.new(options)

  code = client.upload_photo(input)

  info "Successfuly uploaded input photo. Result code = #{code}"
  info "Applying filter '#{filter}'"

  client.apply_filter(code, filter, output)

  info 'Done.'
rescue => e
  info "Error: #{e.message}"

  debug { e.backtrace.join("\n") }

  exit(-1)
ensure
  input.close if input.is_a?(File)
  output.close if output.is_a?(File)
end