Archive for 12月, 2010

中級以降を目指すRubyistが読むべき、かもしれないと勝手に思っている5冊

Time to Read

1分

あえて旬を外れた話題。

人にすすめる、と言うよりは、自分が今読んでいて、今後も読み進めていきたいな~、と言う本のメモだったりする。

バイアスとして、あまりRuby一般の書籍とRailsの書籍を分けていません。そこは突っ込まないでくださいね~。。

Read the rest of this entry »

 

Parse JSON with Yajl-ruby when using HTTParty

When you using HTTParty, you can replace the parser of the response.

Create the subclass of HTTParty::Parser and override the “json” method.

1
2
3
4
5
6
require 'yajl'
class CustomYajlParser < HTTParty::Parser
  def json
    Yajl::Parser.parse(body)
  end
end

“body” attribute implements the HTTP response body, as JSON.

Then, use this parser in your class including HTTParty.

1
2
3
4
5
6
7
class SampleClient
  include HTTParty
  base_uri 'twitter.com'
  parser CustomYajlParser
end
 
SampleClient.get '/statuses/public_timeline.json'

Yajl should be faster than pure ruby JSON lib.

Now you can customize parser, for example you can parse XML-based API by sax-machine directly and then return a object.

More more info: