0

I have an app that allows users to subscirbe to an email list housed on MailChimp, using the Gibbon gem to wrap the MailChimp api. I am able to access the list, subscribe members via the API. Now I want to be able to show accurately in the user profile, that a user is subscribed to the email list and display content that is appropriate based on whether they are subscribed or pending.

I can retrieve an array of hashes corresponding to the subscribers on my list but when I attempt to find a specific user, I have this error and trace:

gb.lists(ENV['MAILCHIMP_LIST_ID']).members.retrieve.find{|h| h['email_address'] == 'test9@gmail.com'}
TypeError: no implicit conversion of String into Integer
    from (irb):26:in `[]'
    from (irb):26:in `block in irb_binding'
    from (irb):26:in `each'
    from (irb):26:in `find'
    from (irb):26
    from /Users/dariusgoore/development/blackops/.bundle/gems/ruby/2.2.0/gems/railties-4.2.3/lib/rails/commands/console.rb:110:in `start'
    from /Users/dariusgoore/development/blackops/.bundle/gems/ruby/2.2.0/gems/railties-4.2.3/lib/rails/commands/console.rb:9:in `start'
    from /Users/dariusgoore/development/blackops/.bundle/gems/ruby/2.2.0/gems/railties-4.2.3/lib/rails/commands/commands_tasks.rb:68:in `console'
    from /Users/dariusgoore/development/blackops/.bundle/gems/ruby/2.2.0/gems/railties-4.2.3/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
    from /Users/dariusgoore/development/blackops/.bundle/gems/ruby/2.2.0/gems/railties-4.2.3/lib/rails/commands.rb:17:in `<top (required)>'
    from /Users/dariusgoore/development/blackops/.bundle/gems/ruby/2.2.0/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:274:in `require'
    from /Users/dariusgoore/development/blackops/.bundle/gems/ruby/2.2.0/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:274:in `block in require'
    from /Users/dariusgoore/development/blackops/.bundle/gems/ruby/2.2.0/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:240:in `load_dependency'
    from /Users/dariusgoore/development/blackops/.bundle/gems/ruby/2.2.0/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:274:in `require'
    from /Users/dariusgoore/development/blackops/bin/rails:8:in `<top (required)>'
    from /Users/dariusgoore/development/blackops/.bundle/gems/ruby/2.2.0/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:268:in `load'
    from /Users/dariusgoore/development/blackops/.bundle/gems/ruby/2.2.0/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:268:in `block in load'
    from /Users/dariusgoore/development/blackops/.bundle/gems/ruby/2.2.0/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:240:in `load_dependency'
    from /Users/dariusgoore/development/blackops/.bundle/gems/ruby/2.2.0/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:268:in `load'
    from /Users/dariusgoore/development/blackops/.bundle/gems/ruby/2.2.0/gems/spring-1.3.6/lib/spring/commands/rails.rb:6:in `call'
    from /Users/dariusgoore/development/blackops/.bundle/gems/ruby/2.2.0/gems/spring-1.3.6/lib/spring/command_wrapper.rb:38:in `call'
    from /Users/dariusgoore/development/blackops/.bundle/gems/ruby/2.2.0/gems/spring-1.3.6/lib/spring/application.rb:183:in `block in serve'
    from /Users/dariusgoore/development/blackops/.bundle/gems/ruby/2.2.0/gems/spring-1.3.6/lib/spring/application.rb:156:in `fork'
    from /Users/dariusgoore/development/blackops/.bundle/gems/ruby/2.2.0/gems/spring-1.3.6/lib/spring/application.rb:156:in `serve'
    from /Users/dariusgoore/development/blackops/.bundle/gems/ruby/2.2.0/gems/spring-1.3.6/lib/spring/application.rb:131:in `block in run'
    from /Users/dariusgoore/development/blackops/.bundle/gems/ruby/2.2.0/gems/spring-1.3.6/lib/spring/application.rb:125:in `loop'
    from /Users/dariusgoore/development/blackops/.bundle/gems/ruby/2.2.0/gems/spring-1.3.6/lib/spring/application.rb:125:in `run'
    from /Users/dariusgoore/development/blackops/.bundle/gems/ruby/2.2.0/gems/spring-1.3.6/lib/spring/application/boot.rb:18:in `<top (required)>'
    from /Users/dariusgoore/.rubies/ruby-2.2.1/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from /Users/dariusgoore/.rubies/ruby-2.2.1/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from -e:1:in `<main>'>> ^C

I also am not sure, it is right to access the entire list this way , just to verify the existence of one user on the list.

Anyone know how to do this?

Update:

Using Pete's suggestion from the comments, I was able to get this working.

Here is the command that works to retrieve the MailChimp subscriber hash: gb.lists(ENV['MAILCHIMP_LIST_ID']).members(Digest::MD5.hexdigest("test9@gmail.com")).retrieve

user2799827
  • 1,077
  • 3
  • 18
  • 54
  • I don't have any experience with mailchimp, but does `gb.lists(ENV['MAILCHIMP_LIST_ID']).members.retrieve` really return an array of hashes? The exception you get hints at it being an array of arrays. – panmari Sep 02 '15 at 15:45
  • Do you have the member's id on Mailchimp? Or just their email address? – Brian Knight Sep 02 '15 at 17:22
  • Ok I was able to parse this array. It turns out that – user2799827 Sep 03 '15 at 12:22
  • Ok I was able to parse this array. It turns out that `gb.lists(ENV['MAILCHIMP_LIST_ID']).members.retrieve` returns a hash of 4 arrays. one of these arrays is the "members" array which includes a hash for each person on the list or pending. so `gb.lists(ENV['MAILCHIMP_LIST_ID']).members.retrieve["members"]` returns an array of hashes. from there, I can access a particular user has from the email address with `gb.lists(ENV['MAILCHIMP_LIST_ID']).members.retrieve["members"].find{|h| h['email_address'] == "#{self.email}"}` – user2799827 Sep 03 '15 at 12:29
  • One note: this is a pretty awful way of doing this. You're essentially retrieving an entire list. I think you can do something like this: `gb.lists(ENV['MAILCHIMP_LIST_ID']).members(Digest::MD5.new('test9@gmail.com'.downcase))` – TooMuchPete Sep 04 '15 at 04:40
  • Thanks Pete! It definitely occurred to me that it wasn't optimal, but I figured I would refactor later. famous last words, I know. Your comment is helpful though. I will try your solution. Thanks again. – user2799827 Sep 04 '15 at 15:43

0 Answers0