A Ruby gem for streaming data from the Bluesky/ATProto firehose

use Hash#dig to get nested optional fields

+10 -10
+10 -10
example/push_notifications.rb
··· 98 98 99 99 if reply = data['reply'] 100 100 # check for replies (direct only) 101 - if reply['parent'] && reply['parent']['uri'] 102 - parent_uri = AtURI.new(reply['parent']['uri']) 101 + if uri = reply.dig('parent', 'uri') 102 + parent_uri = AtURI.new(uri) 103 103 104 104 if parent_uri.did == @user_did 105 105 send_reply_notification(msg, op) ··· 109 109 110 110 if embed = data['embed'] 111 111 # check for quotes 112 - if embed['record'] && embed['record']['uri'] 113 - quoted_uri = AtURI.new(embed['record']['uri']) 112 + if uri = embed.dig('record', 'uri') 113 + quoted_uri = AtURI.new(uri) 114 114 115 115 if quoted_uri.did == @user_did 116 116 send_quote_notification(msg, op) ··· 118 118 end 119 119 120 120 # second type of quote (recordWithMedia) 121 - if embed['record'] && embed['record']['record'] && embed['record']['record']['uri'] 122 - quoted_uri = AtURI.new(embed['record']['record']['uri']) 121 + if uri = embed.dig('record', 'record', 'uri') 122 + quoted_uri = AtURI.new(uri) 123 123 124 124 if quoted_uri.did == @user_did 125 125 send_quote_notification(msg, op) ··· 159 159 def process_like(msg, op) 160 160 data = op.raw_record 161 161 162 - if data['subject'] && data['subject']['uri'] 163 - liked_uri = AtURI.new(data['subject']['uri']) 162 + if uri = data.dig('subject', 'uri') 163 + liked_uri = AtURI.new(uri) 164 164 165 165 if liked_uri.did == @user_did 166 166 case liked_uri.collection ··· 193 193 def process_repost(msg, op) 194 194 data = op.raw_record 195 195 196 - if data['subject'] && data['subject']['uri'] 197 - reposted_uri = AtURI.new(data['subject']['uri']) 196 + if uri = data.dig('subject', 'uri') 197 + reposted_uri = AtURI.new(uri) 198 198 199 199 if reposted_uri.did == @user_did && reposted_uri.collection == 'app.bsky.feed.post' 200 200 send_repost_notification(msg, reposted_uri)