Sha256: 1856fc956078803e2f724d2a1393bac02ed1e0614387658db301415a94f5af2a

Contents?: true

Size: 1.34 KB

Versions: 1

Compression:

Stored size: 1.34 KB

Contents

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

module Converse
  class HTMLConversation < Conversation
    attr_accessor :use_ssl

    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 "hetzner_api", "secret"
      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,  data = nil)
      @request = Net::HTTP::Get.new(path)
      converse(path, data)
    end

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
converse-1.0.5 lib/converse/comms/html/html_conversation.rb