Andy
Andy

Reputation: 541

Find specific value in Ruby hash

I'm trying to get access to "click_me_123" in the following hash, but I cannot figure it out. I've tried using ['actions']['text']['value'] and [:actions][:text][:value] but I retrieve the appropriate value. Keen to understand what I'm doing wrong.

{ "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:[email protected]|[email protected]>", 
             "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"
     }
   ]
 }

Upvotes: 0

Views: 74

Answers (1)

ramblex
ramblex

Reputation: 3057

actions is an array so you need to either access a specific index or loop through it.

For example if your hash is called hsh:

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

This is another way of writing hsh['actions'][0]['value'] but fails gracefully if any of the elements is nil.

or...

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

returns ['click_me_123']

Upvotes: 3

Related Questions