-
Notifications
You must be signed in to change notification settings - Fork 0
/
highrise.rb
140 lines (112 loc) · 3.16 KB
/
highrise.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
require 'rubygems'
require 'active_resource'
module Highrise
module Pagination
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
def find_all_across_pages(options = {})
records = []
each(options) { |record| records << record }
records
end
def each(options = {})
options[:params] ||= {}
options[:params][:n] = 0
loop do
if (records = self.find(:all, options)).any?
records.each { |record| yield record }
options[:params][:n] += records.size
else
break # no people included on that page, thus no more people total
end
end
end
end
end
class Base < ActiveResource::Base
self.site = ENV['SITE']
end
# Abstract super-class, don't instantiate directly. Use Kase, Company, Person instead.
class Subject < Base
def notes
Note.find_all_across_pages(:from => "/#{self.class.collection_name}/#{id}/notes.xml")
end
def emails
Email.find_all_across_pages(:from => "/#{self.class.collection_name}/#{id}/emails.xml")
end
def upcoming_tasks
Task.find(:all, :from => "/#{self.class.collection_name}/#{id}/tasks.xml")
end
end
# The Kase class doesn't offer a regular find(:all). Instead it's split into open and closed cases:
#
# open_classes = Kase.find(:all, :from => :open)
# closed_classes = Kase.find(:all, :from => :closed)
class Kase < Subject
def close!
self.closed_at = Time.now.utc
save
end
end
class Person < Subject
include Pagination
def self.find_all_across_pages_since(time)
find_all_across_pages(:params => { :since => time.to_s(:db).gsub(/[^\d]/, '') })
end
def company
Company.find(company_id) if company_id
end
def name
"#{first_name} #{last_name}".strip
end
end
class Company < Subject
include Pagination
def self.find_all_across_pages_since(time)
find_all_across_pages(:params => { :since => time.to_s(:db).gsub(/[^\d]/, '') })
end
def people
Person.find_all_across_pages(:from => "/companies/#{id}/people.xml")
end
end
class Note < Base
include Pagination
def comments
Comment.find(:all, :from => "/notes/#{id}/comments.xml")
end
end
class Email < Base
include Pagination
def comments
Comment.find(:all, :from => "/emails/#{email_id}/comments.xml")
end
end
class Comment < Base
end
# The Kase class doesn't offer a regular find(:all). Instead it's split into upcoming, assigned, and completed:
#
# open_classes = Kase.find(:all, :from => :open)
# closed_classes = Kase.find(:all, :from => :closed)
class Task < Base
# find(:all, :from => :upcoming)
# find(:all, :from => :assigned)
# find(:all, :from => :completed)
def complete!
load_attributes_from_response(post(:complete))
end
end
class User < Base
def join(group)
Membership.create(:user_id => id, :group_id => group.id)
end
end
class Group < Base
# Auto-loads the users collection
end
class Membership < Base
end
class Account < Base
end
end