Quickstart with YQL and HTTParty with Rails

Ruby on Rails, YQL 1 Comment »

YQL makes it super-easy to consume data in your web app through a unified SQL-like API. And HTTParty by John Nunemakers makes HTTP really easy. A perfect match.

In environment.rb:

config.gem ‘httparty’

Now, just create a model. Be sure to use the public endpoint for now, as the others need oauth authentication.

class News

  include HTTParty
  base_uri  ‘http://query.yahooapis.com/v1/public/yql’
  
  def self.new_york_news
    self.get(”", :query => {:q => ’select title, abstract, url from search.news where query = “%New York%”‘,
        :format => ‘json’

      })
  end

  def self.new_york_news_hash
    self.new_york_news.parsed_response["query"]["results"]["result"]
  end
 
end

In you controller:

class NewsController < ApplicationController

  def index
    @news = News.new_york_news_hash
  end

end

And your view (for example):

<h1>New York News</h1>
<% @news.each do |news_item| %>
  <% content_tag :h2 do %>
    <%= link_to news_item["title"], news_item["url"]  %>
    <% end %>
    <% content_tag :div do  %>
      <% content_tag :p do %>
        <%= news_item["abstract"]  %>
      <% end %>
    <% end %>
  <%  end %>

Be sure to check the API docs of YQL for many other options and watch the excellent screencasts by Christian Heilmann in the YUI theater and elsewhere. The possibilities are endless.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Clearing Memcached at Capistrano Deployment

Capistrano, Ruby on Rails No Comments »

It’s a two step process:

1. Create a rake task (in lib/tasks/clear_memcached.rake for example):

namespace :cache do
  desc ‘Clear memcache’
  task :clear => :environment do
    ActionController::Base.cache_store.clear
  end
end

2. In deploy.rb add the following:

namespace :cache do
  desc “Clear memcach after deployment”
  task :clear, :roles => :app do
    run “cd #{current_release} && rake cache:clear RAILS_ENV=production”
  end
end

and (depending on your task chain):

after “deploy:update_code”, “cache:clear”

Hope it helps.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

odd number of arguments for Hash (ActionView::TemplateError)

Ruby on Rails 1 Comment »

This error can be caused by an outdated version of the globalize2 plugin, so try

 script/plugin remove globalize2

and

script/plugin install git://github.com/joshmh/globalize2.git

Hope it helps.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

RSpec and Webrat for Rails Integration Testing

RSpec, Ruby on Rails, Webrat No Comments »

Getting RSpec and Webrat to cooperate has gotten a bit easier recently, so make sure you have all the latest versions of the webrat, rspec and rspec-rails gems installed. Some quirks remain which might trip you up coming from TestUnit. For example if you

cd spec/integration

and

spec ./user_integration_spec.rb

or

ruby ./user_integration_spec.rb

you might get

undefined method `visit (…)

while a similar procedure would run fine in test/integration with TestUnit

But with RSpec, when you run

spec ./spec/integration/user_integration_spec.rb

or

rake spec:integration

all is well. It seems you have to stand at least in the spec directory or above, otherwise the block

Webrat.configure do |config|
  config.mode = :rails
  config.open_error_files = false # prevents webrat from opening the browser
end

in spec_helper.rb will not run correctly.

So remember that RSpec is a bit less forgiving than TestUnit in how your tests are called.

It’s also important that you run

script/generate rspec

after installing RSpec to have the rake tasks “installed”.

Finally, his procedure will likely not be necessary with recent versions of Webrat and RSpec.

See here for a testing system for Rails that integrates RSpec and Webrat, it makes a  lot of sense.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Using routing_filter with Devise

Ruby on Rails No Comments »

Having used Sven Fuchs’ routing_filter to localize routes (see here for alternative solutions), I now moved my authentication to devise from authlogic. This broke my localized routes with the default configuration, so for example

http://localhost:3000/en/users/sign_in

failed with an

ActionController::UnknownAction

error.

Fixing it was super-easy, simply open your config/initializers/devise.rb file and uncomment (currently line 81)

config.use_default_scope = true

and it should work like before.

Note that this works only with Rails 2.+, not with Rails 3 where it’s probably better to use a scope block to prepend locales to you routes, instead of the routing_filter plugin.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]
WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Log in