Class: IMGKit
- Inherits:
-
Object
- Object
- IMGKit
- 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.5.0"
Class Attribute Summary collapse
-
.configuration ⇒ Object
Configure IMGKit someplace sensible, like config/initializers/imgkit.rb.
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#source ⇒ Object
Returns the value of attribute source.
-
#stylesheets ⇒ Object
Returns the value of attribute stylesheets.
Class Method Summary collapse
Instance Method Summary collapse
-
#capture3(*cmd, &block) ⇒ Object
Lifted from ruby 1.9.2-p290 sources for ruby 1.8 compatibility and modified to work on 1.8.
- #command ⇒ Object
- #executable ⇒ Object
-
#initialize(url_file_or_html, options = {}) ⇒ IMGKit
constructor
A new instance of IMGKit.
- #method_missing(name, *args, &block) ⇒ Object
- #to_file(path) ⇒ Object
- #to_img(format = nil) ⇒ Object
Constructor Details
#initialize(url_file_or_html, options = {}) ⇒ IMGKit
Returns a new instance of IMGKit.
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/imgkit/imgkit.rb', line 36 def initialize(url_file_or_html, = {}) @source = Source.new(url_file_or_html) @stylesheets = [] @options = IMGKit.configuration..merge() @options.merge! (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
121 122 123 124 125 126 127 |
# File 'lib/imgkit/imgkit.rb', line 121 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
.configuration ⇒ Object
Configure IMGKit someplace sensible, like config/initializers/imgkit.rb
38 39 40 |
# File 'lib/imgkit/configuration.rb', line 38 def configuration @configuration end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
34 35 36 |
# File 'lib/imgkit/imgkit.rb', line 34 def @options end |
#source ⇒ Object
Returns the value of attribute source.
33 34 35 |
# File 'lib/imgkit/imgkit.rb', line 33 def source @source end |
#stylesheets ⇒ Object
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
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
78 79 80 |
# File 'lib/imgkit/imgkit.rb', line 78 def capture3(*opts) Open3.capture3 *opts end |
#command ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/imgkit/imgkit.rb', line 47 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 |
#executable ⇒ Object
61 62 63 64 65 66 67 68 69 |
# File 'lib/imgkit/imgkit.rb', line 61 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
115 116 117 118 119 |
# File 'lib/imgkit/imgkit.rb', line 115 def to_file(path) format = File.extname(path).gsub(/^\./,'').to_sym set_format(format) File.open(path,'w:ASCII-8BIT') {|file| file << self.to_img} end |
#to_img(format = nil) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/imgkit/imgkit.rb', line 103 def to_img(format = nil) append_stylesheets set_format(format) opts = @source.html? ? {:stdin_data => @source.to_s} : {} result, stderr = capture3(*(command + [opts])) result.force_encoding("ASCII-8BIT") if result.respond_to? :force_encoding raise CommandFailedError.new(command.join(' '), stderr) if result.size == 0 result end |