Twitter4R on Rails

July 26th, 2007 by comment

Last night Twitter4R version 0.2.4 was released with a fix that makes using Twitter4R in Rails much easier.

So let’s quickly kick the tires to see how this all works:

  1. Install the Twitter4R v0.2.4 (or above) Ruby Gem: sudo gem install twitter4r
  2. Create a new rails application: rails twitter4rails
  3. After setting up your config/database.yml to your personal tastes and tweaking the Rails configuration settings in environment.rb, scroll to the bottom of environment.rb and add the following:

    gem(‘twitter4r’, ‘>=0.2.4′)
    require(‘twitter’) # loads core library
    require(‘twitter/console’) # loads a helper method we will use
    require(‘twitter/rails’) # added Rails extensions for Twitter4R

    module YourAppNamespace
    ENV["RAILS_ENV"] ||= “test” # assume test environment if no RAILS_ENV set.
    ClientContext = Twitter::Client.from_config(“#{RAILS_ROOT}/config/twitter.yml”, ENV["RAILS_ENV"])
    end

  4. Now in your controllers you can access YourAppNamespace::ClientContext object as you need to or any other part of the Twitter4R API.

If you still want more, feel free to check out the following links:

ˆ Back to top

will_paginate array?

July 23rd, 2007 by comment

Today I started putting pagination in the app that I have been working on. Based on recommendations from Obie I decided to use “will_paginate”, a rails plugin for pagination put out by the err the blog guys. It worked amazingly and the view helper was great! I really like the fact that I can apply the same look and feel to all page pagination throughout the app… well umm.. until I wanted to add pagination to a collection not generated from a finder or association. Since I really wanted everything to look the same and behave the same I did the following little trick so that you can call paginate on a plain old array.

class Array
  def paginate(page=1, per_page=15)
    pagination_array = WillPaginate::Collection.new(page, per_page, self.size)
    start_index = pagination_array.offset
    end_index = start_index + (per_page - 1)
    array_to_concat = self[start_index..end_index]
    array_to_concat.nil? ? [] : pagination_array.concat(array_to_concat)
  end
end

Before folks say anything about the above code.. yes I know it could be more concise if I didn’t use all the local variables but I wanted it to be really clear what I was doing here so.. leave it alone.

Now basically you can say

myarray.paginate(params[:page], per_page)

If you want to see it work yourself feel free to run this spec.


require File.dirname(__FILE__) + '/../spec_helper'

describe 'Given we call paginate on an array' do
  it 'should return an array containing the first 3 elements of the org array when page = 1 and per_page_count = 3' do
    array = ["a","b","c","d","e"]
    current_page = 1
    show_per_page = 3
    expected_array = ["a", "b", "c"]
    (array.paginate(current_page, show_per_page)).should == expected_array
  end

  it 'should return an array containing the last 2 elements of the org array when page = 2 and per_page_count = 3' do
    array = ["a","b","c","d","e"]
    current_page = 2
    show_per_page = 3
    expected_array = ["d", "e"]
    (array.paginate(current_page, show_per_page)).should == expected_array
  end

  it 'should return an array containing all the elements of the org array when page = 1 and per_page_count = 5' do
    array = ["a","b","c","d","e"]
    current_page = 1
    show_per_page = 5
    expected_array = ["a","b","c","d","e"]
    (array.paginate(current_page, show_per_page)).should == expected_array
  end

  it 'should return an array containing all the elements of the org array when page = 1 and per_page_count greater than org number of elements i.e = 6' do
    array = ["a","b","c","d","e"]
    current_page = 1
    show_per_page = 6
    expected_array = ["a","b","c","d","e"]
    (array.paginate(current_page, show_per_page)).should == expected_array
  end

  it 'should return an empty array if you ask for a page that does not exist' do
    array = ["a","b","c","d","e"]
    current_page = 3
    show_per_page = 5
    expected_array = []
    (array.paginate(current_page, show_per_page)).should == expected_array
  end

  it 'should return an empty array if you ask for a negative page number' do
    array = ["a","b","c","d","e"]
    current_page = -1
    show_per_page = 5
    expected_array = []
    (array.paginate(current_page, show_per_page)).should == expected_array
  end

  it 'should return an empty array if you ask for a negative per_page number' do
    array = ["a","b","c","d","e"]
    current_page = 1
    show_per_page = -5
    expected_array = []
    (array.paginate(current_page, show_per_page)).should == expected_array
  end
end
ˆ Back to top

FOSCON III: Really Radical Ruby

July 19th, 2007 by comment

The Portland Ruby Brigade is hosting our third annual evening of Ruby fun, next week during OSCON. FOSCON III will be held Tuesday, July 24th, at Holocene in SE Portland. I’ve helped plan this the last two years, and it’s a really great opportunity to learn more about Ruby and meet interesting people who work with it. There’s also still time to sign up for a lightning talk, if you have something you’d like to show off. Here’s the official announcement:

Once again the Portland Ruby Brigade will be hosting an evening of wide ranging talks about Ruby. This year the focus is on people doing strange things with Ruby. Strange, of course, is anything just a bit outside the expected. If you’ve created a new Ruby-based interface for hacking your brand new internet-enabled phone (rPhone anyone?) or composed your latest bit of metaprogramming magic, we’d love to hear about it.

Rather than having a fixed list of presenters, we’re keeping it open-ended and free form. We’ll use the lightning talk idea and give you each a 10 minute (give or take a few) slot to tell us what you can cram in. Make it especially interesting and we might even give you an extra minute or two. Let us know (send email to tlockney+foscon@gmail.com) in advance if you are interested in presenting or show up early on the 24th to sign up for a possible slot.

This will all be taking place on Tuesday, July 24th, 7:30PM at Holocene here in Portland, Oregon. This just happens to be the same week as O’Reilly’s Open Source Convention and the final day of Ubuntu Live. So if you’re in town for either of those or close enough to come join us anyway, you’re more then welcome.

There will be plenty of Ruby fun going on and lots of socializing with other people interested in Ruby. Last year, FOSCON II was overflowing. We’ve found a new venue to fit you all in, so why would you miss it?

ˆ Back to top

cheap research papers