Software engineering notes

1st attempt at a Ruby YQL utility function

I use YQL a lot and I find myself writing query = “select * from …”, passing the query to the YQL webservice, and then JSON-parsing the response, repeat … I searched (briefly) for a Ruby gem for YQL, but couldn’t find one, so I made (a very basic) function to perform the actions listed above.

Drop the code below in a file and run it


require 'net/http'
require 'rubygems'
require 'json'

def yql(query)
  uri = ""

  # everything's requested via POST, which is all I needed when I wrote this
  # likewise, everything coming back is json encoded
  response = Net::HTTP.post_form( URI.parse( uri ), {
    'q' => query,
    'format' => 'json'
  } )

  json = JSON.parse( response.body )
  return json

Written by Erik

February 18, 2010 at 1:08 am

Posted in code

Tagged with ,

2 Responses

Subscribe to comments with RSS.

  1. […] a comment » I can’t tell whether it’s YQL, Ruby, or my lil’ YQL utility function, but I’m having fun. Here’s an example of […]

  2. […] class is based on the SQLite utility class, YQL utility function, and simple key/val layer for YQL storage I recently posted.  The methods available are use(), […]

Comments are closed.

%d bloggers like this: