Commit fe837b14241b3504896be65ad62e4cace935d1c4

James Hilliard 2014-05-23T17:00:26

Add Ruby Api Example

diff --git a/api-example.rb b/api-example.rb
new file mode 100644
index 0000000..28c4402
--- /dev/null
+++ b/api-example.rb
@@ -0,0 +1,38 @@
+#!/usr/bin/env ruby
+
+# Copyright 2014 James Hilliard
+#
+# This program is free software; you can redistribute it and/or modify it under
+# the terms of the GNU General Public License as published by the Free Software
+# Foundation; either version 3 of the License, or (at your option) any later
+# version.  See COPYING for more details.
+
+require 'socket'
+require 'json'
+
+api_command = ARGV[0].split(":")
+
+if ARGV.length == 3
+	api_ip = ARGV[1]
+	api_port = ARGV[2]
+elsif ARGV.length == 2
+	api_ip = ARGV[1]
+	api_port = 4028
+else
+	api_ip = "127.0.0.1"
+	api_port = 4028
+end
+
+s = TCPSocket.open(api_ip, api_port)
+
+if api_command.count == 2
+	s.write({ :command => api_command[0], :parameter => api_command[1]}.to_json)
+else
+	s.write({ :command => api_command[0]}.to_json)
+end
+
+response = s.read.strip
+response = JSON.parse(response)
+
+puts response
+s.close