File: yql.rb

package info (click to toggle)
ruby-oauth 0.5.4-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 584 kB
  • sloc: ruby: 4,070; makefile: 4
file content (44 lines) | stat: -rwxr-xr-x 1,168 bytes parent folder | download | duplicates (5)
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/env ruby -rubygems

# Sample queries:
#  ./yql.rb --consumer-key <key> --consumer-secret <secret> "show tables"
#  ./yql.rb --consumer-key <key> --consumer-secret <secret> "select * from flickr.photos.search where text='Cat' limit 10"

require 'oauth'
require 'optparse'
require 'json'
require 'pp'

options = {}

option_parser = OptionParser.new do |opts|
  opts.banner = "Usage: #{$0} [options] <query>"

  opts.on("--consumer-key KEY", "Specifies the consumer key to use.") do |v|
    options[:consumer_key] = v
  end

  opts.on("--consumer-secret SECRET", "Specifies the consumer secret to use.") do |v|
    options[:consumer_secret] = v
  end
end

option_parser.parse!
query = ARGV.pop
query = STDIN.read if query == "-"

if options[:consumer_key].nil? || options[:consumer_secret].nil? || query.nil?
  puts option_parser.help
  exit 1
end

consumer = OAuth::Consumer.new \
  options[:consumer_key],
  options[:consumer_secret],
  :site => "http://query.yahooapis.com"

access_token = OAuth::AccessToken.new(consumer)

response = access_token.request(:get, "/v1/yql?q=#{OAuth::Helper.escape(query)}&format=json")
rsp = JSON.parse(response.body)
pp rsp