MacPortsでインストールしたTwitter GemでMultiJson::DecodeError
MacPorts-1.9.2を使ってインストールしたrubygemsでTwitter Gemを入れたところ少しハマったので残しておく。
MacPortsはパッケージインストーラで入れただけ。
その後rubygemsをインストール。
$ sudo port install rb-rubygems
ここで
$ which gem ruby
すると両方/opt/localの下を指しているが、
$ gem --version $ ruby --version
の出力を見るとどうもMac標準の/usr/binの下が呼ばれていそうなので、シェルを一回再起動する。
次にTwitter Gemsをインストール。
$ sudo gem install twitter
動作を確認するため、サンプルを書く。
require 'rubygems' require 'twitter' p Twitter.user('junji_furuya').location
これを実行したところ、MultiJson::DecodeErrorになった。
/opt/local/lib/ruby/gems/1.8/gems/multi_json-0.0.5/lib/multi_json.rb:66:in `decode': #<RuntimeError: Did not recognize your engine specification. Please specify either a symbol or a class.> (MultiJson::DecodeError) from /opt/local/lib/ruby/gems/1.8/gems/faraday_middleware-0.3.2/lib/faraday/parse_json.rb:22:in `register_on_complete' from /opt/local/lib/ruby/gems/1.8/gems/faraday-0.5.7/lib/faraday/response.rb:47:in `call' from /opt/local/lib/ruby/gems/1.8/gems/faraday-0.5.7/lib/faraday/response.rb:47:in `finish' from /opt/local/lib/ruby/gems/1.8/gems/faraday-0.5.7/lib/faraday/response.rb:47:in `each' from /opt/local/lib/ruby/gems/1.8/gems/faraday-0.5.7/lib/faraday/response.rb:47:in `finish' from /opt/local/lib/ruby/gems/1.8/gems/faraday-0.5.7/lib/faraday/builder.rb:19:in `inner_app' from /opt/local/lib/ruby/gems/1.8/gems/faraday-0.5.7/lib/faraday/response.rb:17:in `call' from /opt/local/lib/ruby/gems/1.8/gems/faraday-0.5.7/lib/faraday/response.rb:17:in `call' from /opt/local/lib/ruby/gems/1.8/gems/faraday-0.5.7/lib/faraday/adapter/net_http.rb:57:in `call' from /opt/local/lib/ruby/gems/1.8/gems/twitter-1.1.2/lib/faraday/multipart.rb:16:in `call' from /opt/local/lib/ruby/gems/1.8/gems/faraday-0.5.7/lib/faraday/request.rb:85:in `run' from /opt/local/lib/ruby/gems/1.8/gems/faraday-0.5.7/lib/faraday/request.rb:27:in `run' from /opt/local/lib/ruby/gems/1.8/gems/faraday-0.5.7/lib/faraday/connection.rb:177:in `run_request' from /opt/local/lib/ruby/gems/1.8/gems/faraday-0.5.7/lib/faraday/connection.rb:66:in `get' from /opt/local/lib/ruby/gems/1.8/gems/twitter-1.1.2/lib/twitter/request.rb:28:in `send' from /opt/local/lib/ruby/gems/1.8/gems/twitter-1.1.2/lib/twitter/request.rb:28:in `request' from /opt/local/lib/ruby/gems/1.8/gems/twitter-1.1.2/lib/twitter/request.rb:6:in `get' from /opt/local/lib/ruby/gems/1.8/gems/twitter-1.1.2/lib/twitter/client/user.rb:23:in `user' from /opt/local/lib/ruby/gems/1.8/gems/twitter-1.1.2/lib/twitter.rb:21:in `send' from /opt/local/lib/ruby/gems/1.8/gems/twitter-1.1.2/lib/twitter.rb:21:in `method_missing' from hello_world.rb:5
どうも何か設定が足りない様子。
ググってもよく分からなかったので、/opt/local/lib/ruby/gems/1.8/gems/multi_json-0.0.5/lib/multi_json.rbの中身を読と、12〜36行目付近に次のような記述があった。
REQUIREMENT_MAP = [ ["yajl", :yajl], ["json", :json_gem], ["active_support", :active_support], ["json/pure", :json_pure] ] # The default engine based on what you currently # have loaded and installed. First checks to see # if any engines are already loaded, then checks # to see which are installed if none are loaded. def default_engine return :yajl if defined?(::Yajl) return :json_gem if defined?(::JSON) return :active_support if defined?(::ActiveSupport::JSON) REQUIREMENT_MAP.each do |(library, engine)| begin require library return engine rescue LoadError next end end end
どうもREQUIREMENT_MAPの各要素の1番目の要素の文字列を順番にrequireしている様子。
要はJSONのエンジンがインストールされていないのが原因らしい。
そこでyajlをrubygemsからインストール。
$ gems search --remote yajl benofsky-yajl-ruby (0.7.7) filipegiusti-yajl-ruby (0.6.4) sprout-yajl-library (0.0.1) yajl-ruby (0.8.1) $ sudo gems install yajl-ruby
これで、サンプルを動かすことができた。
$ ruby -Ku location.rb "東京都北区"