Skip to content

Instantly share code, notes, and snippets.

@olierxleben
Created May 29, 2012 14:16
Show Gist options
  • Save olierxleben/2828690 to your computer and use it in GitHub Desktop.
Save olierxleben/2828690 to your computer and use it in GitHub Desktop.
Control_Tower/Macruby web-service for wrapping Mac`s Addressbook and gives back as XML (XML so far for Cisco IP-Phones)
# Addressbook-Server bridges tha gap between your local Addressbook and Web-Services
#
#
# uses control_tower Rack-based-Server
# setup:
# download macruby
# install control_tower: sudo macgem install control_tower (script uses version 1.0 so far)
#
#
# XML-Schema is used on Cisco-IP-Telephones
# TODO: Cisco-Versions?
# Partial list return (32 entry each iteration)?
# Handle some more client-notations?
# get client connect infos, switch case for different connect devices
# Webmask for managing Addressbook entries remotely
# should be possible to set iteration length, default 32
#!/usr/local/bin/macruby
# get Cocoa AddressBook API
framework 'AddressBook'
# AddressBookServer Class
class ABServer
# called each time accessed serveraddress
def call(env)
# get parameters, which might be calling
query = env['QUERY_STRING'].split('=')
contactstart = 0
# get all contacts
allcontacts = ABAddressBook.sharedAddressBook.people
# offset=32 : offset is command, then set starting value for contacts
if query.count == 2 && query[0].eql?('offset')
if( contactstart >= allcontacts.count )
contactstart = allcontacts.count - 1
else
contactstart = Integer(query[1])
end
end
# split all contacts on start and about 32 entries
contacts = allcontacts.slice(contactstart, 32)
xml_data = ""
for record in contacts do
# KABPhonePropery is a MultiValue, so we need a Dictionary
phone_numbers = record.valueForProperty(KABPhoneProperty)
name = record.valueForProperty(KABFirstNameProperty).to_s + " " + record.valueForProperty(KABLastNameProperty).to_s
# Phone numbers of record are set when they are of type ABMultiValueCoreDataWrapper
if( phone_numbers.instance_of? ABMultiValueCoreDataWrapper )
xml_data += "\t<DirectoryEntry>\n\t<Name>" + name + "</Name>\n"
# count each phone entry and create for each a new <Telephone>-Entry
count = phone_numbers.count - 1
while count >= 0
# phone entry is not empty, create a Telephone-Tag for it
if !phone_numbers.valueAtIndex(count).nil?
xml_data += "\t\t<Telephone>" + phone_numbers.valueAtIndex(count) + "</Telephone>\n"
end
count -= 1
end
# close Entry for this person of iteration
xml_data += "\t</DirectoryEntry>\n"
end
end
# return xml_data
[200, { 'Content-Type' => 'text/xml' }, "
<CiscoIPPhoneDirectory>\n
\t<Title>Addressbook-Server for Cisco</Title>\n
\t<Prompt>say yay or nay?!</Prompt>\n" + xml_data +
"</CiscoIPPhoneDirectory>
"]
end
end
# take off new server instance
server = ABServer.new
run server
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment