Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,10 @@ intercom.companies.save(company)
# Iterate over all companies
intercom.companies.all.each {|company| puts %Q(#{company.name} - #{company.custom_attributes["referral_source"]}) }
intercom.companies.all.map {|company| company.name }
# Get a list of users in a company
intercom.companies.users(company.id)
# Get a list of users in a company by Intercom Company ID
intercom.companies.users_by_intercom_company_id(company.id)
# Get a list of users in a company by external company_id
intercom.companies.users_by_company_id(company.company_id)
# Get a large list of companies using scroll
intercom.companies.scroll.each { |comp| puts comp.name}
# Please see users scroll for more details of how to use scroll
Expand Down
16 changes: 0 additions & 16 deletions lib/intercom/extended_api_operations/users.rb

This file was deleted.

14 changes: 12 additions & 2 deletions lib/intercom/service/company.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
require 'intercom/api_operations/find_all'
require 'intercom/api_operations/save'
require 'intercom/api_operations/load'
require 'intercom/extended_api_operations/users'
require 'intercom/extended_api_operations/tags'
require 'intercom/extended_api_operations/segments'

Expand All @@ -18,13 +17,24 @@ class Company < BaseService
include ApiOperations::List
include ApiOperations::Scroll
include ApiOperations::Save
include ExtendedApiOperations::Users
include ExtendedApiOperations::Tags
include ExtendedApiOperations::Segments

def collection_class
Intercom::Company
end

def users_by_intercom_company_id(id)
get_users(url: "/companies/#{id}/users")
end

def users_by_company_id(id)
get_users(url: "/companies", params: { company_id: id, type: "user" })
end

private def get_users(url:, params: {})
ClientCollectionProxy.new("users", finder_details: { url: url, params: params }, client: @client)
end
end
end
end
36 changes: 20 additions & 16 deletions spec/unit/intercom/company_spec.rb
Original file line number Diff line number Diff line change
@@ -1,36 +1,40 @@
require 'spec_helper'
require "spec_helper"

describe Intercom::Company do
let (:client) { Intercom::Client.new(app_id: 'app_id', api_key: 'api_key') }
let (:client) { Intercom::Client.new(app_id: "app_id", api_key: "api_key") }

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

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

it 'on load' do
client.expects(:get).with("/companies", {:company_id => '4'}).returns({'type' =>'user', 'id' =>'aaaaaaaaaaaaaaaaaaaaaaaa', 'company_id' => '4', 'name' => 'MyCo'})
company = client.companies.find(:company_id => '4')
client.expects(:get).with('/companies/aaaaaaaaaaaaaaaaaaaaaaaa', {}).returns(nil)
it "on load" do
client.expects(:get).with("/companies", {:company_id => "4"}).returns({"type" =>"user", "id" =>"aaaaaaaaaaaaaaaaaaaaaaaa", "company_id" => "4", "name" => "MyCo"})
company = client.companies.find(:company_id => "4")
client.expects(:get).with("/companies/aaaaaaaaaaaaaaaaaaaaaaaa", {}).returns(nil)
proc {client.companies.load(company)}.must_raise Intercom::HttpError
end
end

it 'gets users in a company' do
it "gets users in a company by intercom ID" do
client.expects(:get).with("/companies/abc123/users", {}).returns(page_of_users(false))
client.companies.users('abc123').each do |u|
end
client.companies.users_by_intercom_company_id("abc123").each { |u| }
end

it "gets users in a company by external company ID" do
client.expects(:get).with("/companies", { company_id: "abc123", type: "user" }).returns(page_of_users(false))
client.companies.users_by_company_id("abc123").each { |u| }
end

it 'finds a company' do
it "finds a company" do
client.expects(:get).with("/companies/531ee472cce572a6ec000006", {}).returns(test_company)
company = client.companies.find(id: '531ee472cce572a6ec000006')
company = client.companies.find(id: "531ee472cce572a6ec000006")
company.name.must_equal("Blue Sun")
end
end