Sha256: 53fdc549b519226af47f2488fab4774bfb54b016daef128e8148492d54841774

Contents?: true

Size: 1.22 KB

Versions: 3

Compression:

Stored size: 1.22 KB

Contents

module ShelbyArena
  module Response
    class Person < Base
      MAP = {
        age: 'Age',
        first_name: 'FirstName',
        last_name: 'LastName',
        person_id: 'PersonID',
        person_link: 'PersonLink',
        gender: 'Gender',
        campus_id: 'CampusID',
        campus: 'CampusName'
      }.freeze

      def format_single(data)
        response = to_h(MAP, data)
        response[:email] = set_email(data)
        response[:emails] = set_emails(data)
        response
      end

      private

      def set_email(raw_response)
        # find_person returns an array of `Emails`
        # list_person return `FirstActiveEmail`
        emails_key = parse_emails_key(raw_response.dig('Emails', 'Email'))

        raw_response.dig('FirstActiveEmail') || emails_key.first
      end

      def set_emails(raw_response)
        # find_person returns an array of `Emails`
        # list_person return `FirstActiveEmail`
        parse_emails_key(raw_response.dig('Emails', 'Email'))
      end

      def parse_emails_key(emails_key)
        if emails_key.is_a?(Array)
          emails_key&.map { |email| email.dig('Address') } || []
        else
          [emails_key&.dig('Address')] || []
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
shelby_arena-0.0.8 lib/shelby_arena/response/person.rb
shelby_arena-0.0.7 lib/shelby_arena/response/person.rb
shelby_arena-0.0.6 lib/shelby_arena/response/person.rb