Class: Workarea::SalesforceEsp::Gateway

Inherits:
Object
  • Object
show all
Defined in:
lib/workarea/salesforce_esp/gateway.rb

Defined Under Namespace

Classes: SalesforceEspEmailError, SalesforceEspListError, SalesforceEspSubscriptionError

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Gateway

Returns a new instance of Gateway.



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/workarea/salesforce_esp/gateway.rb', line 8

def initialize(options = {})
  @client = MarketingCloudSDK::Client.new(
    'client' => {
      'id' => options[:client_id],
      'secret' => options[:secret],
      'account_id' => options[:account_id],
      'use_oAuth2_authentication' => true,
      'request_token_url' => options[:request_token_url]
    }
  )
end

Instance Method Details

#send_triggered_email(email_key, email, attrs = {}) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/workarea/salesforce_esp/gateway.rb', line 66

def send_triggered_email(email_key, email, attrs = {})
  triggered_send = MarketingCloudSDK::TriggeredSend.new
  triggered_send.authStub = @client
  triggered_send.props = {
    "CustomerKey" => email_key
  }
  triggered_send.subscribers = [
    {
      "EmailAddress" => email,
      "SubscriberKey" => email,
      "Attributes" => build_subscriber_attributes(attrs)
    }
  ]

  response = Response.new(triggered_send.send)

  raise SalesforceEspEmailError, response.status_message unless response.success?

  response
end

#subscribe(email, attrs, list_id) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/workarea/salesforce_esp/gateway.rb', line 20

def subscribe(email, attrs, list_id)
  raise SalesforceEspListError, 'No List ID error' unless list_id.present?

  subscriber = build_subscriber(email, "Active", attrs, list_id)
  response = perform_api_call(subscriber, "post")

  if response.failure?
    if response.subscriber_already_exists?
      response = update_subscriber(email, "Active", attrs, list_id)
    end
  end

  response
end

#unsubscribe(email, list_id) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/workarea/salesforce_esp/gateway.rb', line 35

def unsubscribe(email, list_id)
  subscriber = build_subscriber(email, "Unsubscribed", {}, list_id)
  response = perform_api_call(subscriber, "patch")

  if response.failure? && !response.user_not_found?
    raise SalesforceEspSubscriptionError, response.status_message
  end

  response
end

#update_subscriber(email, status, attrs, list_id) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/workarea/salesforce_esp/gateway.rb', line 46

def update_subscriber(email, status, attrs, list_id)
  subscriber = build_subscriber(email, status, attrs, list_id)
  response = perform_api_call(subscriber, "patch")

  if response.failure? && !response.subscriber_already_exists?
    raise SalesforceEspSubscriptionError, response.status_message
  end

  response
end

#write_to_data_extension(data_extension, attrs) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/workarea/salesforce_esp/gateway.rb', line 57

def write_to_data_extension(data_extension, attrs)
  ex = ET_DataExtension::Row.new
  ex.authStub = @client
  ex.Name = data_extension
  ex.props = attrs

  perform_api_call(ex, 'post')
end