intercom 3.5.10 → 4.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.circleci/config.yml +35 -0
- data/.github/PULL_REQUEST_TEMPLATE.md +5 -0
- data/Gemfile +1 -4
- data/README.md +418 -216
- data/RELEASING.md +9 -0
- data/Rakefile +1 -1
- data/changes.txt +141 -0
- data/intercom.gemspec +1 -2
- data/lib/intercom.rb +34 -19
- data/lib/intercom/api_operations/archive.rb +16 -0
- data/lib/intercom/api_operations/delete.rb +4 -1
- data/lib/intercom/api_operations/find.rb +5 -2
- data/lib/intercom/api_operations/find_all.rb +4 -3
- data/lib/intercom/api_operations/list.rb +4 -1
- data/lib/intercom/api_operations/load.rb +4 -2
- data/lib/intercom/api_operations/nested_resource.rb +70 -0
- data/lib/intercom/api_operations/request_hard_delete.rb +12 -0
- data/lib/intercom/api_operations/save.rb +6 -4
- data/lib/intercom/api_operations/scroll.rb +4 -5
- data/lib/intercom/api_operations/search.rb +18 -0
- data/lib/intercom/article.rb +7 -0
- data/lib/intercom/base_collection_proxy.rb +72 -0
- data/lib/intercom/client.rb +66 -18
- data/lib/intercom/client_collection_proxy.rb +17 -39
- data/lib/intercom/collection.rb +7 -0
- data/lib/intercom/company.rb +8 -0
- data/lib/intercom/contact.rb +22 -3
- data/lib/intercom/conversation.rb +5 -0
- data/lib/intercom/data_attribute.rb +7 -0
- data/lib/intercom/deprecated_leads_collection_proxy.rb +22 -0
- data/lib/intercom/deprecated_resources.rb +13 -0
- data/lib/intercom/errors.rb +44 -4
- data/lib/intercom/extended_api_operations/segments.rb +3 -1
- data/lib/intercom/extended_api_operations/tags.rb +3 -1
- data/lib/intercom/lead.rb +21 -0
- data/lib/intercom/lib/typed_json_deserializer.rb +42 -37
- data/lib/intercom/note.rb +4 -0
- data/lib/intercom/request.rb +162 -95
- data/lib/intercom/scroll_collection_proxy.rb +38 -42
- data/lib/intercom/search_collection_proxy.rb +47 -0
- data/lib/intercom/section.rb +23 -0
- data/lib/intercom/segment.rb +4 -0
- data/lib/intercom/service/article.rb +20 -0
- data/lib/intercom/service/base_service.rb +13 -0
- data/lib/intercom/service/collection.rb +24 -0
- data/lib/intercom/service/company.rb +4 -2
- data/lib/intercom/service/contact.rb +29 -6
- data/lib/intercom/service/conversation.rb +23 -2
- data/lib/intercom/service/data_attribute.rb +20 -0
- data/lib/intercom/service/event.rb +12 -0
- data/lib/intercom/service/lead.rb +41 -0
- data/lib/intercom/service/note.rb +4 -8
- data/lib/intercom/service/section.rb +7 -0
- data/lib/intercom/service/tag.rb +8 -8
- data/lib/intercom/service/team.rb +17 -0
- data/lib/intercom/service/user.rb +4 -2
- data/lib/intercom/service/visitor.rb +15 -6
- data/lib/intercom/tag.rb +4 -0
- data/lib/intercom/team.rb +7 -0
- data/lib/intercom/traits/api_resource.rb +48 -27
- data/lib/intercom/traits/dirty_tracking.rb +8 -1
- data/lib/intercom/user.rb +12 -3
- data/lib/intercom/utils.rb +13 -2
- data/lib/intercom/version.rb +1 -1
- data/lib/intercom/visitor.rb +0 -2
- data/spec/spec_helper.rb +881 -436
- data/spec/unit/intercom/admin_spec.rb +2 -2
- data/spec/unit/intercom/article_spec.rb +40 -0
- data/spec/unit/intercom/base_collection_proxy_spec.rb +30 -0
- data/spec/unit/intercom/client_collection_proxy_spec.rb +41 -41
- data/spec/unit/intercom/client_spec.rb +76 -9
- data/spec/unit/intercom/collection_spec.rb +32 -0
- data/spec/unit/intercom/company_spec.rb +29 -21
- data/spec/unit/intercom/contact_spec.rb +365 -29
- data/spec/unit/intercom/conversation_spec.rb +70 -7
- data/spec/unit/intercom/count_spec.rb +4 -4
- data/spec/unit/intercom/data_attribute_spec.rb +40 -0
- data/spec/unit/intercom/deprecated_leads_collection_proxy_spec.rb +17 -0
- data/spec/unit/intercom/event_spec.rb +25 -8
- data/spec/unit/intercom/job_spec.rb +24 -24
- data/spec/unit/intercom/lead_spec.rb +57 -0
- data/spec/unit/intercom/lib/flat_store_spec.rb +22 -20
- data/spec/unit/intercom/message_spec.rb +1 -1
- data/spec/unit/intercom/note_spec.rb +4 -10
- data/spec/unit/intercom/request_spec.rb +150 -9
- data/spec/unit/intercom/scroll_collection_proxy_spec.rb +40 -39
- data/spec/unit/intercom/search_collection_proxy_spec.rb +60 -0
- data/spec/unit/intercom/section_spec.rb +32 -0
- data/spec/unit/intercom/segment_spec.rb +2 -2
- data/spec/unit/intercom/subscription_spec.rb +5 -6
- data/spec/unit/intercom/tag_spec.rb +22 -14
- data/spec/unit/intercom/team_spec.rb +21 -0
- data/spec/unit/intercom/traits/api_resource_spec.rb +129 -47
- data/spec/unit/intercom/user_spec.rb +227 -217
- data/spec/unit/intercom/visitor_spec.rb +49 -0
- data/spec/unit/intercom_spec.rb +5 -3
- metadata +63 -26
- data/.travis.yml +0 -6
- data/lib/intercom/extended_api_operations/users.rb +0 -16
- data/spec/unit/intercom/visitors_spec.rb +0 -61
@@ -0,0 +1,21 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe "Intercom::Team" do
|
4
|
+
let(:client) { Intercom::Client.new(token: 'token') }
|
5
|
+
|
6
|
+
it "returns a CollectionProxy for all without making any requests" do
|
7
|
+
client.expects(:execute_request).never
|
8
|
+
all = client.teams.all
|
9
|
+
_(all).must_be_instance_of(Intercom::ClientCollectionProxy)
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'gets an team list' do
|
13
|
+
client.expects(:get).with("/teams", {}).returns(test_team_list)
|
14
|
+
client.teams.all.each { |t| }
|
15
|
+
end
|
16
|
+
|
17
|
+
it "gets an team" do
|
18
|
+
client.expects(:get).with("/teams/1234", {}).returns(test_team)
|
19
|
+
client.teams.find(:id => "1234")
|
20
|
+
end
|
21
|
+
end
|
@@ -1,93 +1,175 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
2
4
|
|
3
5
|
describe Intercom::Traits::ApiResource do
|
4
6
|
let(:object_json) do
|
5
|
-
{
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
7
|
+
{ 'type' => 'company',
|
8
|
+
'id' => 'aaaaaaaaaaaaaaaaaaaaaaaa',
|
9
|
+
'app_id' => 'some-app-id',
|
10
|
+
'name' => 'SuperSuite',
|
11
|
+
'plan_id' => 1,
|
12
|
+
'remote_company_id' => '8',
|
13
|
+
'remote_created_at' => 103_201,
|
14
|
+
'created_at' => 1_374_056_196,
|
15
|
+
'user_count' => 1,
|
16
|
+
'custom_attributes' => {},
|
17
|
+
'metadata' => {
|
18
|
+
'type' => 'user',
|
19
|
+
'color' => 'cyan'
|
20
|
+
},
|
21
|
+
'nested_fields' => {
|
22
|
+
'type' => 'nested_fields_content',
|
23
|
+
'field_1' => {
|
24
|
+
'type' => 'field_content',
|
25
|
+
'name' => 'Nested Field'
|
26
|
+
}
|
27
|
+
}
|
28
|
+
}
|
29
|
+
end
|
23
30
|
|
24
|
-
|
25
|
-
|
31
|
+
let(:object_hash) do
|
32
|
+
{
|
33
|
+
type: 'company',
|
34
|
+
id: 'aaaaaaaaaaaaaaaaaaaaaaaa',
|
35
|
+
app_id: 'some-app-id',
|
36
|
+
name: 'SuperSuite',
|
37
|
+
plan_id: 1,
|
38
|
+
remote_company_id: '8',
|
39
|
+
remote_created_at: 103_201,
|
40
|
+
created_at: 1_374_056_196,
|
41
|
+
user_count: 1,
|
42
|
+
custom_attributes: { type: 'ping' },
|
43
|
+
metadata: {
|
44
|
+
type: 'user',
|
45
|
+
color: 'cyan'
|
46
|
+
},
|
47
|
+
nested_fields: {
|
48
|
+
type: 'nested_fields_content',
|
49
|
+
field_1: {
|
50
|
+
type: 'field_content',
|
51
|
+
name: 'Nested Field'
|
52
|
+
}
|
53
|
+
}
|
54
|
+
}
|
26
55
|
end
|
27
56
|
|
28
|
-
|
29
|
-
|
57
|
+
let(:api_resource) { DummyClass.new.extend(Intercom::Traits::ApiResource) }
|
58
|
+
|
59
|
+
before(:each) { api_resource.from_response(object_json) }
|
60
|
+
|
61
|
+
it 'coerces time on parsing json' do
|
62
|
+
assert_equal Time.at(1_374_056_196), api_resource.created_at
|
30
63
|
end
|
31
64
|
|
32
|
-
it
|
33
|
-
assert_equal Time.at(
|
65
|
+
it 'exposes string' do
|
66
|
+
assert_equal Time.at(1_374_056_196), api_resource.created_at
|
34
67
|
end
|
35
68
|
|
36
69
|
it "treats 'metadata' as a plain hash, not a typed object" do
|
37
70
|
assert_equal Hash, api_resource.metadata.class
|
38
71
|
end
|
39
72
|
|
40
|
-
it
|
41
|
-
api_resource.wont_respond_to :spiders
|
73
|
+
it 'dynamically defines accessors when a non-existent property is called that looks like a setter' do
|
74
|
+
_(api_resource).wont_respond_to :spiders
|
42
75
|
api_resource.spiders = 4
|
43
|
-
api_resource.must_respond_to :spiders
|
76
|
+
_(api_resource).must_respond_to :spiders
|
44
77
|
end
|
45
78
|
|
46
|
-
it
|
79
|
+
it 'calls dynamically defined getter when asked' do
|
47
80
|
api_resource.foo = 4
|
48
81
|
assert_equal 4, api_resource.foo
|
49
82
|
end
|
50
83
|
|
51
|
-
it
|
52
|
-
api_resource.foo_at =
|
53
|
-
assert_equal
|
84
|
+
it 'accepts unix timestamps into dynamically defined date setters' do
|
85
|
+
api_resource.foo_at = 1_401_200_468
|
86
|
+
assert_equal 1_401_200_468, api_resource.instance_variable_get(:@foo_at)
|
54
87
|
end
|
55
88
|
|
56
|
-
it
|
57
|
-
api_resource.foo_at =
|
58
|
-
assert_equal Time.at(
|
89
|
+
it 'exposes dates correctly for dynamically defined getters' do
|
90
|
+
api_resource.foo_at = 1_401_200_468
|
91
|
+
assert_equal Time.at(1_401_200_468), api_resource.foo_at
|
59
92
|
end
|
60
93
|
|
61
|
-
it
|
94
|
+
it 'throws regular method missing error when non-existent getter is called that is backed by an instance variable' do
|
62
95
|
api_resource.instance_variable_set(:@bar, 'you cant see me')
|
63
|
-
proc { api_resource.bar }.must_raise NoMethodError
|
96
|
+
_(proc { api_resource.bar }).must_raise NoMethodError
|
64
97
|
end
|
65
98
|
|
66
|
-
it
|
67
|
-
proc { api_resource.flubber }.must_raise Intercom::AttributeNotSetError
|
99
|
+
it 'throws attribute not set error when non-existent getter is called that is not backed by an instance variable' do
|
100
|
+
_(proc { api_resource.flubber }).must_raise Intercom::AttributeNotSetError
|
68
101
|
end
|
69
102
|
|
70
|
-
it
|
71
|
-
proc { api_resource.flubber! }.must_raise NoMethodError
|
72
|
-
proc { api_resource.flubber? }.must_raise NoMethodError
|
103
|
+
it 'throws regular method missing error when non-existent method is called that cannot be an accessor' do
|
104
|
+
_(proc { api_resource.flubber! }).must_raise NoMethodError
|
105
|
+
_(proc { api_resource.flubber? }).must_raise NoMethodError
|
73
106
|
end
|
74
107
|
|
75
|
-
it
|
76
|
-
proc { api_resource.send(:flubber=, 'a', 'b') }.must_raise NoMethodError
|
108
|
+
it 'throws regular method missing error when non-existent setter is called with multiple arguments' do
|
109
|
+
_(proc { api_resource.send(:flubber=, 'a', 'b') }).must_raise NoMethodError
|
77
110
|
end
|
78
111
|
|
79
|
-
it
|
112
|
+
it 'an initialized ApiResource is equal to one generated from a response' do
|
80
113
|
class ConcreteApiResource; include Intercom::Traits::ApiResource; end
|
81
114
|
initialized_api_resource = ConcreteApiResource.new(object_json)
|
82
115
|
except(object_json, 'type').keys.each do |attribute|
|
83
116
|
assert_equal initialized_api_resource.send(attribute), api_resource.send(attribute)
|
84
117
|
end
|
85
118
|
end
|
86
|
-
|
119
|
+
|
120
|
+
it 'initialized ApiResource using hash is equal to one generated from response' do
|
121
|
+
class ConcreteApiResource; include Intercom::Traits::ApiResource; end
|
122
|
+
|
123
|
+
api_resource.from_hash(object_hash)
|
124
|
+
initialized_api_resource = ConcreteApiResource.new(object_hash)
|
125
|
+
except(object_json, 'type').keys.each do |attribute|
|
126
|
+
assert_equal initialized_api_resource.send(attribute), api_resource.send(attribute)
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
describe 'correctly equates two resources' do
|
131
|
+
class DummyResource; include Intercom::Traits::ApiResource; end
|
132
|
+
|
133
|
+
specify 'if each resource has the same class and same value' do
|
134
|
+
api_resource1 = DummyResource.new(object_json)
|
135
|
+
api_resource2 = DummyResource.new(object_json)
|
136
|
+
assert_equal (api_resource1 == api_resource2), true
|
137
|
+
end
|
138
|
+
|
139
|
+
specify 'if each resource has the same class and different value' do
|
140
|
+
object2_json = object_json.merge('id' => 'bbbbbb')
|
141
|
+
api_resource1 = DummyResource.new(object_json)
|
142
|
+
api_resource2 = DummyResource.new(object2_json)
|
143
|
+
assert_equal (api_resource1 == api_resource2), false
|
144
|
+
end
|
145
|
+
|
146
|
+
specify 'if each resource has a different class' do
|
147
|
+
dummy_resource = DummyResource.new(object_json)
|
148
|
+
assert_equal (dummy_resource == api_resource), false
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
it 'correctly generates submittable hash when no updates' do
|
153
|
+
assert_equal api_resource.to_submittable_hash, {}
|
154
|
+
end
|
155
|
+
|
156
|
+
it 'correctly generates submittable hash when there are updates' do
|
157
|
+
api_resource.name = 'SuperSuite updated'
|
158
|
+
api_resource.nested_fields.field_1.name = 'Updated Name'
|
159
|
+
assert_equal api_resource.to_submittable_hash, {
|
160
|
+
'name' => 'SuperSuite updated',
|
161
|
+
'nested_fields' => {
|
162
|
+
'field_1' => {
|
163
|
+
'name' => 'Updated Name'
|
164
|
+
}
|
165
|
+
}
|
166
|
+
}
|
167
|
+
end
|
168
|
+
|
87
169
|
def except(h, *keys)
|
88
170
|
keys.each { |key| h.delete(key) }
|
89
171
|
h
|
90
172
|
end
|
91
|
-
|
173
|
+
|
92
174
|
class DummyClass; end
|
93
175
|
end
|
@@ -1,271 +1,284 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe 'Intercom::User' do
|
6
|
+
let (:client) { Intercom::Client.new(token: 'token') }
|
5
7
|
|
6
8
|
it "to_hash'es itself" do
|
7
9
|
created_at = Time.now
|
8
|
-
user = Intercom::User.new(:
|
10
|
+
user = Intercom::User.new(email: 'jim@example.com', user_id: '12345', created_at: created_at, name: 'Jim Bob')
|
9
11
|
as_hash = user.to_hash
|
10
|
-
as_hash[
|
11
|
-
as_hash[
|
12
|
-
as_hash[
|
13
|
-
as_hash[
|
12
|
+
_(as_hash['email']).must_equal 'jim@example.com'
|
13
|
+
_(as_hash['user_id']).must_equal '12345'
|
14
|
+
_(as_hash['created_at']).must_equal created_at.to_i
|
15
|
+
_(as_hash['name']).must_equal 'Jim Bob'
|
14
16
|
end
|
15
17
|
|
16
|
-
it
|
18
|
+
it 'presents created_at and last_impression_at as Date' do
|
17
19
|
now = Time.now
|
18
|
-
user = Intercom::User.from_api(:
|
19
|
-
user.created_at.must_be_kind_of Time
|
20
|
-
user.created_at.to_s.must_equal now.to_s
|
21
|
-
user.last_impression_at.must_be_kind_of Time
|
22
|
-
user.last_impression_at.to_s.must_equal now.to_s
|
20
|
+
user = Intercom::User.from_api(created_at: now, last_impression_at: now)
|
21
|
+
_(user.created_at).must_be_kind_of Time
|
22
|
+
_(user.created_at.to_s).must_equal now.to_s
|
23
|
+
_(user.last_impression_at).must_be_kind_of Time
|
24
|
+
_(user.last_impression_at.to_s).must_equal now.to_s
|
23
25
|
end
|
24
26
|
|
25
|
-
it
|
27
|
+
it 'is throws a Intercom::AttributeNotSetError on trying to access an attribute that has not been set' do
|
26
28
|
user = Intercom::User.new
|
27
|
-
proc { user.foo_property }.must_raise Intercom::AttributeNotSetError
|
29
|
+
_(proc { user.foo_property }).must_raise Intercom::AttributeNotSetError
|
28
30
|
end
|
29
31
|
|
30
|
-
it
|
32
|
+
it 'presents a complete user record correctly' do
|
31
33
|
user = Intercom::User.from_api(test_user)
|
32
|
-
user.user_id.must_equal 'id-from-customers-app'
|
33
|
-
user.email.must_equal 'bob@example.com'
|
34
|
-
user.name.must_equal 'Joe Schmoe'
|
35
|
-
user.app_id.must_equal 'the-app-id'
|
36
|
-
user.session_count.must_equal 123
|
37
|
-
user.created_at.to_i.must_equal
|
38
|
-
user.remote_created_at.to_i.must_equal
|
39
|
-
user.updated_at.to_i.must_equal
|
40
|
-
|
41
|
-
user.avatar.must_be_kind_of Intercom::Avatar
|
42
|
-
user.avatar.image_url.must_equal 'https://graph.facebook.com/1/picture?width=24&height=24'
|
43
|
-
|
44
|
-
user.companies.must_be_kind_of Array
|
45
|
-
user.companies.size.must_equal 1
|
46
|
-
user.companies[0].must_be_kind_of Intercom::Company
|
47
|
-
user.companies[0].company_id.must_equal
|
48
|
-
user.companies[0].id.must_equal
|
49
|
-
user.companies[0].app_id.must_equal
|
50
|
-
user.companies[0].name.must_equal
|
51
|
-
user.companies[0].remote_created_at.to_i.must_equal
|
52
|
-
user.companies[0].created_at.to_i.must_equal
|
53
|
-
user.companies[0].updated_at.to_i.must_equal
|
54
|
-
user.companies[0].last_request_at.to_i.must_equal
|
55
|
-
user.companies[0].monthly_spend.must_equal 0
|
56
|
-
user.companies[0].session_count.must_equal 0
|
57
|
-
user.companies[0].user_count.must_equal 1
|
58
|
-
user.companies[0].tag_ids.must_equal []
|
59
|
-
|
60
|
-
user.custom_attributes.must_be_kind_of Intercom::Lib::FlatStore
|
61
|
-
user.custom_attributes[
|
62
|
-
user.custom_attributes[
|
63
|
-
|
64
|
-
user.social_profiles.size.must_equal 4
|
34
|
+
_(user.user_id).must_equal 'id-from-customers-app'
|
35
|
+
_(user.email).must_equal 'bob@example.com'
|
36
|
+
_(user.name).must_equal 'Joe Schmoe'
|
37
|
+
_(user.app_id).must_equal 'the-app-id'
|
38
|
+
_(user.session_count).must_equal 123
|
39
|
+
_(user.created_at.to_i).must_equal 1_401_970_114
|
40
|
+
_(user.remote_created_at.to_i).must_equal 1_393_613_864
|
41
|
+
_(user.updated_at.to_i).must_equal 1_401_970_114
|
42
|
+
|
43
|
+
_(user.avatar).must_be_kind_of Intercom::Avatar
|
44
|
+
_(user.avatar.image_url).must_equal 'https://graph.facebook.com/1/picture?width=24&height=24'
|
45
|
+
|
46
|
+
_(user.companies).must_be_kind_of Array
|
47
|
+
_(user.companies.size).must_equal 1
|
48
|
+
_(user.companies[0]).must_be_kind_of Intercom::Company
|
49
|
+
_(user.companies[0].company_id).must_equal '123'
|
50
|
+
_(user.companies[0].id).must_equal 'bbbbbbbbbbbbbbbbbbbbbbbb'
|
51
|
+
_(user.companies[0].app_id).must_equal 'the-app-id'
|
52
|
+
_(user.companies[0].name).must_equal 'Company 1'
|
53
|
+
_(user.companies[0].remote_created_at.to_i).must_equal 1_390_936_440
|
54
|
+
_(user.companies[0].created_at.to_i).must_equal 1_401_970_114
|
55
|
+
_(user.companies[0].updated_at.to_i).must_equal 1_401_970_114
|
56
|
+
_(user.companies[0].last_request_at.to_i).must_equal 1_401_970_113
|
57
|
+
_(user.companies[0].monthly_spend).must_equal 0
|
58
|
+
_(user.companies[0].session_count).must_equal 0
|
59
|
+
_(user.companies[0].user_count).must_equal 1
|
60
|
+
_(user.companies[0].tag_ids).must_equal []
|
61
|
+
|
62
|
+
_(user.custom_attributes).must_be_kind_of Intercom::Lib::FlatStore
|
63
|
+
_(user.custom_attributes['a']).must_equal 'b'
|
64
|
+
_(user.custom_attributes['b']).must_equal 2
|
65
|
+
|
66
|
+
_(user.social_profiles.size).must_equal 4
|
65
67
|
twitter_account = user.social_profiles.first
|
66
|
-
twitter_account.must_be_kind_of Intercom::SocialProfile
|
67
|
-
twitter_account.name.must_equal
|
68
|
-
twitter_account.username.must_equal
|
69
|
-
twitter_account.url.must_equal
|
70
|
-
|
71
|
-
user.location_data.must_be_kind_of Intercom::LocationData
|
72
|
-
user.location_data.city_name.must_equal
|
73
|
-
user.location_data.continent_code.must_equal
|
74
|
-
user.location_data.country_name.must_equal
|
75
|
-
user.location_data.latitude.must_equal
|
76
|
-
user.location_data.longitude.must_equal
|
77
|
-
user.location_data.country_code.must_equal
|
78
|
-
|
79
|
-
user.unsubscribed_from_emails.must_equal true
|
80
|
-
user.user_agent_data.must_equal
|
68
|
+
_(twitter_account).must_be_kind_of Intercom::SocialProfile
|
69
|
+
_(twitter_account.name).must_equal 'twitter'
|
70
|
+
_(twitter_account.username).must_equal 'abc'
|
71
|
+
_(twitter_account.url).must_equal 'http://twitter.com/abc'
|
72
|
+
|
73
|
+
_(user.location_data).must_be_kind_of Intercom::LocationData
|
74
|
+
_(user.location_data.city_name).must_equal 'Dublin'
|
75
|
+
_(user.location_data.continent_code).must_equal 'EU'
|
76
|
+
_(user.location_data.country_name).must_equal 'Ireland'
|
77
|
+
_(user.location_data.latitude).must_equal '90'
|
78
|
+
_(user.location_data.longitude).must_equal '10'
|
79
|
+
_(user.location_data.country_code).must_equal 'IRL'
|
80
|
+
|
81
|
+
_(user.unsubscribed_from_emails).must_equal true
|
82
|
+
_(user.user_agent_data).must_equal 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11'
|
81
83
|
end
|
82
84
|
|
83
85
|
it 'allows update_last_request_at' do
|
84
|
-
client.expects(:post).with(
|
85
|
-
client.
|
86
|
+
client.expects(:post).with('/users', 'user_id' => '1224242', 'update_last_request_at' => true, 'custom_attributes' => {}).returns('user_id' => 'i-1224242', 'last_request_at' => 1_414_509_439)
|
87
|
+
client.deprecated__users.create(user_id: '1224242', update_last_request_at: true)
|
86
88
|
end
|
87
89
|
|
88
|
-
it
|
90
|
+
it 'allows easy setting of custom data' do
|
89
91
|
now = Time.now
|
90
92
|
user = Intercom::User.new
|
91
|
-
user.custom_attributes[
|
92
|
-
user.custom_attributes[
|
93
|
-
user.custom_attributes[
|
94
|
-
user.to_hash[
|
93
|
+
user.custom_attributes['mad'] = 123
|
94
|
+
user.custom_attributes['other'] = now.to_i
|
95
|
+
user.custom_attributes['thing'] = 'yay'
|
96
|
+
_(user.to_hash['custom_attributes']).must_equal 'mad' => 123, 'other' => now.to_i, 'thing' => 'yay'
|
95
97
|
end
|
96
98
|
|
97
|
-
it
|
98
|
-
user = Intercom::User.new
|
99
|
+
it 'allows easy setting of multiple companies' do
|
100
|
+
user = Intercom::User.new
|
99
101
|
companies = [
|
100
|
-
{
|
101
|
-
{
|
102
|
+
{ 'name' => 'Intercom', 'company_id' => '6' },
|
103
|
+
{ 'name' => 'Test', 'company_id' => '9' }
|
102
104
|
]
|
103
105
|
user.companies = companies
|
104
|
-
user.to_hash[
|
106
|
+
_(user.to_hash['companies']).must_equal companies
|
105
107
|
end
|
106
108
|
|
107
|
-
it
|
108
|
-
user = Intercom::User.new
|
109
|
+
it 'rejects nested data structures in custom_attributes' do
|
110
|
+
user = Intercom::User.new
|
109
111
|
|
110
|
-
proc { user.custom_attributes[
|
111
|
-
proc { user.custom_attributes[
|
112
|
-
proc { user.custom_attributes[
|
112
|
+
_(proc { user.custom_attributes['thing'] = [1] }).must_raise(ArgumentError)
|
113
|
+
_(proc { user.custom_attributes['thing'] = { 1 => 2 } }).must_raise(ArgumentError)
|
114
|
+
_(proc { user.custom_attributes['thing'] = { 1 => { 2 => 3 } } }).must_raise(ArgumentError)
|
113
115
|
|
114
116
|
user = Intercom::User.from_api(test_user)
|
115
|
-
proc { user.custom_attributes[
|
117
|
+
_(proc { user.custom_attributes['thing'] = [1] }).must_raise(ArgumentError)
|
116
118
|
end
|
117
119
|
|
118
|
-
describe
|
120
|
+
describe 'incrementing custom_attributes fields' do
|
119
121
|
before :each do
|
120
122
|
@now = Time.now
|
121
|
-
@user = Intercom::User.new(
|
123
|
+
@user = Intercom::User.new('email' => 'jo@example.com', :user_id => 'i-1224242', :custom_attributes => { 'mad' => 123, 'another' => 432, 'other' => @now.to_i, :thing => 'yay' })
|
122
124
|
end
|
123
125
|
|
124
|
-
it
|
125
|
-
@user.increment(
|
126
|
-
@user.to_hash[
|
126
|
+
it 'increments up by 1 with no args' do
|
127
|
+
@user.increment('mad')
|
128
|
+
_(@user.to_hash['custom_attributes']['mad']).must_equal 124
|
127
129
|
end
|
128
130
|
|
129
|
-
it
|
130
|
-
@user.increment(
|
131
|
-
@user.to_hash[
|
131
|
+
it 'increments up by given value' do
|
132
|
+
@user.increment('mad', 4)
|
133
|
+
_(@user.to_hash['custom_attributes']['mad']).must_equal 127
|
132
134
|
end
|
133
135
|
|
134
|
-
it
|
135
|
-
@user.increment(
|
136
|
-
@user.to_hash[
|
136
|
+
it 'increments down by given value' do
|
137
|
+
@user.increment('mad', -1)
|
138
|
+
_(@user.to_hash['custom_attributes']['mad']).must_equal 122
|
137
139
|
end
|
138
140
|
|
139
|
-
it
|
140
|
-
@user.increment(
|
141
|
-
@user.to_hash[
|
141
|
+
it 'can increment new custom data fields' do
|
142
|
+
@user.increment('new_field', 3)
|
143
|
+
_(@user.to_hash['custom_attributes']['new_field']).must_equal 3
|
142
144
|
end
|
143
145
|
|
144
|
-
it
|
145
|
-
@user.increment(
|
146
|
-
@user.increment(
|
147
|
-
@user.to_hash[
|
146
|
+
it 'can call increment on the same key twice and increment by 2' do
|
147
|
+
@user.increment('mad')
|
148
|
+
@user.increment('mad')
|
149
|
+
_(@user.to_hash['custom_attributes']['mad']).must_equal 125
|
148
150
|
end
|
149
151
|
end
|
150
152
|
|
151
|
-
describe
|
153
|
+
describe 'decrementing custom_attributes fields' do
|
152
154
|
before :each do
|
153
155
|
@now = Time.now
|
154
|
-
@user = Intercom::User.new(
|
156
|
+
@user = Intercom::User.new('email' => 'jo@example.com', :user_id => 'i-1224242', :custom_attributes => { 'mad' => 123, 'another' => 432, 'other' => @now.to_i, :thing => 'yay' })
|
155
157
|
end
|
156
158
|
|
157
|
-
it
|
158
|
-
@user.decrement(
|
159
|
-
@user.to_hash[
|
159
|
+
it 'decrements down by 1 with no args' do
|
160
|
+
@user.decrement('mad')
|
161
|
+
_(@user.to_hash['custom_attributes']['mad']).must_equal 122
|
160
162
|
end
|
161
163
|
|
162
|
-
it
|
163
|
-
@user.decrement(
|
164
|
-
@user.to_hash[
|
164
|
+
it 'decrements down by given value' do
|
165
|
+
@user.decrement('mad', 3)
|
166
|
+
_(@user.to_hash['custom_attributes']['mad']).must_equal 120
|
165
167
|
end
|
166
168
|
|
167
|
-
it
|
168
|
-
@user.decrement(
|
169
|
-
@user.to_hash[
|
169
|
+
it 'can decrement new custom data fields' do
|
170
|
+
@user.decrement('new_field', 5)
|
171
|
+
_(@user.to_hash['custom_attributes']['new_field']).must_equal(-5)
|
170
172
|
end
|
171
173
|
|
172
|
-
it
|
173
|
-
@user.decrement(
|
174
|
-
@user.decrement(
|
175
|
-
@user.to_hash[
|
174
|
+
it 'can call decrement on the same key twice and decrement by 2' do
|
175
|
+
@user.decrement('mad')
|
176
|
+
@user.decrement('mad')
|
177
|
+
_(@user.to_hash['custom_attributes']['mad']).must_equal 121
|
176
178
|
end
|
177
179
|
end
|
178
180
|
|
179
|
-
it
|
180
|
-
client.expects(:get).with(
|
181
|
-
user = client.
|
182
|
-
user.email.must_equal
|
183
|
-
user.name.must_equal
|
184
|
-
user.session_count.must_equal 123
|
181
|
+
it 'fetches a user' do
|
182
|
+
client.expects(:get).with('/users', 'email' => 'bo@example.com').returns(test_user)
|
183
|
+
user = client.deprecated__users.find('email' => 'bo@example.com')
|
184
|
+
_(user.email).must_equal 'bob@example.com'
|
185
|
+
_(user.name).must_equal 'Joe Schmoe'
|
186
|
+
_(user.session_count).must_equal 123
|
185
187
|
end
|
186
188
|
|
187
189
|
it 'gets users by tag' do
|
188
|
-
client.expects(:get).with(
|
189
|
-
client.
|
190
|
+
client.expects(:get).with('/users?tag_id=124', {}).returns(page_of_users(false))
|
191
|
+
client.deprecated__users.by_tag(124).each { |t| }
|
190
192
|
end
|
191
193
|
|
192
194
|
it 'gets users by segment' do
|
193
|
-
client.expects(:get).with(
|
194
|
-
client.
|
195
|
+
client.expects(:get).with('/users?segment_id=124', {}).returns(page_of_users(false))
|
196
|
+
client.deprecated__users.by_segment(124).each { |t| }
|
195
197
|
end
|
196
198
|
|
197
|
-
it
|
198
|
-
user = Intercom::User.new(
|
199
|
-
client.expects(:post).with(
|
200
|
-
client.
|
199
|
+
it 'saves a user (always sends custom_attributes)' do
|
200
|
+
user = Intercom::User.new('email' => 'jo@example.com', :user_id => 'i-1224242')
|
201
|
+
client.expects(:post).with('/users', 'email' => 'jo@example.com', 'user_id' => 'i-1224242', 'custom_attributes' => {}).returns('email' => 'jo@example.com', 'user_id' => 'i-1224242')
|
202
|
+
client.deprecated__users.save(user)
|
201
203
|
end
|
202
204
|
|
203
|
-
it
|
204
|
-
user = Intercom::User.new(
|
205
|
-
client.expects(:post).with(
|
206
|
-
client.
|
205
|
+
it 'saves a user with a company' do
|
206
|
+
user = Intercom::User.new('email' => 'jo@example.com', :user_id => 'i-1224242', :company => { 'company_id' => 6, 'name' => 'Intercom' })
|
207
|
+
client.expects(:post).with('/users', 'custom_attributes' => {}, 'user_id' => 'i-1224242', 'email' => 'jo@example.com', 'company' => { 'company_id' => 6, 'name' => 'Intercom' }).returns('email' => 'jo@example.com', 'user_id' => 'i-1224242')
|
208
|
+
client.deprecated__users.save(user)
|
207
209
|
end
|
208
210
|
|
209
|
-
it
|
210
|
-
user = Intercom::User.new(
|
211
|
-
client.expects(:post).with(
|
212
|
-
client.
|
211
|
+
it 'saves a user with a companies' do
|
212
|
+
user = Intercom::User.new('email' => 'jo@example.com', :user_id => 'i-1224242', :companies => [{ 'company_id' => 6, 'name' => 'Intercom' }])
|
213
|
+
client.expects(:post).with('/users', 'custom_attributes' => {}, 'email' => 'jo@example.com', 'user_id' => 'i-1224242', 'companies' => [{ 'company_id' => 6, 'name' => 'Intercom' }]).returns('email' => 'jo@example.com', 'user_id' => 'i-1224242')
|
214
|
+
client.deprecated__users.save(user)
|
213
215
|
end
|
214
216
|
|
215
217
|
it 'can save a user with a nil email' do
|
216
|
-
user = Intercom::User.new(
|
217
|
-
client.expects(:post).with(
|
218
|
-
client.
|
218
|
+
user = Intercom::User.new('email' => nil, :user_id => 'i-1224242', :companies => [{ 'company_id' => 6, 'name' => 'Intercom' }])
|
219
|
+
client.expects(:post).with('/users', 'custom_attributes' => {}, 'email' => nil, 'user_id' => 'i-1224242', 'companies' => [{ 'company_id' => 6, 'name' => 'Intercom' }]).returns('email' => nil, 'user_id' => 'i-1224242')
|
220
|
+
client.deprecated__users.save(user)
|
221
|
+
end
|
222
|
+
|
223
|
+
it 'archives a user' do
|
224
|
+
user = Intercom::User.new('id' => '1')
|
225
|
+
client.expects(:delete).with('/users/1', {}).returns(user)
|
226
|
+
client.deprecated__users.archive(user)
|
227
|
+
end
|
228
|
+
it 'has an alias to archive a user' do
|
229
|
+
user = Intercom::User.new('id' => '1')
|
230
|
+
client.expects(:delete).with('/users/1', {}).returns(user)
|
231
|
+
client.deprecated__users.delete(user)
|
219
232
|
end
|
220
233
|
|
221
|
-
it
|
222
|
-
user = Intercom::User.new(
|
223
|
-
client.expects(:
|
224
|
-
client.
|
234
|
+
it 'sends a request for a hard deletion' do
|
235
|
+
user = Intercom::User.new('id' => '1')
|
236
|
+
client.expects(:post).with('/user_delete_requests', intercom_user_id: '1').returns(id: user.id)
|
237
|
+
client.deprecated__users.request_hard_delete(user)
|
225
238
|
end
|
226
239
|
|
227
|
-
it
|
228
|
-
client.expects(:post).with(
|
229
|
-
user = client.
|
230
|
-
user.email.must_equal
|
240
|
+
it 'can use client.users.create for convenience' do
|
241
|
+
client.expects(:post).with('/users', 'custom_attributes' => {}, 'email' => 'jo@example.com', 'user_id' => 'i-1224242').returns('email' => 'jo@example.com', 'user_id' => 'i-1224242')
|
242
|
+
user = client.deprecated__users.create('email' => 'jo@example.com', :user_id => 'i-1224242')
|
243
|
+
_(user.email).must_equal 'jo@example.com'
|
231
244
|
end
|
232
245
|
|
233
|
-
it
|
234
|
-
client.expects(:post).with(
|
235
|
-
user = client.
|
236
|
-
user.session_count.must_equal 4
|
246
|
+
it 'updates the user with attributes as set by the server' do
|
247
|
+
client.expects(:post).with('/users', 'email' => 'jo@example.com', 'user_id' => 'i-1224242', 'custom_attributes' => {}).returns('email' => 'jo@example.com', 'user_id' => 'i-1224242', 'session_count' => 4)
|
248
|
+
user = client.deprecated__users.create('email' => 'jo@example.com', :user_id => 'i-1224242')
|
249
|
+
_(user.session_count).must_equal 4
|
237
250
|
end
|
238
251
|
|
239
|
-
it
|
240
|
-
client.expects(:post).with(
|
241
|
-
user = client.
|
242
|
-
user.remote_created_at
|
252
|
+
it 'allows setting dates to nil without converting them to 0' do
|
253
|
+
client.expects(:post).with('/users', 'email' => 'jo@example.com', 'custom_attributes' => {}, 'remote_created_at' => nil).returns('email' => 'jo@example.com')
|
254
|
+
user = client.deprecated__users.create('email' => 'jo@example.com', 'remote_created_at' => nil)
|
255
|
+
assert_nil user.remote_created_at
|
243
256
|
end
|
244
257
|
|
245
|
-
it
|
246
|
-
params = {
|
258
|
+
it 'sets/gets rw keys' do
|
259
|
+
params = { 'email' => 'me@example.com', :user_id => 'abc123', 'name' => 'Bob Smith', 'last_seen_ip' => '1.2.3.4', 'last_seen_user_agent' => 'ie6', 'created_at' => Time.now }
|
247
260
|
user = Intercom::User.new(params)
|
248
261
|
custom_attributes = (params.keys + ['custom_attributes']).map(&:to_s).sort
|
249
|
-
user.to_hash.keys.sort.must_equal custom_attributes
|
262
|
+
_(user.to_hash.keys.sort).must_equal custom_attributes
|
250
263
|
params.keys.each do |key|
|
251
|
-
user.send(key).to_s.must_equal params[key].to_s
|
264
|
+
_(user.send(key).to_s).must_equal params[key].to_s
|
252
265
|
end
|
253
266
|
end
|
254
267
|
|
255
|
-
it
|
256
|
-
user = Intercom::User.send(:from_api,
|
257
|
-
user.new_param.must_equal
|
268
|
+
it 'will allow extra attributes in response from api' do
|
269
|
+
user = Intercom::User.send(:from_api, 'new_param' => 'some value')
|
270
|
+
_(user.new_param).must_equal 'some value'
|
258
271
|
end
|
259
272
|
|
260
|
-
it
|
273
|
+
it 'returns a ClientCollectionProxy for all without making any requests' do
|
261
274
|
client.expects(:execute_request).never
|
262
|
-
all = client.
|
263
|
-
all.must_be_instance_of(Intercom::ClientCollectionProxy)
|
275
|
+
all = client.deprecated__users.all
|
276
|
+
_(all).must_be_instance_of(Intercom::ClientCollectionProxy)
|
264
277
|
end
|
265
278
|
|
266
279
|
it 'can print users without crashing' do
|
267
|
-
client.expects(:get).with(
|
268
|
-
user = client.
|
280
|
+
client.expects(:get).with('/users', 'email' => 'bo@example.com').returns(test_user)
|
281
|
+
user = client.deprecated__users.find('email' => 'bo@example.com')
|
269
282
|
|
270
283
|
begin
|
271
284
|
orignal_stdout = $stdout
|
@@ -273,91 +286,88 @@ describe "Intercom::User" do
|
|
273
286
|
|
274
287
|
puts user
|
275
288
|
p user
|
276
|
-
|
277
289
|
ensure
|
278
290
|
$stdout = orignal_stdout
|
279
291
|
end
|
280
292
|
end
|
281
293
|
|
282
294
|
describe 'bulk operations' do
|
283
|
-
let (:job)
|
295
|
+
let (:job) do
|
284
296
|
{
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
297
|
+
'app_id' => 'app_id',
|
298
|
+
'id' => 'super_awesome_job',
|
299
|
+
'created_at' => 1_446_033_421,
|
300
|
+
'completed_at' => 1_446_048_736,
|
301
|
+
'closing_at' => 1_446_034_321,
|
302
|
+
'updated_at' => 1_446_048_736,
|
303
|
+
'name' => 'api_bulk_job',
|
304
|
+
'state' => 'completed',
|
305
|
+
'links' =>
|
294
306
|
{
|
295
|
-
|
296
|
-
|
307
|
+
'error' => 'https://api.intercom.io/jobs/super_awesome_job/error',
|
308
|
+
'self' => 'https://api.intercom.io/jobs/super_awesome_job'
|
297
309
|
},
|
298
|
-
|
310
|
+
'tasks' =>
|
299
311
|
[
|
300
312
|
{
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
313
|
+
'id' => 'super_awesome_task',
|
314
|
+
'item_count' => 2,
|
315
|
+
'created_at' => 1_446_033_421,
|
316
|
+
'started_at' => 1_446_033_709,
|
317
|
+
'completed_at' => 1_446_033_709,
|
318
|
+
'state' => 'completed'
|
307
319
|
}
|
308
320
|
]
|
309
|
-
|
310
|
-
|
311
|
-
let(:bulk_request)
|
312
|
-
|
321
|
+
}
|
322
|
+
end
|
323
|
+
let(:bulk_request) do
|
324
|
+
{
|
313
325
|
items: [
|
314
326
|
{
|
315
|
-
method:
|
316
|
-
data_type:
|
317
|
-
data:{
|
327
|
+
method: 'post',
|
328
|
+
data_type: 'user',
|
329
|
+
data: {
|
318
330
|
user_id: 25,
|
319
|
-
email:
|
331
|
+
email: 'alice@example.com'
|
320
332
|
}
|
321
333
|
},
|
322
334
|
{
|
323
|
-
method:
|
324
|
-
data_type:
|
325
|
-
data:{
|
335
|
+
method: 'delete',
|
336
|
+
data_type: 'user',
|
337
|
+
data: {
|
326
338
|
user_id: 26,
|
327
|
-
email:
|
339
|
+
email: 'bob@example.com'
|
328
340
|
}
|
329
341
|
}
|
330
342
|
]
|
331
343
|
}
|
332
|
-
|
333
|
-
let(:users_to_create)
|
344
|
+
end
|
345
|
+
let(:users_to_create) do
|
334
346
|
[
|
335
347
|
{
|
336
348
|
user_id: 25,
|
337
|
-
email:
|
349
|
+
email: 'alice@example.com'
|
338
350
|
}
|
339
351
|
]
|
340
|
-
|
341
|
-
let(:users_to_delete)
|
352
|
+
end
|
353
|
+
let(:users_to_delete) do
|
342
354
|
[
|
343
355
|
{
|
344
356
|
user_id: 26,
|
345
|
-
email:
|
357
|
+
email: 'bob@example.com'
|
346
358
|
}
|
347
359
|
]
|
348
|
-
}
|
349
|
-
|
350
|
-
it "submits a bulk job" do
|
351
|
-
client.expects(:post).with("/bulk/users", bulk_request).returns(job)
|
352
|
-
client.users.submit_bulk_job(create_items: users_to_create, delete_items: users_to_delete)
|
353
360
|
end
|
354
361
|
|
355
|
-
it
|
356
|
-
|
357
|
-
client.
|
358
|
-
client.users.submit_bulk_job(create_items: users_to_create, delete_items: users_to_delete, job_id: 'super_awesome_job')
|
362
|
+
it 'submits a bulk job' do
|
363
|
+
client.expects(:post).with('/bulk/users', bulk_request).returns(job)
|
364
|
+
client.deprecated__users.submit_bulk_job(create_items: users_to_create, delete_items: users_to_delete)
|
359
365
|
end
|
360
366
|
|
367
|
+
it 'adds users to an existing bulk job' do
|
368
|
+
bulk_request[:job] = { id: 'super_awesome_job' }
|
369
|
+
client.expects(:post).with('/bulk/users', bulk_request).returns(job)
|
370
|
+
client.deprecated__users.submit_bulk_job(create_items: users_to_create, delete_items: users_to_delete, job_id: 'super_awesome_job')
|
371
|
+
end
|
361
372
|
end
|
362
|
-
|
363
373
|
end
|