2012-05-21 5 views
0

Foursquare API를 사용하여 작은 응용 프로그램을 만들려고합니다.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') 

위대한 작품이지만, API에서 얻은 응답은 당황 스럽습니다.

[ 
    [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" => [] 
       }, 

위의 내용이 응답의 첫 번째 부분이며 많은 양의 데이터가 다시 나타납니다.

Ruby에서 원하는 구문을 추출하는 가장 좋은 방법은 무엇입니까? 예를 들면 - 결과의 이름이 모두 돌아 오기를 원한다면?

응답 클래스는 이전에 본 적이없는 Hashie :: Mash입니까?

답변

0

해시는 해시로 재생할 설탕을 제공합니다 (https://github.com/intridea/hashie 참조).

response.groups[0].items.each{|i| puts i.name} 
:

는 각 결과의 이름을 얻으려면,이 같은 트릭을 할해야