Method: Osm::Email.get_emails_for_contacts

Defined in:
lib/osm/email.rb

.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