在 Ruby 哈希中查找特定值

Find specific value in Ruby hash

我正在尝试访问以下哈希中的 "click_me_123",但我无法弄清楚。我试过使用 ['actions']['text']['value'][:actions][:text][:value] 但我检索到了适当的值。渴望了解我做错了什么。

{ "type"=>"block_actions", 
  "user"=>{
     "id"=>"xxx", 
     "username"=>"xxx", 
     "name"=>"xxx", 
     "team_id"=>"xxx"
   }, 
   "api_app_id"=>"xxx", 
   "token"=>"tYfwns5zvg0g84d58OFVsWrM", 
   "container"=>{
     "type"=>"message", 
     "message_ts"=>"1643747737.666859", 
     "channel_id"=>"xxx", 
     "is_ephemeral"=>false, 
     "thread_ts"=>"1643747737.666859"
   },
   "trigger_id" => "3043308548484.1420295660643.1e4a0578e0824d3b69ed371a15842fac", 
   "team"=>{
     "id"=>"T01CC8PKEJX", 
     "domain"=>"xxx"
   }, 
   "enterprise"=>nil, 
   "is_enterprise_install"=>false, 
   "channel"=>{
     "id"=>"C0316CSCANP", 
     "name"=>"background-checksdaebc"
   }, 
   "message"=>{
     "bot_id"=>"B02V6G04XMH", 
     "type"=>"message", 
     "text"=>"New check has been started for asd", 
     "user"=>"U03054J13L1", 
     "ts"=>"1643747737.666859", 
     "team"=>"T01CC8PKEJX", 
     "blocks"=>[
       {
         "type"=>"section", 
         "block_id"=>"l2QDl", 
         "text"=>{
           "type"=>"mrkdwn", 
           "text"=>"New check has been started for asd", 
           "verbatim"=>false
         }
       }, 
       {
         "type"=>"section", 
         "block_id"=>"ma=", 
         "fields"=>[
           {
             "type"=>"mrkdwn", 
             "text"=>"*Name:*\nasd asda", 
             "verbatim"=>false
           }, 
           {
             "type"=>"mrkdwn", 
             "text"=>"*Email:*\n<mailto:asd@asdsd.dk|asd@asdsd.dk>", 
             "verbatim"=>false
           }, 
           {
             "type"=>"mrkdwn", 
             "text"=>"*Check started:*\n2022-02-01T20:35:37+00:00", 
             "verbatim"=>false
           }, 
           {
             "type"=>"mrkdwn", 
             "text"=>"*Started by:*\n<@U01CJ7BTT28>", 
             "verbatim"=>false
           }
         ]
       }, 
       {
         "type"=>"actions", 
         "block_id"=>"6Xy", 
         "elements"=>[
           {
             "type"=>"button", 
             "action_id"=>"mjLb", 
             "text"=>{
               "type"=>"plain_text", 
               "text"=>"Approve", 
               "emoji"=>true
             }, 
             "style"=>"primary", 
             "value"=>"click_me_123"
           }, 
           {
             "type"=>"button", 
             "action_id"=>"EaN2", 
             "text"=>{
               "type"=>"plain_text", 
               "text"=>"Deny", 
               "emoji"=>true
             }, 
             "style"=>"danger", 
             "value"=>"click_me_123"
           }
         ]
       }
     ], 
     "thread_ts"=>"1643747737.666859", 
     "reply_count"=>1, 
     "reply_users_count"=>1, 
     "latest_reply"=>"1643749497.845429", 
     "reply_users"=>["U03054J13L1"], 
     "is_locked"=>false, 
     "subscribed"=>true, 
     "last_read"=>"1643749497.845429"
   }, 
   "state"=>{"values"=>{}}, 
   "response_url"=>"https://hooks.slack.com/actions/xx/xx/xx", 
   "actions"=>[
     {
       "action_id"=>"mjLb", 
       "block_id"=>"6Xy", 
       "text"=>{
         "type"=>"plain_text", 
         "text"=>"Approve", 
         "emoji"=>true
       }, 
       "value"=>"click_me_123", 
       "style"=>"primary", 
       "type"=>"button", 
       "action_ts"=>"1643751685.126753"
     }
   ]
 }

actions 是一个数组,因此您需要访问特定索引或循环遍历它。

例如,如果您的哈希名为 hsh:

hsh.dig('actions', 0, 'value')

这是另一种写法 hsh['actions'][0]['value'] 但如果任何元素为 nil 则优雅地失败。

或...

hsh['actions'].map { |action| action['value'] }

returns ['click_me_123']