intercom 1.0.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +0 -8
- data/Gemfile +3 -0
- data/README.md +208 -52
- data/changes.txt +3 -0
- data/intercom.gemspec +2 -2
- data/lib/ext/hash.rb +18 -0
- data/lib/intercom.rb +38 -43
- data/lib/intercom/api_operations/count.rb +16 -0
- data/lib/intercom/api_operations/delete.rb +15 -0
- data/lib/intercom/api_operations/find.rb +22 -0
- data/lib/intercom/api_operations/find_all.rb +33 -0
- data/lib/intercom/api_operations/list.rb +17 -0
- data/lib/intercom/api_operations/load.rb +15 -0
- data/lib/intercom/api_operations/save.rb +44 -0
- data/lib/intercom/collection_proxy.rb +66 -0
- data/lib/intercom/company.rb +29 -0
- data/lib/intercom/conversation.rb +15 -0
- data/lib/intercom/count.rb +21 -0
- data/lib/intercom/errors.rb +52 -0
- data/lib/intercom/event.rb +4 -101
- data/lib/intercom/extended_api_operations/reply.rb +16 -0
- data/lib/intercom/extended_api_operations/tags.rb +14 -0
- data/lib/intercom/extended_api_operations/users.rb +17 -0
- data/lib/intercom/generic_handlers/base_handler.rb +22 -0
- data/lib/intercom/generic_handlers/count.rb +59 -0
- data/lib/intercom/generic_handlers/tag.rb +71 -0
- data/lib/intercom/generic_handlers/tag_find_all.rb +47 -0
- data/lib/intercom/lib/dynamic_accessors.rb +59 -0
- data/lib/intercom/lib/dynamic_accessors_on_method_missing.rb +53 -0
- data/lib/intercom/lib/flat_store.rb +31 -0
- data/lib/intercom/lib/typed_json_deserializer.rb +52 -0
- data/lib/intercom/message.rb +9 -0
- data/lib/intercom/note.rb +14 -42
- data/lib/intercom/request.rb +40 -4
- data/lib/intercom/segment.rb +14 -0
- data/lib/intercom/tag.rb +19 -78
- data/lib/intercom/traits/api_resource.rb +120 -0
- data/lib/intercom/traits/dirty_tracking.rb +33 -0
- data/lib/intercom/traits/generic_handler_binding.rb +29 -0
- data/lib/intercom/traits/incrementable_attributes.rb +23 -0
- data/lib/intercom/user.rb +25 -361
- data/lib/intercom/utils.rb +50 -0
- data/lib/intercom/version.rb +1 -1
- data/spec/spec_helper.rb +64 -33
- data/spec/unit/intercom/collection_proxy_spec.rb +34 -0
- data/spec/unit/intercom/event_spec.rb +25 -0
- data/spec/unit/intercom/{flat_store_spec.rb → lib/flat_store_spec.rb} +7 -7
- data/spec/unit/intercom/note_spec.rb +5 -4
- data/spec/unit/intercom/tag_spec.rb +3 -3
- data/spec/unit/intercom/traits/api_resource_spec.rb +79 -0
- data/spec/unit/intercom/user_spec.rb +101 -119
- data/spec/unit/intercom_spec.rb +7 -7
- metadata +50 -26
- data/lib/intercom/flat_store.rb +0 -27
- data/lib/intercom/hashable_object.rb +0 -22
- data/lib/intercom/impression.rb +0 -63
- data/lib/intercom/message_thread.rb +0 -189
- data/lib/intercom/requires_parameters.rb +0 -10
- data/lib/intercom/social_profile.rb +0 -24
- data/lib/intercom/unix_timestamp_unwrapper.rb +0 -12
- data/lib/intercom/user_collection_proxy.rb +0 -52
- data/lib/intercom/user_resource.rb +0 -82
- data/spec/integration/fixtures/v1-user.json +0 -45
- data/spec/integration/fixtures/v1-users-impression.json +0 -3
- data/spec/integration/fixtures/v1-users-message_thread.json +0 -44
- data/spec/integration/fixtures/v1-users-message_threads.json +0 -46
- data/spec/integration/fixtures/v1-users-note.json +0 -49
- data/spec/integration/fixtures/v1-users.json +0 -144
- data/spec/integration/intercom_api_integration_spec.rb +0 -134
- data/spec/unit/intercom/impression_spec.rb +0 -18
- data/spec/unit/intercom/message_thread_spec.rb +0 -74
- data/spec/unit/intercom/user_collection_proxy_spec.rb +0 -46
- data/spec/unit/intercom/user_event_spec.rb +0 -83
- data/spec/unit/intercom/user_resource_spec.rb +0 -13
data/lib/intercom/version.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
@@ -4,27 +4,57 @@ require 'mocha/setup'
|
|
4
4
|
|
5
5
|
def test_user(email="bob@example.com")
|
6
6
|
{
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
7
|
+
"type" =>"user",
|
8
|
+
"id" =>"aaaaaaaaaaaaaaaaaaaaaaaa",
|
9
|
+
"user_id" => 'id-from-customers-app',
|
10
|
+
"email" => email,
|
11
|
+
"name" => "Joe Schmoe",
|
12
|
+
"avatar" => {"type"=>"avatar", "image_url"=>"https://graph.facebook.com/1/picture?width=24&height=24"},
|
13
|
+
"app_id" => "the-app-id",
|
14
|
+
"created_at" => 1323422442,
|
15
|
+
"custom_attributes" => {"a" => "b", "b" => 2},
|
16
|
+
"companies" =>
|
17
|
+
{"type"=>"company.list",
|
18
|
+
"companies"=>
|
19
|
+
[{"type"=>"company",
|
20
|
+
"company_id"=>"123",
|
21
|
+
"id"=>"bbbbbbbbbbbbbbbbbbbbbbbb",
|
22
|
+
"app_id"=>"the-app-id",
|
23
|
+
"name"=>"Company 1",
|
24
|
+
"remote_created_at"=>1390936440,
|
25
|
+
"created_at"=>1401970114,
|
26
|
+
"updated_at"=>1401970114,
|
27
|
+
"last_request_at"=>1401970113,
|
28
|
+
"monthly_spend"=>0,
|
29
|
+
"session_count"=>0,
|
30
|
+
"user_count"=>1,
|
31
|
+
"tag_ids"=>[],
|
32
|
+
"custom_attributes"=>{"category"=>"Tech"}}]},
|
33
|
+
"session_count" => 123,
|
34
|
+
"unsubscribed_from_emails" => true,
|
35
|
+
"last_request_at" =>1401970113,
|
36
|
+
"created_at" =>1401970114,
|
37
|
+
"remote_created_at" =>1393613864,
|
38
|
+
"updated_at" =>1401970114,
|
39
|
+
"user_agent_data" => "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",
|
40
|
+
"social_profiles" =>{"type"=>"social_profile.list",
|
41
|
+
"social_profiles" => [
|
42
|
+
{"type" => "social_profile", "name" => "twitter", "url" => "http://twitter.com/abc", "username" => "abc", "id" => nil},
|
43
|
+
{"type" => "social_profile", "name" => "twitter", "username" => "abc2", "url" => "http://twitter.com/abc2", "id" => nil},
|
44
|
+
{"type" => "social_profile", "name" => "facebook", "url" => "http://facebook.com/abc", "username" => "abc", "id" => "1234242"},
|
45
|
+
{"type" => "social_profile", "name" => "quora", "url" => "http://facebook.com/abc", "username" => "abc", "id" => "1234242"}
|
46
|
+
]},
|
47
|
+
"location_data"=>
|
48
|
+
{"type"=>"location_data",
|
49
|
+
"city_name"=> 'Dublin',
|
50
|
+
"continent_code"=> 'EU',
|
51
|
+
"country_name"=> 'Ireland',
|
52
|
+
"latitude"=> '90',
|
53
|
+
"longitude"=> '10',
|
54
|
+
"postal_code"=> 'IE',
|
55
|
+
"region_name"=> 'Europe',
|
56
|
+
"timezone"=> '+1000',
|
57
|
+
"country_code" => "IRL"}
|
28
58
|
}
|
29
59
|
end
|
30
60
|
|
@@ -77,19 +107,20 @@ def test_message
|
|
77
107
|
}
|
78
108
|
end
|
79
109
|
|
80
|
-
def page_of_users(
|
81
|
-
|
82
|
-
offset = (page - 1) * per_page
|
83
|
-
limit = page * per_page
|
84
|
-
next_page = limit < all_users.size ? page + 1 : nil
|
85
|
-
previous_page = offset > 0 ? page - 1 : nil
|
110
|
+
def page_of_users(include_next_link: false)
|
111
|
+
all_user =
|
86
112
|
{
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
113
|
+
"type"=>"user.list",
|
114
|
+
"pages"=>
|
115
|
+
{
|
116
|
+
"type"=>"pages",
|
117
|
+
"next"=> (include_next_link ? "https://api.intercom.io/users?per_page=50&page=2" : nil),
|
118
|
+
"page"=>1,
|
119
|
+
"per_page"=>50,
|
120
|
+
"total_pages"=>7
|
121
|
+
},
|
122
|
+
"users"=> [test_user("user1@example.com"), test_user("user2@example.com"), test_user("user3@example.com")],
|
123
|
+
"total_count"=>314
|
93
124
|
}
|
94
125
|
end
|
95
126
|
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Intercom::CollectionProxy do
|
4
|
+
|
5
|
+
it "stops iterating if no next link" do
|
6
|
+
Intercom.expects(:get).with("/users", {}).returns(page_of_users(include_next_link:false))
|
7
|
+
emails = []
|
8
|
+
Intercom::User.all.each { |user| emails << user.email }
|
9
|
+
emails.must_equal %W(user1@example.com user2@example.com user3@example.com)
|
10
|
+
end
|
11
|
+
|
12
|
+
it "keeps iterating if next link" do
|
13
|
+
Intercom.expects(:get).with("/users", {}).returns(page_of_users(include_next_link:true))
|
14
|
+
Intercom.expects(:get).with('https://api.intercom.io/users?per_page=50&page=2', {}).returns(page_of_users(include_next_link:false))
|
15
|
+
emails = []
|
16
|
+
Intercom::User.all.each { |user| emails << user.email }
|
17
|
+
end
|
18
|
+
|
19
|
+
it "supports indexed array access" do
|
20
|
+
Intercom.expects(:get).with("/users", {}).returns(page_of_users(include_next_link:false))
|
21
|
+
Intercom::User.all[0].email.must_equal 'user1@example.com'
|
22
|
+
end
|
23
|
+
|
24
|
+
it "supports map" do
|
25
|
+
Intercom.expects(:get).with("/users", {}).returns(page_of_users(include_next_link:false))
|
26
|
+
emails = Intercom::User.all.map { |user| user.email }
|
27
|
+
emails.must_equal %W(user1@example.com user2@example.com user3@example.com)
|
28
|
+
end
|
29
|
+
|
30
|
+
it "supports querying" do
|
31
|
+
Intercom.expects(:get).with("/users", {:tag_name => 'Taggart J'}).returns(page_of_users(include_next_link:false))
|
32
|
+
Intercom::User.find_all(:tag_name => 'Taggart J').map(&:email).must_equal %W(user1@example.com user2@example.com user3@example.com)
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "Intercom::Event" do
|
4
|
+
|
5
|
+
let (:user) {Intercom::User.new("email" => "jim@example.com", :user_id => "12345", :created_at => Time.now, :name => "Jim Bob")}
|
6
|
+
let (:created_time) {Time.now - 300}
|
7
|
+
|
8
|
+
it "creates an event with metadata" do
|
9
|
+
Intercom.expects(:post).with('/events', {'event_name' => 'Eventful 1', 'created_at' => created_time.to_i, 'email' => 'joe@example.com', 'metadata' => {'invitee_email' => 'pi@example.org', :invite_code => 'ADDAFRIEND', 'found_date' => 12909364407}}).returns(:status => 202)
|
10
|
+
|
11
|
+
Intercom::Event.create(:event_name => "Eventful 1", :created_at => created_time,
|
12
|
+
:email => 'joe@example.com',
|
13
|
+
:metadata => {
|
14
|
+
"invitee_email" => "pi@example.org",
|
15
|
+
:invite_code => "ADDAFRIEND",
|
16
|
+
"found_date" => 12909364407
|
17
|
+
})
|
18
|
+
end
|
19
|
+
|
20
|
+
it "creates an event without metadata" do
|
21
|
+
Intercom.expects(:post).with('/events', {'event_name' => 'sale of item', 'email' => 'joe@example.com'})
|
22
|
+
Intercom::Event.create(:event_name => "sale of item", :email => 'joe@example.com')
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
@@ -1,29 +1,29 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
|
3
|
-
describe Intercom::FlatStore do
|
3
|
+
describe Intercom::Lib::FlatStore do
|
4
4
|
it "raises if you try to set or merge in nested hash structures" do
|
5
|
-
data = Intercom::FlatStore.new()
|
5
|
+
data = Intercom::Lib::FlatStore.new()
|
6
6
|
proc { data["thing"] = [1] }.must_raise ArgumentError
|
7
7
|
proc { data["thing"] = {1 => 2} }.must_raise ArgumentError
|
8
|
-
proc { Intercom::FlatStore.new({1 => {2 => 3}}) }.must_raise ArgumentError
|
8
|
+
proc { Intercom::Lib::FlatStore.new({1 => {2 => 3}}) }.must_raise ArgumentError
|
9
9
|
end
|
10
10
|
|
11
11
|
it "raises if you try to use a non string key" do
|
12
|
-
data =Intercom::FlatStore.new()
|
12
|
+
data =Intercom::Lib::FlatStore.new()
|
13
13
|
proc { data[1] = "something" }.must_raise ArgumentError
|
14
14
|
end
|
15
15
|
|
16
16
|
it "sets and merges valid entries" do
|
17
|
-
data = Intercom::FlatStore.new()
|
17
|
+
data = Intercom::Lib::FlatStore.new()
|
18
18
|
data["a"] = 1
|
19
19
|
data[:b] = 2
|
20
20
|
data[:a].must_equal 1
|
21
21
|
data["b"].must_equal 2
|
22
22
|
data[:b].must_equal 2
|
23
|
-
data = Intercom::FlatStore.new({"a" => 1, :b => 2})
|
23
|
+
data = Intercom::Lib::FlatStore.new({"a" => 1, :b => 2})
|
24
24
|
data["a"].must_equal 1
|
25
25
|
data[:a].must_equal 1
|
26
26
|
data["b"].must_equal 2
|
27
27
|
data[:b].must_equal 2
|
28
28
|
end
|
29
|
-
end
|
29
|
+
end
|
@@ -1,18 +1,19 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
|
3
|
-
describe "
|
3
|
+
describe "notes" do
|
4
4
|
it "creates a note" do
|
5
|
-
Intercom.expects(:post).with("/
|
5
|
+
Intercom.expects(:post).with("/notes", {"body" => "Note to leave on user"}).returns({"body" => "<p>Note to leave on user</p>", "created_at" => 1234567890})
|
6
6
|
note = Intercom::Note.create("body" => "Note to leave on user")
|
7
|
-
note.
|
7
|
+
note.body.must_equal "<p>Note to leave on user</p>"
|
8
8
|
end
|
9
9
|
|
10
10
|
it "sets/gets allowed keys" do
|
11
11
|
params = {"body" => "Note body", "email" => "me@example.com", :user_id => "abc123"}
|
12
12
|
note = Intercom::Note.new(params)
|
13
|
+
|
13
14
|
note.to_hash.keys.sort.must_equal params.keys.map(&:to_s).sort
|
14
15
|
params.keys.each do | key|
|
15
16
|
note.send(key).must_equal params[key]
|
16
17
|
end
|
17
18
|
end
|
18
|
-
end
|
19
|
+
end
|
@@ -2,19 +2,19 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe "Intercom::Tag" do
|
4
4
|
it "gets a tag" do
|
5
|
-
Intercom.expects(:get).with("/
|
5
|
+
Intercom.expects(:get).with("/tags", {:name => "Test Tag"}).returns(test_tag)
|
6
6
|
tag = Intercom::Tag.find(:name => "Test Tag")
|
7
7
|
tag.name.must_equal "Test Tag"
|
8
8
|
end
|
9
9
|
|
10
10
|
it "creates a tag" do
|
11
|
-
Intercom.expects(:post).with("/
|
11
|
+
Intercom.expects(:post).with("/tags", {'name' => "Test Tag"}).returns(test_tag)
|
12
12
|
tag = Intercom::Tag.create(:name => "Test Tag")
|
13
13
|
tag.name.must_equal "Test Tag"
|
14
14
|
end
|
15
15
|
|
16
16
|
it "tags users" do
|
17
|
-
Intercom.expects(:post).with("/
|
17
|
+
Intercom.expects(:post).with("/tags", {'name' => "Test Tag", 'user_ids' => ["abc123", "def456"], 'tag_or_untag' => "tag"}).returns(test_tag)
|
18
18
|
tag = Intercom::Tag.create(:name => "Test Tag", :user_ids => ["abc123", "def456"], :tag_or_untag => "tag")
|
19
19
|
tag.name.must_equal "Test Tag"
|
20
20
|
tag.tagged_user_count.must_equal 2
|
@@ -0,0 +1,79 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Intercom::Traits::ApiResource do
|
4
|
+
let(:object_json) do
|
5
|
+
{"type"=>"company",
|
6
|
+
"id"=>"aaaaaaaaaaaaaaaaaaaaaaaa",
|
7
|
+
"app_id"=>"some-app-id",
|
8
|
+
"name"=>"SuperSuite",
|
9
|
+
"plan_id"=>1,
|
10
|
+
"remote_company_id"=>"8",
|
11
|
+
"remote_created_at"=>103201,
|
12
|
+
"created_at"=>1374056196,
|
13
|
+
"user_count"=>1,
|
14
|
+
"custom_attributes"=>{}}
|
15
|
+
end
|
16
|
+
let(:api_resource) { Object.new.extend(Intercom::Traits::ApiResource)}
|
17
|
+
|
18
|
+
before(:each) { api_resource.from_response(object_json) }
|
19
|
+
|
20
|
+
it "does not set type on parsing json" do
|
21
|
+
assert_nil api_resource.instance_variable_get(:@type)
|
22
|
+
api_resource.wont_respond_to :type
|
23
|
+
end
|
24
|
+
|
25
|
+
it "coerces time on parsing json" do
|
26
|
+
assert_equal Time.at(1374056196), api_resource.created_at
|
27
|
+
end
|
28
|
+
|
29
|
+
it "exposes string" do
|
30
|
+
assert_equal Time.at(1374056196), api_resource.created_at
|
31
|
+
end
|
32
|
+
|
33
|
+
it "dynamically defines accessors when a non-existent property is called that looks like a setter" do
|
34
|
+
api_resource.wont_respond_to :spiders
|
35
|
+
api_resource.spiders = 4
|
36
|
+
api_resource.must_respond_to :spiders
|
37
|
+
end
|
38
|
+
|
39
|
+
it "calls dynamically defined getter when asked" do
|
40
|
+
api_resource.foo = 4
|
41
|
+
assert_equal 4, api_resource.foo
|
42
|
+
end
|
43
|
+
|
44
|
+
it "accepts unix timestamps into dynamically defined date setters" do
|
45
|
+
api_resource.foo_at = 1401200468
|
46
|
+
assert_equal 1401200468, api_resource.instance_variable_get(:@foo_at)
|
47
|
+
end
|
48
|
+
|
49
|
+
it "exposes dates correctly for dynamically defined getters" do
|
50
|
+
api_resource.foo_at = 1401200468
|
51
|
+
assert_equal Time.at(1401200468), api_resource.foo_at
|
52
|
+
end
|
53
|
+
|
54
|
+
it "throws regular method missing error when non-existent getter is called that is backed by an instance variable" do
|
55
|
+
api_resource.instance_variable_set(:@bar, 'you cant see me')
|
56
|
+
proc { api_resource.bar }.must_raise NoMethodError
|
57
|
+
end
|
58
|
+
|
59
|
+
it "throws attribute not set error when non-existent getter is called that is not backed by an instance variable" do
|
60
|
+
proc { api_resource.flubber }.must_raise Intercom::AttributeNotSetError
|
61
|
+
end
|
62
|
+
|
63
|
+
it "throws regular method missing error when non-existent method is called that cannot be an accessor" do
|
64
|
+
proc { api_resource.flubber! }.must_raise NoMethodError
|
65
|
+
proc { api_resource.flubber? }.must_raise NoMethodError
|
66
|
+
end
|
67
|
+
|
68
|
+
it "throws regular method missing error when non-existent setter is called with multiple arguments" do
|
69
|
+
proc { api_resource.send(:flubber=, 'a', 'b') }.must_raise NoMethodError
|
70
|
+
end
|
71
|
+
|
72
|
+
it "an initialized ApiResource is equal to on generated from a response" do
|
73
|
+
class ConcreteApiResource; include Intercom::Traits::ApiResource; end
|
74
|
+
initialized_api_resource = ConcreteApiResource.new(object_json)
|
75
|
+
object_json.except('type').keys.each do |attribute|
|
76
|
+
assert_equal initialized_api_resource.send(attribute), api_resource.send(attribute)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
@@ -3,7 +3,7 @@ require "spec_helper"
|
|
3
3
|
describe "Intercom::User" do
|
4
4
|
it "to_hash'es itself" do
|
5
5
|
created_at = Time.now
|
6
|
-
user = Intercom::User.new(
|
6
|
+
user = Intercom::User.new(:email => "jim@example.com", :user_id => "12345", :created_at => created_at, :name => "Jim Bob")
|
7
7
|
as_hash = user.to_hash
|
8
8
|
as_hash["email"].must_equal "jim@example.com"
|
9
9
|
as_hash["user_id"].must_equal "12345"
|
@@ -20,99 +20,97 @@ describe "Intercom::User" do
|
|
20
20
|
user.last_impression_at.to_s.must_equal now.to_s
|
21
21
|
end
|
22
22
|
|
23
|
-
it "is
|
23
|
+
it "is throws a Intercom::AttributeNotSetError on trying to access an attribute that has not been set" do
|
24
24
|
user = Intercom::User.new
|
25
|
-
user.
|
26
|
-
user.last_impression_at.must_be_nil
|
27
|
-
user.email.must_be_nil
|
28
|
-
user.social_profiles.must_equal([])
|
25
|
+
proc { user.foo_property }.must_raise Intercom::AttributeNotSetError
|
29
26
|
end
|
30
27
|
|
31
28
|
it "presents a complete user record correctly" do
|
32
29
|
user = Intercom::User.from_api(test_user)
|
30
|
+
user.user_id.must_equal 'id-from-customers-app'
|
31
|
+
user.email.must_equal 'bob@example.com'
|
32
|
+
user.name.must_equal 'Joe Schmoe'
|
33
|
+
user.app_id.must_equal 'the-app-id'
|
33
34
|
user.session_count.must_equal 123
|
35
|
+
user.created_at.to_i.must_equal 1401970114
|
36
|
+
user.remote_created_at.to_i.must_equal 1393613864
|
37
|
+
user.updated_at.to_i.must_equal 1401970114
|
38
|
+
|
39
|
+
user.avatar.must_be_kind_of Intercom::Avatar
|
40
|
+
user.avatar.image_url.must_equal 'https://graph.facebook.com/1/picture?width=24&height=24'
|
41
|
+
|
42
|
+
user.companies.must_be_kind_of Array
|
43
|
+
user.companies.size.must_equal 1
|
44
|
+
user.companies[0].must_be_kind_of Intercom::Company
|
45
|
+
user.companies[0].company_id.must_equal "123"
|
46
|
+
user.companies[0].id.must_equal "bbbbbbbbbbbbbbbbbbbbbbbb"
|
47
|
+
user.companies[0].app_id.must_equal "the-app-id"
|
48
|
+
user.companies[0].name.must_equal "Company 1"
|
49
|
+
user.companies[0].remote_created_at.to_i.must_equal 1390936440
|
50
|
+
user.companies[0].created_at.to_i.must_equal 1401970114
|
51
|
+
user.companies[0].updated_at.to_i.must_equal 1401970114
|
52
|
+
user.companies[0].last_request_at.to_i.must_equal 1401970113
|
53
|
+
user.companies[0].monthly_spend.must_equal 0
|
54
|
+
user.companies[0].session_count.must_equal 0
|
55
|
+
user.companies[0].user_count.must_equal 1
|
56
|
+
user.companies[0].tag_ids.must_equal []
|
57
|
+
|
58
|
+
user.custom_attributes.must_be_kind_of Intercom::Lib::FlatStore
|
59
|
+
user.custom_attributes["a"].must_equal "b"
|
60
|
+
user.custom_attributes["b"].must_equal 2
|
61
|
+
|
34
62
|
user.social_profiles.size.must_equal 4
|
35
63
|
twitter_account = user.social_profiles.first
|
36
64
|
twitter_account.must_be_kind_of Intercom::SocialProfile
|
37
|
-
twitter_account.
|
65
|
+
twitter_account.name.must_equal "twitter"
|
38
66
|
twitter_account.username.must_equal "abc"
|
39
67
|
twitter_account.url.must_equal "http://twitter.com/abc"
|
40
|
-
user.custom_data["a"].must_equal "b"
|
41
|
-
user.custom_data["b"].must_equal 2
|
42
|
-
user.relationship_score.must_equal 90
|
43
|
-
user.last_seen_ip.must_equal "1.2.3.4"
|
44
|
-
user.last_seen_user_agent.must_equal "Mozilla blah blah ie6"
|
45
|
-
user.avatar_url.must_equal "http://google.com/logo.png"
|
46
|
-
user.unsubscribed_from_emails.must_equal true
|
47
|
-
user.location_data['country_code'].must_equal "IRL"
|
48
|
-
end
|
49
68
|
|
50
|
-
|
51
|
-
user
|
52
|
-
user.
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
user.to_hash["social_profiles"].must_equal nil
|
58
|
-
proc { user.social_profiles << "a" }.must_raise error_on_modify_frozen, "can't modify frozen array"
|
59
|
-
proc { Intercom::User.new.social_profiles << "a" }.must_raise error_on_modify_frozen, "can't modify frozen array"
|
60
|
-
end
|
69
|
+
user.location_data.must_be_kind_of Intercom::LocationData
|
70
|
+
user.location_data.city_name.must_equal "Dublin"
|
71
|
+
user.location_data.continent_code.must_equal "EU"
|
72
|
+
user.location_data.country_name.must_equal "Ireland"
|
73
|
+
user.location_data.latitude.must_equal "90"
|
74
|
+
user.location_data.longitude.must_equal "10"
|
75
|
+
user.location_data.country_code.must_equal "IRL"
|
61
76
|
|
62
|
-
|
63
|
-
|
64
|
-
user = Intercom::User.new(:location_data => {"city" => "Dublin"})
|
65
|
-
user.location_data.must_equal({"city" => "Dublin"})
|
66
|
-
proc { user.location_data["change"] = "123" }.must_raise error_on_modify_frozen, "can't modify frozen hash"
|
67
|
-
user.to_hash["location_data"].must_equal nil
|
77
|
+
user.unsubscribed_from_emails.must_equal true
|
78
|
+
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"
|
68
79
|
end
|
69
80
|
|
70
81
|
it "allows easy setting of custom data" do
|
71
82
|
now = Time.now
|
72
|
-
user = Intercom::User.new
|
73
|
-
user.
|
74
|
-
user.
|
75
|
-
user.
|
76
|
-
user.to_hash["
|
77
|
-
end
|
78
|
-
|
79
|
-
it "allows easy setting of company data" do
|
80
|
-
user = Intercom::User.new()
|
81
|
-
user.company["name"] = "Intercom"
|
82
|
-
user.company["id"] = 6
|
83
|
-
user.to_hash["company"].must_equal "name" => "Intercom", "id" => 6
|
83
|
+
user = Intercom::User.new
|
84
|
+
user.custom_attributes["mad"] = 123
|
85
|
+
user.custom_attributes["other"] = now.to_i
|
86
|
+
user.custom_attributes["thing"] = "yay"
|
87
|
+
user.to_hash["custom_attributes"].must_equal "mad" => 123, "other" => now.to_i, "thing" => "yay"
|
84
88
|
end
|
85
89
|
|
86
90
|
it "allows easy setting of multiple companies" do
|
87
91
|
user = Intercom::User.new()
|
88
92
|
companies = [
|
89
|
-
{"name" => "Intercom", "
|
90
|
-
{"name" => "Test", "
|
93
|
+
{"name" => "Intercom", "company_id" => "6"},
|
94
|
+
{"name" => "Test", "company_id" => "9"}
|
91
95
|
]
|
92
96
|
user.companies = companies
|
93
97
|
user.to_hash["companies"].must_equal companies
|
94
98
|
end
|
95
99
|
|
96
|
-
it "rejects
|
100
|
+
it "rejects nested data structures in custom_attributes" do
|
97
101
|
user = Intercom::User.new()
|
98
|
-
proc { user.
|
99
|
-
proc { user.
|
100
|
-
|
101
|
-
|
102
|
-
it "rejects nested data structures in custom_data" do
|
103
|
-
user = Intercom::User.new()
|
104
|
-
proc { user.custom_data["thing"] = [1] }.must_raise ArgumentError
|
105
|
-
proc { user.custom_data["thing"] = {1 => 2} }.must_raise ArgumentError
|
106
|
-
proc { user.custom_data = {1 => {2 => 3}} }.must_raise ArgumentError
|
102
|
+
proc { user.custom_attributes["thing"] = [1] }.must_raise ArgumentError
|
103
|
+
proc { user.custom_attributes["thing"] = {1 => 2} }.must_raise ArgumentError
|
104
|
+
proc { user.custom_attributes = {1 => {2 => 3}} }.must_raise ArgumentError
|
107
105
|
|
108
106
|
user = Intercom::User.from_api(test_user)
|
109
|
-
proc { user.
|
107
|
+
proc { user.custom_attributes["thing"] = [1] }.must_raise ArgumentError
|
110
108
|
end
|
111
109
|
|
112
|
-
describe "incrementing
|
110
|
+
describe "incrementing custom_attributes fields" do
|
113
111
|
before :each do
|
114
112
|
@now = Time.now
|
115
|
-
@user = Intercom::User.new("email" => "jo@example.com", :user_id => "i-1224242", :
|
113
|
+
@user = Intercom::User.new("email" => "jo@example.com", :user_id => "i-1224242", :custom_attributes => {"mad" => 123, "another" => 432, "other" => @now.to_i, :thing => "yay"})
|
116
114
|
end
|
117
115
|
|
118
116
|
it "increments up by 1 with no args" do
|
@@ -131,8 +129,8 @@ describe "Intercom::User" do
|
|
131
129
|
end
|
132
130
|
|
133
131
|
it "doesn't allow direct access to increments hash" do
|
134
|
-
proc { @user.increments["mad"] = 1 }.must_raise
|
135
|
-
proc { @user.increments }.must_raise
|
132
|
+
proc { @user.increments["mad"] = 1 }.must_raise Intercom::AttributeNotSetError
|
133
|
+
proc { @user.increments }.must_raise Intercom::AttributeNotSetError
|
136
134
|
end
|
137
135
|
|
138
136
|
it "can update the increments hash without losing previous changes" do
|
@@ -155,67 +153,59 @@ describe "Intercom::User" do
|
|
155
153
|
end
|
156
154
|
|
157
155
|
it "fetches a user" do
|
158
|
-
Intercom.expects(:get).with("/
|
156
|
+
Intercom.expects(:get).with("/users", {"email" => "bo@example.com"}).returns(test_user)
|
159
157
|
user = Intercom::User.find("email" => "bo@example.com")
|
160
158
|
user.email.must_equal "bob@example.com"
|
161
159
|
user.name.must_equal "Joe Schmoe"
|
162
160
|
user.session_count.must_equal 123
|
163
161
|
end
|
164
162
|
|
165
|
-
it "saves a user" do
|
163
|
+
it "saves a user (always sends custom_attributes)" do
|
166
164
|
user = Intercom::User.new("email" => "jo@example.com", :user_id => "i-1224242")
|
167
|
-
Intercom.expects(:post).with("/
|
165
|
+
Intercom.expects(:post).with("/users", {"email" => "jo@example.com", "user_id" => "i-1224242", "custom_attributes" => {}}).returns({"email" => "jo@example.com", "user_id" => "i-1224242"})
|
168
166
|
user.save
|
169
167
|
end
|
170
168
|
|
171
169
|
it "saves a user with a company" do
|
172
|
-
user = Intercom::User.new("email" => "jo@example.com", :user_id => "i-1224242", :company => {
|
173
|
-
Intercom.expects(:post).with("/
|
170
|
+
user = Intercom::User.new("email" => "jo@example.com", :user_id => "i-1224242", :company => {'company_id' => 6, 'name' => "Intercom"})
|
171
|
+
Intercom.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"})
|
174
172
|
user.save
|
175
173
|
end
|
176
174
|
|
177
175
|
it "saves a user with a companies" do
|
178
|
-
user = Intercom::User.new("email" => "jo@example.com", :user_id => "i-1224242", :companies => [{
|
179
|
-
Intercom.expects(:post).with("/
|
176
|
+
user = Intercom::User.new("email" => "jo@example.com", :user_id => "i-1224242", :companies => [{'company_id' => 6, 'name' => "Intercom"}])
|
177
|
+
Intercom.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"})
|
180
178
|
user.save
|
181
179
|
end
|
182
180
|
|
183
181
|
it "deletes a user" do
|
184
|
-
|
185
|
-
Intercom
|
182
|
+
user = Intercom::User.new("id" => "1")
|
183
|
+
Intercom.expects(:delete).with("/users/1", {}).returns(user)
|
184
|
+
user.delete
|
186
185
|
end
|
187
186
|
|
188
187
|
it "can use User.create for convenience" do
|
189
|
-
Intercom.expects(:post).with("/
|
188
|
+
Intercom.expects(:post).with("/users", {'custom_attributes' => {}, "email" => "jo@example.com", "user_id" => "i-1224242"}).returns({"email" => "jo@example.com", "user_id" => "i-1224242"})
|
190
189
|
user = Intercom::User.create("email" => "jo@example.com", :user_id => "i-1224242")
|
191
190
|
user.email.must_equal "jo@example.com"
|
192
191
|
end
|
193
192
|
|
194
193
|
it "updates the @user with attributes as set by the server" do
|
195
|
-
Intercom.expects(:post).with("/
|
194
|
+
Intercom.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})
|
196
195
|
user = Intercom::User.create("email" => "jo@example.com", :user_id => "i-1224242")
|
197
196
|
user.session_count.must_equal 4
|
198
197
|
end
|
199
198
|
|
200
|
-
it "raises argument error when trying to set a read only attribute" do
|
201
|
-
read_only_attributes = %w(relationship_score session_count)
|
202
|
-
read_only_attributes.each do |read_only|
|
203
|
-
proc { Intercom::User.create("email" => "jo@example.com", read_only => "some value") }.must_raise NoMethodError
|
204
|
-
user = Intercom::User.new
|
205
|
-
user.public_methods.include?("#{read_only}=").must_equal false
|
206
|
-
end
|
207
|
-
end
|
208
|
-
|
209
199
|
it "allows setting dates to nil without converting them to 0" do
|
210
|
-
Intercom.expects(:post).with("/
|
211
|
-
user = Intercom::User.create("email" => "jo@example.com", "
|
212
|
-
user.
|
200
|
+
Intercom.expects(:post).with("/users", {"email" => "jo@example.com", 'custom_attributes' => {}, "remote_created_at" => nil}).returns({"email" => "jo@example.com"})
|
201
|
+
user = Intercom::User.create("email" => "jo@example.com", "remote_created_at" => nil)
|
202
|
+
user.remote_created_at.must_equal nil
|
213
203
|
end
|
214
204
|
|
215
205
|
it "sets/gets rw keys" do
|
216
206
|
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}
|
217
207
|
user = Intercom::User.new(params)
|
218
|
-
user.to_hash.keys.sort.must_equal params.keys.map(&:to_s).sort
|
208
|
+
user.to_hash.keys.sort.must_equal (params.keys + ['custom_attributes']).map(&:to_s).sort
|
219
209
|
params.keys.each do |key|
|
220
210
|
user.send(key).to_s.must_equal params[key].to_s
|
221
211
|
end
|
@@ -226,49 +216,41 @@ describe "Intercom::User" do
|
|
226
216
|
user.new_param.must_equal "some value"
|
227
217
|
end
|
228
218
|
|
229
|
-
it "returns a
|
219
|
+
it "returns a CollectionProxy for all without making any requests" do
|
230
220
|
Intercom.expects(:execute_request).never
|
231
221
|
all = Intercom::User.all
|
232
|
-
all.must_be_instance_of(Intercom::
|
222
|
+
all.must_be_instance_of(Intercom::CollectionProxy)
|
233
223
|
end
|
234
224
|
|
235
225
|
it "returns the total number of users" do
|
236
|
-
Intercom.expects(:
|
237
|
-
Intercom::User.count
|
226
|
+
Intercom::Count.expects(:user_count).returns('count_info')
|
227
|
+
Intercom::User.count
|
238
228
|
end
|
239
229
|
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
end
|
230
|
+
##TODO: Investigate
|
231
|
+
#it "converts company created_at values to unix timestamps" do
|
232
|
+
# time = Time.now
|
244
233
|
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
234
|
+
# user = Intercom::User.new("companies" => [
|
235
|
+
# { "created_at" => time },
|
236
|
+
# { "created_at" => time.to_i }
|
237
|
+
# ])
|
249
238
|
|
250
|
-
|
251
|
-
|
239
|
+
# as_hash = user.to_hash
|
240
|
+
# require 'ruby-debug' ; debugger
|
241
|
+
# first_company_as_hash = as_hash["companies"][0]
|
242
|
+
# second_company_as_hash = as_hash["companies"][1]
|
252
243
|
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
])
|
257
|
-
|
258
|
-
as_hash = user.to_hash
|
259
|
-
first_company_as_hash = as_hash["companies"][0]
|
260
|
-
second_company_as_hash = as_hash["companies"][1]
|
261
|
-
|
262
|
-
first_company_as_hash["created_at"].must_equal time.to_i
|
263
|
-
second_company_as_hash["created_at"].must_equal time.to_i
|
264
|
-
end
|
244
|
+
# first_company_as_hash["created_at"].must_equal time.to_i
|
245
|
+
# second_company_as_hash["created_at"].must_equal time.to_i
|
246
|
+
#end
|
265
247
|
|
266
|
-
it "tracks events" do
|
267
|
-
user = Intercom::User.new("email" => "jim@example.com", :user_id => "12345", :created_at => Time.now, :name => "Jim Bob")
|
268
|
-
Intercom::Event.expects(:create).with(:event_name => 'registration', :user => user)
|
269
|
-
event = user.track_event('registration')
|
248
|
+
# it "tracks events" do
|
249
|
+
# user = Intercom::User.new("email" => "jim@example.com", :user_id => "12345", :created_at => Time.now, :name => "Jim Bob")
|
250
|
+
# Intercom::Event.expects(:create).with(:event_name => 'registration', :user => user)
|
251
|
+
# event = user.track_event('registration')
|
270
252
|
|
271
|
-
Intercom::Event.expects(:create).with(:event_name => 'another', :user => user, :created_at => 1391691571)
|
272
|
-
event = user.track_event("another", {:created_at => 1391691571})
|
273
|
-
end
|
253
|
+
# Intercom::Event.expects(:create).with(:event_name => 'another', :user => user, :created_at => 1391691571)
|
254
|
+
# event = user.track_event("another", {:created_at => 1391691571})
|
255
|
+
# end
|
274
256
|
end
|