如何检查对象是否为零?

时间:2019-07-09 15:01:47

标签: ruby-on-rails ruby rubygems rest-client

我执行以下调用以获取Ruby对象,该对象从Spotify的API返回数据:

yaml

返回:

app_server_name: ************
app_server_username: $USERNAME$
app_server_password: ‘**********’ #trying to parse from local drive
app_yaml_file: /devl/galaxy/common/edwEnvironmentConfig.yml
environment_profile: /home/$USERNAME$/eproduct/difenv
teradata_logon_file: /home/$USERNAME$/.priv/devl/.td_connection
project_root_directory: /home/$USERNAME$/eproduct/
teradata_auth_type: ‘ldap’
tmode: ‘TERA’

当我尝试获取spotify_track = RSpotify::AudioFeatures.find('3RmAqYZZjiSyMV40hm6rDL')的属性之一时,出现404错误:

#<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

诸如> spotify_track.acousticness Traceback (most recent call last): 1: from (irb):25 RestClient::NotFound (404 Not Found) nilspotify_track.acousticness.present?之类的常见事物都返回相同的RestClient 404错误。

我正在使用RSpotify宝石,它使用REST Client宝石。

1 个答案:

答案 0 :(得分:0)

您可以营救并向其中添加一些代码,

rescue RestClient::ExceptionWithResponse => err

引用rest-client#response-callbacks-error-handling