Sha256: 9e4bfc460d8d233bcdc72cb3e5665b12c71e18df5ed308946ea338455c13e070

Contents?: true

Size: 1.2 KB

Versions: 2

Compression:

Stored size: 1.2 KB

Contents

require 'net/http'

module Sikulirc
  module Command
    
    COMMANDS = {}
    
    def self.load
      Dir.foreach(File.expand_path("../commands/", __FILE__)) do |file_name|
        if file_name == ".." or file_name == "."
          next
        end
        cmd = {}
        File.open(File.expand_path("../commands/#{file_name}", __FILE__)).each_with_index do |line, index|
          cmd["cmd#{index}"] = line.tr("\n", '')
        end
        COMMANDS[file_name] = cmd
      end
    end
    
    load
    
    def execute_command(uri, cmd_name, args_hash=nil, &block)
      req = Net::HTTP::Post.new(uri.path)
      req.set_form_data(args_hash.nil? ? {"type"=> "sikuli"}.merge(COMMANDS.fetch(cmd_name)) : {"type"=> "sikuli"}.merge(COMMANDS.fetch(cmd_name)).merge(build_arg(args_hash)))
      res = Net::HTTP.start(uri.hostname, uri.port) do |http|
        http.request(req)
      end
      if block_given?
        yield res.body
      else
        res.body
      end
    end
    
    private
    
    def build_arg(args_hash)
      arg = Hash['args' => '|']
      args_hash.each do |k, v|
        arg['args'] = arg['args'] + "#{k}=#{v}|"
      end
      arg
    end
    
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sikulinewrc-0.0.3 lib/sikulirc/command.rb
sikulinewrc-0.0.1 lib/sikulirc/command.rb