Archive for the 'ruby' Category

Windows Azure Tables adapter for DataMapper

The Past

Last Friday, we shipped the first Major version (v1.0.0) of the Windows Azure Storage API gem for ruby, started a few months ago by my friend Johnny Halife. As it is an open-source project, I had the opportunity to contribute with:

  • Support for table service to query, get_one, insert, update, merge and delete entities.
  • Support for running against the Storage Developement Fabriq shipped with Microsoft SDK.
  • Signature support for Tables service according to msdn.microsoft.com/en-us/library/dd179428.aspx
  • Support to enumerate, create, and delete tables on give storage account.
  • Give feedback to Improve the support for stacked connection management.

This release of waz-storage for ruby includes numerous features collected thru 0.5.6 to 1.0.0, for more information you can visit the http://waz-storage.heroku.com/ where you will find all the gem documentation, or if you like to read the source code, contribute or giving us feedback you can get it from on http://github.com/johnnyhalife/waz-storage.

The Present

One of the objectives of having Tables support on the gem was to have an interface to interact with Tables and Entities that we can consume from an adapter as we usually do with our favorite ORM written in ruby which is DataMapper.

This is why this weekend was pretty much to make the dream come true, creating a new project on github called dm-waztables-adapter (http://github.com/jpgarcia/dm-waztables-adapter) and spitting some lines of code.

Writing the adapter

As everything in Ruby wonderful world, it was really easy to have a first version running with the features provided by Datamapper.

It took me a few hours to write down 85 lines of code to cover the whole adapter (Create, Read, Update and Delete methods)

Sorry, I’m forgetting the aditional 30 minutes I spent on writing 32 more lines to cover the Migrations stuff. So you won’t worry about creating the tables when you design your models (As Windows Azure doesn’t have support for schemas inside tables, migrations exists just to make sure that you have the tables. It won’t modify attributes of existing data).

Below you will find some code samples. I hope you like it.

Getting started

sudo gem install dm-waztables-adapter --source http://gemcutter.org

Usage

require 'dm-waztables-adapter'

# set up a DataMapper with your Windwows Azure account
DataMapper.setup(:default, { :adapter => 'WAZTables',
                                         :account_name => 'name',
                                         :access_key => 'your_access_key' })

# define a new model
class Guitarist
    include DataMapper::Resource

    property :id, String, :key => true
    property :name, String
    property :age, Integer
end

# set up database table on Windows Azure for a specific model
Guitarist.auto_migrate! # (destructive)
Guitarist.auto_upgrade! # (safe)

# set up database table on Windows Azure for all defined models
Datamapper.auto_migrate! # (destructive)
Datamapper.auto_upgrade! # (safe)

# play with DataMapper as usual
Guitarist.create(:id => '1', :name => 'Ritchie Blackmore', :age => 65)

yngwie = Guitarist.new(:id => '2', :name => 'Yngwio Malmsteen', :age => 46)
yngwie.name = "Yngwie Malmsteen"
yngwie.save

# retrieving a unique record by its id
ritchie = Guitarist.get('1')
ritchie.age # => 65

# updating records
ritchie.age = 66
ritchie.save

# retrieving all guitarists
    Guitarist.all.length # => 2

# performing queries
    older_guitar_players = Guitarist.all( { :age.gte => 50 } )

# deleting records
older_guitar_players.destroy!

TODO

  • Allow users to define the model partition key by using :partition_key => true option on the property.
  • Allow users to set the partition key as an additional attribute of the model with a lambda as default value.
  • Allow users to set the partition key as a method on the model.
  • Implement “in” operator in queries
  • Implement “order” query option
  • Retrieve more than 1000 fields using Windows Azure :continuation_token

Known Issues

  • Like statements are not working since Microsoft service API is throwing a NotImplemented exception when
    using startswith and endswith filters (more information here)
  • There’s no way to tell thru the entity which is the partition key of our entity, so there’s no out-of-the-box load balancing support (for mor info on the tables model that a look at http://msdn.microsoft.com/en-us/library/dd179338.aspx)

Dropzone extension leveraging Ruby waz-storage gem

It’s been a long time since my last post, so today I want to share with you my experience on using the waz-storage gem, created by my friend Johhny Halife. If you are not aware about his amazing job you should check this post to get more context about what I’m going to show you.

What can I do?

After reviewing Johnny’s code I was very excited on creating something and to start playing with that toy, but obviously the question was WHAT?

The answer came to my mind while reading a blog about an application for Mac OSX desktop called Dropzone. This is a excerpt taken from Dropzone’s website.

“Drag a file onto the dock icon and your fully customizable grid of destinations flies smoothly out using core animation. Drop the file onto a destination and Dropzone will take care of the rest. Whether you’re installing an app, uploading a file to an FTP server or sharing your photos on Flickr.”

There is a section regarding how to extend the Dropzone’s features, and how to contribute creating plugins. Dropzone can easily be extended using simple ruby scripts.

So, I thought about writing a script that allow users to easily drag an drop files from your computer and store them as Blobs on the Windows Azure Storage Services.

Coding for fun!

I started reading the Dropzone Scripting API documentation and I was surprised how easy it was. There are only two methods to implement which are dragged, clicked and that’s it.

Beyond the simplicity that gives the Dropzone’s API, I had the joy of coding in Ruby and the waz-storage gem easiness.

You can find the source code on the following url http://github.com/jpgarcia/dropzone-user-scripts/blob/master/WAZBlobs.dropzone

How can you try it?

Installation and configuration:

  1. Download the Dropzone program from here
  2. Install the waz-storage gem if the gem isn’t installed yet
    sudo gem install waz-storage –version >= 0.5.4 –source http://gemcutter.org
  3. Download the dropzone extension that I created from the github repositories. The file is called WAZBlobs.dropzone
  4. Open the WAZBlobs.dropzone file and provide your Windows Azure Services credentials as depicted below:

The script and the functionality is very simple:

  1. Drag and drop your files to the Azure’s icon on the Dropzone panel
  2. The files will be uploaded to a public container called dropzone.
  3. The following picture shows how the Picture 3, that I dragged & dropped above, is already on Windows Azure Blobs servers. So I will play a little bit with the console to show you that the Blob is already there :)

On my next post I will show you a new application I’m developing on Heroku that uses the same gem to manage the Blobs via Web. Stay tuned!