intercom_event_wrapper 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2b0abe404f7a0bcbce8b47cba2eedc18846b3c3a
4
+ data.tar.gz: 1d5fc8f53cabdfa610dbb82b2dad9b455ebf1c99
5
+ SHA512:
6
+ metadata.gz: ad676bae938dd10268294b3c9815e5454eb4f75fbcfc46c9b113f06e58d4d27a55c31daf5a24205c436345f37244ab7ec33f18ec6d13554eb0b5018fb1f51e51
7
+ data.tar.gz: f1b5e1bc346e528cc1cbcb7988e3d7b71bc9735a58b607816eb1755f85595f0595332be34931ecfde4dd131caca79c5b4eb2099e19cdb00dde6cc9febe90683b
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ doc
2
+ *.gem
3
+ *.iml
4
+ .bundle
5
+ Gemfile.lock
6
+ pkg/*
7
+ .rakeTasks
8
+ .yardoc
9
+ spike.rb
10
+ html
11
+ .idea
12
+ *.swp
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem 'intercom'
4
+
5
+ group :development, :test do
6
+ gem 'rspec'
7
+ end
8
+
data/README.md ADDED
@@ -0,0 +1,38 @@
1
+ # Intercom-ruby wrapper
2
+
3
+ This is a simple wrapper to intercom that lets you create intercom events to multiple intercom apps from a single service. Unfortunately Intercom implementation of the ruby gem is a global singleton that demands ```app_id``` and ```app_api_key```. This means that you cannot use multiple intercom apps from within your application reliably.
4
+
5
+ This gem overloads the ::Intercom::Event.create method, so you can pass app_id and app_api_key to it. This allows you to create intercom events to multiple intercom apps from a single service.
6
+
7
+ # Installation
8
+
9
+ Add these to your gemfile:
10
+ ```
11
+ gem 'intercom'
12
+ gem 'intercom_event_wrapper'
13
+ ```
14
+
15
+ Require the gem somewhere
16
+ ```
17
+ require 'intercom'
18
+ require 'intercom_event_wrapper'
19
+ ```
20
+ # Usage
21
+ Before, if you wanted to use intercom api and wanted to create an event, you had to do something like this:
22
+ ```
23
+ ::Intercom.app_id = "your_app_id"
24
+ ::Intercom.app_api_key = "your_app_api_key"
25
+ ... # somewhere later
26
+ ::Intercom::Event.create your_event_data
27
+ ```
28
+
29
+ However, if you wanted to create events to different apps from a single service, you had to change the ```app_id``` and ```app_api_key``` before use. This is prone to race conditions in an asynchronous or multithreaded program.
30
+
31
+ With this wrapped you can instead just create an event like that
32
+ ```
33
+ ::Intercom::EventWrapper.create your_event_data, your_app_id, your_app_api_key
34
+ ```
35
+
36
+ # Disclaimer
37
+
38
+ You are free to use and modify this wrapper in any way you want. You can also send me (pull)requests to update or add something new to it. I, however, will take no responsibility over anything you do with this library.
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+
4
+ Gem::Specification.new do |spec|
5
+ spec.name = "intercom_event_wrapper"
6
+ spec.version = "0.0.0"
7
+ spec.authors = ["Stenver Jerkku"]
8
+ spec.email = ["stenver1010@gmail.com"]
9
+ spec.summary = %q{Wrapper for Intercom api, to get around the singleton restrictions that the original intercom gem imposes on you}
10
+ spec.description = %Q{This is a wrapper for Intercom ruby gem(https://github.com/intercom/intercom-ruby) that offers a way for the developer to fire intercom events to different interom apps from a single application or service. }
11
+ spec.license = "MIT"
12
+ spec.homepage = "https://github.com/stenver/intercom_event_wrapper"
13
+
14
+ spec.files = `git ls-files`.split("\n")
15
+ spec.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
+ spec.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
+ spec.require_paths = ["lib"]
18
+
19
+ spec.add_development_dependency 'rspec', '~>3'
20
+ spec.add_dependency 'intercom', '~>2.1'
21
+
22
+ spec.required_ruby_version = '~> 2.0'
23
+ end
@@ -0,0 +1,25 @@
1
+ require 'intercom'
2
+ require 'intercom_wrapper'
3
+ require 'intercom/api_operations/save'
4
+ require 'intercom/traits/api_resource'
5
+
6
+ module Intercom
7
+ class EventWrapper < Event
8
+
9
+ def self.create(params, app_id, app_api_key)
10
+ instance = self.new(params)
11
+ instance.mark_fields_as_changed!(params.keys)
12
+ instance.save(app_id, app_api_key)
13
+ end
14
+
15
+ def save(app_id, app_api_key)
16
+ collection_name = Utils.resource_class_to_collection_name(self.class.superclass)
17
+ if id_present? && !posted_updates?
18
+ response = IntercomWrapper.put("/#{collection_name}/#{id}", to_submittable_hash, app_id, app_api_key)
19
+ else
20
+ response = IntercomWrapper.post("/#{collection_name}", to_submittable_hash.merge(identity_hash), app_id, app_api_key)
21
+ end
22
+ from_response(response) if response # may be nil we received back a 202
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,34 @@
1
+ require 'intercom'
2
+
3
+ module IntercomWrapper
4
+ extend Intercom
5
+
6
+ def self.post(path, payload_hash, app_id, app_api_key)
7
+ send_request_to_path(Intercom::Request.post(path, payload_hash), app_id, app_api_key)
8
+ end
9
+
10
+ def self.put(path, payload_hash, app_id, app_api_key)
11
+ send_request_to_path(Intercom::Request.put(path, payload_hash), app_id, app_api_key)
12
+ end
13
+
14
+ def self.send_request_to_path(request, app_id, app_api_key)
15
+ request.execute(target_base_url(app_id, app_api_key))
16
+ rescue Intercom::ServiceUnavailableError => e
17
+ if Intercom::endpoints.length > 1
18
+ retry_on_alternative_endpoint(request, app_id, app_api_key)
19
+ else
20
+ raise e
21
+ end
22
+ end
23
+
24
+ def self.retry_on_alternative_endpoint(request, app_id, app_api_key)
25
+ Intercom.current_endpoint = Intercom::alternative_random_endpoint
26
+ request.execute(target_base_url, app_id, app_api_key)
27
+ end
28
+
29
+ def self.target_base_url(app_id, app_api_key)
30
+ raise ArgumentError, "#{Intercom.configuration_required_text} #{Intercom.related_docs_text}" if [app_id, app_api_key].any?(&:nil?)
31
+ basic_auth_part = "#{app_id}:#{app_api_key}@"
32
+ Intercom.current_endpoint.gsub(/(https?:\/\/)(.*)/, "\\1#{basic_auth_part}\\2")
33
+ end
34
+ end
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+ require 'intercom_event_wrapper'
3
+ require 'net/https'
4
+
5
+ describe Intercom::EventWrapper do
6
+
7
+ let(:intercom_event_wrapper_class) { subject.intercom::EventWrapper }
8
+ let(:event) {{
9
+ event_name: "visitor-left",
10
+ user_id: 1,
11
+ created_at: Time.now
12
+ }}
13
+ let(:app_id) { "pi3243fa" }
14
+ let(:app_api_key) { "da39a3ee5e6b4b0d3255bfef95601890afd80709" }
15
+
16
+ # Since intercom doesnt provide free testing accounts, then this is the best test we can make.
17
+ # Not a very good one, but if we get unauthorized exception, then we can assume that the logic at least
18
+ # hit the right target
19
+ it 'sends an event to intercom' do
20
+ expect{::Intercom::EventWrapper.create event, app_id, app_api_key}.to raise_error(Intercom::AuthenticationError)
21
+ end
22
+ end
File without changes
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: intercom_event_wrapper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Stenver Jerkku
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: intercom
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.1'
41
+ description: 'This is a wrapper for Intercom ruby gem(https://github.com/intercom/intercom-ruby)
42
+ that offers a way for the developer to fire intercom events to different interom
43
+ apps from a single application or service. '
44
+ email:
45
+ - stenver1010@gmail.com
46
+ executables: []
47
+ extensions: []
48
+ extra_rdoc_files: []
49
+ files:
50
+ - ".gitignore"
51
+ - Gemfile
52
+ - README.md
53
+ - intercom_event_wrapper.gemspec
54
+ - lib/intercom_event_wrapper.rb
55
+ - lib/intercom_wrapper.rb
56
+ - spec/intercom_wrapper/intercom_event_wrapper_spec.rb
57
+ - spec/spec_helper.rb
58
+ homepage: https://github.com/stenver/intercom_event_wrapper
59
+ licenses:
60
+ - MIT
61
+ metadata: {}
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - "~>"
69
+ - !ruby/object:Gem::Version
70
+ version: '2.0'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubyforge_project:
78
+ rubygems_version: 2.2.2
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: Wrapper for Intercom api, to get around the singleton restrictions that the
82
+ original intercom gem imposes on you
83
+ test_files:
84
+ - spec/intercom_wrapper/intercom_event_wrapper_spec.rb
85
+ - spec/spec_helper.rb