Sha256: 708acfd949dcaf8b752eebc9a9a96fa4b21daa4b367c5c20aa91f16727ca9cfd

Contents?: true

Size: 1014 Bytes

Versions: 1

Compression:

Stored size: 1014 Bytes

Contents

# -*- coding: utf-8 -*-
require 'net/http'
require 'uri'
require 'pathname'
require 'active_support/all'

module Ikachan
  mattr_accessor :url, :channel

  module_function

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

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

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

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

  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

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ikachan-0.0.2 lib/ikachan.rb