intercom 2.5.4 → 3.0.0b1

Sign up to get free protection for your applications and to get access to all the features.
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,9 +1,7 @@
1
- require 'intercom/api_operations/list'
2
1
  require 'intercom/traits/api_resource'
3
2
 
4
3
  module Intercom
5
4
  class Admin
6
- include ApiOperations::List
7
5
  include Traits::ApiResource
8
6
  end
9
7
  end
@@ -3,13 +3,13 @@ require 'intercom/traits/api_resource'
3
3
  module Intercom
4
4
  module ApiOperations
5
5
  module Convert
6
- def convert(user)
7
- from_response(
8
- Intercom.post(
6
+ def convert(contact, user)
7
+ Intercom::User.new.from_response(
8
+ @client.post(
9
9
  "/contacts/convert",
10
10
  {
11
- contact: { user_id: user_id },
12
- user: user.identity_hash
11
+ contact: { user_id: contact.user_id },
12
+ user: identity_hash(user)
13
13
  }
14
14
  )
15
15
  )
@@ -3,13 +3,11 @@ require 'intercom/traits/api_resource'
3
3
  module Intercom
4
4
  module ApiOperations
5
5
  module Delete
6
-
7
- def delete
8
- collection_name = Utils.resource_class_to_collection_name(self.class)
9
- Intercom.delete("/#{collection_name}/#{id}", {})
10
- self
6
+ def delete(object)
7
+ collection_name = Utils.resource_class_to_collection_name(collection_class)
8
+ @client.delete("/#{collection_name}/#{object.id}", {})
9
+ object
11
10
  end
12
-
13
11
  end
14
12
  end
15
13
  end
@@ -1,22 +1,16 @@
1
1
  module Intercom
2
2
  module ApiOperations
3
3
  module Find
4
- module ClassMethods
5
- def find(params)
6
- raise BadRequestError, "#{self}#find takes a hash as its parameter but you supplied #{params.inspect}" unless params.is_a? Hash
7
- collection_name = Utils.resource_class_to_collection_name(self)
8
- if params[:id]
9
- response = Intercom.get("/#{collection_name}/#{params[:id]}", {})
10
- else
11
- response = Intercom.get("/#{collection_name}", params)
12
- end
13
- raise Intercom::HttpError.new('Http Error - No response entity returned') unless response
14
- from_api(response)
4
+ def find(params)
5
+ raise BadRequestError, "#{self}#find takes a hash as its parameter but you supplied #{params.inspect}" unless params.is_a? Hash
6
+ collection_name = Utils.resource_class_to_collection_name(collection_class)
7
+ if params[:id]
8
+ response = @client.get("/#{collection_name}/#{params[:id]}", {})
9
+ else
10
+ response = @client.get("/#{collection_name}", params)
15
11
  end
16
- end
17
-
18
- def self.included(base)
19
- base.extend(ClassMethods)
12
+ raise Intercom::HttpError.new('Http Error - No response entity returned') unless response
13
+ from_api(response)
20
14
  end
21
15
  end
22
16
  end
@@ -1,32 +1,27 @@
1
- require 'intercom/collection_proxy'
1
+ require 'intercom/client_collection_proxy'
2
2
 
3
3
  module Intercom
4
4
  module ApiOperations
5
5
  module FindAll
6
- module ClassMethods
7
- def find_all(params)
8
- raise BadRequestError, "#{self}#find takes a hash as its parameter but you supplied #{params.inspect}" unless params.is_a? Hash
9
- collection_name = Utils.resource_class_to_collection_name(self)
10
- finder_details = {}
11
- if params[:id] && !type_switched_finder?(params)
12
- finder_details[:url] = "/#{collection_name}/#{params[:id]}"
13
- finder_details[:params] = {}
14
- else
15
- finder_details[:url] = "/#{collection_name}"
16
- finder_details[:params] = params
17
- end
18
- CollectionProxy.new(collection_name, finder_details)
19
- end
20
-
21
- private
22
6
 
23
- def type_switched_finder?(params)
24
- params.include?(:type)
7
+ def find_all(params)
8
+ raise BadRequestError, "#find takes a hash as its parameter but you supplied #{params.inspect}" unless params.is_a? Hash
9
+ collection_name = Utils.resource_class_to_collection_name(collection_class)
10
+ finder_details = {}
11
+ if params[:id] && !type_switched_finder?(params)
12
+ finder_details[:url] = "/#{collection_name}/#{params[:id]}"
13
+ finder_details[:params] = {}
14
+ else
15
+ finder_details[:url] = "/#{collection_name}"
16
+ finder_details[:params] = params
25
17
  end
18
+ ClientCollectionProxy.new(collection_name, finder_details: finder_details, client: @client)
26
19
  end
27
20
 
28
- def self.included(base)
29
- base.extend(ClassMethods)
21
+ private
22
+
23
+ def type_switched_finder?(params)
24
+ params.include?(:type)
30
25
  end
31
26
  end
32
27
  end
@@ -1,16 +1,10 @@
1
- require 'intercom/collection_proxy'
1
+ require 'intercom/client_collection_proxy'
2
2
 
3
3
  module Intercom
4
4
  module ApiOperations
5
- module List # TODO: Should we rename to All
6
- module ClassMethods
7
- def all
8
- CollectionProxy.new(Utils.resource_class_to_collection_name(self))
9
- end
10
- end
11
-
12
- def self.included(base)
13
- base.extend(ClassMethods)
5
+ module List
6
+ def all
7
+ ClientCollectionProxy.new(Utils.resource_class_to_collection_name(collection_class), client: @client)
14
8
  end
15
9
  end
16
10
  end
@@ -1,15 +1,15 @@
1
1
  module Intercom
2
2
  module ApiOperations
3
3
  module Load
4
- def load
5
- collection_name = Utils.resource_class_to_collection_name(self.class)
6
- if id
7
- response = Intercom.get("/#{collection_name}/#{id}", {})
4
+ def load(object)
5
+ collection_name = Utils.resource_class_to_collection_name(collection_class)
6
+ if object.id
7
+ response = @client.get("/#{collection_name}/#{object.id}", {})
8
8
  else
9
- raise "Cannot load #{self.class} as it does not have a valid id."
9
+ raise "Cannot load #{collection_class} as it does not have a valid id."
10
10
  end
11
11
  raise Intercom::HttpError.new('Http Error - No response entity returned') unless response
12
- from_response(response)
12
+ object.from_response(response)
13
13
  end
14
14
  end
15
15
  end
@@ -3,48 +3,43 @@ require 'intercom/traits/api_resource'
3
3
  module Intercom
4
4
  module ApiOperations
5
5
  module Save
6
-
7
- module ClassMethods
8
- PARAMS_NOT_PROVIDED = Object.new
9
- def create(params = PARAMS_NOT_PROVIDED)
10
- if self.ancestors.include?(Intercom::Contact) && params == PARAMS_NOT_PROVIDED
11
- params = Hash.new
12
- elsif params == PARAMS_NOT_PROVIDED
13
- raise ArgumentError, '.create requires 1 parameter'
14
- end
15
-
16
- instance = self.new(params)
17
- instance.mark_fields_as_changed!(params.keys)
18
- instance.save
6
+ PARAMS_NOT_PROVIDED = Object.new
7
+ private_constant :PARAMS_NOT_PROVIDED
8
+
9
+ def create(params = PARAMS_NOT_PROVIDED)
10
+ if collection_class.ancestors.include?(Intercom::Contact) && params == PARAMS_NOT_PROVIDED
11
+ params = Hash.new
12
+ elsif params == PARAMS_NOT_PROVIDED
13
+ raise ArgumentError, '.create requires 1 parameter'
19
14
  end
20
- end
21
15
 
22
- def self.included(base)
23
- base.extend(ClassMethods)
16
+ instance = collection_class.new(params)
17
+ instance.mark_fields_as_changed!(params.keys)
18
+ save(instance)
24
19
  end
25
20
 
26
- def save
27
- collection_name = Utils.resource_class_to_collection_name(self.class)
28
- if id_present? && !posted_updates?
29
- response = Intercom.put("/#{collection_name}/#{id}", to_submittable_hash)
21
+ def save(object)
22
+ collection_name = Utils.resource_class_to_collection_name(collection_class)
23
+ if id_present?(object) && !posted_updates?(object)
24
+ response = @client.put("/#{collection_name}/#{id}", object.to_submittable_hash)
30
25
  else
31
- response = Intercom.post("/#{collection_name}", to_submittable_hash.merge(identity_hash))
26
+ response = @client.post("/#{collection_name}", object.to_submittable_hash.merge(identity_hash(object)))
32
27
  end
33
- from_response(response) if response # may be nil we received back a 202
28
+ object.from_response(response) if response # may be nil we received back a 202
34
29
  end
35
30
 
36
- def identity_hash
37
- respond_to?(:identity_vars) ? SliceableHash.new(to_hash).slice(*(identity_vars.map(&:to_s))) : {}
31
+ def identity_hash(object)
32
+ object.respond_to?(:identity_vars) ? SliceableHash.new(object.to_hash).slice(*(object.identity_vars.map(&:to_s))) : {}
38
33
  end
39
34
 
40
35
  private
41
36
 
42
- def id_present?
43
- id && id.to_s != ''
37
+ def id_present?(object)
38
+ object.id && object.id.to_s != ''
44
39
  end
45
40
 
46
- def posted_updates?
47
- respond_to?(:update_verb) && update_verb == 'post'
41
+ def posted_updates?(object)
42
+ object.respond_to?(:update_verb) && object.update_verb == 'post'
48
43
  end
49
44
  end
50
45
  end
@@ -0,0 +1,95 @@
1
+ module Intercom
2
+ class Client
3
+ include Options
4
+ attr_reader :base_url, :rate_limit_details
5
+
6
+ class << self
7
+ def set_base_url(base_url)
8
+ return Proc.new do |o|
9
+ old_url = o.base_url
10
+ o.send(:base_url=, base_url)
11
+ Proc.new { |obj| set_base_url(old_url).call(o) }
12
+ end
13
+ end
14
+ end
15
+
16
+ def initialize(app_id: 'my_app_id', api_key: 'my_api_key')
17
+ @app_id = app_id
18
+ @api_key = api_key
19
+ @base_url = 'https://api.intercom.io'
20
+ @rate_limit_details = {}
21
+ end
22
+
23
+ def admins
24
+ Intercom::Service::Admin.new(self)
25
+ end
26
+
27
+ def companies
28
+ Intercom::Service::Company.new(self)
29
+ end
30
+
31
+ def contacts
32
+ Intercom::Service::Contact.new(self)
33
+ end
34
+
35
+ def conversations
36
+ Intercom::Service::Conversation.new(self)
37
+ end
38
+
39
+ def events
40
+ Intercom::Service::Event.new(self)
41
+ end
42
+
43
+ def messages
44
+ Intercom::Service::Message.new(self)
45
+ end
46
+
47
+ def notes
48
+ Intercom::Service::Note.new(self)
49
+ end
50
+
51
+ def subscriptions
52
+ Intercom::Service::Subscription.new(self)
53
+ end
54
+
55
+ def segments
56
+ Intercom::Service::Segment.new(self)
57
+ end
58
+
59
+ def tags
60
+ Intercom::Service::Tag.new(self)
61
+ end
62
+
63
+ def users
64
+ Intercom::Service::User.new(self)
65
+ end
66
+
67
+ def get(path, params)
68
+ execute_request Intercom::Request.get(path, params)
69
+ end
70
+
71
+ def post(path, payload_hash)
72
+ execute_request Intercom::Request.post(path, payload_hash)
73
+ end
74
+
75
+ def put(path, payload_hash)
76
+ execute_request Intercom::Request.put(path, payload_hash)
77
+ end
78
+
79
+ def delete(path, payload_hash)
80
+ execute_request Intercom::Request.delete(path, payload_hash)
81
+ end
82
+
83
+ private
84
+
85
+ def execute_request(request)
86
+ result = request.execute(@base_url, username: @app_id, secret: @api_key)
87
+ @rate_limit_details = request.rate_limit_details
88
+ result
89
+ end
90
+
91
+ def base_url=(new_url)
92
+ @base_url = new_url
93
+ end
94
+ end
95
+ end
@@ -2,24 +2,25 @@ require "intercom/utils"
2
2
  require "ext/sliceable_hash"
3
3
 
4
4
  module Intercom
5
- class CollectionProxy
5
+ class ClientCollectionProxy
6
6
 
7
7
  attr_reader :resource_name
8
8
 
9
- def initialize(resource_name, finder_details = {})
9
+ def initialize(resource_name, finder_details: {}, client: client)
10
10
  @resource_name = resource_name
11
11
  @resource_class = Utils.constantize_resource_name(resource_name)
12
12
  @finder_url = (finder_details[:url] || "/#{@resource_name}")
13
13
  @finder_params = (finder_details[:params] || {})
14
+ @client = client
14
15
  end
15
16
 
16
17
  def each(&block)
17
18
  next_page = nil
18
19
  loop do
19
20
  if next_page
20
- response_hash = Intercom.get(next_page, {})
21
+ response_hash = @client.get(next_page, {})
21
22
  else
22
- response_hash = Intercom.get(@finder_url, @finder_params)
23
+ response_hash = @client.get(@finder_url, @finder_params)
23
24
  end
24
25
  raise Intercom::HttpError.new('Http Error - No response entity returned') unless response_hash
25
26
  deserialize_response_hash(response_hash, block)
@@ -38,10 +39,6 @@ module Intercom
38
39
 
39
40
  include Enumerable
40
41
 
41
- def count
42
- raise NoMethodError, "undefined method `count' for #{self.class}. Consider using the dedicated Intercom::Count interface if suitable"
43
- end
44
-
45
42
  private
46
43
 
47
44
  def resource_class; @resource_class; end
@@ -1,24 +1,8 @@
1
- require 'intercom/api_operations/count'
2
- require 'intercom/api_operations/list'
3
- require 'intercom/api_operations/find'
4
- require 'intercom/api_operations/find_all'
5
- require 'intercom/api_operations/save'
6
- require 'intercom/api_operations/load'
7
- require 'intercom/extended_api_operations/users'
8
- require 'intercom/extended_api_operations/tags'
9
1
  require 'intercom/traits/incrementable_attributes'
10
2
  require 'intercom/traits/api_resource'
11
3
 
12
4
  module Intercom
13
5
  class Company
14
- include ApiOperations::Count
15
- include ApiOperations::List
16
- include ApiOperations::Find
17
- include ApiOperations::FindAll
18
- include ApiOperations::Save
19
- include ApiOperations::Load
20
- include ExtendedApiOperations::Users
21
- include ExtendedApiOperations::Tags
22
6
  include Traits::IncrementableAttributes
23
7
  include Traits::ApiResource
24
8
 
@@ -1,18 +1,7 @@
1
- require 'intercom/api_operations/count'
2
- require 'intercom/api_operations/load'
3
- require 'intercom/api_operations/find'
4
- require 'intercom/api_operations/find_all'
5
- require 'intercom/api_operations/save'
6
- require 'intercom/api_operations/convert'
7
1
  require 'intercom/traits/api_resource'
8
2
 
9
3
  module Intercom
10
4
  class Contact
11
- include ApiOperations::Load
12
- include ApiOperations::Find
13
- include ApiOperations::FindAll
14
- include ApiOperations::Save
15
- include ApiOperations::Convert
16
5
  include Traits::ApiResource
17
6
 
18
7
  def identity_vars ; [:email, :user_id] ; end
@@ -1,17 +1,7 @@
1
- require 'intercom/extended_api_operations/reply'
2
- require 'intercom/api_operations/find_all'
3
- require 'intercom/api_operations/find'
4
- require 'intercom/api_operations/load'
5
- require 'intercom/api_operations/save'
6
1
  require 'intercom/traits/api_resource'
7
2
 
8
3
  module Intercom
9
4
  class Conversation
10
- include ExtendedApiOperations::Reply
11
- include ApiOperations::FindAll
12
- include ApiOperations::Find
13
- include ApiOperations::Load
14
- include ApiOperations::Save
15
5
  include Traits::ApiResource
16
6
  end
17
7
  end