Report abuse

##
#  Plurk Post API using Mechanize
#  http://www.mashupgarage.com
#  Jason Torres (http://twitter.com/jasontorres)
#


require 'rubygems'
require 'mechanize'

class Plurk

  @username = ''
  @password = ''

  @agent = nil


  def initialize(username, password)
    @username = username
    @password = password

    @agent = WWW::Mechanize.new { |a|
      a.user_agent_alias = 'Mac Safari' 
    }
  end

  def send_message(message, qualifier = 'says', lang = 'en', no_comments = '0')

    @agent.get('http://www.plurk.com') do |plurk|

      my_page = plurk.form_with(:action => '/Users/login') do |form|
        form.nick_name  = @username
        form.password = @password
      end.submit

      user_id = my_page.body.match(/\"user_id\":\s([0-9]+)/)[1] # needs to fetch the ID
      post_params = { :uid => "#{user_id}", 
                :content => message,
                :lang => lang,
                :qualifier => qualifier,
                :no_comments => no_comments
              }
      @agent.post('http://www.plurk.com/TimeLine/addPlurk', post_params)

    end

  end

end

PLURK_USERNAME = 'yourloginoremail'
PLURK_PASSWORD = 'youruserpassword'

# Usage
plurk = Plurk.new(PLURK_USERNAME, PLURK_PASSWORD)
plurk.send_message('trying out another remote post to plurk. and saying who needs a plurk API? hehe.')