api-example.rb 846 B

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