From b68f6c759d70388c99f2aaa9ff6e8869274210ea Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Fri, 18 Nov 2011 08:47:02 -0500 Subject: [PATCH] Working through chapter 16, though skipping past less-interesting exercises 1 and 2 --- .gitignore | 1 + cookbook/016/03.rb | 12 ++++++++++++ cookbook/016/03b.rb | 25 +++++++++++++++++++++++++ 3 files changed, 38 insertions(+) create mode 100644 cookbook/016/03.rb create mode 100644 cookbook/016/03b.rb diff --git a/.gitignore b/.gitignore index f590948..f13477d 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ koans/.path_progress *.sqlite3 .rbenv-version +*.rbc diff --git a/cookbook/016/03.rb b/cookbook/016/03.rb new file mode 100644 index 0000000..456529f --- /dev/null +++ b/cookbook/016/03.rb @@ -0,0 +1,12 @@ +require 'xmlrpc/client' + +server = XMLRPC::Client.new2('http://betty.userland.com/RPC2') +puts server.call('examples.getStateName', 5) + + +begin + server.call('noSuchMethod') +rescue XMLRPC::FaultException => e + puts "Error: fault code #{e.faultCode}" + puts e.faultString +end diff --git a/cookbook/016/03b.rb b/cookbook/016/03b.rb new file mode 100644 index 0000000..09a9ffd --- /dev/null +++ b/cookbook/016/03b.rb @@ -0,0 +1,25 @@ +require 'xmlrpc/client' + + +def lookup_upc(upc, rpc_key) + server = XMLRPC::Client.new2('http://www.upcdatabase.com/xmlrpc') + begin + response = server.call('lookup', :upc => upc, :rpc_key => rpc_key) + return response['found'] ? response : nil + rescue XMLRPC::FaultException => e + puts "Error: " + puts e.faultCode + puts e.faultString + end +end + + +['018787765654', 'no such UPC'].each do |upc| + product = lookup_upc(upc, ARGV.first.strip) + if product + puts product['description'] + puts product['size'] + else + puts "Oh no! product with upc='#{upc}' is #{product.inspect}" + end +end