class Osm::Email

Constants

TAGS

Public Class Methods

get_emails_for_contacts(api, section, contacts, members) click to toggle source

Get a list of selected email address for selected members ready to pass to send_email method @param [Osm::Api] api The api to use to make the request @param [Osm::Section, Fixnum, to_i] section The section (or its ID) to send the message to @param [Array<symbol>] contacts The contacts to get for members (:primary, :secondary and/or :member) @param [Array<Osm::Member, Fixnum, to_i>] members The members (or their IDs) to get the email addresses for @return [Hash] member_id -> {firstname [String], lastname [String], emails [Array<String>]}

# 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) click to toggle source

Get a list of selected email address for selected members ready to pass to send_email method @param [Osm::Api] api The api to use to make the request @param [Osm::Section, Fixnum, to_i] section The section (or its ID) to send the message to @param [Hash] send_to Email addresses to send the email to: member_id -> {firstname [String], lastname [String], emails [Array<String>]} @param [String, nil] cc Email address (if any) to cc @param [String] from Email address to send the email from @param [String] from Email subject The subject of the email @param [String] from Email body The bosy of the email @return [Boolean] Whether OSM reported the email as sent

# 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