I've created a test list in MailChimp and I added 2 subscribers.
I created a Ruby script that will retrieve emails of all subscribers with the help of gibbon gem
.
The problem is that I'm the beginner in Ruby and I'm still really not confident with syntax.
Here's the code
require 'gibbon'
require 'byebug'
def mailchimp
gibbon = Gibbon::Request.new(api_key:"my-api-key")
gibbon.timeout = 10
gibbon.lists("my_list_id").members.retrieve
end
debugger
mailchimp
When I test this in debugger with mailchimp.body
, I can see that I get response, but it's in hash and with tons of information, while I only need to get email_address.
How can I dig or loop through this hash and only return email_address ?
You can restrict the request to only the fields you'd like to see. Only retrieve emails for fetched lists:
response = gibbon.lists("my_list_id").members.retrieve(params: {"fields": "members.email_address"})
With that response body you can call keys
to see what fields are available to you. A little less noise than printing out the entire body in IRB.