Class: WickedPdf::Binary

Inherits:
Object
  • Object
show all
Defined in:
lib/wicked_pdf/binary.rb

Constant Summary collapse

EXE_NAME =
'wkhtmltopdf'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(binary_path, default_version = WickedPdf::DEFAULT_BINARY_VERSION) ⇒ Binary

Returns a new instance of Binary.


7
8
9
10
11
12
13
14
# File 'lib/wicked_pdf/binary.rb', line 7

def initialize(binary_path, default_version = WickedPdf::DEFAULT_BINARY_VERSION)
  @path = binary_path || find_binary_path
  @default_version = default_version

  raise "Location of #{EXE_NAME} unknown" if @path.empty?
  raise "Bad #{EXE_NAME}'s path: #{@path}" unless File.exist?(@path)
  raise "#{EXE_NAME} is not executable" unless File.executable?(@path)
end

Instance Attribute Details

#default_versionObject (readonly)

Returns the value of attribute default_version.


5
6
7
# File 'lib/wicked_pdf/binary.rb', line 5

def default_version
  @default_version
end

#pathObject (readonly)

Returns the value of attribute path.


5
6
7
# File 'lib/wicked_pdf/binary.rb', line 5

def path
  @path
end

Instance Method Details

#parse_version_string(version_info) ⇒ Object


20
21
22
23
24
25
26
27
# File 'lib/wicked_pdf/binary.rb', line 20

def parse_version_string(version_info)
  match_data = /wkhtmltopdf\s*(\d*\.\d*\.\d*\w*)/.match(version_info)
  if match_data && (match_data.length == 2)
    Gem::Version.new(match_data[1])
  else
    default_version
  end
end

#versionObject


16
17
18
# File 'lib/wicked_pdf/binary.rb', line 16

def version
  @version ||= retrieve_binary_version
end

#xvfb_run_pathObject

Raises:

  • (StandardError)

29
30
31
32
33
34
# File 'lib/wicked_pdf/binary.rb', line 29

def xvfb_run_path
  path = possible_binary_locations.map { |l| File.expand_path("#{l}/xvfb-run") }.find { |location| File.exist?(location) }
  raise StandardError, 'Could not find binary xvfb-run on the system.' unless path

  path
end