Class: TikaWrapper::Instance

Inherits:
Object
  • Object
show all
Defined in:
lib/tika_wrapper/instance.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Instance

Returns a new instance of Instance.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :url (String)
  • :version (String)
  • :port (String)
  • :version_file (String)
  • :instance_dir (String)
  • :download_path (String)
  • :md5sum (String)
  • :tika_xml (String)
  • :verbose (Boolean)
  • :managed (Boolean)
  • :ignore_md5sum (Boolean)
  • :tika_options (Hash)
  • :env (Hash)


29
30
31
# File 'lib/tika_wrapper/instance.rb', line 29

def initialize(options = {})
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



12
13
14
# File 'lib/tika_wrapper/instance.rb', line 12

def options
  @options
end

#pidObject (readonly)

Returns the value of attribute pid.



12
13
14
# File 'lib/tika_wrapper/instance.rb', line 12

def pid
  @pid
end

Instance Method Details

#clean!Object

Clean up any files tika_wrapper may have downloaded



96
97
98
99
100
# File 'lib/tika_wrapper/instance.rb', line 96

def clean!
  stop
  FileUtils.remove_entry(download_path) if File.exists? download_path
  FileUtils.remove_entry(md5sum_path) if File.exists? md5sum_path
end

#portObject

Get the port this tika instance is running at



90
91
92
# File 'lib/tika_wrapper/instance.rb', line 90

def port
  options.fetch(:port, "9998").to_s
end

#startObject

Start tika and wait for it to become available



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

def start
  download
  if managed?
    exec(p: port)

    # Wait for tika to start
    unless status
      sleep 1
    end
  end
end

#started?Boolean

Is tika running?

Returns:

  • (Boolean)


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

def started?
  !!status
end

#statusObject

Check the status of a managed tika service



71
72
73
74
75
76
77
78
79
80
# File 'lib/tika_wrapper/instance.rb', line 71

def status
  return true unless managed?

  begin
    open(url + "version")
    true
  rescue
    false
  end
end

#stopObject

Stop tika and wait for it to finish exiting



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/tika_wrapper/instance.rb', line 56

def stop
  if managed? && started?
    Process.kill("KILL", pid.to_i)

    # Wait for tika to stop
    while status
      sleep 1
    end
  end

  @pid = nil
end

#urlObject

Get a (likely) URL to the tika instance



104
105
106
# File 'lib/tika_wrapper/instance.rb', line 104

def url
  "http://127.0.0.1:#{port}/"
end

#wrap(&_block) ⇒ Object



33
34
35
36
37
38
# File 'lib/tika_wrapper/instance.rb', line 33

def wrap(&_block)
  start
  yield self
ensure
  stop
end