From 5303b8aea74c7757daaa6c46ac5ef9d4c190e973 Mon Sep 17 00:00:00 2001 From: Neville Kadwa Date: Thu, 16 Aug 2012 22:44:09 -0400 Subject: [PATCH] Add command line executable for call and sms. --- bin/googlevoice | 13 +++++ lib/googlevoiceapi/cli.rb | 89 +++++++++++++++++++++++++++++++++++ lib/googlevoiceapi/version.rb | 2 +- 3 files changed, 103 insertions(+), 1 deletion(-) create mode 100755 bin/googlevoice create mode 100644 lib/googlevoiceapi/cli.rb diff --git a/bin/googlevoice b/bin/googlevoice new file mode 100755 index 0000000..4bc01d4 --- /dev/null +++ b/bin/googlevoice @@ -0,0 +1,13 @@ +#!/usr/bin/env ruby +# encoding: UTF-8 + +# resolve bin path, ignoring symlinks +require 'pathname' +bin_file = Pathname.new(__FILE__).realpath + +# add self to libpath +$:.unshift File.expand_path('../../lib', bin_file) + +# start up the CLI +require 'googlevoiceapi/cli' +GoogleVoice::CLI.new(ARGV).run diff --git a/lib/googlevoiceapi/cli.rb b/lib/googlevoiceapi/cli.rb new file mode 100644 index 0000000..0846166 --- /dev/null +++ b/lib/googlevoiceapi/cli.rb @@ -0,0 +1,89 @@ +require 'googlevoiceapi' +require 'optparse' + +module GoogleVoice + class CLI + + def initialize(args) + @args = args + end + + def run + options = {} + + optparse = OptionParser.new do |opts| + + opts.on('-u U', '--username U', 'Account username (optional)') do |username| + options[:username] = username + end + + opts.on('--call', 'Connect a call: [number from] [number to]') do + options[:call] = true + end + + opts.on('--sms', 'Send SMS message: [number to]') do + options[:sms] = true + end + + opts.on('--smstext T', 'SMS message text (Optional)') do |smstext| + options[:smstext] = smstext + end + + opts.on('-h', '--help', 'Print help') do + STDERR.puts optparse + exit 1 + end + + end + + optparse.parse!(@args) + + if options[:help] + STDERR.puts optparse + exit 1 + end + + unless options[:call] || options[:get_number] || options[:sms] + STDERR.puts "Missing command: [call, sms]" + STDERR.puts optparse + exit 1 + end + + unless options[:username] + print "Username: " + options[:username] = STDIN.gets.chomp; + end + + print "Password: " + begin + system "stty -echo" + options[:password] = STDIN.gets.chomp; puts "\n" + ensure + system "stty echo" + end + + gv = GoogleVoice::Api.new(options[:username], options[:password]) + if options[:call] + if @args.length != 2 + STDERR.puts "Invalid call parameters: [number from] [number to]" + STDERR.puts optparse + exit 1 + end + gv.call(@args[0], @args[1]) + elsif options[:sms] + unless options[:smstext] + print "SMS Text: " + options[:smstext] = STDIN.gets.chomp! + end + if @args.length != 1 + STDERR.puts "Invalid sms parameter: [number to]" + STDERR.puts optparse + exit 1 + end + gv.sms(@args[0], options[:smstext]) + end + + end + + end +end diff --git a/lib/googlevoiceapi/version.rb b/lib/googlevoiceapi/version.rb index b777e2e..056fedc 100644 --- a/lib/googlevoiceapi/version.rb +++ b/lib/googlevoiceapi/version.rb @@ -1,3 +1,3 @@ module GoogleVoice - VERSION = "0.1.7" + VERSION = "0.2.0" end