Sha256: 85316fccde11c64592a3e2e660735497b86363786edbd54d29802dd4cc48377e

Contents?: true

Size: 1.2 KB

Versions: 1

Compression:

Stored size: 1.2 KB

Contents

require 'net/http'
require 'net/https'

module Clickatell
  class API
   
    # Used to run commands agains the Clickatell gateway.
    class CommandExecutor
      def initialize(authentication_hash, secure=false, debug=false)
        @authentication_hash = authentication_hash
        @debug = debug
        @secure = secure
      end
      
      # Builds a command object and sends it using HTTP GET. 
      # Will output URLs as they are requested to stdout when 
      # debugging is enabled.
      def execute(command_name, parameters={})
        request_uri = command(command_name, parameters)
        puts "[debug] Sending request to #{request_uri}" if @debug
        get_response(request_uri).first
      end
      
      protected
        def command(command_name, parameters) #:nodoc:
          Command.new(command_name, :secure => @secure).with_params(
            parameters.merge(@authentication_hash)
          )
        end
        
        def get_response(uri)
          http = Net::HTTP.new(uri.host, uri.port)
          http.use_ssl = (uri.scheme == 'https')
          http.start do |http|
            resp, body = http.get([uri.path, uri.query].join('?'))
          end
        end
    end 
  
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
clickatell-0.4.1 lib/clickatell/api/command_executor.rb