intercom 2.5.4 → 3.0.0b1

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 (72) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +0 -2
  3. data/README.md +97 -118
  4. data/changes.txt +3 -0
  5. data/intercom.gemspec +1 -1
  6. data/lib/intercom.rb +13 -153
  7. data/lib/intercom/admin.rb +0 -2
  8. data/lib/intercom/api_operations/convert.rb +5 -5
  9. data/lib/intercom/api_operations/delete.rb +4 -6
  10. data/lib/intercom/api_operations/find.rb +9 -15
  11. data/lib/intercom/api_operations/find_all.rb +16 -21
  12. data/lib/intercom/api_operations/list.rb +4 -10
  13. data/lib/intercom/api_operations/load.rb +6 -6
  14. data/lib/intercom/api_operations/save.rb +23 -28
  15. data/lib/intercom/client.rb +95 -0
  16. data/lib/intercom/{collection_proxy.rb → client_collection_proxy.rb} +5 -8
  17. data/lib/intercom/company.rb +0 -16
  18. data/lib/intercom/contact.rb +0 -11
  19. data/lib/intercom/conversation.rb +0 -10
  20. data/lib/intercom/event.rb +0 -2
  21. data/lib/intercom/extended_api_operations/segments.rb +13 -0
  22. data/lib/intercom/extended_api_operations/tags.rb +4 -5
  23. data/lib/intercom/extended_api_operations/users.rb +3 -5
  24. data/lib/intercom/message.rb +0 -2
  25. data/lib/intercom/note.rb +1 -10
  26. data/lib/intercom/options.rb +11 -0
  27. data/lib/intercom/request.rb +9 -5
  28. data/lib/intercom/segment.rb +0 -7
  29. data/lib/intercom/service/admin.rb +14 -0
  30. data/lib/intercom/service/base_service.rb +21 -0
  31. data/lib/intercom/service/company.rb +28 -0
  32. data/lib/intercom/service/contact.rb +22 -0
  33. data/lib/intercom/service/conversation.rb +31 -0
  34. data/lib/intercom/service/event.rb +14 -0
  35. data/lib/intercom/service/message.rb +14 -0
  36. data/lib/intercom/service/note.rb +22 -0
  37. data/lib/intercom/service/segment.rb +16 -0
  38. data/lib/intercom/service/subscription.rb +21 -0
  39. data/lib/intercom/service/tag.rb +30 -0
  40. data/lib/intercom/service/user.rb +28 -0
  41. data/lib/intercom/subscription.rb +0 -8
  42. data/lib/intercom/tag.rb +0 -16
  43. data/lib/intercom/user.rb +0 -16
  44. data/lib/intercom/utils.rb +0 -1
  45. data/lib/intercom/version.rb +1 -1
  46. data/spec/spec_helper.rb +117 -0
  47. data/spec/unit/intercom/admin_spec.rb +11 -4
  48. data/spec/unit/intercom/client_collection_proxy_spec.rb +35 -0
  49. data/spec/unit/intercom/client_spec.rb +21 -0
  50. data/spec/unit/intercom/company_spec.rb +24 -11
  51. data/spec/unit/intercom/contact_spec.rb +7 -5
  52. data/spec/unit/intercom/conversation_spec.rb +28 -0
  53. data/spec/unit/intercom/event_spec.rb +6 -5
  54. data/spec/unit/intercom/message_spec.rb +9 -8
  55. data/spec/unit/intercom/note_spec.rb +9 -2
  56. data/spec/unit/intercom/segment_spec.rb +12 -0
  57. data/spec/unit/intercom/subscription_spec.rb +7 -5
  58. data/spec/unit/intercom/tag_spec.rb +13 -7
  59. data/spec/unit/intercom/user_spec.rb +41 -33
  60. data/spec/unit/intercom_spec.rb +0 -83
  61. metadata +30 -20
  62. data/lib/intercom/api_operations/count.rb +0 -16
  63. data/lib/intercom/count.rb +0 -21
  64. data/lib/intercom/extended_api_operations/reply.rb +0 -16
  65. data/lib/intercom/generic_handlers/base_handler.rb +0 -22
  66. data/lib/intercom/generic_handlers/count.rb +0 -59
  67. data/lib/intercom/generic_handlers/tag.rb +0 -71
  68. data/lib/intercom/generic_handlers/tag_find_all.rb +0 -47
  69. data/lib/intercom/notification.rb +0 -20
  70. data/lib/intercom/traits/generic_handler_binding.rb +0 -29
  71. data/spec/unit/intercom/collection_proxy_spec.rb +0 -34
  72. data/spec/unit/intercom/notification_spec.rb +0 -68
@@ -1,16 +0,0 @@
1
- module Intercom
2
- module ApiOperations
3
- module Count
4
- module ClassMethods
5
- def count
6
- singular_resource_name = Utils.resource_class_to_singular_name(self)
7
- Intercom::Count.send("#{singular_resource_name}_count")
8
- end
9
- end
10
-
11
- def self.included(base)
12
- base.extend(ClassMethods)
13
- end
14
- end
15
- end
16
- end
@@ -1,21 +0,0 @@
1
- require 'intercom/traits/api_resource'
2
- require 'intercom/api_operations/find'
3
- require 'intercom/traits/generic_handler_binding'
4
- require 'intercom/generic_handlers/count'
5
-
6
- module Intercom
7
- class Count
8
- include ApiOperations::Find
9
- include Traits::ApiResource
10
- include Traits::GenericHandlerBinding
11
- include GenericHandlers::Count
12
-
13
- def self.fetch_for_app
14
- Intercom::Count.find({})
15
- end
16
-
17
- def self.fetch_broken_down_count(entity_to_count, count_context)
18
- Intercom::Count.find(:type => entity_to_count, :count => count_context)
19
- end
20
- end
21
- end
@@ -1,16 +0,0 @@
1
- require 'intercom/traits/api_resource'
2
-
3
- module Intercom
4
- module ExtendedApiOperations
5
- module Reply
6
-
7
- def reply(reply_data)
8
- collection_name = Utils.resource_class_to_collection_name(self.class)
9
- # TODO: For server, we should not need to merge in :conversation_id here (already in the URL)
10
- response = Intercom.post("/#{collection_name}/#{id}/reply", reply_data.merge(:conversation_id => id))
11
- from_response(response)
12
- end
13
-
14
- end
15
- end
16
- end
@@ -1,22 +0,0 @@
1
- module Intercom
2
- module GenericHandlers
3
- class BaseHandler
4
- attr_reader :method_sym, :arguments, :entity
5
-
6
- def initialize(method_sym, arguments, entity)
7
- @method_sym = method_sym
8
- @arguments = arguments
9
- @entity = entity
10
- end
11
-
12
- def method_string
13
- method_sym.to_s
14
- end
15
-
16
- def raise_no_method_missing_handler
17
- raise Intercom::NoMethodMissingHandler, "Could not handle '#{method_string}'"
18
- end
19
- end
20
-
21
- end
22
- end
@@ -1,59 +0,0 @@
1
- require 'intercom/generic_handlers/base_handler'
2
-
3
- module Intercom
4
- module GenericHandlers
5
- module Count
6
- module ClassMethods
7
- def generic_count(method_sym, *arguments, &block)
8
-
9
- handler_class = Class.new(GenericHandlers::BaseHandler) do
10
- def handle
11
- match = method_string.match(GenericHandlers::Count.count_breakdown_matcher)
12
- if match && match[1] && match[2] && match[3].nil?
13
- do_broken_down_count(match[1], match[2])
14
- elsif method_string.end_with? '_count'
15
- return do_count
16
- else
17
- raise_no_method_missing_handler
18
- end
19
- end
20
-
21
- private
22
-
23
- def do_count
24
- entity.fetch_for_app.send(appwide_entity_to_count)['count']
25
- rescue Intercom::AttributeNotSetError
26
- # Indicates this this kind of counting is not supported
27
- raise_no_method_missing_handler
28
- end
29
-
30
- def do_broken_down_count(entity_to_count, count_context)
31
- result = entity.fetch_broken_down_count(entity_to_count, count_context)
32
- result.send(entity_to_count)[count_context]
33
- rescue Intercom::BadRequestError => ex
34
- # Indicates this this kind of counting is not supported
35
- ex.application_error_code == 'parameter_invalid' ? raise_no_method_missing_handler : raise
36
- end
37
-
38
- def appwide_entity_to_count; method_string.gsub(/_count$/, ''); end
39
- end
40
-
41
- handler_class.new(method_sym, arguments, self).handle
42
- end
43
-
44
- end
45
-
46
- def self.count_breakdown_matcher
47
- /([[:alnum:]]+)_counts_for_each_([[:alnum:]]+)/
48
- end
49
-
50
- def self.handles_method?(method_sym)
51
- method_sym.to_s.end_with? '_count' or method_sym.to_s.match(count_breakdown_matcher)
52
- end
53
-
54
- def self.included(base)
55
- base.extend(ClassMethods)
56
- end
57
- end
58
- end
59
- end
@@ -1,71 +0,0 @@
1
- require 'intercom/generic_handlers/base_handler'
2
-
3
- module Intercom
4
- module GenericHandlers
5
- module Tag
6
- module ClassMethods
7
- def generic_tag(method_sym, *arguments, &block)
8
-
9
- handler_class = Class.new(GenericHandlers::BaseHandler) do
10
- def handle
11
- if method_string.start_with? 'tag_'
12
- return do_tagging
13
- elsif method_string.start_with? 'untag_'
14
- return do_untagging
15
- else
16
- raise_no_method_missing_handler
17
- end
18
- end
19
-
20
- private
21
-
22
- def do_tagging
23
- entity.create(:name => arguments[0], tagging_context.to_sym => tag_object_list(arguments))
24
- end
25
-
26
- def do_untagging
27
- current_tag = find_tag(arguments[0])
28
- return untag_via_save(current_tag) if current_tag
29
- end
30
-
31
- def find_tag(name_arg)
32
- Intercom::Tag.find(:name => name_arg)
33
- rescue Intercom::ResourceNotFound
34
- return nil # Ignore if tag has since been deleted
35
- end
36
-
37
- def untag_via_save(current_tag)
38
- current_tag.name = arguments[0]
39
- current_tag.send("#{untagging_context}=", untag_object_list(arguments))
40
- current_tag.save
41
- end
42
-
43
- def tag_object_list(args)
44
- args[1].map { |id| { :id => id } }
45
- end
46
-
47
- def untag_object_list(args)
48
- to_tag = tag_object_list(args)
49
- to_tag.map { |tag_object| tag_object[:untag] = true }
50
- to_tag
51
- end
52
-
53
- def tagging_context; method_string.gsub(/^tag_/, ''); end
54
- def untagging_context; method_string.gsub(/^untag_/, ''); end
55
- end
56
-
57
- handler_class.new(method_sym, arguments, self).handle
58
- end
59
-
60
- end
61
-
62
- def self.handles_method?(method_sym)
63
- method_sym.to_s.start_with?('tag_') || method_sym.to_s.start_with?('untag_')
64
- end
65
-
66
- def self.included(base)
67
- base.extend(ClassMethods)
68
- end
69
- end
70
- end
71
- end
@@ -1,47 +0,0 @@
1
- require 'intercom/generic_handlers/base_handler'
2
-
3
- module Intercom
4
- module GenericHandlers
5
- module TagFindAll
6
- module ClassMethods
7
- def generic_tag_find_all(method_sym, *arguments, &block)
8
-
9
- handler_class = Class.new(GenericHandlers::BaseHandler) do
10
- def handle
11
- if method_string.start_with? 'find_all_for_'
12
- return do_tag_find_all
13
- else
14
- raise_no_method_missing_handler
15
- end
16
- end
17
-
18
- private
19
-
20
- def do_tag_find_all
21
- Intercom::Tag.find_all(cleaned_arguments.merge(:taggable_type => Utils.singularize(context)))
22
- end
23
-
24
- def cleaned_arguments
25
- cleaned_args = arguments[0]
26
- cleaned_args[:taggable_id] = cleaned_args.delete(:id) if cleaned_args.has_key?(:id)
27
- cleaned_args
28
- end
29
-
30
- def context; method_string.gsub(/^find_all_for_/, ''); end
31
- end
32
-
33
- handler_class.new(method_sym, arguments, self).handle
34
- end
35
-
36
- end
37
-
38
- def self.handles_method?(method_sym)
39
- method_sym.to_s.start_with? 'find_all_for_'
40
- end
41
-
42
- def self.included(base)
43
- base.extend(ClassMethods)
44
- end
45
- end
46
- end
47
- end
@@ -1,20 +0,0 @@
1
- require 'intercom/traits/api_resource'
2
-
3
- module Intercom
4
- class Notification
5
- include Traits::ApiResource
6
-
7
- def model
8
- data.item
9
- end
10
-
11
- def model_type
12
- model.class
13
- end
14
-
15
- def load
16
- model.load
17
- end
18
-
19
- end
20
- end
@@ -1,29 +0,0 @@
1
- module Intercom
2
- module Traits
3
-
4
- # Allows us to have one class level method missing handler across all entities
5
- # which can dispatch to the appropriate function based on the method name
6
- module GenericHandlerBinding
7
-
8
- module ClassMethods
9
- def method_missing(method_sym, *arguments, &block)
10
- if respond_to? :generic_tag and GenericHandlers::Tag.handles_method?(method_sym)
11
- return generic_tag(method_sym, *arguments, block)
12
- elsif respond_to? :generic_tag_find_all and GenericHandlers::TagFindAll.handles_method?(method_sym)
13
- return generic_tag_find_all(method_sym, *arguments, block)
14
- elsif respond_to? :generic_count and GenericHandlers::Count.handles_method?(method_sym)
15
- return generic_count(method_sym, *arguments, block)
16
- end
17
- super
18
- rescue Intercom::NoMethodMissingHandler
19
- super
20
- end
21
- end
22
-
23
- def self.included(base)
24
- base.extend(ClassMethods)
25
- end
26
-
27
- end
28
- end
29
- end
@@ -1,34 +0,0 @@
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(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(true))
14
- Intercom.expects(:get).with('https://api.intercom.io/users?per_page=50&page=2', {}).returns(page_of_users(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(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(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(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
@@ -1,68 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe "Intercom::Notification" do
4
-
5
- it "converts notification hash to object" do
6
- payload = Intercom::Notification.new(test_user_notification)
7
- payload.must_be_instance_of Intercom::Notification
8
- end
9
-
10
- it "returns correct model type for User" do
11
- payload = Intercom::Notification.new(test_user_notification)
12
- payload.model_type.must_equal Intercom::User
13
- end
14
-
15
- it "returns correct User notification topic" do
16
- payload = Intercom::Notification.new(test_user_notification)
17
- payload.topic.must_equal "user.created"
18
- end
19
-
20
- it "returns instance of User" do
21
- payload = Intercom::Notification.new(test_user_notification)
22
- payload.model.must_be_instance_of Intercom::User
23
- end
24
-
25
- it "returns instance of Conversation" do
26
- payload = Intercom::Notification.new(test_conversation_notification)
27
- payload.model.must_be_instance_of Intercom::Conversation
28
- end
29
-
30
- it "returns correct model type for Conversation" do
31
- payload = Intercom::Notification.new(test_conversation_notification)
32
- payload.model_type.must_equal Intercom::Conversation
33
- end
34
-
35
- it "returns correct Conversation notification topic" do
36
- payload = Intercom::Notification.new(test_conversation_notification)
37
- payload.topic.must_equal "conversation.user.created"
38
- end
39
-
40
- it "returns inner User object for Conversation" do
41
- payload = Intercom::Notification.new(test_conversation_notification)
42
- payload.model.user.must_be_instance_of Intercom::User
43
- end
44
-
45
- it "returns inner User object with nil tags" do
46
- user_notification = {
47
- "type" => "notification_event",
48
- "app_id" => "aa11aa",
49
- "data" => {
50
- "type" => "notification_event_data",
51
- "item" => {
52
- "type" => "user",
53
- "id" => "abc123def",
54
- "user_id" => "666",
55
- "email" => "joe@example.com",
56
- "name" => "Joe",
57
- "tags" => {
58
- "type" => "tag.list",
59
- "tags" => nil
60
- }
61
- }
62
- },
63
- }
64
- payload = Intercom::Notification.new(user_notification)
65
- payload.model.tags.must_equal([])
66
- end
67
-
68
- end