Sha256: dad87c57b622d2833712499e97ba846e8e163e002012d879c2caba1a0a4a6cd1

Contents?: true

Size: 1012 Bytes

Versions: 4

Compression:

Stored size: 1012 Bytes

Contents

require 'net/https'
require 'cgi'

module CanControlsGateway

  class RcaCommand 

    attr_reader :arguments, :target, :rci_uri

    def initialize rci_uri, target, arguments={}
      @rci_uri   = rci_uri
      @target    = target
      @arguments = prepare_command_arguments arguments
    end

    def send_command
      post_data = "target=#{@target}&arguments=#{@arguments}"
      url       = URI.parse(@rci_uri)
      request   = Net::HTTP::Post.new(url.path)
      begin
        response = Net::HTTP::start(url.host, url.port) { |http|
                     http.request(request, post_data)
                 }
        return response.code.to_i, response.body
      rescue Exception => e
        return 500, "#{e}"
      end
    end

    private

      def prepare_command_arguments arguments
        my_arguments_list = []
        arguments.keys.sort.each do |key|
          my_arguments_list << "#{key}=#{arguments[key]}"
        end
        CGI::escape(my_arguments_list.join("&"))
      end
  
  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
tg-can-controls-gateway-0.1.3 lib/rca_command.rb
tg-can-controls-gateway-0.1.2 lib/rca_command.rb
tg-can-controls-gateway-0.1.1 lib/rca_command.rb
tg-can-controls-gateway-0.1.0 lib/rca_command.rb