Skip to content

Commit ccd063f

Browse files
committed
moving logic from workspace
0 parents  commit ccd063f

File tree

11 files changed

+214
-0
lines changed

11 files changed

+214
-0
lines changed

.gitignore

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
*.gem
2+
*.rbc
3+
.bundle
4+
.config
5+
.yardoc
6+
Gemfile.lock
7+
InstalledFiles
8+
_yardoc
9+
coverage
10+
doc/
11+
lib/bundler/man
12+
pkg
13+
rdoc
14+
spec/reports
15+
test/tmp
16+
test/version_tmp
17+
tmp

Gemfile

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
source 'https://rubygems.org'
2+
3+
# Specify your gem's dependencies in macabee.gemspec
4+
gemspec

LICENSE

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Copyright (c) 2012 Jason May
2+
3+
MIT License
4+
5+
Permission is hereby granted, free of charge, to any person obtaining
6+
a copy of this software and associated documentation files (the
7+
"Software"), to deal in the Software without restriction, including
8+
without limitation the rights to use, copy, modify, merge, publish,
9+
distribute, sublicense, and/or sell copies of the Software, and to
10+
permit persons to whom the Software is furnished to do so, subject to
11+
the following conditions:
12+
13+
The above copyright notice and this permission notice shall be
14+
included in all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Macabee
2+
3+
TODO: Write a gem description
4+
5+
## Installation
6+
7+
Add this line to your application's Gemfile:
8+
9+
gem 'macabee'
10+
11+
And then execute:
12+
13+
$ bundle
14+
15+
Or install it yourself as:
16+
17+
$ gem install macabee
18+
19+
## Usage
20+
21+
TODO: Write usage instructions here
22+
23+
## Contributing
24+
25+
1. Fork it
26+
2. Create your feature branch (`git checkout -b my-new-feature`)
27+
3. Commit your changes (`git commit -am 'Added some feature'`)
28+
4. Push to the branch (`git push origin my-new-feature`)
29+
5. Create new Pull Request

Rakefile

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/usr/bin/env rake
2+
require "bundler/gem_tasks"

bin/macabee

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env ruby
2+
3+
require "macabee"
4+
require "json"
5+
6+
abid = ARGV.first
7+
8+
ab = Macabee::Contacts.new
9+
contact = ab.fetch(abid)
10+
if contact
11+
puts JSON.pretty_generate(contact.to_hash)
12+
else
13+
$stderr.puts "No such contact #{abid}"
14+
end

lib/macabee.rb

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
require "macabee/version"
2+
3+
require_relative "macabee/contacts"
4+
5+
module Macabee
6+
# Your code goes here...
7+
end

lib/macabee/contact.rb

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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

lib/macabee/contacts.rb

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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)) && Macabee::Contact.new(rec)
15+
end
16+
17+
def all
18+
@contacts ||= @ab.people.get.map {|abperson| Macabee::Contact(abperson)}
19+
end
20+
end

lib/macabee/version.rb

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module Macabee
2+
VERSION = "0.0.1"
3+
end

macabee.gemspec

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# -*- encoding: utf-8 -*-
2+
require File.expand_path('../lib/macabee/version', __FILE__)
3+
4+
Gem::Specification.new do |gem|
5+
gem.authors = ["Jason May"]
6+
gem.email = ["[email protected]"]
7+
gem.description = %q{Move updates in and out of MacOSX Address Book}
8+
gem.summary = %q{Move updates in and out of MacOSX Address Book}
9+
gem.homepage = ""
10+
11+
gem.files = `git ls-files`.split($\)
12+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14+
gem.name = "macabee"
15+
gem.require_paths = ["lib"]
16+
gem.version = Macabee::VERSION
17+
18+
gem.add_dependency 'rb-appscript'
19+
20+
gem.add_development_dependency "rake", "~> 0.9.2"
21+
gem.add_development_dependency "rspec", "~> 2.9.0"
22+
gem.add_development_dependency "guard-rspec", "~> 0.7.0"
23+
gem.add_development_dependency "ruby_gntp", "~> 0.3.4"
24+
end

0 commit comments

Comments
 (0)