Sha256: ff05871eb1fe41f50054dfc5d78c4550dc0b86e562db474acbd31b6a3d7d3556

Contents?: true

Size: 1.47 KB

Versions: 5

Compression:

Stored size: 1.47 KB

Contents

module Workarea
  module MailChimp
    module Tasks
      module Ecommerce
        extend self

        # Creates an ecommerce store in Mail Chimp
        def create_store
          raise RuntimeError.new("No list id defined in store configuration!") unless Workarea::MailChimp.config.default_store[:list_id].present?

          if Workarea::MailChimp::Store.where(mail_chimp_id: Workarea::MailChimp.config.default_store[:id]).exists?
            raise RuntimeError, "Mailchimp store with id #{Workarea::MailChimp.config.default_store[:id]} already exists"
          end

          request = Workarea::MailChimp.request
          begin
            response = request.ecommerce.stores.create(body: Workarea::MailChimp.config.default_store)
          rescue Gibbon::MailChimpError => error
            raise RuntimeError, error.detail
          end

          Workarea::MailChimp::Store.create!(
            mail_chimp_id: response.body["id"],
            list_id: response.body["list_id"],
            name: response.body["name"],
            currency_code: response.body["currency_code"],
            connected_site: response.body.dig("connected_site"),
          )

          connected_site_id = response.body.dig("connected_site", "site_foreign_id")

          Workarea::MailChimp
            .request
            .connected_sites
            .add_path_part(connected_site_id)
            .actions
            .send('verify-script-installation')
            .create
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
workarea-mail_chimp-3.1.0 lib/workarea/mail_chimp/tasks/ecommerce.rb
workarea-mail_chimp-3.0.5 lib/workarea/mail_chimp/tasks/ecommerce.rb
workarea-mail_chimp-3.0.4 lib/workarea/mail_chimp/tasks/ecommerce.rb
workarea-mail_chimp-3.0.3 lib/workarea/mail_chimp/tasks/ecommerce.rb
workarea-mail_chimp-3.0.2 lib/workarea/mail_chimp/tasks/ecommerce.rb