Class: Converse::HTMLConversation

Inherits:
Conversation show all
Defined in:
lib/converse/comms/html/html_conversation.rb

Instance Attribute Summary collapse

Attributes inherited from Conversation

#body, #connection, #headers, #host, #path, #port, #request, #response, #subscribers, #uri

Instance Method Summary collapse

Methods inherited from Conversation

#notify_subscribers, #subscribe

Constructor Details

#initialize(uri) ⇒ HTMLConversation

Returns a new instance of HTMLConversation.



10
11
12
13
14
# File 'lib/converse/comms/html/html_conversation.rb', line 10

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

Instance Attribute Details

#passwordObject

Returns the value of attribute password.



8
9
10
# File 'lib/converse/comms/html/html_conversation.rb', line 8

def password
  @password
end

#use_sslObject

Returns the value of attribute use_ssl.



6
7
8
# File 'lib/converse/comms/html/html_conversation.rb', line 6

def use_ssl
  @use_ssl
end

#usernameObject

Returns the value of attribute username.



7
8
9
# File 'lib/converse/comms/html/html_conversation.rb', line 7

def username
  @username
end

Instance Method Details

#ask(path = @path, data = nil) ⇒ Object



50
51
52
53
# File 'lib/converse/comms/html/html_conversation.rb', line 50

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

#connectObject



42
43
44
45
46
47
48
# File 'lib/converse/comms/html/html_conversation.rb', line 42

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

#converse(path, data = nil) ⇒ Object



35
36
37
38
39
40
# File 'lib/converse/comms/html/html_conversation.rb', line 35

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

#notify_subscribers_of_request(path) ⇒ Object



16
17
18
19
20
21
# File 'lib/converse/comms/html/html_conversation.rb', line 16

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

#notify_subscribers_of_responseObject



23
24
25
26
27
# File 'lib/converse/comms/html/html_conversation.rb', line 23

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

#populate_request(path, data) ⇒ Object



29
30
31
32
33
# File 'lib/converse/comms/html/html_conversation.rb', line 29

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

#say(path = @path, data = nil) ⇒ Object



55
56
57
58
# File 'lib/converse/comms/html/html_conversation.rb', line 55

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