0

I am trying to build a little app using the Foursquare API.

require 'rubygems'
require 'rest-client'
require 'json'
require 'foursquare2'
require 'awesome_print'

client = Foursquare2::Client.new(:client_id => 'xxx', :client_secret => 'xxx')

response = client.search_venues(:ll => '36.142064,-86.816086', :query => 'nails')

This works great, however the response I get from the API is some what baffling!

[
    [0] "groups",
    [1] [
        [0] {
             "type" => "places",
             "name" => "Places",
            "items" => [
                [ 0] {
                            "id" => "4e24ce97fa76c80b5f5adfcc",
                          "name" => "The Nail Spa",
                       "contact" => {
                                 "phone" => "6152975895",
                        "formattedPhone" => "(615) 297-5895"
                    },
                      "location" => {
                            "address" => "2126 Abbot Martin Rd. Suite 102",
                        "crossStreet" => "at Green Hills Mall",
                                "lat" => 36.10713765714286,
                                "lng" => -86.817004,
                           "distance" => 3888,
                         "postalCode" => "37215",
                               "city" => "Nashville",
                              "state" => "TN",
                            "country" => "United States"
                    },
                    "categories" => [
                        [0] {
                                    "id" => "4bf58dd8d48988d10c951735",
                                  "name" => "Cosmetics Shop",
                            "pluralName" => "Cosmetics Shops",
                             "shortName" => "Beauty / Cosmetic",
                                  "icon" => "https://foursquare.com/img/categories/shops/beauty_cosmetic.png",
                               "parents" => [
                                [0] "Shops & Services"
                            ],
                               "primary" => true
                        }
                    ],
                      "verified" => false,
                         "stats" => {
                        "checkinsCount" => 103,
                           "usersCount" => 90,
                             "tipCount" => 5
                    },
                      "specials" => []
                },

The above is the first part of the response, a lot of data comes back.

Whats the best way in Ruby to pull out the pars you wanted. For example - If I wanted the Name of each of the results coming back?

The class of the response is Hashie::Mash which I have never seen before?

richarbernal
  • 1,063
  • 2
  • 14
  • 32
Charlie Davies
  • 1,814
  • 4
  • 29
  • 50

1 Answers1

0

Hashie gives you some sugar to play with Hashes (see https://github.com/intridea/hashie).

To get the name of each result, something like this should do the trick:

response.groups[0].items.each{|i| puts i.name}
ccyrille
  • 873
  • 7
  • 18