Skip to content

Commit 93a546d

Browse files
jonnyomchoran
authored andcommittedOct 2, 2018
Allowing company users to be listed by company_id (#428)
* Allowing company users to be listed by company_id * Removing require
1 parent 74e9c43 commit 93a546d

File tree

4 files changed

+36
-36
lines changed

4 files changed

+36
-36
lines changed
 

‎README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,10 @@ intercom.companies.save(company)
132132
# Iterate over all companies
133133
intercom.companies.all.each {|company| puts %Q(#{company.name} - #{company.custom_attributes["referral_source"]}) }
134134
intercom.companies.all.map {|company| company.name }
135-
# Get a list of users in a company
136-
intercom.companies.users(company.id)
135+
# Get a list of users in a company by Intercom Company ID
136+
intercom.companies.users_by_intercom_company_id(company.id)
137+
# Get a list of users in a company by external company_id
138+
intercom.companies.users_by_company_id(company.company_id)
137139
# Get a large list of companies using scroll
138140
intercom.companies.scroll.each { |comp| puts comp.name}
139141
# Please see users scroll for more details of how to use scroll

‎lib/intercom/extended_api_operations/users.rb

-16
This file was deleted.

‎lib/intercom/service/company.rb

+12-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
require 'intercom/api_operations/find_all'
66
require 'intercom/api_operations/save'
77
require 'intercom/api_operations/load'
8-
require 'intercom/extended_api_operations/users'
98
require 'intercom/extended_api_operations/tags'
109
require 'intercom/extended_api_operations/segments'
1110

@@ -18,13 +17,24 @@ class Company < BaseService
1817
include ApiOperations::List
1918
include ApiOperations::Scroll
2019
include ApiOperations::Save
21-
include ExtendedApiOperations::Users
2220
include ExtendedApiOperations::Tags
2321
include ExtendedApiOperations::Segments
2422

2523
def collection_class
2624
Intercom::Company
2725
end
26+
27+
def users_by_intercom_company_id(id)
28+
get_users(url: "/companies/#{id}/users")
29+
end
30+
31+
def users_by_company_id(id)
32+
get_users(url: "/companies", params: { company_id: id, type: "user" })
33+
end
34+
35+
private def get_users(url:, params: {})
36+
ClientCollectionProxy.new("users", finder_details: { url: url, params: params }, client: @client)
37+
end
2838
end
2939
end
3040
end

‎spec/unit/intercom/company_spec.rb

+20-16
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,40 @@
1-
require 'spec_helper'
1+
require "spec_helper"
22

33
describe Intercom::Company do
4-
let (:client) { Intercom::Client.new(app_id: 'app_id', api_key: 'api_key') }
4+
let (:client) { Intercom::Client.new(app_id: "app_id", api_key: "api_key") }
55

6-
describe 'when no response raises error' do
7-
it 'on find' do
8-
client.expects(:get).with("/companies", {:company_id => '4'}).returns(nil)
9-
proc {client.companies.find(:company_id => '4')}.must_raise Intercom::HttpError
6+
describe "when no response raises error" do
7+
it "on find" do
8+
client.expects(:get).with("/companies", {:company_id => "4"}).returns(nil)
9+
proc {client.companies.find(:company_id => "4")}.must_raise Intercom::HttpError
1010
end
1111

12-
it 'on find_all' do
12+
it "on find_all" do
1313
client.expects(:get).with("/companies", {}).returns(nil)
1414
proc {client.companies.all.each {|company| }}.must_raise Intercom::HttpError
1515
end
1616

17-
it 'on load' do
18-
client.expects(:get).with("/companies", {:company_id => '4'}).returns({'type' =>'user', 'id' =>'aaaaaaaaaaaaaaaaaaaaaaaa', 'company_id' => '4', 'name' => 'MyCo'})
19-
company = client.companies.find(:company_id => '4')
20-
client.expects(:get).with('/companies/aaaaaaaaaaaaaaaaaaaaaaaa', {}).returns(nil)
17+
it "on load" do
18+
client.expects(:get).with("/companies", {:company_id => "4"}).returns({"type" =>"user", "id" =>"aaaaaaaaaaaaaaaaaaaaaaaa", "company_id" => "4", "name" => "MyCo"})
19+
company = client.companies.find(:company_id => "4")
20+
client.expects(:get).with("/companies/aaaaaaaaaaaaaaaaaaaaaaaa", {}).returns(nil)
2121
proc {client.companies.load(company)}.must_raise Intercom::HttpError
2222
end
2323
end
2424

25-
it 'gets users in a company' do
25+
it "gets users in a company by intercom ID" do
2626
client.expects(:get).with("/companies/abc123/users", {}).returns(page_of_users(false))
27-
client.companies.users('abc123').each do |u|
28-
end
27+
client.companies.users_by_intercom_company_id("abc123").each { |u| }
28+
end
29+
30+
it "gets users in a company by external company ID" do
31+
client.expects(:get).with("/companies", { company_id: "abc123", type: "user" }).returns(page_of_users(false))
32+
client.companies.users_by_company_id("abc123").each { |u| }
2933
end
3034

31-
it 'finds a company' do
35+
it "finds a company" do
3236
client.expects(:get).with("/companies/531ee472cce572a6ec000006", {}).returns(test_company)
33-
company = client.companies.find(id: '531ee472cce572a6ec000006')
37+
company = client.companies.find(id: "531ee472cce572a6ec000006")
3438
company.name.must_equal("Blue Sun")
3539
end
3640
end

0 commit comments

Comments
 (0)
Please sign in to comment.