Dezember 2, 2011 at 12:49 pm · Filed under Rails, Ruby
Sometimes you need to save a locally created file to S3 instead of an uploaded file, as is the standard. Here is how:
has_attached_file :tagged_text_file, STORAGE_OPTIONS.merge({
:processors => []
})
def save_tagged_text_file
file = File.open("#{RAILS_ROOT}/tmp/tagged_text_#{id}.txt", 'w+')
file << tagged_text
self.tagged_text_file = file
save!
file.close
end
Dezember 2, 2011 at 12:46 pm · Filed under Allgemein
SSH tunneling is, of course, useful for a ton of services, but I happened to stumble upon it, when I wanted to connect to a remote redis server.
If you have a redis server running on , you can easily connect
to it (given you have ssh access to it, of course):
ssh -L 9999:localhost:6379 @
will open a tunnel from the remote port 6379 (redis standard) to the local port 9999.
You can now use the redis on your local port 9999 like you would if it was running locally. Nice.
November 28, 2011 at 7:33 pm · Filed under Note to self, Ruby
You use two heroku apps as staging and production for your project. You added both as git remotes, e.g. “production” and “staging”.
Now you want to push your local branch “poster” to remote “staging”, use
git push staging poster:master
Note, that you can only push to “master” on herokus side. This is just git syntax, but I keep forgetting it, so here it is for future reference.
Oktober 7, 2010 at 8:49 am · Filed under Note to self, Ruby
Sometimes you may wish to map an Array to a Hash in Ruby, like say, you got the output of I18n.available_locales
locales = [:en, :de, :fr]
and want to transform that into a Hash to fill a select element, like so:
[:en => 'EN', :de => 'DE', :fr => 'FR']
How do you do that in the most concise way?
First, there is always inject:
locales.inject({}) {|hsh, sym| hsh[sym] = sym.to_s.upcase; hsh}
But I like the following approach way better, mainly because it emphasizes my intention more clearly (and, btw. is faster too):
Hash[locales.map{|sym| [sym, sym.to_s.upcase]}]
Remember: Hash[:a,:b,:c,:d] produces {:a => :b, :c => :d}.
Juli 8, 2010 at 12:27 pm · Filed under AWS, Ruby
Building on this article here is a simple ruby script, that copies files between two buckets of the same S3 account, omitting files already present (by name).
This variant adds a list of path prefixes, so you can selectively copy only certain directories of your buckets.
Furthermore it copies the original buckets ACLs for each key.
require 'rubygems'
require 'right_aws'
aws_access_key_id = 'YOUR AMAZON ACCESS KEY'
aws_secret_access_key = 'YOUR AMAZON SECRET ACCESS KEY'
source_bucket = 'SOURCE BUCKET NAME'
target_bucket = 'TARGET BUCKET NAME'
prefixes = [PATH_PREFIX1, PATH_PREFIX2, ...]
s3 = RightAws::S3Interface.new(aws_access_key_id, aws_secret_access_key)
copied_keys = Array.new
(prefixes || ['']).each do |prefix|
s3.incrementally_list_bucket(target_bucket, {:prefix => prefix}) do |key_set|
copied_keys << key_set[:contents].map{|k| k[:key]}.flatten
end
end
copied_keys.flatten!
(prefixes || ['']).each do |prefix|
s3.incrementally_list_bucket(source_bucket, {:prefix => prefix}) do |key_set|
key_set[:contents].each do |key|
key = key[:key]
if copied_keys.include?(key)
puts "#{target_bucket} #{key} already exists. Skipping..."
else
puts "Copying #{source_bucket} #{key}, setting acl"
retries=0
begin
s3.copy(source_bucket, key, target_bucket)
acl = s3.get_acl(source_bucket, key)
s3.put_acl(target_bucket, key, acl[:object])
rescue Exception => e
puts "cannot copy key, #{e.inspect}\nretrying #{retries} out of 10 times..."
retries += 1
retry if retries <= 10
end
end
end
end
end
November 28, 2009 at 12:26 pm · Filed under Allgemein, Javascript
Coverflow has become a de facto visualization standard for the presentation of collections of images, be it covers or portraits.
There are a number of implementations for usage on web pages (e.g. this one) but the usable ones require Adobes Flash and thus won’t run on the iPhone.
When looking for HTML5 canvas based implementations I found this promising implementation based on the YUI library.
Though workig, it has three major drawbacks: It is rather overengineered and difficult to tweak, it uses YUI (whereas I prefer the more lightweight jQuery) and it performs poorly with image sizes bigger than thumbnails.
After trying to change the code for a while I decided to do a reimplementation in jQuery. The result can be seen on the MomoFlow demo page. Here are two screenshots:

CoverFlow using canvas and jQuery

Quicklook mode
The used 3D transformation is superbly described on the YUI blog .
My implementation caches the rendered canvases per rendering angle. Further speed increments are made possible by adjusting the mesh width used for the slicing transformation depending on the achieved framerate.
The result performs beautifully in recent Safari, Chrome and Opera, decently on Firefox. It also works flawlessly on the iPhone. Keyboard control is coming soon.
I do still need help on IE, maybe the image composition is too demanding for ExplorerCanvas?
The code is available on github: http://github.com/momolog/momoflow.
Comments and improvements are very much welcome!
Oktober 21, 2009 at 11:03 am · Filed under Allgemein
Since MySQL does neither have real check constraints nor a way to raise an exception in a stored procedure, we found it not instantly obvious, how we could *reject* a certain row on insert, based on a certain condition.
A nice way we found was to set the value in question to NULL, based on the condition and let the NOT NULL constraint do its work.
ALTER TABLE sessions MODIFY session_id varchar(255) NOT NULL;
DROP TRIGGER IF EXISTS check_sessionid;
DELIMITER $$
CREATE TRIGGER check_sessionid BEFORE INSERT ON sessions
FOR EACH ROW BEGIN
IF NOT NEW.session_id REGEXP '^[[:xdigit:]]{32}$' THEN
SET NEW.session_id = NULL;
END IF;
END;
$$
DELIMITER ;
The trigger will let any 32 character string with only HEX characters for the column session_id pass and rejects the rest.
> INSERT INTO sessions (session_id) VALUES ('ffffffffffffffffffffffffffffffff');
Query OK, 1 row affected (0.01 sec)
> INSERT INTO sessions (session_id) VALUES ('fffffffffffffffffffffffffffffffg');
ERROR 1048 (23000): Column 'session_id' cannot be null
Happy triggering.
Oktober 18, 2009 at 5:46 pm · Filed under Allgemein, MacOSX, Note to self, Ruby
Suche nach Wort unter dem Cursor in vim: #.
jssh ist eine JavaScript Shell, die den Firefox per Port 9997 fernsteuerbar macht.
Download z.B. hier.
y erzeugt einen YAML dump auf der Rails console, mehr dazu hier.
=3D ist ein escaptes “=” in quoted_printable.
sudo /usr/libexec/locate.updatedb aktualisiert unter MacOSX sofort die locate Datenbank.
rake db:migrate:redo führt unter rails die letzte Migration rückwärts und sofort wieder vorwärts aus, so dass sich die Vorwärts-Action korrigieren läßt
ack -Q bringt ack dazu, literal, also ohne RegExp zu suchen.
Oktober 1, 2009 at 11:24 am · Filed under MacOSX, Note to self
In den single user mode (root shell) starten, beim Neustart:
[CMS] s
In den verbose mode (boot log) starten:
[CMD] v
In den safe mode (nur core kexts) starten:
[SHIFT]
September 4, 2009 at 11:05 am · Filed under Javascript
Javascript allows naming and assigning functions at the same time like:
var vname = function fname() {}
The function name fname is available only inside the function as a local variable:
var vname = function fname(){
console.log(typeof vname); // function
console.log(typeof fname); // function
}
console.log(typeof vname); // function
console.log(typeof fname); // undefined
If we “redefine” this local variable inside of the function, we get a strange effect:
var vname = function fname(){
console.log(typeof vname); // function
console.log(typeof fname); // undefined !!!
var fname = 1;
console.log(typeof fname); // number
}
console.log(typeof vname); // function
console.log(typeof fname); // undefined
Obviously the interpreter sees the variable declaration var fname on entrance into the function and does not provide the function variable at all.
Next entries »