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.



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

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.



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

def password
  @password
end

#use_sslObject

Returns the value of attribute use_ssl.



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

def use_ssl
  @use_ssl
end

#usernameObject

Returns the value of attribute username.



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

def username
  @username
end

Instance Method Details

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



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

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

#connectObject



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

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



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

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



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

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

#notify_subscribers_of_responseObject



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

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

#populate_request(path, data) ⇒ Object



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

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



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

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