Rails 4 - анализ и итерация ответа JSON API Google Адресов

Использование Rails 4 с гемом http для простых запросов get. Я бью gigles api с URL и парой параметров, чтобы вернуть все близлежащие стадионы в json.

Моя цель здесь состоит в том, чтобы пройтись по всем различным результатам на стадионе и вытащить photo_reference.

Я могу извлечь photo_reference из первого массива следующим образом:

def nearby_places(lat, long)
  response = HTTP.get("https://maps.googleapis.com/maps/api/place/nearbysearch/json?key=mykey&location=#{lat},#{long}&radius=3000&types=stadium")
  parsed_response = JSON.parse(response.body)
  parsed_response["results"][0]["photos"][0]["photo_reference"]
end

Но у меня возникают проблемы с повторением и извлечением всех ссылок на фотографии, если возвращается несколько стадионов. Каков наилучший способ сделать это?

Вот пример ответа от Google.

{    "html_attributions" : [],    "results" : [
      {
         "geometry" : {
            "location" : {
               "lat" : 32.2285419,
               "lng" : -110.9488008
            }
         },
         "icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
         "id" : "09b3cd5bd149c550db4740a67671b1d33b008531",
         "name" : "Arizona Stadium",
         "photos" : [
            {
               "height" : 1371,
               "html_attributions" : [
                  "\u003ca href=\"https://maps.google.com/maps/contrib/108405732138529965447/photos\"\u003eGoogle one\u003c/a\u003e"
               ],
               "photo_reference" : "CmRdAAAALCkhLHgNGOfNExFwmbdFw4vg_J5iWBomKOCh-HgF8ggTMiymuqUvSW7hSpfOjbcQ4iS6jUHf3lWTqzNHYF4b6gtq06ik9MKMCrGjDQgn70vrItbHHBGpHZ77wk8yVCPtEhB9D3W_Kn1W214cqoiRSwZsGhSlMG2x44K3CqOIbgUu-5a_KnGKbg",
               "width" : 2054
            }
         ],
         "place_id" : "ChIJs_Gb76px1oYRPAYW8gEqUC4",
         "price_level" : 1,
         "rating" : 3.8,
         "reference" : "CnRiAAAAcUcY_BnrsZ4Ik9MI7W53JtIvHxVAvvl7lDl1vOkGCQ3uYwOpoR6OYp1cRNkS7LLACJv1Nc_HQjMeu7AIOm1H-9ciunC5Xl6M79wHZ7MB7n1NiQ4GD1HkCxoDTqoBx_qXx3czLkRYG31WSvtUMBNu3hIQzzQK_tSjvMMefqSuNzl3whoUz5o8eZbHCewDHXU7_Pig0bx_w4w",
         "scope" : "GOOGLE",
         "types" : [ "stadium", "point_of_interest", "establishment" ],
         "vicinity" : "1 National Championship Drive, Tucson"
      },
      {
         "geometry" : {
            "location" : {
               "lat" : 32.2301948,
               "lng" : -110.9460055
            }
         },
         "icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
         "id" : "33aa72ea7a9688ca20333c774ecbbc0f4b29eedc",
         "name" : "McKale Memorial Center",
         "opening_hours" : {
            "open_now" : false,
            "weekday_text" : []
         },
         "photos" : [
            {
               "height" : 2132,
               "html_attributions" : [
                  "\u003ca href=\"https://maps.google.com/maps/contrib/102117061543892797349/photos\"\u003eJohn Saunders\u003c/a\u003e"
               ],
               "photo_reference" : "CmRdAAAAcM0Q5qsRVvptq77wWhfth_hkITyu1bd8jMdJWwDHNqQgb-IAExfc67QJTJU0XCDZ8MnXbLwcfa0lWfoTJT8K051_VsmT-CBS00Na40rL-MglE10RMs4sQBXCOAzkyoqzEhCXc9pTCQvp_DtQDvr0HjU_GhTbWyvV14IraM7osdnYkb2R4cebEQ",
               "width" : 3097
            }
         ],
         "place_id" : "ChIJDwAsG6tx1oYRjFkokCR3jWY",
         "price_level" : 1,
         "rating" : 4,
         "reference" : "CnRpAAAAEAbkIN4gKquA3i2IC0vS4c8K7erBkDTyB6Z3eIEX4u4W3uMBQSgaOeySEOTVng2oIT1pWa1KsIpWqLSYaFVJDhaS800FEEBno5W0Me-TBy7qStmOMFO_jpeQ4iltCrds3enAtMgM9uQNoEPe8ntjtRIQ-lW3pOaDSJ1R4V5RS5UXNBoUF8OhthXnVaCSf6V3nxEFSNBlQ_s",
         "scope" : "GOOGLE",
         "types" : [ "stadium", "point_of_interest", "establishment" ],
         "vicinity" : "1 National Championship Drive, Tucson"
      },
      {
         "geometry" : {
            "location" : {
               "lat" : 32.2135012,
               "lng" : -110.9189719
            }
         },
         "icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
         "id" : "d387dca1054f23d5a8767f03f9bb9f7b04f6363c",
         "name" : "Hi Corbett Field",
         "opening_hours" : {
            "open_now" : false,
            "weekday_text" : []
         },
         "photos" : [
            {
               "height" : 2048,
               "html_attributions" : [
                  "\u003ca href=\"https://maps.google.com/maps/contrib/104635440524137932181/photos\"\u003eJose Olmos\u003c/a\u003e"
               ],
               "photo_reference" : "CmRdAAAAV1gOFNPOQG-xqMMwhYiFt-p8go-4HXJWtAScAVYSuresCzbOdpz5x5dSMSAc2zlxNVtE_6JzwXKY4OG0ScY-QCEEs5t6XIqAfkcjf8CEqm4vLmkvdJ2Gxi0MbXDkSqwgEhAoCxH8BddGX41yTSh6i6U8GhQpdxiX-S9YVBchYzttaCAbhphbBg",
               "width" : 1536
            }
         ],
         "place_id" : "ChIJB3zRTjxw1oYRxg2TEV6XwA8",
         "reference" : "CnRjAAAAg-sLDnKo_p9oI52MaNlzBBA2dCn874cSwE-Hxxl5dEjOu1HVIGcqXboWQxmVR44GUwgahIMa8XKdEarPggMgBl8eCDWb71u85pjRm8-j-JUuokKq_sxqfjcNf7KnbeM7bb4vl2Soz5vx5wI30udRUBIQOCz6nPaUc6cCGJ7Hi5vCfBoU7aFJXLr2tDCTBos1_3AWwPpqHEY",
         "scope" : "GOOGLE",
         "types" : [ "stadium", "point_of_interest", "establishment" ],
         "vicinity" : "700 South Randolph Way, Tucson"
      },
      {
         "geometry" : {
            "location" : {
               "lat" : 32.2332841,
               "lng" : -110.9488008
            }
         },
         "icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
         "id" : "a911fe55eaff462d3908c8fea14686f6591660d6",
         "name" : "Arizona Wildcats",
         "place_id" : "ChIJ____k6lx1oYR5ZkM9y--p6Y",
         "reference" : "CnRkAAAA7_fi19QO-sax4_ksm1jRDgGaO9nVNucqo9cCYL8JtwVkt3k_0i0J25QC9AhgkxMHrnPUcNHHtsda_wCqRSDBFUdmaBAmMX4jxE8WUBw-35rxcOoDHpGqftt-wlgLYUgn1JDJP9-Awe03b1hhHbWHwBIQYQIlDlKYKtIKdHRQfuVi7hoUh0w6evypLyiF1GCPy9zuFhuD5R0",
         "scope" : "GOOGLE",
         "types" : [ "stadium", "point_of_interest", "establishment" ],
         "vicinity" : "Tucson"
      },
      {
         "geometry" : {
            "location" : {
               "lat" : 32.2291294,
               "lng" : -110.9470374
            }
         },
         "icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
         "id" : "28b19ac3c12af2a0de83694b851a1627c6458ad6",
         "name" : "MC Kaltr",
         "place_id" : "ChIJY7koG6tx1oYR5la-CtsxnbA",
         "reference" : "CmRcAAAAZ3bqbYMAtgcG3poewZ0t_7blrOgDXn6oE-e1UyevY6m_lxK7CLSc0MwTsi7m5t-fNUqlrmd-H_kyimy_caYyzrxki5iX580Vu3S5ttBUcTdjbWtjg1v8dy2HiPcLIPU4EhCh52vDSrwqlIbzsCqiliBbGhRipMYv__guCbPYKp1ja8uJkvurGg",
         "scope" : "GOOGLE",
         "types" : [ "stadium", "point_of_interest", "establishment" ],
         "vicinity" : "1 National Championship Drive, Tucson"
      },
      {
         "geometry" : {
            "location" : {
               "lat" : 32.21510419999999,
               "lng" : -110.9384699
            }
         },
         "icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
         "id" : "626eb52ef7968c58f0c8c4c9aca05bff1fc38962",
         "name" : "Drachman Stadium",
         "place_id" : "ChIJvc__01pw1oYRV9wdmSg4Pw8",
         "reference" : "CnRjAAAALn4CwTuCPfF0KkjO8nJl4k57eSovzcl5z4kjh6gf4UoC126lNyY1Lo-BNMvnFQjK3oPUb1Na5XvvM-BmmshaP2If19jiNdURUCCHftp6zw_2o8W0-ucOtvj4V0oi026dvZJMCFzu8YseLHmTjn5CDRIQenzfrkgA06wBd_4EoJb_eBoU-PESnLdv-4UMmR0SDQJ9P30lXNw",
         "scope" : "GOOGLE",
         "types" : [ "stadium", "point_of_interest", "establishment" ],
         "vicinity" : "the of 85719, 501 South Plumer Avenue, Tucson"
      }    ],    "status" : "OK" }

0 ответов

Другие вопросы по тегам