Class: Irc::Bot::WebMessage

Inherits:
Object show all
Defined in:
/home/apoc/projects/ruby/rbot/lib/rbot/core/webservice.rb

Overview

A WebMessage is a web request and response object combined with helper methods.

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (WebMessage) initialize(bot, req, res)

Returns a new instance of WebMessage



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File '/home/apoc/projects/ruby/rbot/lib/rbot/core/webservice.rb', line 53

def initialize(bot, req, res)
  @bot = bot
  @req = req
  @res = res

  @method = req.request_method
  @post = {}
  if req.body and not req.body.empty?
    @post = parse_query(req.body)
  end
  @args = {}
  if req.query_string and not req.query_string.empty?
    @args = parse_query(req.query_string)
  end
  @client = req.peeraddr[3]

  # login a botuser with http authentication
  WEBrick::HTTPAuth.basic_auth(req, res, 'RBotAuth') { |username, password|
    if username
      botuser = @bot.auth.get_botuser(Auth::BotUser.sanitize_username(username))
      if botuser and botuser.password == password
        @source = botuser
        true
      else
        false
      end
    else
      true # no need to request auth at this point
    end
  }

  @path = req.path
  debug '@path = ' + @path.inspect
end

Instance Attribute Details

- (Object) args (readonly)

Parsed url parameters.



43
44
45
# File '/home/apoc/projects/ruby/rbot/lib/rbot/core/webservice.rb', line 43

def args
  @args
end

- (Object) bot (readonly)

Bot instance



28
29
30
# File '/home/apoc/projects/ruby/rbot/lib/rbot/core/webservice.rb', line 28

def bot
  @bot
end

- (Object) client (readonly)

Client IP.



46
47
48
# File '/home/apoc/projects/ruby/rbot/lib/rbot/core/webservice.rb', line 46

def client
  @client
end

- (Object) method (readonly)

HTTP method (POST, GET, etc.)



31
32
33
# File '/home/apoc/projects/ruby/rbot/lib/rbot/core/webservice.rb', line 31

def method
  @method
end

- (Object) path (readonly)

URL Path.



49
50
51
# File '/home/apoc/projects/ruby/rbot/lib/rbot/core/webservice.rb', line 49

def path
  @path
end

- (Object) post (readonly)

Parsed post request parameters.



40
41
42
# File '/home/apoc/projects/ruby/rbot/lib/rbot/core/webservice.rb', line 40

def post
  @post
end

- (Object) req (readonly)

Request object, a instance of WEBrick::HTTPRequest (docs)



34
35
36
# File '/home/apoc/projects/ruby/rbot/lib/rbot/core/webservice.rb', line 34

def req
  @req
end

- (Object) res (readonly)

Response object, a instance of WEBrick::HTTPResponse (docs)



37
38
39
# File '/home/apoc/projects/ruby/rbot/lib/rbot/core/webservice.rb', line 37

def res
  @res
end

- (Object) source (readonly)

The bot user issuing the command.



52
53
54
# File '/home/apoc/projects/ruby/rbot/lib/rbot/core/webservice.rb', line 52

def source
  @source
end

Instance Method Details

- (Object) parse_query(query)



88
89
90
91
92
93
94
# File '/home/apoc/projects/ruby/rbot/lib/rbot/core/webservice.rb', line 88

def parse_query(query)
  params = CGI::parse(query)
  params.each_pair do |key, val|
    params[key] = val.last
  end
  params
end

- (Boolean) private?

Remote messages are always 'private'

Returns:

  • (Boolean)


102
103
104
# File '/home/apoc/projects/ruby/rbot/lib/rbot/core/webservice.rb', line 102

def private?
  true
end

- (Object) send_html(body, status = 200)

Sends a html response



124
125
126
# File '/home/apoc/projects/ruby/rbot/lib/rbot/core/webservice.rb', line 124

def send_html(body, status=200)
  send_response(body, status, 'text/html')
end

- (Object) send_json(body, status = 200)

Sends a json response



119
120
121
# File '/home/apoc/projects/ruby/rbot/lib/rbot/core/webservice.rb', line 119

def send_json(body, status=200)
  send_response(body, status, 'application/json')
end

- (Object) send_plaintext(body, status = 200)

Sends a plaintext response



114
115
116
# File '/home/apoc/projects/ruby/rbot/lib/rbot/core/webservice.rb', line 114

def send_plaintext(body, status=200)
  send_response(body, status, 'text/plain')
end

- (Object) send_response(body, status, type)

Sends a response with the specified body, status and content type.



107
108
109
110
111
# File '/home/apoc/projects/ruby/rbot/lib/rbot/core/webservice.rb', line 107

def send_response(body, status, type)
  @res.status = status
  @res['Content-Type'] = type
  @res.body = body
end

- (Object) target

The target of a RemoteMessage



97
98
99
# File '/home/apoc/projects/ruby/rbot/lib/rbot/core/webservice.rb', line 97

def target
  @bot
end