0

所以我正在使用 yelp API,在我发出 GET 请求后,我得到了企业的响应。为了处理该响应,我正在使用 .map

例子:

mappedResults = yelpSearch.businesses.map {|l| {id: l.id, name: l.name, categories:l.categories, rating: l.rating, review_count: l.review_count, url: l.url, phone: l.phone}}

我的问题是,有时响应中的某些记录没有返回 l.phone,我收到错误:

undefined method `phone' for #<BurstStruct::Burst:0x007fba47c7a228>

我的问题是如何重构此代码,以便如果记录没有电话,它将使其为空(或最差的转换为空字符串)

任何帮助表示赞赏

JSON结构对于响应中的每个业务都是如此

{
    region: {
        span: {
            latitude_delta: 0,
            longitude_delta: 0
        },
        center: {
            latitude: 38.054117,
            longitude: -84.439002
        }
    },
    total: 23,
    businesses: [
        {
            is_claimed: false,
            rating: 5,
            mobile_url: "http://m.yelp.com/biz/vineyard-community-church-lexington",
            rating_img_url: "http://s3-media1.ak.yelpcdn.com/assets/2/www/img/f1def11e4e79/ico/stars/v1/stars_5.png",
            review_count: 2,
            name: "Vineyard Community Church",
            snippet_image_url: "http://s3-media4.ak.yelpcdn.com/photo/VoeMtbk7NRFi6diksSUtOQ/ms.jpg",
            rating_img_url_small: "http://s3-media1.ak.yelpcdn.com/assets/2/www/img/c7623205d5cd/ico/stars/v1/stars_small_5.png",
            url: "http://www.yelp.com/biz/vineyard-community-church-lexington",
            phone: "8592582300",
            snippet_text: "I have been a member of Vineyard Community Church since 2004. Here you will find a modern worship service with a full band, witty speakers who teach...",
            image_url: "http://s3-media3.ak.yelpcdn.com/bphoto/D71eikniuaHjdOC8DB6ziA/ms.jpg",
            categories: [
                [
                    "Churches",
                    "churches"
                ]
            ],
            display_phone: "+1-859-258-2300",
            rating_img_url_large: "http://s3-media3.ak.yelpcdn.com/assets/2/www/img/22affc4e6c38/ico/stars/v1/stars_large_5.png",
            id: "vineyard-community-church-lexington",
            is_closed: false,
            location: {
                city: "Lexington",
                display_address: [
                    "1881 Eastland Pwky",
                    "Lexington, KY 40505"
                ],
                geo_accuracy: 8,
                postal_code: "40505",
                country_code: "US",
                address: [
                    "1881 Eastland Pwky"
                ],
                coordinate: {
                    latitude: 38.054117,
                    longitude: -84.439002
                },
                state_code: "KY"
            }
        }
    ]
}
4

1 回答 1

1

如果您使用的是 Rails 4.0 或更新版本,#presence方法对此非常有帮助。你会像这样使用它:

mappedResults = yelpSearch.businesses.map {|l| {id: l.id.presence, #... etc

或者像这样

mappedResults = yelpSearch.businesses.map {|l| {id: l.id.presence || "default id", # ...

更新

再次阅读您的代码,#presence 在这种情况下可能不起作用,因为未定义该方法。这是一个更长(更丑)的片段,应该可以工作:

mappedResults = yelpSearch.businesses.map do |l|
  id: l.respond_to(:id) ? l.id : "default id",
  # ... other properties
end

从 OP 更新

这有效 - 谢谢!注意我不得不稍微调整一下语法以响应响应?('method_name')

  mappedResults = yelpSearch.businesses.map {|l|
    {
      name: l.respond_to?("name") ? l.name : "nameless",
    rating: l.respond_to?("rating") ? l.rating : "unrated",
    # ... other properties
    }}
于 2014-05-08T00:28:32.940 回答