Sha256: d05f1521990bda44e5b4fa57888f8943efbb30eb28f3457a816481eeca5b5ea5

Contents?: true

Size: 1.45 KB

Versions: 14

Compression:

Stored size: 1.45 KB

Contents

require 'net/http'
require "converse/comms/conversation"
require "json"

module Converse
  class HTMLConversation < Conversation
    attr_accessor :use_ssl
    attr_accessor :username
    attr_accessor :password

    def initialize(uri)
      super(uri)
      @use_ssl = false
      @port = @use_ssl ? 443 : 80 if @port == nil
    end

    def notify_subscribers_of_request(path)
      output = "HTTP=>\n"
      output += "#{path}\n"
      output += "#{@request.body}\n"
      notify_subscribers(output)
    end

    def notify_subscribers_of_response
      output = "<=HTTP\n"
      output += "#{@response.body}\n"
      notify_subscribers(output)
    end

    def populate_request(path, data)
      @request.body = data if not data.nil?
      @request.basic_auth @username, @password if @username != nil or @password != nil
      notify_subscribers_of_request(path)
    end

    def converse(path, data = nil)
      populate_request(path, data)
      @response = connect.request @request
      notify_subscribers_of_response
      return @response
    end

    def connect
      if (@use_ssl)
        Net::HTTP.start(@host, @port, :use_ssl => @use_ssl ? "yes" : "no")
      else
        Net::HTTP.start(@host, @port)
      end
    end

    def ask(path = @path,  data = nil)
      @request = Net::HTTP::Get.new(path)
      converse(path, data)
    end

    def say(path = @path,  data = nil)
      @request = Net::HTTP::Post.new(path)
      converse(path, data)
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
converse-1.0.25 lib/converse/comms/html/html_conversation.rb
converse-1.0.24 lib/converse/comms/html/html_conversation.rb
converse-1.0.23 lib/converse/comms/html/html_conversation.rb
converse-1.0.22 lib/converse/comms/html/html_conversation.rb
converse-1.0.21 lib/converse/comms/html/html_conversation.rb
converse-1.0.20 lib/converse/comms/html/html_conversation.rb
converse-1.0.19 lib/converse/comms/html/html_conversation.rb
converse-1.0.18 lib/converse/comms/html/html_conversation.rb
converse-1.0.17 lib/converse/comms/html/html_conversation.rb
converse-1.0.16 lib/converse/comms/html/html_conversation.rb
converse-1.0.15 lib/converse/comms/html/html_conversation.rb
converse-1.0.12 lib/converse/comms/html/html_conversation.rb
converse-1.0.11 lib/converse/comms/html/html_conversation.rb
converse-1.0.10 lib/converse/comms/html/html_conversation.rb