Module: Ikachan

Defined in:
lib/ikachan.rb,
lib/ikachan/version.rb

Constant Summary collapse

VERSION =
"0.0.2"

Class Method Summary collapse

Class Method Details

.joinObject



12
13
14
# File 'lib/ikachan.rb', line 12

def join
  request('/join', { 'channel' => @@channel })
end

.loggerObject



48
49
50
# File 'lib/ikachan.rb', line 48

def logger
  @@_logger ||= Rails.logger
end

.notice(message) ⇒ Object



16
17
18
19
# File 'lib/ikachan.rb', line 16

def notice(message)
  join
  request('/notice', { 'channel' => @@channel, 'message' => message })
end

.privmsg(message) ⇒ Object



21
22
23
24
# File 'lib/ikachan.rb', line 21

def privmsg(message)
  join
  request('/privmsg', { 'channel' => @@channel, 'message' => message })
end

.request(path, params) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ikachan.rb', line 32

def request(path, params)
  begin
    uri = uri_for(path)

    http = Net::HTTP.new(uri.host, uri.port)
    http.open_timeout = http.read_timeout = 10

    req = Net::HTTP::Post.new(uri.path)
    req.form_data = params

    http.request(req)
  rescue StandardError, TimeoutError => e
    logger.warn("#{e.class} #{e.message}")
  end
end

.uri_for(path = nil) ⇒ Object



26
27
28
29
30
# File 'lib/ikachan.rb', line 26

def uri_for(path = nil)
  uri = URI.parse("#{@@url}/#{path}")
  uri.path = Pathname.new(uri.path).cleanpath.to_s
  uri
end