Sha256: 6624b8e46750c2c89dafb7f239c2045de4a56c94534a2eb68b714e1fd3935d9e

Contents?: true

Size: 1.33 KB

Versions: 1

Compression:

Stored size: 1.33 KB

Contents

require 'net/http'
require "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.4 lib/converse/comms/html/html_conversation.rb