Class: IMGKit

Inherits:
Object
  • Object
show all
Defined in:
lib/imgkit/imgkit.rb,
lib/imgkit/source.rb,
lib/imgkit/version.rb,
lib/imgkit/configuration.rb

Defined Under Namespace

Classes: CommandFailedError, Configuration, ImproperSourceError, NoExecutableError, Source, UnknownFormatError

Constant Summary collapse

KNOWN_FORMATS =
[:jpg, :jpeg, :png]
VERSION =
"1.6.1"

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url_file_or_html, options = {}) ⇒ IMGKit

Returns a new instance of IMGKit.

Raises:



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

def initialize(url_file_or_html, options = {})
  @source = Source.new(url_file_or_html)

  @stylesheets = []
  @javascripts = []

  @options = IMGKit.configuration.default_options.merge(options)
  @options.merge! find_options_in_meta(url_file_or_html) unless source.url?

  raise NoExecutableError.new unless File.exists?(IMGKit.configuration.wkhtmltoimage)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



127
128
129
130
131
132
133
# File 'lib/imgkit/imgkit.rb', line 127

def method_missing(name, *args, &block)
  if(m = name.to_s.match(/^to_(\w+)/))
    self.send(:to_img, m[1].to_sym)
  else
    super
  end
end

Class Attribute Details

.configurationObject

Configure IMGKit someplace sensible, like config/initializers/imgkit.rb

Examples:

IMGKit.configure do |config|
  config.wkhtmltoimage = '/usr/bin/wkhtmltoimage'
end


38
39
40
# File 'lib/imgkit/configuration.rb', line 38

def configuration
  @configuration
end

Instance Attribute Details

#javascriptsObject

Returns the value of attribute javascripts.



33
34
35
# File 'lib/imgkit/imgkit.rb', line 33

def javascripts
  @javascripts
end

#optionsObject (readonly)

Returns the value of attribute options.



34
35
36
# File 'lib/imgkit/imgkit.rb', line 34

def options
  @options
end

#sourceObject

Returns the value of attribute source.



33
34
35
# File 'lib/imgkit/imgkit.rb', line 33

def source
  @source
end

#stylesheetsObject

Returns the value of attribute stylesheets.



33
34
35
# File 'lib/imgkit/imgkit.rb', line 33

def stylesheets
  @stylesheets
end

Class Method Details

.configure {|configuration| ... } ⇒ Object

Yields:



43
44
45
46
# File 'lib/imgkit/configuration.rb', line 43

def self.configure
  self.configuration
  yield(configuration)
end

Instance Method Details

#capture3(*cmd, &block) ⇒ Object

Lifted from ruby 1.9.2-p290 sources for ruby 1.8 compatibility and modified to work on 1.8



84
85
86
# File 'lib/imgkit/imgkit.rb', line 84

def capture3(*opts)
  Open3.capture3 *opts
end

#command(output_file = nil) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/imgkit/imgkit.rb', line 48

def command(output_file = nil)
  args = [executable]
  args += normalize_options(@options).to_a.flatten.compact

  if @source.html?
    args << '-' # Get HTML from stdin
  else
    args << @source.to_s
  end

  if output_file
    args << output_file.to_s
  else
    args << '-' # Read IMG from stdout
  end

  args
end

#executableObject



67
68
69
70
71
72
73
74
75
# File 'lib/imgkit/imgkit.rb', line 67

def executable
  default = IMGKit.configuration.wkhtmltoimage
  return default if default !~ /^\// # its not a path, so nothing we can do
  if File.exist?(default)
    default
  else
    default.split('/').last
  end
end

#to_file(path) ⇒ Object



121
122
123
124
125
# File 'lib/imgkit/imgkit.rb', line 121

def to_file(path)
  format = File.extname(path).gsub(/^\./,'').to_sym
  self.to_img(format, path)
  File.new(path)
end

#to_img(format = nil, path = nil) ⇒ Object

Raises:



109
110
111
112
113
114
115
116
117
118
119
# File 'lib/imgkit/imgkit.rb', line 109

def to_img(format = nil, path = nil)
  append_stylesheets
  append_javascripts
  set_format(format)

  opts = @source.html? ? {:stdin_data => @source.to_s} : {}
  result, stderr = capture3(*(command(path) + [opts]))
  result.force_encoding("ASCII-8BIT") if result.respond_to? :force_encoding
  raise CommandFailedError.new(command.join(' '), stderr) if path.nil? and result.size == 0
  result
end