|
| 1 | +# Macabee::Contacts is ruby representation of Mac Address Book |
| 2 | + |
| 3 | +require "appscript" |
| 4 | + |
| 5 | +class Macabee::Contacts |
| 6 | + attr_reader :ab, :contacts |
| 7 | + |
| 8 | + # suck all the contacts from local MacOSX Address Book into a single array |
| 9 | + def initialize |
| 10 | + @ab = Appscript.app("Address Book") |
| 11 | + end |
| 12 | + |
| 13 | + def fetch(ab_id) |
| 14 | + (rec = @ab.people.ID(ab_id)) && transform(rec) |
| 15 | + end |
| 16 | + |
| 17 | + def all |
| 18 | + @contacts ||= @ab.people.get.map {|c| transform(c)} |
| 19 | + end |
| 20 | + |
| 21 | + # transform an individual contact to our standard structure |
| 22 | + def transform(p) |
| 23 | + raw = { |
| 24 | + :properties => p.properties_.get.select {|k,v| v != :missing_value && ![:class_, :vcard, :selected, :image].include?(k)}, |
| 25 | + :addresses => p.addresses.get.map {|a| a.properties_.get.select {|k,v| v != :missing_value && ![:class_, :id_, :formatted_address].include?(k)}}, |
| 26 | + :emails => p.emails.get.map {|a| a.properties_.get.select {|k,v| v != :missing_value && ![:class_, :id_].include?(k)}}, |
| 27 | + :phones => p.phones.get.map {|a| a.properties_.get.select {|k,v| v != :missing_value && ![:class_, :id_].include?(k)}}, |
| 28 | + :urls => p.urls.get.map {|a| a.properties_.get.select {|k,v| v != :missing_value && ![:class_, :id_].include?(k)}}, |
| 29 | + :social_profiles => p.social_profiles.get.map {|a| a.properties_.get.select {|k,v| v != :missing_value && ![:class_, :id_].include?(k)}} |
| 30 | + } |
| 31 | + tweaked = {} |
| 32 | + raw.each do |k,v| |
| 33 | + case v |
| 34 | + when Array |
| 35 | + tweaked[k.to_s] = v.map {|h| h.stringify_keys} |
| 36 | + else |
| 37 | + tweaked[k.to_s] = v.stringify_keys |
| 38 | + end |
| 39 | + end |
| 40 | + c = tweaked |
| 41 | + |
| 42 | + props = c['properties'] |
| 43 | + |
| 44 | + abxref = props.select {|k,v| k == 'id_'} |
| 45 | + # don't trust creation_date or modification_date; these are local to the machine |
| 46 | + |
| 47 | + names = { |
| 48 | + 'full' => props['name'], |
| 49 | + 'first' => props['first_name'], |
| 50 | + 'last' => props['last_name'], |
| 51 | + 'suffix' => props['suffix'] |
| 52 | + }.reject {|k,v| v.nil?} |
| 53 | + |
| 54 | + other = props.select {|k,v| ['company', 'note', 'birth_date', 'job_title', 'organization'].include?(k)}.select {|k,v| v} |
| 55 | + phones = c['phones'].each_with_object({}) {|h,x| x[h['label']] = { 'phone' => h['value'] }} |
| 56 | + emails = c['emails'].each_with_object({}) {|h,x| x[h['label']] = { 'email' => h['value'] }} |
| 57 | + |
| 58 | + phone_mappings = {'value' => 'phone'} |
| 59 | + |
| 60 | + { |
| 61 | + 'name' => names, |
| 62 | + 'other' => other, |
| 63 | + 'xref' => { |
| 64 | + 'ab' => abxref |
| 65 | + }, |
| 66 | + 'phones' => phones, |
| 67 | + 'addresses' => c['addresses'].unroll('label'), |
| 68 | + 'emails' => emails, |
| 69 | + 'links' => c['urls'].unroll('label').merge(c['social_profiles'].unroll('service_name')) |
| 70 | + }.reject {|k,v| v.nil? || v.empty?} |
| 71 | + end |
| 72 | +end |
0 commit comments