How to convert ppt to images in Ruby? - ruby

I'm using Ruby for displaying the contents of powerpoint files in a webpage. I've found solutions using the win32ole but I'm in the linux environment and it doesn't work. I think the application could trigger a openoffice command for conversion.

I recommend using Docsplit.
Install the gem, and then you can do something like:
Docsplit.extract_images(filename, :size => '920x', :format => [:png])

You can use Rjb and JODConverter to export your powerpoint to flash (or any other format).
Here is a pice of code to do it :
require 'rubygems'
require 'rjb'
classpath = nil
Rjb::load( classpath, ['-Djava.awt.headless=true'] )
jFile = Rjb::import( 'java.io.File' )
jSocketOpenOfficeConnection = Rjb::import( 'com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection' )
jOpenOfficeDocumentConverter = Rjb::import( 'com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter' )
input = jFile.new( "your-doc.ppt" )
output = jFile.new( "your-doc.swf" )
# connect to an OpenOffice.org instance running on port 8100
connection = jSocketOpenOfficeConnection.new( 8100 )
connection.connect()
# convert
converter = jOpenOfficeDocumentConverter.new( connection )
converter.convert( input, output )
# close the connection
connection.disconnect()
You need to start an OOo.org server :
soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard
And to add jodconverter-cli-X.X.X.jar to your CLASSPATH

Related

Can't 'file.open.read' a url within a ruby if-block

I want to create a ruby script which will take barcodes from a text file, search a webservice for that barcode and download the result.
First I tried to test the webservice download. In a file when I hardcode the query things work fine:
result_download = open('http://webservice.org/api/?query=barcode:78686112327', 'User-Agent' => 'UserAgent email#gmail.com').read
It all works fine.
When I try to take the barcode from a textfile and run the query I run into problems.
IO.foreach(filename) {|barcode| barcode
website = "'http://webservice.org/api/?query=barcode:"+barcode.to_str.chomp + "', 'User-Agent' => 'UserAgent email#gmail.com'"
website = website.to_s
mb_metadata = open(website).read
}
The result of this is:
/home/user/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/open-uri.rb:37:in `initialize': No such file or directory # rb_sysopen - http://webservice.org/api/?query=barcode:78686112327', 'User-Agent' => 'UserAgent email#gmail.com' (Errno::ENOENT)
I can't figure out if this problem occurs because the string I generate somehow isn't a valid url and ruby is trying to open a non-existent file, or is the issue that I am doing all this in a for loop and the file/url doesn't exist there. I have tried using open(website).write instead of open(website).read but that produces the same error.
Any help would be much appreciated.
The error message you get explicitly states, that there is no such file:
http://webservice.org/api/?query=barcode:78686112327', 'User-Agent' => 'UserAgent email#gmail.com'.
You try to pass all the parameters to open method using 1 big string (website), which is wrong. You should do it like that.
IO.foreach(filename) do |barcode|
website = "http://webservice.org/api/?query=barcode:#{barcode.to_str.chomp}"
mb_metadata = open(website, 'User-Agent' => 'UserAgent email#gmail.com').read
end

How to create publicly readable object store containers with ruby openstack gem?

I have tried to create publicly readable openstack object store containers like this:
os = OpenStack::Connection.create(...)
container = os.create_container(container_name)
container.set_metadata({'X-Container-Read' => '.r:*'})
Using my code above, the newly created containers are private.
What is the correct way to create containers with public read permissions with the ruby openstack gem?
You can try the following way.
You can redfine the create_container method
then
class MyStack < OpenStack::Swift::Connection
def create_container(containername)
super
#connection.req("PUT", path, {:headers=>{"Content-Length"=>"0", "X-Container-Read" => ".r:*", "X-Container-Write" => ".r:*}})
OpenStack::Swift::Container.new(self, containername)
end
end
These "X-Container-Read" => ".r:*", "X-Container-Write" => ".r:*" header value you need to set.
or
container.set_metadata({"X-Container-Read" => ".r:*", "X-Container-Write" => ".r:*"})
Here's what I ended up doing:
module PubliclyRedableContainerMonkeyPatch
def create_publicy_readable_container(containername)
raise OpenStack::Exception::InvalidArgument.new("Container name cannot contain '/'") if containername.match("/")
raise OpenStack::Exception::InvalidArgument.new("Container name is limited to 256 characters") if containername.length > 256
path = "/#{URI.encode(containername.to_s)}"
#connection.req("PUT", path, {:headers=>{"Content-Length"=>"0", "X-Container-Read" => ".r:*"}})
OpenStack::Swift::Container.new(self, containername)
end
end
OpenStack::Swift::Connection.include PubliclyRedableContainerMonkeyPatch
os = OpenStack::Connection.create(...)
container = os.create_publicy_readable_container(container_name)
Worksforme. :)

How to generate a image file from base 64 string in Lua?

I can get the base 64 string from WCF ,and need to convert it into image file by Lua?
Any one have idea about this?
thanks
You can do something like this with LuaSocket:
local ltn12 = require "ltn12"
local mime = require "mime"
mystring = "somedata"
myoutfile = "out.gif"
ltn12.pump.all(
ltn12.source.string(mystring),
ltn12.sink.chain(
mime.decode("base64"),
ltn12.sink.file(io.open(outfile,"w"))
)
)
There is a base64 C library for Lua at http://www.tecgraf.puc-rio.br/~lhf/ftp/lua/#lbase64.
If you're using luarocks:
luarocks install lbase64
Also check this out:
https://github.com/LuaDist/lbase64

Correct way of calling mongodb cloneCollection from ruby

I am trying to call the mongodb cloneCollection command from ruby. Based on the first question at https://github.com/mongodb/mongo-ruby-driver/wiki/FAQ I made this test script:
require "mongo"
include Mongo
db = MongoClient.new("localhost", 27017).db("test")
coll = "users2"
cmd = {}
cmd['cloneCollection'] = coll
cmd['from'] = "test.example.com:27017"
db.command(cmd)
However, instead of cloning the users2 collection, it creates an empty database with the same name. Cannot figure out what I'm doing wrong. Any ideas? Thanks!
The following Ruby program is a self-contained working example that should answer your question, complete with startup of source and destination mongod servers.
It appears that the arg to cloneCollection must be a fully qualified name, e.g., "test.users" for the "users" collection in the "test" database.
require "mongo"
# startup source and destination mongod servers
source = { 'port' => 27018, 'dbpath' => 'data27018' }
dest = { 'port' => 27019, 'dbpath' => 'data27019' }
[ source, dest ].each do |server|
dbpath = server['dbpath']
system("rm -fr #{dbpath} && mkdir #{dbpath} && mongod --port #{server['port']} --dbpath #{dbpath} &")
end
sleep 10 # delay for mongod startup
db_name = 'test'
coll_name = 'users'
db_coll_name = "#{db_name}.#{coll_name}"
# create source collection
db = Mongo::MongoClient.new("localhost", source['port']).db(db_name)
coll = db[coll_name]
coll.insert({'name' => 'Gary'})
# cloneCollection from source to dest
db = Mongo::MongoClient.new("localhost", dest['port']).db(db_name)
cmd = BSON::OrderedHash.new
cmd['cloneCollection'] = db_coll_name
cmd['from'] = "localhost:#{source['port']}"
db.command(cmd)
# verify cloneCollection
p db[coll_name].find.to_a
# kill mongod servers
pids = `pgrep mongod`.split(/\n/).sort.slice(-2,2)
system("kill #{pids.join(' ')}")
Please let me know if you have any further questions.
After being surprised that Mongo::DB does not have a cloneCollection method, and reading #Gary Murakami's excellent answer above, I wrote this piece of monkey patch that I though may be useful to others:
class Mongo::DB
def cloneCollection(remote_host, collection_name)
cmd = BSON::OrderedHash.new
cmd['cloneCollection'] = name + "." + collection_name
cmd['from'] = remote_host
self.command(cmd)
end
end
Just drop it anywhere in your source file to get a Mongo::DB.cloneCollection implementation.

How to create a link in Sinatra that points to a directory of files?

I have an application that calls an other application that populates a directory. Once it is finished I want to provide a link to the directory that contains the created files and people can examine or download them via the browser:
For example this works to provide a link to a single file: (Note this uses HAML) but the idea is the same
%p
- output_href = File.join("..","..","test_runs",File.basename(#dealclick_test_run.result_filename) )
Result file =
%a{:id => "result-file", :href => "#{output_href}"}
= File.basename(#dealclick_test_run.result_filename)`
The corresponding code for the directory doesn't work:
%p
Results:
- output_href = File.join("..","..","test_runs",File.basename(#dp_test_run.result_filename) )
%a( id = "dealprocessor_results" href = "#{output_href}" )
= File.basename(#dp_test_run.result_filename)
What am I doing wrong?
%a( id = "dealprocessor_results" href = "#{output_href}" )
should be
%a(:id = "dealprocessor_results" :href = "#{output_href}" )

Resources