Ruby で Twitter

RubyTwitter をやってみました。

本家:ここに簡単な例が出ています。http://twitter.rubyforge.org/
ドキュメント http://twitter.rubyforge.org/twitter/

"gem 'twitter'" がないと "uninitialized constant Twitter (NameError)" というエラーが出る。
http://cento.sakura.ne.jp/sb/log/amarok2twitter.html を参考に "gem 'twitter'" を記述した。

"gem 'twitter'" は初めてのおまじないだけど、これ何?

require 'rubygems'
gem     'twitter'
require 'twitter'

httpauth = Twitter::HTTPAuth.new('sirocco_jp','password')
base     = Twitter::Base.new(httpauth)

# Time Line を表示
base.friends_timeline.each do |line|
  puts line['user'].name << " : " << line['text']
end

# 投稿
base.update("Test::Ruby Twitter Gem by John Nunemaker")

# ユーザ名を指定して Time Line を表示
Twitter::Search.new().from('user_name').each do |line| 
  puts line['text']
end

CentOSirb からだとOK。path の関係?

$ cat /etc/redhat-release
CentOS release 5.4 (Final)
$ irb
irb(main):001:0> require 'rubygems'
=> true
irb(main):002:0> require 'twitter'
=> true
irb(main):003:0> httpauth = Twitter::HTTPAuth.new('sirocco_jp','password')
=> #<Twitter::HTTPAuth:0xb78a68f0 @password="password", @options={:ssl=>false}, @username="sirocco_jp">
irb(main):004:0> base     = Twitter::Base.new(httpauth)
=> #<Twitter::Base:0xb78a38f8 @client=#<Twitter::HTTPAuth:0xb78a68f0 @password="password", @options={:ssl=>false}, @username="sirocco_jp">>
irb(main):005:0> puts base.friends_timeline[0]['text']
「ウウウ……」と彼は永い息を吐いて、脊骨の針を除こうと思いながら、それでも考え続けるのだった。 魯迅『幸福な家庭』
=> nil

Ubuntuirb からだとNG。

$ uname -a
Linux h-desktop 2.6.31-16-generic #53-Ubuntu SMP Tue Dec 8 04:01:29 UTC 2009 i686 GNU/Linux
$ irb
irb(main):001:0> require 'rubygems'
=> true
irb(main):002:0> require 'twitter'
NameError: uninitialized constant Twitter
	from ./twitter.rb:10
	from /usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
	from /usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `require'
	from (irb):2
	from (null):0

/usr/lib/ruby/1.8/rubygems/custom_require.rb

# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
# All rights reserved.
# See LICENSE.txt for permissions.
#++

require 'rubygems'

module Kernel

  ##
  # The Kernel#require from before RubyGems was loaded.

  alias gem_original_require require

  ##
  # When RubyGems is required, Kernel#require is replaced with our own which
  # is capable of loading gems on demand.
  #
  # When you call <tt>require 'x'</tt>, this is what happens:
  # * If the file can be loaded from the existing Ruby loadpath, it
  #   is.
  # * Otherwise, installed gems are searched for a file that matches.
  #   If it's found in gem 'y', that gem is activated (added to the
  #   loadpath).
  #
  # The normal <tt>require</tt> functionality of returning false if
  # that file has already been loaded is preserved.

  def require(path) # :doc:
    gem_original_require path
  rescue LoadError => load_error
    if load_error.message =~ /#{Regexp.escape path}\z/ and
       spec = Gem.searcher.find(path) then
      Gem.activate(spec.name, "= #{spec.version}")
      gem_original_require path
    else
      raise load_error
    end
  end

  private :require
  private :gem_original_require

end