intercom 1.0.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (75) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +0 -8
  3. data/Gemfile +3 -0
  4. data/README.md +208 -52
  5. data/changes.txt +3 -0
  6. data/intercom.gemspec +2 -2
  7. data/lib/ext/hash.rb +18 -0
  8. data/lib/intercom.rb +38 -43
  9. data/lib/intercom/api_operations/count.rb +16 -0
  10. data/lib/intercom/api_operations/delete.rb +15 -0
  11. data/lib/intercom/api_operations/find.rb +22 -0
  12. data/lib/intercom/api_operations/find_all.rb +33 -0
  13. data/lib/intercom/api_operations/list.rb +17 -0
  14. data/lib/intercom/api_operations/load.rb +15 -0
  15. data/lib/intercom/api_operations/save.rb +44 -0
  16. data/lib/intercom/collection_proxy.rb +66 -0
  17. data/lib/intercom/company.rb +29 -0
  18. data/lib/intercom/conversation.rb +15 -0
  19. data/lib/intercom/count.rb +21 -0
  20. data/lib/intercom/errors.rb +52 -0
  21. data/lib/intercom/event.rb +4 -101
  22. data/lib/intercom/extended_api_operations/reply.rb +16 -0
  23. data/lib/intercom/extended_api_operations/tags.rb +14 -0
  24. data/lib/intercom/extended_api_operations/users.rb +17 -0
  25. data/lib/intercom/generic_handlers/base_handler.rb +22 -0
  26. data/lib/intercom/generic_handlers/count.rb +59 -0
  27. data/lib/intercom/generic_handlers/tag.rb +71 -0
  28. data/lib/intercom/generic_handlers/tag_find_all.rb +47 -0
  29. data/lib/intercom/lib/dynamic_accessors.rb +59 -0
  30. data/lib/intercom/lib/dynamic_accessors_on_method_missing.rb +53 -0
  31. data/lib/intercom/lib/flat_store.rb +31 -0
  32. data/lib/intercom/lib/typed_json_deserializer.rb +52 -0
  33. data/lib/intercom/message.rb +9 -0
  34. data/lib/intercom/note.rb +14 -42
  35. data/lib/intercom/request.rb +40 -4
  36. data/lib/intercom/segment.rb +14 -0
  37. data/lib/intercom/tag.rb +19 -78
  38. data/lib/intercom/traits/api_resource.rb +120 -0
  39. data/lib/intercom/traits/dirty_tracking.rb +33 -0
  40. data/lib/intercom/traits/generic_handler_binding.rb +29 -0
  41. data/lib/intercom/traits/incrementable_attributes.rb +23 -0
  42. data/lib/intercom/user.rb +25 -361
  43. data/lib/intercom/utils.rb +50 -0
  44. data/lib/intercom/version.rb +1 -1
  45. data/spec/spec_helper.rb +64 -33
  46. data/spec/unit/intercom/collection_proxy_spec.rb +34 -0
  47. data/spec/unit/intercom/event_spec.rb +25 -0
  48. data/spec/unit/intercom/{flat_store_spec.rb → lib/flat_store_spec.rb} +7 -7
  49. data/spec/unit/intercom/note_spec.rb +5 -4
  50. data/spec/unit/intercom/tag_spec.rb +3 -3
  51. data/spec/unit/intercom/traits/api_resource_spec.rb +79 -0
  52. data/spec/unit/intercom/user_spec.rb +101 -119
  53. data/spec/unit/intercom_spec.rb +7 -7
  54. metadata +50 -26
  55. data/lib/intercom/flat_store.rb +0 -27
  56. data/lib/intercom/hashable_object.rb +0 -22
  57. data/lib/intercom/impression.rb +0 -63
  58. data/lib/intercom/message_thread.rb +0 -189
  59. data/lib/intercom/requires_parameters.rb +0 -10
  60. data/lib/intercom/social_profile.rb +0 -24
  61. data/lib/intercom/unix_timestamp_unwrapper.rb +0 -12
  62. data/lib/intercom/user_collection_proxy.rb +0 -52
  63. data/lib/intercom/user_resource.rb +0 -82
  64. data/spec/integration/fixtures/v1-user.json +0 -45
  65. data/spec/integration/fixtures/v1-users-impression.json +0 -3
  66. data/spec/integration/fixtures/v1-users-message_thread.json +0 -44
  67. data/spec/integration/fixtures/v1-users-message_threads.json +0 -46
  68. data/spec/integration/fixtures/v1-users-note.json +0 -49
  69. data/spec/integration/fixtures/v1-users.json +0 -144
  70. data/spec/integration/intercom_api_integration_spec.rb +0 -134
  71. data/spec/unit/intercom/impression_spec.rb +0 -18
  72. data/spec/unit/intercom/message_thread_spec.rb +0 -74
  73. data/spec/unit/intercom/user_collection_proxy_spec.rb +0 -46
  74. data/spec/unit/intercom/user_event_spec.rb +0 -83
  75. data/spec/unit/intercom/user_resource_spec.rb +0 -13
@@ -1,3 +1,3 @@
1
1
  module Intercom #:nodoc:
2
- VERSION = "1.0.0"
2
+ VERSION = "2.0.0"
3
3
  end
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
- :user_id => 'id-from-customers-app',
8
- :email => email,
9
- :name => "Joe Schmoe",
10
- :created_at => 1323422442,
11
- :last_seen_ip => "1.2.3.4",
12
- :last_seen_user_agent => "Mozilla blah blah ie6",
13
- :custom_data => {"a" => "b", "b" => 2},
14
- :relationship_score => 90,
15
- :session_count => 123,
16
- :last_impression_at => 1323422442,
17
- :unsubscribed_from_emails => true,
18
- :avatar_url => "http://google.com/logo.png",
19
- :social_profiles => [
20
- {"type" => "twitter", "url" => "http://twitter.com/abc", "username" => "abc"},
21
- {"type" => "twitter", "username" => "abc2", "url" => "http://twitter.com/abc2"},
22
- {"type" => "facebook", "url" => "http://facebook.com/abc", "username" => "abc", "id" => "1234242"},
23
- {"type" => "quora", "url" => "http://facebook.com/abc", "username" => "abc", "id" => "1234242"}
24
- ],
25
- :location_data => {
26
- "country_code" => "IRL"
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(page=1, per_page=10)
81
- all_users = [test_user("user1@example.com"), test_user("user2@example.com"), test_user("user3@example.com")]
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
- "users" => all_users[offset..limit-1],
88
- "total_count" => all_users.size,
89
- "page" => page,
90
- "next_page" => next_page,
91
- "previous_page" => previous_page,
92
- "total_pages" => (all_users.size.to_f / per_page).ceil.to_i
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 "/v1/notes" do
3
+ describe "notes" do
4
4
  it "creates a note" do
5
- Intercom.expects(:post).with("/v1/users/notes", {"body" => "Note to leave on user"}).returns({"html" => "<p>Note to leave on user</p>", "created_at" => 1234567890})
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.html.must_equal "<p>Note to leave on user</p>"
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("/v1/tags", {:name => "Test Tag"}).returns(test_tag)
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("/v1/tags", {:name => "Test Tag"}).returns(test_tag)
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("/v1/tags", {:name => "Test Tag", :user_ids => ["abc123", "def456"], :tag_or_untag => "tag"}).returns(test_tag)
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("email" => "jim@example.com", :user_id => "12345", :created_at => created_at, :name => "Jim Bob")
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 ok on missing methods" do
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.created_at.must_be_nil
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.type.must_equal "twitter"
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
- it "has read-only social accounts" do
51
- user = Intercom::User.new(:social_profiles => ["url" => "http://twitter.com/abc", "username" => "abc", "type" => "twitter"])
52
- user.social_profiles.size.must_equal 1
53
- twitter = user.social_profiles.first
54
- twitter.type.must_equal "twitter"
55
- twitter.url.must_equal "http://twitter.com/abc"
56
- twitter.username.must_equal "abc"
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
- it "has read-only location data" do
63
- Intercom::User.new.location_data.must_equal({})
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.custom_data["mad"] = 123
74
- user.custom_data["other"] = now.to_i
75
- user.custom_data["thing"] = "yay"
76
- user.to_hash["custom_data"].must_equal "mad" => 123, "other" => now.to_i, "thing" => "yay"
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", "id" => "6"},
90
- {"name" => "Test", "id" => "9"}
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 setting companies without an array of hashes" do
100
+ it "rejects nested data structures in custom_attributes" do
97
101
  user = Intercom::User.new()
98
- proc { user.companies = {"name" => "test"} }.must_raise ArgumentError
99
- proc { user.companies = [ ["name", "test"] ]}.must_raise ArgumentError
100
- end
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.custom_data["thing"] = [1] }.must_raise ArgumentError
107
+ proc { user.custom_attributes["thing"] = [1] }.must_raise ArgumentError
110
108
  end
111
109
 
112
- describe "incrementing custom_data fields" do
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", :custom_data => {"mad" => 123, "another" => 432, "other" => @now.to_i, :thing => "yay"})
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 NoMethodError
135
- proc { @user.increments }.must_raise NoMethodError
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("/v1/users", {"email" => "bo@example.com"}).returns(test_user)
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("/v1/users", {"email" => "jo@example.com", "user_id" => "i-1224242"}).returns({"email" => "jo@example.com", "user_id" => "i-1224242"})
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 => {:id => 6, :name => "Intercom"})
173
- Intercom.expects(:post).with("/v1/users", {"email" => "jo@example.com", "user_id" => "i-1224242", "company" => {"id" => 6, "name" => "Intercom"}}).returns({"email" => "jo@example.com", "user_id" => "i-1224242"})
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 => [{:id => 6, :name => "Intercom"}])
179
- Intercom.expects(:post).with("/v1/users", {"email" => "jo@example.com", "user_id" => "i-1224242", "companies" => [{"id" => 6, "name" => "Intercom"}]}).returns({"email" => "jo@example.com", "user_id" => "i-1224242"})
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
- Intercom.expects(:delete).with("/v1/users", {"email" => "jo@example.com", "user_id" => "i-1224242"}).returns({"email" => "jo@example.com", "user_id" => "i-1224242"})
185
- Intercom::User.delete("email" => "jo@example.com", "user_id" => "i-1224242")
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("/v1/users", {"email" => "jo@example.com", "user_id" => "i-1224242"}).returns({"email" => "jo@example.com", "user_id" => "i-1224242"})
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("/v1/users", {"email" => "jo@example.com", "user_id" => "i-1224242"}).returns({"email" => "jo@example.com", "user_id" => "i-1224242", "session_count" => 4})
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("/v1/users", {"email" => "jo@example.com", "created_at" => nil}).returns({"email" => "jo@example.com"})
211
- user = Intercom::User.create("email" => "jo@example.com", "created_at" => nil)
212
- user.created_at.must_equal nil
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 UserCollectionProxy for all without making any requests" do
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::UserCollectionProxy)
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(:get).with("/v1/users", {:per_page => 1}).returns(page_of_users)
237
- Intercom::User.count.must_be_kind_of(Integer)
226
+ Intercom::Count.expects(:user_count).returns('count_info')
227
+ Intercom::User.count
238
228
  end
239
229
 
240
- it "can find_by_email" do
241
- Intercom::User.expects(:find).with(:email => "bob@example.com")
242
- Intercom::User.find_by_email("bob@example.com")
243
- end
230
+ ##TODO: Investigate
231
+ #it "converts company created_at values to unix timestamps" do
232
+ # time = Time.now
244
233
 
245
- it "can find_by_user_id" do
246
- Intercom::User.expects(:find).with(:user_id => "abc123")
247
- Intercom::User.find_by_user_id("abc123")
248
- end
234
+ # user = Intercom::User.new("companies" => [
235
+ # { "created_at" => time },
236
+ # { "created_at" => time.to_i }
237
+ # ])
249
238
 
250
- it "converts company created_at values to unix timestamps" do
251
- time = Time.now
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
- user = Intercom::User.new("companies" => [
254
- { "created_at" => time },
255
- { "created_at" => time.to_i }
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