Class: Spree::SubscriptionsController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/spree/subscriptions_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/controllers/spree/subscriptions_controller.rb', line 7

def create
  @errors = []

  if params[:email].blank?
    @errors << t('missing_email')
  elsif params[:email] !~ /[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}/i
    @errors << t('invalid_email_address')
  else
    begin
      self.class.benchmark "Checking if address exists and/or is valid" do
        @mc_member = hominid.member_info(Spree::Config.get(:mailchimp_list_id), params[:email])
      end
      rescue Hominid::ListError => e
    end

    if @mc_member
      @errors << t('that_address_is_already_subscribed')
    else
      begin
        self.class.benchmark "Adding mailchimp subscriber" do
          hominid.subscribe(Spree::Config.get(:mailchimp_list_id), params[:email], {}, MailChimpSync::Sync::mc_subscription_opts)
        end
      rescue Hominid::ValidationError => e
        @errors << t('invalid_email_address')
      end
    end
  end

  respond_to do |wants|
    wants.js
  end
end

#hominidObject



3
4
5
# File 'app/controllers/spree/subscriptions_controller.rb', line 3

def hominid
  @hominid ||= Hominid::Base.new({ :api_key => Spree::Config.get(:mailchimp_api_key) })
end