Class: Imaginator

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

Defined Under Namespace

Classes: Graphviz, LaTeX, Server

Constant Summary collapse

VERSION =
'0.1.5'

Instance Method Summary collapse

Constructor Details

#initialize(uri, dir) {|_self| ... } ⇒ Imaginator

Returns a new instance of Imaginator.

Yields:

  • (_self)

Yield Parameters:

  • _self (Imaginator)

    the object that the method was called on



180
181
182
183
184
185
186
# File 'lib/imaginator.rb', line 180

def initialize(uri, dir)
  @uri = uri
  @dir = dir
  @renderer = {}
  FileUtils.mkdir_p(@dir, :mode => 0755)
  yield(self) if block_given?
end

Instance Method Details

#add_renderer(type, renderer) ⇒ Object



188
189
190
# File 'lib/imaginator.rb', line 188

def add_renderer(type, renderer)
  @renderer[type.to_sym] = renderer
end

#enqueue(type, code) ⇒ Object

Raises:

  • (ArgumentError)


192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/imaginator.rb', line 192

def enqueue(type, code)
  type = type.to_sym
  renderer = @renderer[type]
  raise(ArgumentError, "Renderer not found") if !renderer
  format = renderer.format
  code = renderer.process(code)
  name = Digest::MD5.hexdigest(code) + '.' + format
  file = File.join(@dir, name)
  if !File.exists?(file)
    server.enqueue(file, type, code)
  end
  name
end

#result(name) ⇒ Object

Raises:

  • (RuntimeError)


206
207
208
209
210
211
212
213
214
215
216
# File 'lib/imaginator.rb', line 206

def result(name)
  file = File.join(@dir, name)
  if !File.exists?(file)
    20.times do
      break if !server.enqueued?(file)
      sleep 0.5
    end
  end
  raise(RuntimeError, 'Image could not be generated') if !File.exists?(file)
  file
end

#serverObject



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/imaginator.rb', line 218

def server
  begin
    @server ||= DRb::DRbObject.new(nil, @uri)
    @server.respond_to? :enqueue
    @server
  rescue
    if @uri =~ %r{^drbunix://(.+)$}
      File.unlink($1) rescue nil
    end
    server = Server.new(@renderer)
    drb_server = DRb.start_service(@uri, server)
    Thread.new do
	server.run
	drb_server.stop_service
      DRb.primary_server = nil if DRb.primary_server == drb_server
	@server = nil
    end
    @server = server
  end
end