3

I'm still new to Ruby and trying to use the HTTParty gem to help me write an API Wrapper. I feed HTTParty::get a URI and it parses JSON data. From a quick glance and the way the returned result behaves, it looks like a Hash, but is it? I can't seem to find information online. Another post on StackOverflow shows to use HTTParty::get(...).parsed_response to get the Hash, but this seems outdated.

DillPixel
  • 955
  • 1
  • 14
  • 20

1 Answers1

5

Do this in the console:

>require 'httparty'
 => true
> response = HTTParty.get( "..." )
 ....
> response.class
 => HTTParty::Response

So the response from HTTParty.get is an HTTParty::Response object.

See this blogpost titled "It's Time To HTTParty!" to learn more about how to work this response.

Prakash Murthy
  • 12,923
  • 3
  • 46
  • 74
  • 1
    This is slightly off-topic. When writing an API wrapper, would it be preferred to return this Response object, or to convert it to a Hash? Or to create an instance of a Class with keys as fields/attributes? – DillPixel Feb 20 '13 at 16:30
  • Depends on the specific requirements I guess. I haven't worked much on API wrappers; probably best to ask a separate question. – Prakash Murthy Feb 20 '13 at 16:32
  • 2
    @DillPixel By returning a non-standard object such as `HTTParty::Response` the API and its users are more insulated to change than using a simpler data type such as a hash. See [this SO answer](http://stackoverflow.com/a/1568230/335847) for lots of good reasons. It's the same principle. – ian Feb 20 '13 at 16:58