Class: Osm::Email::DeliveryReport

Inherits:
Model
  • Object
show all
Defined in:
lib/osm/email.rb

Defined Under Namespace

Classes: Email, Recipient

Constant Summary collapse

TIME_FORMAT =
'%d/%m/%Y %H:%M'
VALID_STATUSES =
[:processed, :delivered, :bounced]

Constants inherited from Model

Model::SORT_BY

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Model

#<, #<=, #>, #>=, #between?, #changed_attributes, configure, #reset_changed_attributes, #to_i

Constructor Details

#initializeObject

Initialize a new DeliveryReport

Parameters:

  • attributes (Hash)

    The hash of attributes (see attributes for descriptions, use Symbol of attribute name as the key)



# File 'lib/osm/email.rb', line 99

Instance Attribute Details

#idFixnum

Returns the id of the email.

Returns:

  • (Fixnum)

    the id of the email



82
# File 'lib/osm/email.rb', line 82

attribute :id, type: Integer

#recipientsArray<Osm::DeliveryReport::Email::Recipient>

Returns:

  • (Array<Osm::DeliveryReport::Email::Recipient>)


82
# File 'lib/osm/email.rb', line 82

attribute :id, type: Integer

#section_idFixnum

Returns the id of the section which sent the email.

Returns:

  • (Fixnum)

    the id of the section which sent the email



82
# File 'lib/osm/email.rb', line 82

attribute :id, type: Integer

#sent_atTime

Returns when the email was sent.

Returns:

  • (Time)

    when the email was sent



82
# File 'lib/osm/email.rb', line 82

attribute :id, type: Integer

#subjectString

Returns the subject line of the email.

Returns:

  • (String)

    the subject line of the email



82
# File 'lib/osm/email.rb', line 82

attribute :id, type: Integer

Class Method Details

.get_for_section(api, section, options = {}) ⇒ Array<Osm::Email::DeliveryReport>

Get delivery reports

Parameters:

  • api (Osm::Api)

    The api to use to make the request

  • section (Osm::Section, Fixnum, #to_i)

    The section (or its ID) to get the reports for

Returns:



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/osm/email.rb', line 109

def self.get_for_section(api, section, options={})
  Osm::Model.require_access_to_section(api, section, options)
  section_id = section.to_i
  cache_key = ['email_delivery_reports', section_id]

  if !options[:no_cache] && Osm::Model.cache_exist?(api, cache_key)
    return cache_read(api, cache_key)
  end

  reports = []
  recipients = {}
  data = api.perform_query("ext/settings/emails/?action=getDeliveryReport&sectionid=#{section_id}")
  data.each do |item|
    case item['type']

    when 'email'
      # Create an Osm::Email::DeliveryReport in reports array
      id = Osm::to_i_or_nil(item['id'])
      sent_at_str, subject = item['name'].to_s.split(' - ', 2).map{ |i| i.to_s.strip }
      reports.push Osm::Email::DeliveryReport.new(
        id:         id,
        sent_at:    Time.strptime(sent_at_str, TIME_FORMAT),
        subject:    subject,
        section_id: section_id,
      )
      recipients[id] = []

    when 'oneEmail'
      # Create an Osm::Email::DeliveryReport::Email::Recipient in recipients[email_id] array
      report_id, id = item['id'].to_s.strip.split('-').map{ |i| Osm::to_i_or_nil(i) }
      status = item['status_raw'].to_sym
      status = :bounced if status.eql?(:bounce)
      member_id = Osm::to_i_or_nil(item['member_id'])
      recipients[report_id].push Osm::Email::DeliveryReport::Recipient.new(
        id:         id,
        address:    item['email'],
        status:     status,
        member_id:  member_id,
      )

    end
  end # each item in data

  # Add recipients to reports
  reports.each do |report|
    recs = recipients[report.id]
    # Set report for each recipient
    recs.each do |recipient|
      recipient.delivery_report = report
    end
    report.recipients = recs
  end

  cache_write(api, cache_key, reports)
  return reports
end

Instance Method Details

#<=>(another) ⇒ Object



216
217
218
219
220
# File 'lib/osm/email.rb', line 216

def <=>(another)
  result = self.sent_at <=> another.try(:sent_at)
  result = self.id <=> another.try(:id) if result.eql?(0)
  return result
end

#bounced_recipientsArray<Osm::Email::DeliveryReport::Recipient>

Count the recipients of the message with a status of :bounced



202
203
204
205
206
207
208
209
# File 'lib/osm/email.rb', line 202

VALID_STATUSES.each do |attribute|
  define_method "#{attribute}_recipients" do
    recipients.select{ |r| r.status.eql?(attribute) }
  end
  define_method "#{attribute}_recipients?" do
    send("#{attribute}_recipients").any?
  end
end

#bounced_recipients?Boolean

Whether there are recipients of the message with a status of :bounced

Returns:

  • (Boolean)


202
203
204
205
206
207
208
209
# File 'lib/osm/email.rb', line 202

VALID_STATUSES.each do |attribute|
  define_method "#{attribute}_recipients" do
    recipients.select{ |r| r.status.eql?(attribute) }
  end
  define_method "#{attribute}_recipients?" do
    send("#{attribute}_recipients").any?
  end
end

#delivered_recipientsArray<Osm::Email::DeliveryReport::Recipient>

Count the recipients of the message with a status of :delivered



202
203
204
205
206
207
208
209
# File 'lib/osm/email.rb', line 202

VALID_STATUSES.each do |attribute|
  define_method "#{attribute}_recipients" do
    recipients.select{ |r| r.status.eql?(attribute) }
  end
  define_method "#{attribute}_recipients?" do
    send("#{attribute}_recipients").any?
  end
end

#delivered_recipients?Boolean

Whether there are recipients of the message with a status of :delivered

Returns:

  • (Boolean)


202
203
204
205
206
207
208
209
# File 'lib/osm/email.rb', line 202

VALID_STATUSES.each do |attribute|
  define_method "#{attribute}_recipients" do
    recipients.select{ |r| r.status.eql?(attribute) }
  end
  define_method "#{attribute}_recipients?" do
    send("#{attribute}_recipients").any?
  end
end

#get_email(api, options = {}) ⇒ Osm::Email::DeliveryReport::Email

Get email contents for this report

Parameters:

  • api (Osm::Api)

    The api to use to make the request

Returns:



170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/osm/email.rb', line 170

def get_email(api, options={})
  Osm::Model.require_access_to_section(api, section_id, options)
  cache_key = ['email_delivery_reports_email', section_id, id]

  if !options[:no_cache] && Osm::Model.cache_exist?(api, cache_key)
    return cache_read(api, cache_key)
  end

  email = Osm::Email::DeliveryReport::Email.fetch_from_osm(api, section_id, id)

  cache_write(api, cache_key, email)
  return email
end

#processed_recipientsArray<Osm::Email::DeliveryReport::Recipient>

The recipients of the message with a status of :processed



202
203
204
205
206
207
208
209
# File 'lib/osm/email.rb', line 202

VALID_STATUSES.each do |attribute|
  define_method "#{attribute}_recipients" do
    recipients.select{ |r| r.status.eql?(attribute) }
  end
  define_method "#{attribute}_recipients?" do
    send("#{attribute}_recipients").any?
  end
end

#processed_recipients?Boolean

Whether there are recipients of the message with a status of :processed

Returns:

  • (Boolean)


202
203
204
205
206
207
208
209
# File 'lib/osm/email.rb', line 202

VALID_STATUSES.each do |attribute|
  define_method "#{attribute}_recipients" do
    recipients.select{ |r| r.status.eql?(attribute) }
  end
  define_method "#{attribute}_recipients?" do
    send("#{attribute}_recipients").any?
  end
end

#to_sObject



212
213
214
# File 'lib/osm/email.rb', line 212

def to_s
  "#{sent_at.strftime(TIME_FORMAT)} - #{subject}"
end