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
Constant Summary collapse
- VERSION =
"1.0.2"
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
- #command ⇒ Object
- #executable ⇒ Object
-
#initialize(url_file_or_html, options = {}) ⇒ IMGKit
constructor
A new instance of IMGKit.
- #to_file(path) ⇒ Object
- #to_img ⇒ Object
Constructor Details
#initialize(url_file_or_html, options = {}) ⇒ IMGKit
Returns a new instance of IMGKit.
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/imgkit/imgkit.rb', line 29 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? @options = (@options) raise NoExecutableError.new unless File.exists?(IMGKit.configuration.wkhtmltoimage) end |
Class Attribute Details
.configuration ⇒ Object
Configure IMGKit someplace sensible, like config/initializers/imgkit.rb
24 25 26 |
# File 'lib/imgkit/configuration.rb', line 24 def configuration @configuration end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
27 28 29 |
# File 'lib/imgkit/imgkit.rb', line 27 def @options end |
#source ⇒ Object
Returns the value of attribute source.
26 27 28 |
# File 'lib/imgkit/imgkit.rb', line 26 def source @source end |
#stylesheets ⇒ Object
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
29 30 31 32 |
# File 'lib/imgkit/configuration.rb', line 29 def self.configure self.configuration yield(configuration) end |
Instance Method Details
#command ⇒ Object
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 |
#executable ⇒ Object
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_img ⇒ Object
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 |