Class: Front::MailchimpController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/c/front/mailchimp_controller.rb

Instance Method Summary collapse

Instance Method Details

#subscribeObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/controllers/c/front/mailchimp_controller.rb', line 6

def subscribe
  begin
    @mc = Mailchimp::API.new(Setting.get('mailchimp_api_key'))
    email = params['email']
    begin
      @mc.lists.subscribe(Setting.get('mailchimp_list_id'), 'email' => email)
      flash[:success] = "#{email} subscribed successfully"
    rescue Mailchimp::ListAlreadySubscribedError
      flash[:error] = "#{email} is already subscribed to the list"
    rescue Mailchimp::ListDoesNotExistError
      flash[:error] = 'The list could not be found'
      redirect_back(fallback_location: front_end_root_path)
      return
    rescue Mailchimp::Error => ex
      flash[:error] = if ex.message
                        ex.message
                      else
                        'An unknown error occurred'
                      end
    end
  rescue Mailchimp::InvalidApiKeyError => ex
    flash[:error] = ex.message
  end
  redirect_back(fallback_location: front_end_root_path)
end