convertible 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -21,6 +21,9 @@ options to override. When using STDIN and/or STDOUT you have to use one or both
21
21
  to specify the format of the data. To specify a file format, use the well known file name postfix
22
22
  (i.e. pdf for PDF documents) or the official mime type string like application/pdf.
23
23
 
24
+ When passing an URL as input file the convertible.io service will attempt to load the input data
25
+ directly from there.
26
+
24
27
  convert pdf to plain text:
25
28
  convertible document.pdf document.txt
26
29
 
@@ -33,6 +36,9 @@ convertible -c -o txt document.pdf > document.txt
33
36
  convert pdf to plain text using both STDIN/STDOUT:
34
37
  convertible -i pdf -o txt < document.pdf > document.txt
35
38
 
39
+ scale down flickr image to 700x700px with cropping and write it to image.jpg:
40
+ convertible --width=700 --height=700 --crop http://farm7.static.flickr.com/6123/5994459827_ba62c5a5bb_b_d.jpg image.jpg
41
+
36
42
 
37
43
  # Querying supported conversions
38
44
 
@@ -2,10 +2,12 @@
2
2
 
3
3
  require 'optparse'
4
4
  require 'convertible/cli'
5
+ require 'yaml'
5
6
  require 'pp'
6
7
 
7
8
  @options = { :debug => false }
8
9
  @max_file_args = 2
10
+ @config = File.expand_path '~/.convertible'
9
11
 
10
12
  OptionParser.new do |optparser|
11
13
  optparser.banner = <<-HELP
@@ -77,9 +79,24 @@ HELP
77
79
  @max_file_args -= 1
78
80
  end
79
81
 
82
+ optparser.on('-a', '--api-key=KEY', "use the given API key (will be stored in #{@config} for future use)") do |arg|
83
+ @api_key = arg
84
+ end
85
+
86
+ optparser.on(nil, '--width=WIDTH', "Image scaling: output width") do |arg|
87
+ @options[:width] = arg.to_i
88
+ end
89
+ optparser.on(nil, '--height=HEIGHT', "Image scaling: output height") do |arg|
90
+ @options[:height] = arg.to_i
91
+ end
92
+ optparser.on(nil, '--crop', "Image scaling: crop image to fit desired output dimensions") do
93
+ @options[:crop] = true
94
+ end
95
+
96
+
80
97
  files = optparser.permute!(ARGV)
81
98
  (puts optparser; exit(1)) unless files.size <= @max_file_args
82
-
99
+
83
100
  @out ||= files.pop || 'STDOUT'
84
101
  @in = files.pop || 'STDIN'
85
102
  STDERR.puts "#{@in} => #{@out}\n#{@options.inspect}" if @options[:debug]
@@ -87,8 +104,20 @@ HELP
87
104
  @command ||= :convert
88
105
  end
89
106
 
107
+ if @api_key
108
+ (File.open(@config, 'w') << {:key => @api_key}.to_yaml).close
109
+ elsif File.readable?(@config)
110
+ @api_key = YAML.load(File.read(@config))[:key] rescue nil
111
+ end
112
+
113
+ if @api_key.nil?
114
+ puts "Please use the -a option to set your API key."
115
+ puts optparser
116
+ exit(1)
117
+ end
118
+
90
119
  begin
91
- Convertible::Cli.new(@in, @out, @options).send @command
120
+ Convertible::Cli.new(@api_key, @in, @out, @options).send @command
92
121
  rescue ArgumentError
93
122
  STDERR.puts $!
94
123
  exit 1
@@ -3,19 +3,27 @@ require 'convertible/mime_types'
3
3
 
4
4
  module Convertible
5
5
  class Cli
6
- def initialize(input, output = nil, options = {})
6
+ def initialize(key, input, output = nil, options = {})
7
+ @key = key
7
8
  @input = input
8
9
  @output = output
9
10
  @options = options
10
11
  end
11
12
 
12
13
  def convert
13
- unless @input == 'STDIN' || File.readable?(@input)
14
- raise ArgumentError, "cannot open #{@input} for reading"
14
+ data = case @input
15
+ when 'STDIN'
16
+ $stdin.read
17
+ when /^https?:\/\//
18
+ URI.parse @input
19
+ else
20
+ File.read(@input) if File.readable?(@input)
15
21
  end
16
- check_mimetypes
22
+ raise ArgumentError, "cannot open #{@input} for reading" unless data
23
+
24
+ check_mimetypes !(URI === data)
17
25
  $stderr.puts "converting #{@input} to #{@output} with options #{@options.inspect}" if @options[:debug]
18
- data = @input == 'STDIN' ? $stdin.read : File.read(@input)
26
+
19
27
  response = client.convert data, @input_type, @output_type, @options
20
28
  if @output == 'STDOUT'
21
29
  $stdout << response
@@ -1,5 +1,6 @@
1
1
  require 'httparty'
2
2
  require 'json'
3
+ require 'uri'
3
4
 
4
5
  module Convertible
5
6
  API_ENDPOINT = 'http://api.convertible.io/'
@@ -7,10 +8,19 @@ module Convertible
7
8
  class Client
8
9
  include HTTParty
9
10
  base_uri API_ENDPOINT
11
+ CONVERT = '/convert'
10
12
 
11
13
  # content_type and output_content_type must be valid mime types like 'application/pdf' or 'text/plain'
14
+ # if an URI instance is passed as data, the content will be fetched server side from said uri. content_type is
15
+ # ignored an should be nil in this case (convertible.io will use the content type from the remote resource).
12
16
  def convert(data, content_type, output_content_type, options = {})
13
- response = self.class.post '/convert', :body => data, :headers => { 'Content-Type' => content_type, 'Accept' => output_content_type, 'X-Convert-Options' => option_string(options) }
17
+ response = if URI === data
18
+ self.class.get CONVERT, :headers => { 'Accept' => output_content_type,
19
+ 'X-Convert-Options' => option_string(options),
20
+ 'X-Source' => data.to_s }
21
+ else
22
+ self.class.post CONVERT, :body => data, :headers => { 'Content-Type' => content_type, 'Accept' => output_content_type, 'X-Convert-Options' => option_string(options) }
23
+ end
14
24
  if response.code == 200
15
25
  return response
16
26
  else
@@ -1,3 +1,3 @@
1
1
  module Convertible
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: convertible
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.1
5
+ version: 0.0.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - Jens Kraemer
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-06-13 00:00:00 +02:00
13
+ date: 2011-10-03 00:00:00 +02:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency