Sha256: 4834a50e8dc2e07ad1544a6269a07e1677bbdb105ff13fbe292337a50f5dc9e3

Contents?: true

Size: 1.23 KB

Versions: 3

Compression:

Stored size: 1.23 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, service, parameters={})
        request_uri = command(command_name, service, parameters)
        puts "[debug] Sending request to #{request_uri}" if @debug
        get_response(request_uri).first
      end
      
      protected
        def command(command_name, service, parameters) #:nodoc:
          Command.new(command_name, service, :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

3 entries across 3 versions & 2 rubygems

Version Path
commonthread-clickatell-0.5.1 lib/clickatell/api/command_executor.rb
commonthread-clickatell-0.5.2 lib/clickatell/api/command_executor.rb
clickatell-0.5.0 lib/clickatell/api/command_executor.rb