Class: Osm::Email

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

Defined Under Namespace

Classes: DeliveryReport

Constant Summary collapse

TAGS =
[{id: 'FIRSTNAME', description: "Member's first name"}, {id: 'LASTNAME', description: "Member's last name"}]

Class Method Summary collapse

Class Method Details

.get_emails_for_contacts(api, section, contacts, members) ⇒ Hash

Get a list of selected email address for selected members ready to pass to send_email method

Parameters:

  • api (Osm::Api)

    The api to use to make the request

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

    The section (or its ID) to send the message to

  • contacts (Array<symbol>)

    The contacts to get for members (:primary, :secondary and/or :member)

  • members (Array<Osm::Member, Fixnum, #to_i>)

    The members (or their IDs) to get the email addresses for

Returns:

  • (Hash)

    member_id -> [String], lastname [String], emails [Array<String>]



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
39
40
# File 'lib/osm/email.rb', line 12

def self.get_emails_for_contacts(api, section, contacts, members)
  # Convert contacts into OSM's format
  contacts = [*contacts]
  fail ArgumentError, "You must pass at least one contact" if contacts.none?
  contact_group_map = {
    primary: '"contact_primary_1"',
    secondary: '"contact_primary_2"',
    member: '"contact_primary_member"'
  }
  contacts.map! do |contact|
    mapped = contact_group_map[contact]
    fail ArgumentError, "Invalid contact - #{contact.inspect}" if mapped.nil?
    mapped
  end

  # Convert member_ids to array of numbers
  members = [*members]
  fail ArgumentError, "You must pass at least one member" if members.none?
  members.map!{ |member| member.to_i }

  data = api.perform_query("/ext/members/email/?action=getSelectedEmailsFromContacts&sectionid=#{section.to_i}&scouts=#{members.join(',')}", {
    'contactGroups' => "[#{contacts.join(',')}]"
  })
  if data.is_a?(Hash)
    data = data['emails']
    return data if data.is_a?(Hash)
  end
  return false
end

.send_email(api, section, send_to, cc = '', from, subject, body) ⇒ Boolean

Get a list of selected email address for selected members ready to pass to send_email method

Parameters:

  • api (Osm::Api)

    The api to use to make the request

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

    The section (or its ID) to send the message to

  • send_to (Hash)

    Email addresses to send the email to: member_id -> [String], lastname [String], emails [Array<String>]

  • cc (String, nil) (defaults to: '')

    Email address (if any) to cc

  • from (String)

    Email address to send the email from

  • from (String)

    Email subject The subject of the email

  • from (String)

    Email body The bosy of the email

Returns:

  • (Boolean)

    Whether OSM reported the email as sent



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/osm/email.rb', line 51

def self.send_email(api, section, send_to, cc='', from, subject, body)
  data = api.perform_query('ext/members/email/?action=send', {
    'sectionid' => section.to_i,
    'emails' => send_to.to_json,
    'scouts' => send_to.keys.join(','),
    'cc' => cc,
    'from' => from,
    'subject' => subject,
    'body' => body,
  })

  return data.is_a?(Hash) && data['ok']
end