Skip to content

Commit 6b881c3

Browse files
authored
Merge pull request #127 from sparkapi/model-extractions
Model extractions
2 parents ea0afcb + d5e1d80 commit 6b881c3

File tree

5 files changed

+120
-0
lines changed

5 files changed

+120
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ tmp/*
1717
.buildpath
1818
.project
1919
*.swp
20+
.bundle

lib/spark_api/models.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
require 'spark_api/models/concerns'
88

99
require 'spark_api/models/account'
10+
require 'spark_api/models/account_report'
11+
require 'spark_api/models/account_roster'
1012
require 'spark_api/models/activity'
1113
require 'spark_api/models/connect_prefs'
1214
require 'spark_api/models/contact'
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
module SparkApi
2+
module Models
3+
class AccountReport < Account
4+
def self.report(account_id, arguments={})
5+
collect(connection.get("/accounts/#{account_id}/report", arguments)).first
6+
end
7+
8+
def DisplayName
9+
self.Name
10+
end
11+
12+
def primary_email
13+
if Array(emails).any? && emails.primary
14+
emails.primary.Address
15+
end
16+
end
17+
18+
def primary_phone
19+
if Array(phones).any? && phones.primary
20+
phones.primary.Number
21+
end
22+
end
23+
24+
def logo
25+
if images.kind_of? Array
26+
images.find { |image| image.Type == "Logo" }
27+
end
28+
end
29+
end
30+
end
31+
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module SparkApi
2+
module Models
3+
class AccountRoster < Account
4+
def self.roster(account_id, arguments={})
5+
collect(connection.get("/accounts/#{account_id}/roster", arguments)).first
6+
end
7+
end
8+
end
9+
end
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
require './spec/spec_helper'
2+
3+
describe AccountReport do
4+
5+
let(:account_report) {
6+
AccountReport.new({
7+
"Id" => "12345",
8+
"Name" => "Agent McAgentson",
9+
"Office" => "Office Name",
10+
"Emails"=> [],
11+
"Phones"=> [],
12+
"Websites"=> [],
13+
"Addresses"=> []
14+
})
15+
}
16+
17+
describe 'primary_email' do
18+
19+
it 'returns the primary email address' do
20+
account_report.emails << double(:Address => 'foo@foo.com', :primary? => true)
21+
expect(account_report.primary_email).to eq account_report.emails.primary.Address
22+
end
23+
24+
it 'returns nil when there is no primary email address' do
25+
account_report.emails << double(:Address => 'foo@foo.com', :primary? => false)
26+
expect(account_report.primary_email).to eq nil
27+
end
28+
29+
it 'returns nil when there are no email addresses' do
30+
allow(account_report).to receive(:emails).and_return nil
31+
expect(account_report.primary_email).to eq nil
32+
end
33+
34+
end
35+
36+
describe 'primary_phone' do
37+
38+
it 'returns the primary phone number' do
39+
account_report.phones << double(:Number => '88', :primary? => true)
40+
expect(account_report.primary_phone).to eq account_report.phones.primary.Number
41+
end
42+
43+
it 'returns nil when there is no primary phone number' do
44+
account_report.phones << double(:Number => '88', :primary? => false)
45+
expect(account_report.primary_phone).to eq nil
46+
end
47+
48+
it 'returns nil when there are no phone numbers' do
49+
allow(account_report).to receive(:phones).and_return nil
50+
expect(account_report.primary_phone).to eq nil
51+
end
52+
53+
end
54+
55+
describe 'logo' do
56+
57+
it 'returns the logo' do
58+
logo = SparkApi::Models::Base.new( {"Type" => "Logo"} )
59+
not_logo = SparkApi::Models::Base.new( {"Type" => "Nope" } )
60+
account_report.images = [logo, not_logo]
61+
expect(account_report.logo).to be logo
62+
end
63+
64+
it 'returns nil if there is no logo' do
65+
not_logo = SparkApi::Models::Base.new( {"Type" => "Nope" } )
66+
account_report.images = [not_logo]
67+
expect(account_report.logo).to be nil
68+
end
69+
70+
it 'returns nil if there are no images' do
71+
expect(account_report.images).to be nil
72+
expect(account_report.logo).to be nil
73+
end
74+
75+
end
76+
77+
end

0 commit comments

Comments
 (0)