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]

Convert between XML, Hash, YAML, JSON in Ruby - Conversion Cheat Sheet

Ruby, Uncategorized 6 Comments »

Here’s a little XML/JSON/YAML/Hash conversion cheat sheet for Ruby:

First, let’s create an XML document:

require ‘rubygems’

require ‘nokogiri’

builder = Nokogiri::XML::Builder.new do |xml|

    xml.root {
      xml.products {
        xml.widget {
          xml.id_ “10″
          xml.name “Awesome widget”
        }
      }
    }
  end

my_xml = builder.to_xml

XML To Hash:

require ‘active_support’ #if you have Rails installed

my_hash = Hash.from_xml(my_xml)

Without Rails/ActiveSupport, have a look at Crack which very fast and will usually give you enough fields. If you have attributes and a text value in the same node however (<person age=”10″>joe</person), you will only get the value back, not the attribute. Update (again): For text nodes (any node that contains a string), crack will return an attributes hash in addition to the text content.

my_hash = Crack::XML.parse(my_xml)

Hash To Object?

Have a look here: http://blog.jayfields.com/2008/01/ruby-hashtomod.html

Hash To JSON:

require ‘json’

my_json = my_hash.to_json

JSON back to Hash:

my_hash = JSON.parse(my_json)

Also have a look at Crack:

my_hash = Crack::JSON.parse(my_json)

Hash To YAML:

my_yaml = my_hash.to_yaml

YAML back to Hash:

my_hash =  YAML::load(my_yaml)

Bonus Points –  Hash to XML:

require ‘xmlsimple’

my_xml = XmlSimple.xml_out(my_hash, {’KeepRoot’ => true})

There is currently no way to preserve the attributes (like <person age=”10″>Joe</person>) with such conversion from Hash to XML.

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

Back to BlogJet as My Blog Editor on Linux

Blogging Software, Linux No Comments »

Having tried Bilbo/Blogilo for a while now, I found it to be a fine editor but a bit too intrusive in altering the HTML code I created in code view when I switched back to Bilbo’s WYSIWYG view.

So I tried good old BlogJet again and found there’s a (free) upgrade to the new version 2.5 (currently 2.5.0.15), among other goodies making it Unicode compatible and letting you create categories from within the editor (only for Wordpress blogs), still a rare feature outside of Live Writer.

To install it, proceed just like with the prior versions, as outlined here. You have to open the downloaded installer (BlogJetSetup.2.x.x.x.exe) from the file menu in IEs4Linux and proceed. Simply clicking the downloaded installer will give you an installation without necessary HTML components and it won’t display any content.

[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]

CSS Specificity Algorithm Table from Evan Sharp’s Mix 2010 talk

CSS No Comments »

Having watched Evan Sharp’s “Comp to Code” talk at Mix 2010 video (check out the other Mix videos also, most are excellent), searching the web for the CSS specificity algorithm table turned up nothing as simple as his pseudo-decimal algorithm. So I reproduced the table from the talk verbatim here for reference. As you can see, id selector elements will dramatically increase the specificity of the selector, while class selector elements are an order of magitude less specific:

CSS specificity algorithm table

selector

id

class

element

sum

#logo

1 0 0 100

.header .nav li

0 2 1 21

.header h1#logo

1 1 1 111

div div * a:hover

0 0 4 4

#nav img#logo

2 0 1 201

Exercise for the reader: Say “specificity” three times fast. Extra points: Say “specificity algorithm” three times fast.

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