Module: ImKayac

Defined in:
lib/im-kayac.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

VERSION =
'0.0.4'

Class Method Summary collapse

Class Method Details

.post(user, message, opts = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/im-kayac.rb', line 13

def ImKayac.post(user, message, opts = {})
  uri = URI.parse("http://im.kayac.com/api/post/#{user}")
  params = {:message => URI.encode(message)}
  opts = {} unless opts

  # http://im.kayac.com/#docs
  # keys of "opts" are [:handler, :password, :sig]
  opts.each{|k,v|
    params[k] = v
  }
  query = params.map{|k,v| "#{k}=#{v}"}.join('&')
  message.gsub!(/&/, '_')
  res = nil
  Net::HTTP.start(uri.host, uri.port){|http|
    http_res = http.post(uri.path, query)
    res = JSON.parse(http_res.body)
  }
  unless res['result'].to_s == 'posted' and res['error'].to_s == ''
    error = res['error'].to_s
    if error.size > 0
      raise Error.new(error)
    else
      raise Error.new('unknown error')
    end
  end
  return res
end