我执行以下调用来获取Ruby对象,该对象从Spotify的API返回数据:
spotify_track = RSpotify::AudioFeatures.find('3RmAqYZZjiSyMV40hm6rDL')
这将返回:
#<RSpotify::AudioFeatures:0x00007fe0cc079b30 @acousticness=nil, @analysis_url="https://api.spotify.com/v1/audio-analysis/3RmAqYZZjiSyMV40hm6rDL", @danceability=nil, @duration_ms=35361, @energy=nil, @instrumentalness=nil, @key=1, @liveness=nil, @loudness=-12.186, @mode=1, @speechiness=nil, @tempo=0, @time_signature=nil, @track_href="https://api.spotify.com/v1/tracks/3RmAqYZZjiSyMV40hm6rDL", @valence=nil, @external_urls=nil, @href=nil, @id="3RmAqYZZjiSyMV40hm6rDL", @type="audio_features", @uri="spotify:track:3RmAqYZZjiSyMV40hm6rDL"> 当我尝试获取其中一个为nil的属性时,我得到一个404错误:
> spotify_track.acousticness
Traceback (most recent call last):
1: from (irb):25
RestClient::NotFound (404 Not Found)那么,如何检查属性是否为nil呢?
像spotify_track.acousticness.present?、spotify_track.acousticness.blank?和spotify_track.try(:acousticness)这样的常见代码都会返回相同的RestClient 404错误。
我使用的是RSpotify gem,它利用了REST Client gem。
发布于 2019-07-09 23:10:42
你可以拯救它并添加一些代码,
rescue RestClient::ExceptionWithResponse => errhttps://stackoverflow.com/questions/56955480
复制相似问题