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

Constant Summary collapse

VERSION =
"1.0.2"

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:



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/imgkit/imgkit.rb', line 29

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

  @options = IMGKit.configuration.default_options.merge(options)
  @options.merge! find_options_in_meta(url_file_or_html) unless source.url?
  @options = normalize_options(@options)
  
  raise NoExecutableError.new unless File.exists?(IMGKit.configuration.wkhtmltoimage)
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


24
25
26
# File 'lib/imgkit/configuration.rb', line 24

def configuration
  @configuration
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#sourceObject

Returns the value of attribute source.



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

def source
  @source
end

#stylesheetsObject

Returns the value of attribute stylesheets.



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

def stylesheets
  @stylesheets
end

Class Method Details

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

Yields:



29
30
31
32
# File 'lib/imgkit/configuration.rb', line 29

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

Instance Method Details

#commandObject



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/imgkit/imgkit.rb', line 41

def command
  args = [executable]
  args += @options.to_a.flatten.compact
  
  if @source.html?
    args << '-' # Get HTML from stdin
  else
    args << @source.to_s
  end
  
  args << '-' # Read IMG from stdout
  args
end

#executableObject



55
56
57
58
59
60
61
62
63
# File 'lib/imgkit/imgkit.rb', line 55

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



93
94
95
# File 'lib/imgkit/imgkit.rb', line 93

def to_file(path)
  File.open(path,'w') {|file| file << self.to_img}
end

#to_imgObject

Raises:



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/imgkit/imgkit.rb', line 65

def to_img
  append_stylesheets

=begin  Need to use Open3 b.c wkhtmltoimage has to --quiet option
      and we need to slience $stderr
  image = Kernel.open('|-', "w+")
  exec(*command) if image.nil?
  image.puts(@source.to_s) if @source.html?
  image.close_write
  result = image.gets(nil)
  image.close_read
=end

  result = nil
  stderr_output = nil
  Open3.popen3(*command) do |stdin,stdout,stderr|
    stdin << (@source.to_s) if @source.html?
    stdin.close
    result = stdout.gets(nil)
    stderr_output = stderr.readlines.join
    stdout.close
    stderr.close
  end
  
  raise CommandFailedError.new(command.join(' '), stderr_output)  unless result
  return result
end