2

Hello Im using HTTParty to call for a remote json file that I need to extract the URL's to use in one of my tests.. the json format goes something like:

  "manifest" : {
    "header" : {
      "generated" : "xxxxxxxxxxxxxxx",
      "name" : "xxxxxxxxxxx",
      "version" : "1.0.0"
    },
    "files" : [ {
      "file" : "blimp.zip",
      "url" : "http://www.xxx.xx/restaurants_blimp.zip",
      "checksum" : "ee98c9455b8d7ba6556f53256f95"
    }, {
      "file" : "yard.zip",
      "url" : "www.xxx.xx/yard.zip",
      "checksum" : "e66aa3d123f804f34afc622b5"
    }

on irb I can get all the sub hashes inside example: ['manifest']['files'] and I can only get the url if I expecify which one.. like for example puts file['manifest']['files']['1']['url'] <-- this does work on irb but since I need to get ALL url's this is why I use .each but it gives me a cant convert to string error or similar

#!/usr/bin/env ruby

require 'httparty'

HOST=ARGV[0]
ID=ARGV[1]
VERSION=ARGV[2]


class MyApi
  include HTTParty
end

file = MyApi.get("http://#{HOST}/v1/dc/manifest/#{ID}/#{VERSION}")


file.each do |item|
 puts item['manifest']['files']['url']
end

not working but I can on IRB do a:

puts item['manifest']['files'][2]['url'] <-- and this will give me the url but with the .each will just complaint about cant convert to string or similar

cfernandezlinux
  • 861
  • 1
  • 10
  • 23

2 Answers2

3

Try the following:

#!/usr/bin/env ruby

require 'httparty'

(HOST, ID, VERSION) = ARGV

class MyApi
  include HTTParty
  format :json
end

response = MyApi.get("http://#{HOST}/v1/dc/manifest/#{ID}/#{VERSION}")

puts response.inspect

The addition of the format :json tells HTTParty to parse the response as JSON. Then you'll get a hash you can iterate over properly.

Mark Thomas
  • 37,131
  • 11
  • 74
  • 101
  • My `require HTTParty` and code for performing the API call are located in a controller. I tried adding `format :json` and I got a new error message: `JSON::ParserError`. I can see the JSON form I received in response to my API call, so I'm not sure what the ParserError is about. – max pleaner Mar 23 '14 at 20:22
  • @macsplean That's not enough info to go on. You should post another question with your code, the full error message, and the full response from the service. – Mark Thomas Mar 23 '14 at 20:38
2

Try:

file['manifest']['files'].each do |item|
   puts item['url']
end
Michael
  • 751
  • 4
  • 8
  • usr/lib/ruby/1.9.1/json/common.rb:146:in `initialize': can't convert HTTParty::Response into String (TypeError) from /usr/lib/ruby/1.9.1/json/common.rb:146:in `new' from /usr/lib/ruby/1.9.1/json/common.rb:146:in `parse' from ./check_manifest.rb3:19:in `
    '
    – cfernandezlinux Mar 22 '12 at 00:09
  • @ChrisAnarkoMeow That's because you are trying to call `JSON.parse` on an `HTTParty::Response` object, not a JSON string. You shouldn't need to do that, as the Response object has your response in a hash. – Mark Thomas Mar 22 '12 at 00:36
  • cool that works too but I think is cleaner to use Mark solution addin format :json since im already using that lib, no point to pull two libs. – cfernandezlinux Mar 22 '12 at 04:17