Class: RakeNotifier::Ikachan::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/rake_notifier/ikachan.rb

Instance Method Summary collapse

Constructor Details

#initialize(url, channel) ⇒ Client

Returns a new instance of Client.



39
40
41
42
# File 'lib/rake_notifier/ikachan.rb', line 39

def initialize(url, channel)
  @url     = url
  @channel = channel
end

Instance Method Details

#joinObject



44
45
46
# File 'lib/rake_notifier/ikachan.rb', line 44

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

#notice(message) ⇒ Object



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

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

#request(path, params) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rake_notifier/ikachan.rb', line 59

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
    $stderr.puts "#{e.class} #{e.message}"
  end
end

#uri_for(path = nil) ⇒ Object



53
54
55
56
57
# File 'lib/rake_notifier/ikachan.rb', line 53

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