class Zenps::Payload

Class for structuring subjects into array of hashes from:

- (array of) string(s)
- (array of) hash(es)
- (array of) object(s)

Attributes

subjects[R]

Public Instance Methods

get(subjects) click to toggle source
# File lib/zenps/payload.rb, line 7
def get(subjects)
  @subjects = subjects
  payload
end

Private Instance Methods

array_but_not_hash?() click to toggle source
# File lib/zenps/payload.rb, line 42
def array_but_not_hash?
  subjects.respond_to?(:each) && !subjects.respond_to?(:keys)
end
build_payload() click to toggle source
# File lib/zenps/payload.rb, line 21
def build_payload
  subjects.map do |subject|
    {
      email: get_email(subject),
      first_name: get_attribute(subject, :first_name),
      last_name: get_attribute(subject, :last_name),
      locale: get_attribute(subject, :locale)
    }.compact
  end
end
get_attribute(subject, attribute) click to toggle source
# File lib/zenps/payload.rb, line 36
def get_attribute(subject, attribute)
  return if subject.class == String
  return subject[attribute.to_s] || subject[attribute] if subject.class == Hash
  return subject.send(attribute) if subject.respond_to?(attribute)
end
get_email(subject) click to toggle source
# File lib/zenps/payload.rb, line 32
def get_email(subject)
  subject.class == String ? subject : get_attribute(subject, :email)
end
payload() click to toggle source
# File lib/zenps/payload.rb, line 16
def payload
  @subjects = [subjects] unless array_but_not_hash?
  build_payload
end