Class: Responsys

Inherits:
Object
  • Object
show all
Defined in:
lib/responsys.rb

Defined Under Namespace

Classes: MethodsNotSupportedError, ResponsysTimeoutError, TooManyMembersError

Constant Summary collapse

TIMEOUT =
180
MAX_MEMBERS =
200

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username, password, options = {}) ⇒ Responsys

Creates a client object to connect to Responsys via SOAP API

<username> - The login username <password> - The login password <options>

timeout_threshold - number of seconds (default 180)


26
27
28
29
30
31
32
33
# File 'lib/responsys.rb', line 26

def initialize(username, password, options = {})
  @username = username
  @password = password
  @client = ResponsysWS.new
  @keep_alive = false

  self.timeout_threshold = options[:timeout_threshold] || TIMEOUT
end

Instance Attribute Details

#session_idObject (readonly)

Returns the value of attribute session_id.



17
18
19
# File 'lib/responsys.rb', line 17

def session_id
  @session_id
end

#timeout_thresholdObject

Returns the value of attribute timeout_threshold.



18
19
20
# File 'lib/responsys.rb', line 18

def timeout_threshold
  @timeout_threshold
end

Instance Method Details

#assign_sessionObject



55
56
57
58
# File 'lib/responsys.rb', line 55

def assign_session
  session_header_request = SessionHeader.new(@session_id)
  @client.headerhandler << session_header_request
end

#loginObject



44
45
46
47
48
49
50
51
52
53
# File 'lib/responsys.rb', line 44

def 
  with_application_error do
     = Login.new
    .username = @username
    .password = @password
    response = @client.()
    @session_id = response.result.sessionId
    assign_session
  end
end

#logoutObject



60
61
62
63
64
65
66
67
# File 'lib/responsys.rb', line 60

def logout
  begin
    logout_request = Logout.new
    @client.logout(logout_request)
  ensure
    @session_id = nil
  end
end

#send_email(campaign_name, recipients) ⇒ Object

Each element in the list is a hash:

{
  id: athlete_id,
  email: email address,
  options: {}
}


80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/responsys.rb', line 80

def send_email(campaign_name, recipients)
  raise TooManyMembersError if recipients.size > MAX_MEMBERS

  trigger_campaign_message = TriggerCampaignMessage.new
  interact_object = InteractObject.new
  interact_object.folderName = 'ignored'
  interact_object.objectName = campaign_name
  trigger_campaign_message.campaign = interact_object
  trigger_campaign_message.recipientData = []

  recipients.each do |recipient_info|
    options = recipient_info.has_key?('options') ? recipient_info['options'] : {foo: 'bar'}

    recipient = Recipient.new
    recipient.emailAddress = recipient_info['email'] if recipient_info['email']
    recipient.customerId = recipient_info['id'] if recipient_info['id']
    recipient_data = RecipientData.new
    recipient_data.recipient = recipient
    recipient_data.optionalData = []
    options.each_pair do |k, v|
      optional_data = OptionalData.new
      optional_data.name = k
      v.gsub!(/[[:cntrl:]]/, ' ') if v.is_a? String
      optional_data.value = v
      recipient_data.optionalData << optional_data
    end

    trigger_campaign_message.recipientData << recipient_data
  end

  with_session do
    @client.triggerCampaignMessage(trigger_campaign_message)
  end
end

#with_sessionObject



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/responsys.rb', line 121

def with_session
  begin
    with_timeout do
       if @session_id.nil?
    end
    with_application_error do
      with_timeout do
        yield
      end
    end
  ensure
    with_timeout do
      logout unless @keep_alive
    end
  end
end

#with_timeoutObject



115
116
117
118
119
# File 'lib/responsys.rb', line 115

def with_timeout
  Timeout::timeout(timeout_threshold, ResponsysTimeoutError) do
    yield
  end
end