0

I am writing an application that is supposed to fetch the source code of a page and show it.

It will be a simple JSON format, no HTML nor CSS whatsoever. So the source code of the site that I'm trying to fetch looks like this:

{"latitude" : "n/a","longitude" : "n/a","internalTemperature" : "n/a","light" : "n/a","distance" : "n/a" ...

and every time, using httparty, I get something like this:

#, @headers={"x-frame-options"=>["sameorigin"], "x-xss-protection"=>["1; mode=block"], "content-type"=>["text/html;charset=utf-8"], "content-length"=>["86692"], "server"=>["WEBrick/1.3.1 (Ruby/1.9.2/2012-04-20)"], "date"=>["Wed, 09 May 2012 17:46:34 GMT"], "connection"=>["close"]}> 

What is that type of data?, I don't understand.

I have in my erb file a line like this:

<%= Representative.get('http://0.0.0.0:4568/').inspect %>

And in my .rb file:

class Representative
  include HTTParty
end

Obviously the site that I'm reaching is on my localhost so you can't see it, but it returns a simple JSON like I showed.

Zippie
  • 6,018
  • 6
  • 31
  • 46

1 Answers1

1

Don't inspect the result. The result of a Httparty request is supposed to be the JSON, decoded into a hash if your URL points to a JSON document. If not, you can tell Httparty to decode to JSON explicitly.

See "Changing Body Type To JSON Using HTTParty".

Community
  • 1
  • 1
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
  • Thank you very much. Bonus question: When i do it with out `inspect` I get the correct JSON file, but prefixed with a string `http://0.0.0.0:4568` . Do you why is that and how would i be able to lose it? – Zippie May 09 '12 at 19:48