Class: Convertible::Cli

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

Instance Method Summary collapse

Constructor Details

#initialize(key, input, output = nil, options = {}) ⇒ Cli

Returns a new instance of Cli.



6
7
8
9
10
11
# File 'lib/convertible/cli.rb', line 6

def initialize(key, input, output = nil, options = {})
  @key = key
  @input = input
  @output = output
  @options = options
end

Instance Method Details

#convertObject

Raises:

  • (ArgumentError)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/convertible/cli.rb', line 13

def convert
  data = case @input
  when 'STDIN'
    $stdin.read
  when /^https?:\/\//
    URI.parse @input
  else
    File.read(@input) if File.readable?(@input)
  end
  raise ArgumentError, "cannot open #{@input} for reading" unless data

  check_mimetypes !(URI === data)
  $stderr.puts "converting #{@input} to #{@output} with options #{@options.inspect}" if @options[:debug]
  
  response = client.convert data, @input_type, @output_type, @options
  if @output == 'STDOUT'
    $stdout << response
  else
    (File.open(@output, 'wb') << response).close
  end
end

#showObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/convertible/cli.rb', line 35

def show
  check_mimetypes false, false
  if in_t = @input_type
    if out_t = @output_type
      if client.supported?(in_t, out_t)
        puts 'supported'
      else
        puts 'not supported' # no worky
      end
    else
      conversions = client.supported_conversions @input_type
      $stderr.puts "Supported output formats for #{@input_type}:"
      puts conversions.join(', ')
    end
  else
    conversions = client.supported_conversions
    $stderr.puts "Supported conversions:"
    conversions.keys.sort.each do |input|
      puts "#{input} => " << conversions[input].join(', ')
    end
  end
end