/ api-example.rb
api-example.rb
1 #!/usr/bin/env ruby 2 3 # Copyright 2014 James Hilliard 4 # 5 # This program is free software; you can redistribute it and/or modify it under 6 # the terms of the GNU General Public License as published by the Free Software 7 # Foundation; either version 3 of the License, or (at your option) any later 8 # version. See COPYING for more details. 9 10 require 'socket' 11 require 'json' 12 13 api_command = ARGV[0].split(":") 14 15 if ARGV.length == 3 16 api_ip = ARGV[1] 17 api_port = ARGV[2] 18 elsif ARGV.length == 2 19 api_ip = ARGV[1] 20 api_port = 4028 21 else 22 api_ip = "127.0.0.1" 23 api_port = 4028 24 end 25 26 s = TCPSocket.open(api_ip, api_port) 27 28 if api_command.count == 2 29 s.write({ :command => api_command[0], :parameter => api_command[1]}.to_json) 30 else 31 s.write({ :command => api_command[0]}.to_json) 32 end 33 34 response = s.read.strip 35 response = JSON.parse(response) 36 37 puts response 38 s.close