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:
- https://github.com/jnunemaker/httparty
- https://github.com/jnunemaker/httparty/blob/master/lib/httparty/parser.rb
- https://github.com/jnunemaker/httparty/blob/master/examples/custom_parsers.rb




