Ruby

http://s.erious.ly

Author Archive

Amazon Reduced Redundancy Storage Released

Amazon recently announced their new storage service, Reduced Redundancy Storage (RRS). We are pleased to introduce a new storage option for Amazon S3 called Reduced Redundancy Storage (RRS) that enables customers to reduce their costs by storing non-c...

Rack::Bug setup

Lately, I’ve been using Rack::Bug to help me find some slow queries. Here’s a quick 6 step tutorial on setting it up. 1. Install Plugin: script/plugin install git://github.com/brynary/rack-bug.git 2. Add this to development.rb: config.middleware.use ("Rack::Bug", :secret_key => "epT5uCIchlsHCeR9dloOeAPG66PtHd9K8l0q9avitiaA/KUrY7DE52hD4yWY+8z1", :password => "some_pass" ) If you want restricted access by IP, you can add this to the hash :ip_masks => [IPAddr.new("127.0.0.1")] 3....

Code Readability vs Optimization

There are times when I debate whether to use one-liners or break them out into more readable blocks. Recently I had a situation where I needed to check certain fields on an object depending on the status of other fields. Let’s say I have a Book object and I wanted to see if it’s condition...

Exporting MySQL database

Exporting mySQL dumps can sometimes be tricky. Some sites suggest exporting dump like so:

mysqldump database_name > dump.sql

However, the problem with this method, is that the stream redirect might not be able to handle UTF-8 encoding correctly on certain OSes. I recently had a project that uses exotic characters and some characters...

Executing SQL commands in Rails

Most of the times, ActiveRecord's helpers to access database info is all you need; sometimes, you want to do some hacky stuff.

For example, I had to figure out a database's timezone and schema but I had no shell access to the server. So I ran this used Base.connection.execute and fetch_row to get the...

Copying files between S3 accounts

Recently, I had to transfer a all files from one S3 account to another one. Since I didn't like to bother Amazon with my petty problems. I decided to use a ruby script to do it. Here's the script and some steps I took to do it.

Setup

First thing we need to do is to...

Counting total number of objects in S3

Sometimes it comes in handy to get the total number of objects you have in S3 but it is not as straight forward. Here's a snippet I use to get total number of objects using right_aws gem.

require 'rubygems' 
require 'right_aws' 

AWS_ID='id
        

Lazy blogging with Twitter and Tumblr

Recently, I was bored with trying to follow links on Twitter Trends for videos and images. So I put this script together quickly to search for all new Twitter results then post the tweets with links on Tumblr, where it would show images and videos.

Setup

You'll need to have a few gems:

  • hpricot
  • twitter
  • ruby-tumblr

sudo gem install hpricot...

Scraping Images from Twitpics

Recently, I've been scraping Images and videos from Twitter and one site that has not been too easy to grab pics from is Twitpics. Here's a snippet of code that I've been using to grab the image from Twitpic with Hpricot:

require 'net/http'
require 'hpricot'

def...

Finding Memory Leaks with Bleak House

Sometimes a project can get large with many lines of code and some of those parts may have some leaks. These leaks might not be bad at first, but it will eventually can eat up all the memory on a server and cause it to act slow. Then, the thins/mongrels will have to be...

Copy S3 assets with right_aws

Lately, I've been using right_aws to interact with S3. One thing that I found helpful was copying assets between buckets and keeping the same permissions on them. However, it's not as simple as just copying the assets over. You need to get the Access Control Policy from the source and put it in the...