Julio Capote http://juliocapote.com Thinkings and Codings posterous.com Wed, 25 Jan 2012 09:45:00 -0800 Finagle with scala-bootstrapper http://juliocapote.com/finagle-with-scala-bootstrapper http://juliocapote.com/finagle-with-scala-bootstrapper

I've been fascinated by the concepts in finagle for some time, but being a scala noob, I never knew how to bootstrap a finagle project. Turns out twitter has a gem, scala-bootstrapper, that generates a simple thirft based key/value store for you. There's even a tutorial on how to extend the example project into a distributed search service.

This is a guide on setting it all up locally, it assumes you have Git, Homebrew, and OS X.

1) Install scala 2.8.1

1
2
3
4
5
6
$ brew versions scala
$ cd /usr/local/ (or wherever you have homebrew installed)
$ git checkout -b scala281 0e16b9d (make sure the SHA matches versions output)
$ brew install scala
$ git checkout master
$ git branch -D scala281

2) Install sbt 0.7.4 (assumes you have a ~/bin in your $PATH)

1
2
$ curl -O http://simple-build-tool.googlecode.com/files/sbt-launch-0.7.4.jar > ~/bin/sbt-launch.jar
$ echo 'java -Xmx1G -jar `dirname $0`/sbt-launch.jar "$@"' > ~/bin/sbt

3) Install scala-bootstrapper

1
$ gem install scala-bootstrapper

4) Generate finagle project

1
2
3
4
5
$ mkdir newbird
$ cd newbird
$ scala-bootstrapper newbird
$ sbt update
$ sbt test

5) Add a Client class

create newbird/src/main/scala/com/twitter/newbird/Client.scala with

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.twitter.newbird

import com.twitter.finagle.builder.ClientBuilder
import com.twitter.finagle.thrift.ThriftClientFramedCodec
import com.twitter.newbird.thrift._
import org.apache.thrift.protocol.TBinaryProtocol
import java.net.InetSocketAddress

class Client {
  val service = ClientBuilder()
    .hosts(Seq(new InetSocketAddress("localhost", 9999)))
    .codec(ThriftClientFramedCodec())
    .hostConnectionLimit(1)
    .build()

  val client = new NewbirdServiceClientAdapter(
      new thrift.NewbirdService.ServiceToClient(
        service, new TBinaryProtocol.Factory))

  def get(key: String) = client.get(key)()
  def put(key: String, value: String) = client.put(key, value)()
}

6) Running the server

1
2
3
$ cd newbird
$ sbt
> run -f config/development.scala

7) Playing with the client

1
2
3
4
5
6
$ cd newbird
$ sbt console
scala> import com.twitter.newbird.Client
scala> val client = new Client()
scala> client.put("foo", "bar")
scala> client.get("foo")

Bonus

finagle exports a stats url you can curl:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
$ curl http://localhost:9900/stats.txt

counters:
  Newbird/connects: 1
  Newbird/requests: 4
  Newbird/success: 4
gauges:
  Newbird/connections: 0
  Newbird/pending: 0
  jvm_heap_committed: 588251136
  jvm_heap_max: 2146828288
  jvm_heap_used: 64354560
  jvm_nonheap_committed: 83267584
  jvm_nonheap_max: 318767104
  jvm_nonheap_used: 68655360
  jvm_num_cpus: 4
  jvm_start_time: 1327511164928
  jvm_thread_count: 14
  jvm_thread_daemon_count: 9
  jvm_thread_peak_count: 14
  jvm_uptime: 2626505
labels:
metrics:
  Newbird/connection_duration: (average=2590412, count=1, maximum=2590412, minimum=2590412, p25=2590412, p50=2590412, p75=2590412, p90=2590412, p99=2590412, p999=2590412, p9999=2590412)
  Newbird/connection_received_bytes: (average=192, count=1, maximum=192, minimum=192, p25=192, p50=192, p75=192, p90=192, p99=192, p999=192, p9999=192)
  Newbird/connection_requests: (average=4, count=1, maximum=4, minimum=4, p25=4, p50=4, p75=4, p90=4, p99=4, p999=4, p9999=4)
  Newbird/connection_sent_bytes: (average=120, count=1, maximum=120, minimum=120, p25=120, p50=120, p75=120, p90=120, p99=120, p999=120, p9999=120)
  Newbird/request_latency_ms: (average=14, count=4, maximum=39, minimum=2, p25=2, p50=8, p75=10, p90=39, p99=39, p999=39, p9999=39)

 

 

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/1332725/Julio_Capote_reasonably_small.jpeg http://posterous.com/users/hMLlrHnMS Julio Capote @capotej Julio Capote
Mon, 09 Jan 2012 23:36:00 -0800 Alfred Extension for creating Wunderlist tasks http://juliocapote.com/alfred-extension-for-creating-wunderlist-task http://juliocapote.com/alfred-extension-for-creating-wunderlist-task

While looking for a way to add wunderlist tasks via alfred, I came upon this: http://jdfwarrior.tumblr.com/post/13163220116/wunderlist-for-alfred

Looked cool, but I wanted to write my own that didn't depend on php.

I used lsof to figure out the location of the db, then used file to see what kind of db it was. Luckily, it was sqlite3, so I was able to poke around and figure out the sql to create a task.

Here's the alfred extenstion that ties it all together:

user=`whoami` wunderdb="/Users/$user/Library/Wunderlist/wunderlist.db" sqlite3 $wunderdb "insert into tasks (name, list_id) values ('{query}', 1)"

Download it here: http://dl.dropbox.com/u/42561/wunderlist-capotej.alfredextension

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/1332725/Julio_Capote_reasonably_small.jpeg http://posterous.com/users/hMLlrHnMS Julio Capote @capotej Julio Capote
Tue, 13 Sep 2011 08:59:26 -0700 Render image links directly inside Adium http://juliocapote.com/render-image-links-directly-inside-adium http://juliocapote.com/render-image-links-directly-inside-adium

Last night I delightfully discovered that Adium Message Styles are just html, css, and javascript rendered inside a webview. The next natural step was to write something in it, so I wrote a Message Style that tries to render any image link directly inline the conversation (campfire style).

X

The code was written at midnight after a long day, so its not best. Basically, it's a setInterval that runs every 2.5 seconds that loops through all message elements, appending an img tag to the body of the message if an image link is detected. It also removes the processing class as to not reprocess the same messages.

Installation is simple, just download: 

http://dl.dropbox.com/u/42561/Stockholm.AdiumMessageStyle.zip

and extract into ~/Library/Adium 2.0/Message Styles (create if necessary). Then choose the TOP Stockholm theme (no idea why there are two entries), and close your chat window. It should be activated next time a chat window opens.

Y

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/1332725/Julio_Capote_reasonably_small.jpeg http://posterous.com/users/hMLlrHnMS Julio Capote @capotej Julio Capote
Thu, 18 Aug 2011 20:14:00 -0700 Don't pee in the pool. http://juliocapote.com/dont-pee-in-the-pool http://juliocapote.com/dont-pee-in-the-pool

A rich man builds a pool containing only bottled water and invites a few friends over. He gathers everyone around and says, "Guys, I know it's tempting and convenient to pee in the pool, but this pool is 100% Evian, let's all try to use the bathroom next to the pool instead." So everyone listens and enjoys a urine-free, delicious pool for the rest of the day. Until, of course, one of them breaks the rule, ruining it for everyone.

That pool is your commit history and the merge commits are the pee in the pool. So please, keep your pee out of the pool and rebase instead.

Disclaimer: I don't really care if you do or do not pee in regular pools. This is all made up to illustrate a point.

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/1332725/Julio_Capote_reasonably_small.jpeg http://posterous.com/users/hMLlrHnMS Julio Capote @capotej Julio Capote
Mon, 25 Jul 2011 13:37:00 -0700 Sane Lion Gestures http://juliocapote.com/sane-lion-gestures http://juliocapote.com/sane-lion-gestures

Here's how you disable lion's dumb default gestures that break front/back on browsing and next/prev in emacs:

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/1332725/Julio_Capote_reasonably_small.jpeg http://posterous.com/users/hMLlrHnMS Julio Capote @capotej Julio Capote
Wed, 01 Jun 2011 10:18:00 -0700 My cloud experiment http://juliocapote.com/my-cloud-experiment http://juliocapote.com/my-cloud-experiment

Lately I've been thinking about the best way to store and backup large amounts of personal data. It irked me that I've never been able to keep a collection of photos past 2-3 years. Either it's a machine swap or a misplaced backup, something always happens. 

When the idea of storing or backing up everything offsite comes up, people usually throw out objections like "What if they lose all my stuff or go out of business?" or "I don't like the idea of my files on someone else's computers". However, based on my past performance, I concluded that any solution is better than my current one.

The ideal situation was to have my entire home directory on Dropbox. This is feasible only if you have about 50-100gbs you care about. For most people, including myself, this is not the case. So how do you reduce your total data footprint to 50gbs?

  • No more local mp3 storage. I went and uploaded my current mp3 collection (15 gbs or so) to Amazon Cloud Player. I also deleted anything rdio already had in its collection. 
  • I deleted the random assortment of movies and tv shows I've acrued over the years, since 98% of them they are already on Netflix and Hulu Plus.

  • Pictures were the last remaining swath of disk space. I wrote a script using the (awesome) smugmug api, and uploaded all my photos to smugmug.

After these changes I'm going to put my entire home directory up on Dropbox, since it will fit now. Admittedly, all these services cost money, but it's a small price to pay for never having to worry about backups or the location of your files (since they can be accessed anywhere).

Im curious as to how others have solved this problem or how Apple will solve it with their supposed "iCloud".

 

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/1332725/Julio_Capote_reasonably_small.jpeg http://posterous.com/users/hMLlrHnMS Julio Capote @capotej Julio Capote
Mon, 03 Jan 2011 09:48:00 -0800 MigrationFor: Write migrations right from the command line! http://juliocapote.com/post/2583891119/migrationfor-write-migrations-right-from-the-command http://juliocapote.com/post/2583891119/migrationfor-write-migrations-right-from-the-command

As someone who mostly stays in the rails console, I’ve always hated forgetting a field, creating a migration, finding it among your other 500 migration files, then adding the one line you need to add, then running it. This is probably the most annoying part of the Rails experience. I’ve always wanted to write a better migration generator that could take a list of commands/fields and write the migration for you, since most of the time what you name a migration has all the info it needs (add_index_to_post_id). Thanks to the heavily refactored plugin/generator API in Rails 3, I was able to do just that.

Let’s take a look at how it works:

First, install it (only works for Rails 3)

rails plugin install git://github.com/capotej/migration_for.git

Then, you can create migrations like so:

rails g migration_for add_index:posts:posts_id

It would generate db/migrate/20110103182654_add_index_posts_posts_id.rb):

class AddIndexPostsPostsId < ActiveRecord::Migration

   def self.up

      add_index 'posts','posts_id'

   end

   def self.down
     #waiting for reversible migrations in rails 3.1!
   end

end

Which you can then run normally with rake db:migrate

Let’s look at a more complex example:

rails g migration_for create_table:posts add_column:posts:title:string add_column:posts:user_id:integer add_index:posts:user_id

Would generate:

class CreateTablePostsaddColumnPostsTitleStringaddColumnPostsUserIdIntegeraddIndexPostsUserId < ActiveRecord::Migration

   def self.up

      create_table 'posts'

      add_column 'posts','title','string'

      add_column 'posts','user_id','integer'

      add_index 'posts','user_id'

   end

   def self.down
     #waiting for reversible migrations in rails 3.1!
   end

end

It uses a lookup table with all the activerecord transformations and will only insert an expression into a migration if the method name is valid and it has the right number of arguments, so botched commands wont mess up the migration. Hope you enjoy it as much as I have!

Source available here: https://github.com/capotej/migration_for

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/1332725/Julio_Capote_reasonably_small.jpeg http://posterous.com/users/hMLlrHnMS Julio Capote @capotej Julio Capote
Fri, 31 Dec 2010 13:29:00 -0800 What I released in 2010 http://juliocapote.com/post/2546786852/what-i-released-in-2010 http://juliocapote.com/post/2546786852/what-i-released-in-2010

Here’s a recap of what I’ve worked on and released in 2010:

Youtube Fraiche: I couldn’t find a youtube downloader that worked on github, so I wrote my own one evening https://github.com/capotej/youtube_fraiche

Uploadd and paperclip_uploadd: I wanted to upload and store images off-site (using paperclip/rails) on an server which has cheaper bandwidth rates than S3. Using rainbows, this tiny rack script has handled over a 1.5 million uploads at a peak of 10-15 uploads/sec. Also, it’s been running for about 6 months now without a single crash. Thank you Eric Wong! https://github.com/capotej/uploadd There is also a plugin for the popular paperclip gem to use uploadd as a storage backend transparently. https://github.com/capotej/paperclip_uploadd

mrskinner: Tiny javascript for making the site gutters clickable based on a fixed width layout: https://github.com/capotej/mrskinner/blob/master/mrskinner.js

existential: Completely inspired by Nick Kallen’s post on authorization, I wanted to extract that pattern into a rails plugin that I could use for all my projects. I use devise/existential for all my projects now. https://github.com/capotej/existential

has_opengraph: Easy way to participate in opengraph and draw facebook like buttons. Just annotate your models with meta data, and draw it in your view easily. https://github.com/capotej/has_opengraph

chewbacca: I kinda feel bad that I took a cool name for such a lame script. Anyway it’s a set of rake tasks that provide a hair of abstraction above scp. Useful when you have a set of files locally that map to a different set of files remotely. https://github.com/capotej/chewbacca

I already have tons of stuff in the works for 2011!

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/1332725/Julio_Capote_reasonably_small.jpeg http://posterous.com/users/hMLlrHnMS Julio Capote @capotej Julio Capote
Sun, 14 Feb 2010 18:31:00 -0800 On Google Buzz http://juliocapote.com/post/390050440/on-google-buzz http://juliocapote.com/post/390050440/on-google-buzz

Summary: In this post I attempt to rationalize the creation of Google Buzz.

How I see it
Google Buzz is a subset of email, optimized for sharing information and discussing said information amongst a network of followers. I’m sure Google noticed that a large number of users send short messages, occasionally accompanied by a link or an attached image, to the same people over and over. I know I do; Regardless of Twitter or Facebook, Email is still my preferred medium for propagating content to my close friends. In fact, the first browser add-on I install is the “send via gmail” to automate this process. So upon seeing Google Buzz, instead of seeing it as “another social network”, I saw it as a streamlining of my current link sharing workflow.

Sharing
Sharing things on Google Buzz is pretty easy (although I’m still waiting for my chrome extension). It’s faster than sending out an email because you can create groups of those you would normally CC and you don’t have to think about a subject line.

Private Groups
I believe that this is Google Buzz’s killer feature. As our social graphs increase and intersect/overlap, the importance of segregating information increases. For instance, your Mom/Boss/Girlfriend(s) seeing pictures of you at that raunchy party the other night. This is a common problem that anyone with a Facebook/Myspace can attest to having (at least once). Not just for privacy’s sake but for the sake of your followers. I used to have every tweet posted to my Facebook but people complained that they didn’t understand 99.9% of the things I posted (programming links/info usually). And inversely if I started posting non technical/silly things to Twitter, I’ll lose the technical audience there (this is why this app rules). Google Buzz allows me to share content with just the people I want (and discuss with the people I want).

Discussion
Let’s face it, trying to have a deep discussion is next to impossible on Twitter (not that this is bad). Meanwhile, Google Buzz inherits gmail’s threaded conversation feature (one of it’s best features) which makes discussing buzzed items a breeze. Also the “Like” concept is not to be underestimated, how many times have you seen email replies with “+1” in them? Or, if a discussion you don’t care about keeps showing up on your stream you can simply Mute it. Crazy Speculation: This may be what’s coming next for Google Groups.

Pain Points
Nothing is perfect, and Google Buzz is surely is not. For one, it’s, some say, intrusive, arrival rubbed a lot of people the wrong way. You’ll find all kinds of posts on the internet about how they feel betrayed by Google for forcing a social network down their throat. Others felt that their gmail was their “work area” and didn’t want to bothered with such folly (understandably so). That’s why I feel it should’ve been deployed like this: Wave style invites and if you buzz someone that doesn’t have an invite, they just get it like a regular email with a link on the bottom to join. The early adopters will bring in those they feel will like the service and use it with them. The other point is that you can’t filter out buzzed tweets (when you tweet, Google Buzz will buzz it automatically) so you end seeing tweets twice.

Conclusion
So in conclusion, will Buzz replace Twitter for me? No. But it will definitely replace those quickie emails I fire off to the same group of people over and over. I feel like they are trying to market Google Wave to a bigger, more mainstream audience. If that’s the case, I look forward to see what else Google Buzz will bring to the table in the future.

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/1332725/Julio_Capote_reasonably_small.jpeg http://posterous.com/users/hMLlrHnMS Julio Capote @capotej Julio Capote
Sun, 15 Nov 2009 16:16:39 -0800 On PHP Frameworks... http://juliocapote.com/post/245405012/on-php-frameworks http://juliocapote.com/post/245405012/on-php-frameworks

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/1332725/Julio_Capote_reasonably_small.jpeg http://posterous.com/users/hMLlrHnMS Julio Capote @capotej Julio Capote
Sun, 19 Jul 2009 18:51:00 -0700 Using Rack applications inside GWT Hosted mode http://juliocapote.com/post/145035194/using-rack-applications-inside-gwt-hosted-mode http://juliocapote.com/post/145035194/using-rack-applications-inside-gwt-hosted-mode

This guide will show you how you can use JRuby to run any Rack application inside Google Web Toolkit’s (GWT) hosted mode server so your interface and your backend are of the Same Origin.

Background
GWT has two ways of interacting with a server: GWT Remote Procedure Call (RPC) and plain HTTP (XHR). GWT-RPC is a high level library designed for interacting with server-side Java code. GWT-RPC implements the GWT Remote Service interface allowing you to call those methods from the user interface. Essentially, GWT handles the dirty work for you. However, it only works on Java backends that can implement that interface. Since most of my backends are Sinatra/Rack applications, I’ll be using the plain HTTP library.

The problem
Due to the restriction of the Same Origin policy, the interface served out of GWT’s development, or Hosted Mode server can only make requests back to itself. If you were using real servlets or GWT’s RemoteService this wouldn’t be an issue; but since Rack applications listen on their own port, you cannot make requests from GWT to our application without resorting to something like JSONP or server-side proxying. This leaves you having to compile our interface to HTML/JS/CSS, which is lengthy process, and serve it from the origin of the Rack application to see our changes.

The solution
Since I wanted to develop using GWT’s development environment with a Rack backend, I devised a way to use jruby-rack to load arbitrary Rack applications alongside our interface.

First let’s setup our environment:

1. Download and unpack the latest GWT for your platform (mine’s being linux) and goto it:

wget http://google-web-toolkit.googlecode.com/files/gwt-linux-1.7.0.tar.bz2tar -xvjpf gwt-linux-1.7.0.tar.bz2cd gwt-linux-1.7.0

2 .Download the latest jruby-complete.jar:

wget http://repository.codehaus.org/org/jruby/jruby-complete/1.3.1/jruby-complete-1.3.1.jarmv jruby-complete-1.3.1.jar jruby-complete.jar

3. Download the latest jruby-rack.jar

wget http://repository.codehaus.org/org/jruby/rack/jruby-rack/0.9.4/jruby-rack-0.9.4.jarmv jruby-rack-0.9.4.jar jruby-rack.jar

Now let’s create our GWT application using an example Sinatra backend:

4. Create an app with webAppCreator:

./webAppCreator -out MySinatra com.example.MySinatracd MySinatra

5. In order for this to work you have to package any gem dependencies your backend needs (sinatra, in our case) as jars within your application. For Sinatra it looks like this:

java -jar jruby-complete.jar -S gem install -i ./sinatra sinatra --no-rdoc --no-ri jar cf sinatra.jar -C sinatra .

6. Add jruby-complete.jar, jruby-rack.jar, sinatra.jar (and any other jars you’ve created) to the libs target of your build.xml:

<target name="libs" description="Copy libs to WEB-INF/lib"> <mkdir dir="war/WEB-INF/lib" /> <copy todir="war/WEB-INF/lib" file="${gwt.sdk}/gwt-servlet.jar" /> <!-- Add any additional server libs that need to be copied --> <copy todir="war/WEB-INF/lib" file="${gwt.sdk}/jruby-complete.jar" /> <copy todir="war/WEB-INF/lib" file="${gwt.sdk}/jruby-rack.jar" /> <copy todir="war/WEB-INF/lib" file="${gwt.sdk}/sinatra.jar" /> </target>

7. Add these lines right after <web-app> in war/WEB-INF/web.xml:

<context-param> <param-name>rackup</param-name> <param-value> require 'rubygems' require './lib/sinatra_app' map '/api' do run MyApp  end </param-value> </context-param> <filter> <filter-name>RackFilter</filter-name> <filter-class>org.jruby.rack.RackFilter</filter-class> </filter> <filter-mapping> <filter-name>RackFilter</filter-name> <url-pattern>/api/*</url-pattern> </filter-mapping> <listener> <listener-class>org.jruby.rack.RackServletContextListener</listener-class> </listener>

Note: All you’re doing here is passing the contents of a config.ru file into the <param-value> element for the <context-param> (make sure this is HTML encoded!). This states that any request to /api is to be handled by your Sinatra application and not GWT’s Hosted mode servlet.

8. Create your Sinatra backend and place it in war/WEB-INF/lib/sinatra_app.rb

require 'sinatra'require 'open-uri'class MyApp < Sinatra::Base  get '/showpage' do     open('http://www.yahoo.com').read  end  get '/helloworld' do    'hello world'  endend

9. Run your new awesome setup:

ant hosted

Now when navigate to http://localhost:8888/api/helloworld or http://localhost:8888/api/showpage you should see the Sinatra application being served via GWT.

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/1332725/Julio_Capote_reasonably_small.jpeg http://posterous.com/users/hMLlrHnMS Julio Capote @capotej Julio Capote
Thu, 01 Jan 2009 15:50:00 -0800 Useful Rails Routing tips http://juliocapote.com/post/67873462/useful-rails-routing-tips http://juliocapote.com/post/67873462/useful-rails-routing-tips

Even though I have been using Rails for fun and profit for about 2 years now, I felt I never really used it’s routing engine to its full potential. So I checked out new Rails Routing from the outside in guide and discovered bunch of useful tricks that I (and maybe you) had no idea you could do. Here they are:

Multiple resource definitions on a single line

map.resources :photos, :books, :videos

Impose a certain format for resource identifiers

map.resources :photos, :requirements => { :id => /[A-Z][A-Z][0-9]+/ }


This way, /photos/3 would not work, but /photos/DA321 would.

Friendlier action names

Say for your application ‘create’ and ‘change’ make more sense than the default ‘new’ and ‘edit’ you can do

map.resources :photos, :path_names => { :new => 'make', :edit => 'change' }

You can also do this site-wide also, in your environment.rb

config.action_controller.resources_path_names = { :new => 'make', :edit => 'change' }

Trim the fat off resources with :only and :except

When you use map.resources, rails generates 7 restful routes for that resource; But what if that resource only needed to be seen and listed, never edited or created?

map.resources :photos, :only => [:index, :show]

If your application uses alot of map.resources calls but not neccesarily all its generated routes, you can save memory this way.

Adding extra routes to your resources

Instead of fighting the map.resources generator by placing a horror like this atop your routes.rb

map.connect '/photos/:id/preview', { :controller => 'photos', :action => 'preview' }


You can do this to your already mapped resource

map.resources :photos, :member => { :preview => :get }


This will map all GET’s to /photos/3 to the preview action of your photos controller

This can also be  used in collections instead of singular members, just change :member to :collection

map.resources :photos, :collection => { :search => :get }

This will give you /photos/search and hit the search action within the photos controller

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/1332725/Julio_Capote_reasonably_small.jpeg http://posterous.com/users/hMLlrHnMS Julio Capote @capotej Julio Capote
Tue, 28 Oct 2008 21:05:00 -0700 So you want to click that button? http://juliocapote.com/post/56866975/so-you-want-to-click-that-button http://juliocapote.com/post/56866975/so-you-want-to-click-that-button

I stumbled upon http://clickthatbutton.com during my routine lurking of hacker news. After being amused for about 10 seconds,  I decided to take it to the next level; I wanted to click on it really, really fast. After going through a few solutions (simple js while loop in firebug, then curl/wget) and failing, the idea of using selenium popped into my head. So I went off to their site and installed the extension. I figured a simple recording of the mouse event, then wrapping it around a loop in selenium would do the trick, but I quickly found that selenium doesn’t support loops. Not to be stopped, I searched google and ended up with this. After installing the plugin for selenium (a plugin for a plugin!?) and restarting firefox, I tried it again and to my surprise it worked! The click counter was going up steadily on its own (18k clicks and counting). Here is my selenium test case for those of you following along:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head profile="http://selenium-ide.openqa.org/profiles/test-case">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="selenium.base" href="http://clickthatbutton.com/" />
<title>haha</title>
</head>
<body>
<table cellpadding="1" cellspacing="1" border="1">
<thead>
<tr><td rowspan="1" colspan="3">haha</td></tr>
</thead><tbody>
<tr>
    <td>open</td>
    <td>/</td>
    <td></td>
</tr>
<tr>
    <td>store</td>
    <td>x</td>
    <td>1</td>
</tr>
<tr>
    <td>while</td>
    <td>storedVars['x'] == storedVars['x']</td>
    <td></td>
</tr>
<tr>
    <td>click</td>
    <td>submit</td>
    <td></td>
</tr>
<tr>
    <td>endWhile</td>
    <td></td>
    <td></td>
</tr>

</tbody></table>
</body>
</html>

Just paste that into a file, open it with selenium ide, hit play and you should be good to go.

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/1332725/Julio_Capote_reasonably_small.jpeg http://posterous.com/users/hMLlrHnMS Julio Capote @capotej Julio Capote
Sun, 12 Oct 2008 17:41:00 -0700 Arrow key navigation for text fields http://juliocapote.com/post/54266325/arrow-key-navigation-for-text-fields http://juliocapote.com/post/54266325/arrow-key-navigation-for-text-fields

Here is a class for enabling the use of arrow keys to navigate through a grid of input fields: (using mootools)

var FocusMover = new Class({

        initialize: function(sel, col_num){

                this.sel = sel
                this.col_num = col_num
                this.inputs = $$(this.sel)
                this.current_focus = 0

                var self = this

                this.inputs.each(function(item, index){
                        item.addEvent('keydown',function(key){
                                $try(function(){
                                        self[key.key]()
                                })
                        })
                        item.addEvent('focus',function(e){
                                self.refresh(e)
                        })

                        item.set('myid', index)
                })

                this.inputs[0].focus()

        },


        refresh: function(e){
                this.current_focus = e.target.get('myid')
        },

        down: function(){
                i = parseInt(this.current_focus) + parseInt(this.col_num)
                this.inputs[i].focus()
        },

        up: function(){
                i = parseInt(this.current_focus) - parseInt(this.col_num)
                this.inputs[i].focus()
        },

        left: function(){
                i = parseInt(this.current_focus) - 1
                this.inputs[i].focus()
        },

        right: function(){
                i = parseInt(this.current_focus) + 1
                this.inputs[i].focus()
        }

})

As you can see, the constructor takes two arguments: a selector (which should return a list of all your input fields), and the number of input field columns. So for a 4x2 table, you would set it up like this:

var FM = new FocusMover('#mytable input', 4)

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/1332725/Julio_Capote_reasonably_small.jpeg http://posterous.com/users/hMLlrHnMS Julio Capote @capotej Julio Capote
Sat, 11 Oct 2008 01:54:00 -0700 Tabbing through fields vertically http://juliocapote.com/post/54058512/tabbing-through-fields-vertically http://juliocapote.com/post/54058512/tabbing-through-fields-vertically

Sometimes it’s useful to switch the browser’s default tabbing behavior (left to right) to the opposite (top to bottom) when your input fields are in a grid layout instead the of the usual single column layout. Having to do this manually is a real pain, especially for large grids; So here is a solution in javascript, using mootools:

window.addEvent('domready', function(){
    var trs = $$('#mytable tr')
    var accum = 0
    trs.each(function(tr, trindex){
        accum = trindex + 1
        tr.getChildren().each(function(td, tdindex){
            td.getChildren('input')[0].tabIndex = accum
            accum = accum + trs.length
        })            
    })
})

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/1332725/Julio_Capote_reasonably_small.jpeg http://posterous.com/users/hMLlrHnMS Julio Capote @capotej Julio Capote
Tue, 30 Sep 2008 10:26:00 -0700 Why MooTools (or Why not JQuery) http://juliocapote.com/post/52467447/why-mootools-or-why-not-jquery http://juliocapote.com/post/52467447/why-mootools-or-why-not-jquery

I’ve been toying around with MooTools a bit lately, and I’ve found the experience quite enjoyable and refreshing. Naturally, I twittered about it and went along my merry way. Moments later (and much to my surprise), I had a direct message from John Resig himself asking “Why, what’s wrong with jQuery?”. I was pretty taken aback that he would take time from his surely busy day to message a total stranger in an effort to improve his project or at least gain an insight in the everyday life of a js developer (it’s not like DHH would personally message people that are dumping rails to use merb). I figured he deserved a straight, honest answer; One that at least would be longer than 140 characters (even though I managed to use every single one). So it begs the question, Why MooTools?

  1. Class support.
    JQuery’s SQL-like syntax is fine for quick and dirty javascripting, but eventually you’ll want real classes to structure your UI logic.
  2. It smells, feels and tastes like regular javascript.
    JQuery doesn’t even look like javascript, which isn’t necessarily a bad thing, since that’s kind of their goal. MooTools however, feels like just an extension of the language (more on this at point #9).
  3. Faster.
    ‘Nuff Said EDIT: This was pointed out to be false; It is only faster in certain cases (such as mine, WebKit nightly on OS X).
  4. Robert Penner’s easing equations baked right in.
    This could just be me, but I find the animations that mootools creates are alot smoother than JQuery’s (especially the easing).
  5. Creating new DOM elements is a snap.
    Need to create a dom element? var el = new Element(‘a’, { ‘href’: ‘juliocapote.com’}); Done.
  6. Modular.
    I like that I can just build and pull down a moo.js that only contains the functionality I need.
  7. Better Documented.
    Or at least, its faster to find what you need.
  8. Easier to hack on and extend.
    While I haven’t personally delved into the internals of either system, the consensus seems to be that jquery is an unintelligible mess when it comes to modifying how it works.
  9. Prototype Approach (versus a namespaced approach)
    This is really just matter of preference; MooTools achieves it’s magic by just extending the prototypes of common objects (Array, String, etc); While this is obstrusive, it makes for shorter, more natural code. JQuery does its thing via a main object (which you can name, hence the namespace), that you wrap around whatever you want to make magical; This is unobstrusive, but you pay for that by having to wrap anything you want to use (which ends up being everything). It basically boils down to arr.each(fn) vs $.each(arr, fn)
  10. It’s not a revolution.
    It feels as if JQuery is trying to take on the world (it seems like it too, since its now included with visual studio and the nokia sdk). However, I’m not; I’m just trying to write some javascript here.

It’s not like I’m never going to use JQuery again; It simply isn’t my default js framework any longer.

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/1332725/Julio_Capote_reasonably_small.jpeg http://posterous.com/users/hMLlrHnMS Julio Capote @capotej Julio Capote
Mon, 29 Sep 2008 19:30:46 -0700 Untitled http://juliocapote.com/post/52369865/i-would-buy-and-frame-this http://juliocapote.com/post/52369865/i-would-buy-and-frame-this

Wi8e7cohkehgj6n97zlclljso1_500

I would buy and frame this.

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/1332725/Julio_Capote_reasonably_small.jpeg http://posterous.com/users/hMLlrHnMS Julio Capote @capotej Julio Capote
Sat, 27 Sep 2008 19:47:00 -0700 Highlight link based on current page in rails http://juliocapote.com/post/52081481/highlight-link-based-on-current-page-in-rails http://juliocapote.com/post/52081481/highlight-link-based-on-current-page-in-rails

This is common pattern in website navigation, where it highlights the link (usually by setting class=”active”) that took you to the current page while you are on that page.

First, define a helper:

def is_active?(page_name)
    "active" if params[:action] == page_name
  end

Then call it in your link_to’s in your layout as such:

link_to 'Home', '/', :class => is_active?("index")
link_to 'About', '/about', :class => is_active?("about")
link_to 'contact', '/contact', :class => is_active?("contact")

This effect is achieved due to how link_to handles being passed nil for its :class, so when is_active? returns nil (because its not the current page), link_to outputs nothing as its class (not class=”” as you might expect).

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/1332725/Julio_Capote_reasonably_small.jpeg http://posterous.com/users/hMLlrHnMS Julio Capote @capotej Julio Capote
Sat, 27 Sep 2008 14:20:00 -0700 Git Server tutorial http://juliocapote.com/post/52055667/git-server-tutorial http://juliocapote.com/post/52055667/git-server-tutorial

good git server tutorial, http://blog.commonthread.com/2008/4/14/setting-up-a-git-server

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/1332725/Julio_Capote_reasonably_small.jpeg http://posterous.com/users/hMLlrHnMS Julio Capote @capotej Julio Capote
Mon, 15 Sep 2008 12:10:04 -0700 No way. http://juliocapote.com/post/50284462/no-way http://juliocapote.com/post/50284462/no-way

I’m sure we’ve all been in a variant of this situation…

Me:  I'd like to talk to you about something...
Him: Let me guess - you want to use Smalltalk.
Me:  Er, no...
Him: Lisp?
Me: Right.
Him:  No way.

Taken from here

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/1332725/Julio_Capote_reasonably_small.jpeg http://posterous.com/users/hMLlrHnMS Julio Capote @capotej Julio Capote