2

I'm using Gibbon, version 2.2.1 for Mailchimp, and I'd like to be able to create an interest inside an interest group. For instance, I have users who are subscribed to a class. My interest group is "Lessons", and an interest inside that interest group would be "Foo Lesson".

I'd like to be able to add the ability to add a new class in my site's CMS, which would make an API request on after_create.

class Lesson < ActiveRecord::Base
  after_create :create_class_on_mailchimp

  def create_class_on_mailchimp
    require 'mailchimp_service'
    mailchimp = MailchimpService.new(self)
    response = mailchimp.create_class
    self.interest_id = response.id
    self.save
  end
end


class MailchimpService
  def initialize(lesson)
    @lesson = lesson
    @list_id = ENV['MAILCHIMP_LIST_ID']
  end

  def create_class
    GB.lists(@list_id).interest_categories(ENV['MAILCHIMP_CLASSES_CATEGORY_ID']).interests.create(
      body: {
        name: 'foobar'
      }
    )
  end
end

I keep getting this error:

Gibbon::MailChimpError:the server responded with status 404 @title="Resource Not Found",
@detail="The requested resource could not be found.",
@body={  
  "type"  =>"http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/",
  "title"  =>"Resource Not Found",
  "status"  =>404,
  "detail"  =>"The requested resource could not be found.",
  "instance"  =>""
},
@raw_body="{  
  \"type\":  \"http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/\",
  \"title\":\"Resource Not Found\",
  \"status\":404,
  \"detail\":\"The requested resource could not be found.\",
  \"instance\":\"\"
}",
@status_code=404

What this tells me is that I'm not using the correct resource name? There doesn't seem to be any documentation for this kind of request in Gibbon's limited docs, nor does it seem to be something that Mailchimp goes over. Here is a link to Mailchimp's docs that goes over the requests for interests inside interest-groupings, however, there doesn't seem to be a create option... Just read, edit, and delete. This seems silly to me, as I can imagine people would want to create interests from somewhere other than Mailchimp's dashboard.

I've tried using name, title, and interest_name for the resource name, but none work. I've also tried using REST API calls, but I receive the same response.

Am I doing something wrong, or is this really something that Mailchimp doesn't offer? It'd be a huge bummer if so, since I'll be creating many classes that I want people to be able to subscribe to, and it would be a major pain to have to do this all manually.

ekad
  • 14,436
  • 26
  • 44
  • 46
Doug
  • 1,517
  • 3
  • 18
  • 40

1 Answers1

1

I'm pretty sure POST works to create interests, although it does appear to be missing from the documentation. What is probably happening is that either your list ID or interest category ID is incorrect. You might want to try using the API Playground to track down the exact IDs for both of those entities.

TooMuchPete
  • 4,583
  • 2
  • 17
  • 21
  • Thanks for the feedback. I was playing around a lot directly in the terminal to make sure that `GB.lists(@list_id).interest_categories(ENV['MAILCHIMP_CLASSES_CATEGORY_ID']).interests` returns a list of interests, and as far as I can remember, that part was working properly. I'll make absolute sure, in a few hours, that this is still returning a valid response. – Doug Jan 13 '16 at 20:30
  • 1
    You were 100% right. For some reason, my env variable `MAILCHIMP_CLASSES_CATEGORY_ID` wasn't being recognized in the console tab I was testing the request in. Even after resetting the console several times, the env variable didn't show up, but would show up in separate tabs in my terminal... Odd, and I'm not sure why I had to close the tab to get everything to reinitialize correctly, but once I did, the request worked. – Doug Jan 14 '16 at 23:53
  • That's weird, but I'm glad you got it sorted out! – TooMuchPete Jan 15 '16 at 19:27