Created
August 31, 2012 10:42
-
-
Save timurvafin/3551356 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
$LOAD_PATH.unshift File.join(File.dirname(__FILE__), '../lib') | |
require 'rubygems' | |
require 'bundler/setup' | |
require 'highrise' | |
require 'notescript' | |
HIGHRISE_SITE = 'https://fsateam.highrisehq.com' | |
HIGHRISE_USER = '016387efef4dd406559aaec5696feae5' | |
NOTES = ['note one blah-blah-blah', 'ko-ko-ko kudah-tah-tah'] | |
email = ARGV.shift || raise("User should be specified") | |
NoteScript.setup_highrise(HIGHRISE_SITE, HIGHRISE_USER) | |
note = NoteScript.new(email, NOTES).create! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class NoteScript | |
def self.setup_highrise(site, user) | |
Highrise::Base.site = site | |
Highrise::Base.user = user | |
Highrise::Base.format = :xml | |
end | |
def initialize(email, notes) | |
@email, @notes = email, notes | |
end | |
def create! | |
@person = find_person || create_person | |
create_notes | |
end | |
private | |
def persons | |
@persons ||= Highrise::Person.all | |
end | |
def find_person | |
persons.each do |person| | |
if person.contact_data.email_addresses.find { |email| email.address == @email } | |
puts "Person found with email #{@email}" | |
return person | |
end | |
end | |
nil | |
end | |
def create_person | |
puts "No person found. Creating new person with email #{@email}" | |
Highrise::Person.create( | |
:first_name => @email.split('@').first, | |
:contact_data => { | |
:email_addresses => [:address => @email, :location => 'Work'] | |
} | |
) | |
end | |
def person_notes | |
@person_notes ||= @person.notes.map{ |note| note.body } | |
end | |
def create_notes | |
@notes.each do |note| | |
create_note(note) unless note_exists?(note) | |
end | |
end | |
def create_note(body) | |
note = @person.add_note | |
note.body = body | |
note.save | |
puts "...#{note.body}..." | |
end | |
def note_exists?(note) | |
person_notes.include?(note) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment