From 783e34dbc519345fd08c882c9f44cb76ca792f86 Mon Sep 17 00:00:00 2001 From: Khem Date: Wed, 20 Jan 2021 12:22:12 -0500 Subject: [PATCH] Add another example --- examples/twitter.rb | 42 ++++++++++++++++++++++++++++++++++++++++++ examples/yql.rb | 2 +- 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 examples/twitter.rb diff --git a/examples/twitter.rb b/examples/twitter.rb new file mode 100644 index 00000000..e10eb422 --- /dev/null +++ b/examples/twitter.rb @@ -0,0 +1,42 @@ +#!/usr/bin/env ruby -r rubygems +# +# ./twitter.rb --consumer-key --consumer-secret + +require 'oauth' +require 'optparse' +require 'json' +require 'pp' + +options = {} + +option_parser = OptionParser.new do |opts| + opts.banner = "Usage: #{$0} [options] " + + 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 => "https://api.twitter.com" + +access_token = OAuth::AccessToken.new(consumer) + +response = access_token.request(:get, "/1.1/statuses/show/#{OAuth::Helper.escape(query)}.json") +rsp = JSON.parse(response.body) +pp rsp diff --git a/examples/yql.rb b/examples/yql.rb index 41bb1f21..c9c6e343 100755 --- a/examples/yql.rb +++ b/examples/yql.rb @@ -1,4 +1,4 @@ -#!/usr/bin/env ruby -rubygems +#!/usr/bin/env ruby -r rubygems # Sample queries: # ./yql.rb --consumer-key --consumer-secret "show tables"