lucid_intercom 0.4.2 → 0.5.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.
- checksums.yaml +4 -4
- data/README.md +12 -22
- data/lib/lucid_intercom/attributes.rb +10 -42
- data/lib/lucid_intercom/changed_plan_event.rb +31 -0
- data/lib/lucid_intercom/company_attributes.rb +21 -0
- data/lib/lucid_intercom/company_custom_attributes.rb +48 -0
- data/lib/lucid_intercom/config.rb +44 -0
- data/lib/lucid_intercom/convert.rb +32 -0
- data/lib/lucid_intercom/{errors/error.rb → error.rb} +0 -0
- data/lib/lucid_intercom/event.rb +77 -0
- data/lib/lucid_intercom/installed_event.rb +31 -0
- data/lib/lucid_intercom/post_request.rb +26 -0
- data/lib/lucid_intercom/render_snippet.rb +28 -24
- data/lib/lucid_intercom/response.rb +51 -0
- data/lib/lucid_intercom/send_event.rb +22 -60
- data/lib/lucid_intercom/snippet.html.erb +2 -14
- data/lib/lucid_intercom/uninstalled_event.rb +27 -0
- data/lib/lucid_intercom/update_user.rb +24 -54
- data/lib/lucid_intercom/user_attributes.rb +47 -0
- data/lib/lucid_intercom/version.rb +1 -1
- data/lib/lucid_intercom.rb +18 -6
- metadata +56 -16
- data/lib/lucid_intercom/attributes/base.rb +0 -63
- data/lib/lucid_intercom/attributes/company.rb +0 -22
- data/lib/lucid_intercom/attributes/custom.rb +0 -47
- data/lib/lucid_intercom/attributes/user.rb +0 -28
- data/lib/lucid_intercom/credentials.rb +0 -35
- data/lib/lucid_intercom/errors/missing_credentials_error.rb +0 -7
- data/lib/lucid_intercom/errors/request_error.rb +0 -17
- data/lib/lucid_intercom/errors.rb +0 -3
- data/lib/lucid_intercom/events/base.rb +0 -23
- data/lib/lucid_intercom/events/changed_plan.rb +0 -37
- data/lib/lucid_intercom/events/installed.rb +0 -37
- data/lib/lucid_intercom/events/uninstalled.rb +0 -33
- data/lib/lucid_intercom/events.rb +0 -5
@@ -1,14 +1,2 @@
|
|
1
|
-
<script>
|
2
|
-
|
3
|
-
app_id: "<%= credentials.app_id %>",
|
4
|
-
<% if attributes %>
|
5
|
-
<% attributes.user_browser.each do |k, v| %><%= k %>: <%= h v %>,<% end %>
|
6
|
-
company: {
|
7
|
-
<% attributes.company_browser.each do |k, v| %><%= k %>: <%= h v %>,<% end %>
|
8
|
-
<% attributes.custom.each do |k, v| %><%= k %>: <%= h v %>,<% end %>
|
9
|
-
},
|
10
|
-
<% end %>
|
11
|
-
};
|
12
|
-
</script>
|
13
|
-
|
14
|
-
<script>(function(){var w=window;var ic=w.Intercom;if(typeof ic==="function"){ic('reattach_activator');ic('update',intercomSettings);}else{var d=document;var i=function(){i.c(arguments)};i.q=[];i.c=function(args){i.q.push(args)};w.Intercom=i;function l(){var s=d.createElement('script');s.type='text/javascript';s.async=true;s.src='https://widget.intercom.io/widget/<%= credentials.app_id %>';var x=d.getElementsByTagName('script')[0];x.parentNode.insertBefore(s,x);}if(w.attachEvent){w.attachEvent('onload',l);}else{w.addEventListener('load',l,false);}}})()</script>
|
1
|
+
<script>window.intercomSettings={app_id:'<%= LucidIntercom.app_id %>',<% if attributes %><% user.each do |k, v| %><%= k %>:<%= escape v %>,<% end %>company:{<% company.each do |k, v| %><%= k %>:<%= escape v %>,<% end %><% company_custom.each do |k, v| %><%= k %>:<%= escape v %>,<% end %>},<% end %>};</script>
|
2
|
+
<script>(function(){var w=window;var ic=w.Intercom;if(typeof ic==='function'){ic('reattach_activator');ic('update',intercomSettings);}else{var d=document;var i=function(){i.c(arguments)};i.q=[];i.c=function(args){i.q.push(args)};w.Intercom=i;function l(){var s=d.createElement('script');s.type='text/javascript';s.async=true;s.src='https://widget.intercom.io/widget/<%= LucidIntercom.app_id %>';var x=d.getElementsByTagName('script')[0];x.parentNode.insertBefore(s,x);}if(w.attachEvent){w.attachEvent('onload',l);}else{w.addEventListener('load',l,false);}}})()</script>
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'lucid_intercom/event'
|
4
|
+
|
5
|
+
module LucidIntercom
|
6
|
+
class UninstalledEvent < Event
|
7
|
+
event :uninstalled
|
8
|
+
|
9
|
+
#
|
10
|
+
# @return [Hash]
|
11
|
+
#
|
12
|
+
def event_metadata
|
13
|
+
{
|
14
|
+
company_id: company.to_h[:company_id],
|
15
|
+
}
|
16
|
+
end
|
17
|
+
|
18
|
+
#
|
19
|
+
# @return [Hash]
|
20
|
+
#
|
21
|
+
def app_data
|
22
|
+
{
|
23
|
+
plan: nil,
|
24
|
+
}
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -1,74 +1,44 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require '
|
4
|
-
|
5
|
-
require '
|
6
|
-
require 'lucid_intercom/attributes'
|
7
|
-
require 'lucid_intercom/credentials'
|
8
|
-
require 'lucid_intercom/errors'
|
3
|
+
require 'dry-initializer'
|
4
|
+
|
5
|
+
require 'lucid_intercom/post_request'
|
9
6
|
|
10
7
|
module LucidIntercom
|
11
8
|
class UpdateUser
|
12
|
-
|
13
|
-
# @param shop_attributes [Hash] shop attributes in format returned by the Shopify API
|
14
|
-
# @param app_attributes [Hash] app-specific attributes (unprefixed)
|
15
|
-
# @param credentials [LucidIntercom::Credentials]
|
16
|
-
#
|
17
|
-
def initialize(shop_attributes, app_attributes, credentials = LucidIntercom.credentials)
|
18
|
-
@attributes = Attributes.new(shop_attributes, app_attributes, credentials)
|
19
|
-
@credentials = credentials
|
20
|
-
end
|
9
|
+
extend Dry::Initializer
|
21
10
|
|
22
|
-
# @return [
|
23
|
-
|
24
|
-
# @return [LucidIntercom::Credentials]
|
25
|
-
attr_reader :credentials
|
11
|
+
# @return [PostRequest]
|
12
|
+
option :post_request, default: proc { PostRequest.new }
|
26
13
|
|
27
14
|
#
|
28
|
-
# Create or update user identified by attributes.
|
15
|
+
# Create or update user identified by event attributes. This is only used in
|
16
|
+
# the context of events.
|
29
17
|
#
|
30
|
-
# @
|
18
|
+
# @param event [Events::Event]
|
31
19
|
#
|
32
|
-
|
33
|
-
|
34
|
-
http.request(req, data)
|
35
|
-
end
|
36
|
-
|
37
|
-
status = res.code.to_i
|
38
|
-
|
39
|
-
if status >= 400 # rubocop:disable Style/GuardClause
|
40
|
-
raise LucidIntercom::RequestError.new(status), 'invalid response code %s' % status
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
20
|
+
# @raise [Response::ClientError] for status 4xx
|
21
|
+
# @raise [Response::ServerError] for status 5xx
|
44
22
|
#
|
45
|
-
|
46
|
-
|
47
|
-
private def uri
|
48
|
-
URI('https://api.intercom.io/users')
|
49
|
-
end
|
23
|
+
def call(event)
|
24
|
+
data = data(event)
|
50
25
|
|
51
|
-
|
52
|
-
# @return [Net::HTTP::Post]
|
53
|
-
#
|
54
|
-
private def req
|
55
|
-
req = Net::HTTP::Post.new(uri)
|
56
|
-
req['Authorization'] = "Bearer #{credentials.access_token}"
|
57
|
-
req['Accept'] = 'application/json'
|
58
|
-
req['Content-Type'] = 'application/json'
|
59
|
-
|
60
|
-
req
|
26
|
+
post_request.('users', data).assert!
|
61
27
|
end
|
62
28
|
|
29
|
+
#
|
30
|
+
# @param event [Events::Event]
|
63
31
|
#
|
64
32
|
# @return [Hash]
|
65
33
|
#
|
66
|
-
private def data
|
67
|
-
user
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
34
|
+
private def data(event)
|
35
|
+
event.user.to_h.merge(
|
36
|
+
companies: [
|
37
|
+
event.company.to_h.merge(
|
38
|
+
custom_attributes: event.company_custom.to_h
|
39
|
+
),
|
40
|
+
]
|
41
|
+
)
|
72
42
|
end
|
73
43
|
end
|
74
44
|
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'lucid_intercom/attributes'
|
4
|
+
require 'lucid_intercom/config'
|
5
|
+
|
6
|
+
module LucidIntercom
|
7
|
+
class UserAttributes < Attributes
|
8
|
+
#
|
9
|
+
# @param browser [Boolean] format for browser snippet
|
10
|
+
# @param convert [#call]
|
11
|
+
#
|
12
|
+
# @return [Hash]
|
13
|
+
#
|
14
|
+
def to_h(browser: false, convert: Convert.new)
|
15
|
+
convert.({}.tap do |h|
|
16
|
+
h[:user_hash] = user_hash(id) if browser # or myshopify_domain
|
17
|
+
h[:email] = shopify_data['email']
|
18
|
+
h[:name] = shopify_data['shop_owner']
|
19
|
+
end)
|
20
|
+
end
|
21
|
+
|
22
|
+
#
|
23
|
+
# Either {:user_id} or {:email}. Currently, we are using the email address.
|
24
|
+
#
|
25
|
+
# @return [Symbol]
|
26
|
+
#
|
27
|
+
def id_key
|
28
|
+
:email
|
29
|
+
end
|
30
|
+
|
31
|
+
#
|
32
|
+
# @return [String]
|
33
|
+
#
|
34
|
+
def id
|
35
|
+
shopify_data[id_key.to_s]
|
36
|
+
end
|
37
|
+
|
38
|
+
#
|
39
|
+
# @param email [String]
|
40
|
+
#
|
41
|
+
# @return [String]
|
42
|
+
#
|
43
|
+
private def user_hash(email)
|
44
|
+
OpenSSL::HMAC.hexdigest('sha256', LucidIntercom.secret, email)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
data/lib/lucid_intercom.rb
CHANGED
@@ -2,9 +2,21 @@
|
|
2
2
|
|
3
3
|
# Primarily for Bundler.
|
4
4
|
|
5
|
-
require 'lucid_intercom/attributes'
|
6
|
-
require 'lucid_intercom/
|
7
|
-
require 'lucid_intercom/
|
8
|
-
require 'lucid_intercom/
|
9
|
-
require 'lucid_intercom/
|
10
|
-
require 'lucid_intercom/
|
5
|
+
require 'lucid_intercom/attributes.rb'
|
6
|
+
require 'lucid_intercom/changed_plan_event.rb'
|
7
|
+
require 'lucid_intercom/company_attributes.rb'
|
8
|
+
require 'lucid_intercom/company_custom_attributes.rb'
|
9
|
+
require 'lucid_intercom/config.rb'
|
10
|
+
require 'lucid_intercom/convert.rb'
|
11
|
+
require 'lucid_intercom/error.rb'
|
12
|
+
require 'lucid_intercom/event.rb'
|
13
|
+
require 'lucid_intercom/installed_event.rb'
|
14
|
+
require 'lucid_intercom/post_request.rb'
|
15
|
+
require 'lucid_intercom/render_snippet.rb'
|
16
|
+
require 'lucid_intercom/response.rb'
|
17
|
+
require 'lucid_intercom/send_event.rb'
|
18
|
+
require 'lucid_intercom/snippet.html.erb'
|
19
|
+
require 'lucid_intercom/uninstalled_event.rb'
|
20
|
+
require 'lucid_intercom/update_user.rb'
|
21
|
+
require 'lucid_intercom/user_attributes.rb'
|
22
|
+
require 'lucid_intercom/version.rb'
|
metadata
CHANGED
@@ -1,15 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lucid_intercom
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kelsey Judson
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-03-
|
11
|
+
date: 2018-03-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rake
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '12.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '12.3'
|
13
27
|
- !ruby/object:Gem::Dependency
|
14
28
|
name: rspec
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -38,6 +52,34 @@ dependencies:
|
|
38
52
|
- - '='
|
39
53
|
- !ruby/object:Gem::Version
|
40
54
|
version: 0.52.0
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: dry-initializer
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.3'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.3'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: http
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3.0'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3.0'
|
41
83
|
description:
|
42
84
|
email: kelsey@lucid.nz
|
43
85
|
executables: []
|
@@ -47,24 +89,22 @@ files:
|
|
47
89
|
- README.md
|
48
90
|
- lib/lucid_intercom.rb
|
49
91
|
- lib/lucid_intercom/attributes.rb
|
50
|
-
- lib/lucid_intercom/
|
51
|
-
- lib/lucid_intercom/
|
52
|
-
- lib/lucid_intercom/
|
53
|
-
- lib/lucid_intercom/
|
54
|
-
- lib/lucid_intercom/
|
55
|
-
- lib/lucid_intercom/
|
56
|
-
- lib/lucid_intercom/
|
57
|
-
- lib/lucid_intercom/
|
58
|
-
- lib/lucid_intercom/
|
59
|
-
- lib/lucid_intercom/events.rb
|
60
|
-
- lib/lucid_intercom/events/base.rb
|
61
|
-
- lib/lucid_intercom/events/changed_plan.rb
|
62
|
-
- lib/lucid_intercom/events/installed.rb
|
63
|
-
- lib/lucid_intercom/events/uninstalled.rb
|
92
|
+
- lib/lucid_intercom/changed_plan_event.rb
|
93
|
+
- lib/lucid_intercom/company_attributes.rb
|
94
|
+
- lib/lucid_intercom/company_custom_attributes.rb
|
95
|
+
- lib/lucid_intercom/config.rb
|
96
|
+
- lib/lucid_intercom/convert.rb
|
97
|
+
- lib/lucid_intercom/error.rb
|
98
|
+
- lib/lucid_intercom/event.rb
|
99
|
+
- lib/lucid_intercom/installed_event.rb
|
100
|
+
- lib/lucid_intercom/post_request.rb
|
64
101
|
- lib/lucid_intercom/render_snippet.rb
|
102
|
+
- lib/lucid_intercom/response.rb
|
65
103
|
- lib/lucid_intercom/send_event.rb
|
66
104
|
- lib/lucid_intercom/snippet.html.erb
|
105
|
+
- lib/lucid_intercom/uninstalled_event.rb
|
67
106
|
- lib/lucid_intercom/update_user.rb
|
107
|
+
- lib/lucid_intercom/user_attributes.rb
|
68
108
|
- lib/lucid_intercom/version.rb
|
69
109
|
homepage: https://github.com/lucidnz/gem-lucid_intercom
|
70
110
|
licenses:
|
@@ -1,63 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'lucid_intercom/credentials'
|
4
|
-
|
5
|
-
module LucidIntercom
|
6
|
-
class Attributes
|
7
|
-
class Base
|
8
|
-
#
|
9
|
-
# @param shop_attributes [Hash] shop attributes in format returned by the Shopify API
|
10
|
-
# @param app_attributes [Hash] app-specific attributes (unprefixed)
|
11
|
-
# @param credentials [LucidIntercom::Credentials]
|
12
|
-
#
|
13
|
-
def initialize(shop_attributes = {}, app_attributes = {}, credentials = LucidIntercom.credentials)
|
14
|
-
@credentials = credentials
|
15
|
-
@shop_attributes = shop_attributes
|
16
|
-
@app_attributes = app_attributes
|
17
|
-
end
|
18
|
-
|
19
|
-
# @return [LucidIntercom::Credentials]
|
20
|
-
attr_reader :credentials
|
21
|
-
# @return [Hash]
|
22
|
-
attr_reader :shop_attributes
|
23
|
-
# @return [Hash]
|
24
|
-
attr_reader :app_attributes
|
25
|
-
|
26
|
-
#
|
27
|
-
# @return [Hash]
|
28
|
-
#
|
29
|
-
def call
|
30
|
-
normalize_values(attributes)
|
31
|
-
end
|
32
|
-
|
33
|
-
#
|
34
|
-
# @return [Hash]
|
35
|
-
#
|
36
|
-
private def attributes
|
37
|
-
{}
|
38
|
-
end
|
39
|
-
|
40
|
-
#
|
41
|
-
# Convert attribute values to valid Intercom types.
|
42
|
-
#
|
43
|
-
# @param attributes [Hash]
|
44
|
-
#
|
45
|
-
# @return [Hash]
|
46
|
-
#
|
47
|
-
private def normalize_values(attributes)
|
48
|
-
attributes.each_with_object({}) do |(k, v), a|
|
49
|
-
a[k] = case v
|
50
|
-
when Integer, Float
|
51
|
-
v
|
52
|
-
when Time
|
53
|
-
v.to_i
|
54
|
-
when nil
|
55
|
-
nil # unset attribute
|
56
|
-
else
|
57
|
-
v.to_s
|
58
|
-
end
|
59
|
-
end
|
60
|
-
end
|
61
|
-
end
|
62
|
-
end
|
63
|
-
end
|
@@ -1,22 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'lucid_intercom/attributes/base'
|
4
|
-
|
5
|
-
module LucidIntercom
|
6
|
-
class Attributes
|
7
|
-
class Company < Base
|
8
|
-
#
|
9
|
-
# Standard Intercom company attributes.
|
10
|
-
#
|
11
|
-
# @return [Hash]
|
12
|
-
#
|
13
|
-
private def attributes
|
14
|
-
{
|
15
|
-
company_id: shop_attributes['myshopify_domain'],
|
16
|
-
name: shop_attributes['name'],
|
17
|
-
plan: shop_attributes['plan_name'],
|
18
|
-
}
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
@@ -1,47 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'lucid_intercom/attributes/base'
|
4
|
-
require 'lucid_intercom/credentials'
|
5
|
-
|
6
|
-
module LucidIntercom
|
7
|
-
class Attributes
|
8
|
-
class Custom < Base
|
9
|
-
#
|
10
|
-
# Custom company attributes.
|
11
|
-
#
|
12
|
-
# @return [Hash]
|
13
|
-
#
|
14
|
-
private def attributes
|
15
|
-
super
|
16
|
-
.merge(attributes_shopify)
|
17
|
-
.merge(attributes_app)
|
18
|
-
end
|
19
|
-
|
20
|
-
#
|
21
|
-
# These custom attributes are prefixed with 'merchant_' to distinguish
|
22
|
-
# from the Shopify intergration's 'shopify_' prefix.
|
23
|
-
#
|
24
|
-
# @return [Hash]
|
25
|
-
#
|
26
|
-
private def attributes_shopify
|
27
|
-
{
|
28
|
-
merchant_domain: shop_attributes['domain'],
|
29
|
-
merchant_myshopify_domain: shop_attributes['myshopify_domain'],
|
30
|
-
merchant_shop_owner: shop_attributes['shop_owner'],
|
31
|
-
merchant_timezone: shop_attributes['timezone'],
|
32
|
-
}
|
33
|
-
end
|
34
|
-
|
35
|
-
#
|
36
|
-
# Anything app-specific.
|
37
|
-
#
|
38
|
-
# @return [Hash]
|
39
|
-
#
|
40
|
-
private def attributes_app
|
41
|
-
app_attributes.each_with_object({}) do |(k, v), a|
|
42
|
-
a["#{credentials.app_prefix}_#{k}"] = v
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|
@@ -1,28 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'openssl'
|
4
|
-
require 'lucid_intercom/attributes/base'
|
5
|
-
|
6
|
-
module LucidIntercom
|
7
|
-
class Attributes
|
8
|
-
class User < Base
|
9
|
-
#
|
10
|
-
# Standard Intercom user attributes.
|
11
|
-
#
|
12
|
-
# @return [Hash]
|
13
|
-
#
|
14
|
-
private def attributes
|
15
|
-
{
|
16
|
-
# NOTE: currently unused in favour of email # user_id: shop_attributes['myshopify_domain'],
|
17
|
-
user_hash: user_hash(shop_attributes['email']),
|
18
|
-
email: shop_attributes['email'],
|
19
|
-
name: shop_attributes['shop_owner'],
|
20
|
-
}
|
21
|
-
end
|
22
|
-
|
23
|
-
private def user_hash(email)
|
24
|
-
OpenSSL::HMAC.hexdigest('sha256', credentials.secret, email)
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
@@ -1,35 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module LucidIntercom
|
4
|
-
#
|
5
|
-
# @!attribute [rw] access_token
|
6
|
-
# @return [String]
|
7
|
-
# @!attribute [rw] secret
|
8
|
-
# @return [String]
|
9
|
-
# @!attribute [rw] app_id
|
10
|
-
# @return [String]
|
11
|
-
# @!attribute [rw] app_prefix
|
12
|
-
# @return [String] the snakecased app name, e.g. 'smart_order_tags'
|
13
|
-
#
|
14
|
-
Credentials = Struct.new(:access_token, :secret, :app_id, :app_prefix)
|
15
|
-
end
|
16
|
-
|
17
|
-
class << LucidIntercom
|
18
|
-
#
|
19
|
-
# Assign default API credentials.
|
20
|
-
#
|
21
|
-
# @param credentials [LucidIntercom::Credentials]
|
22
|
-
#
|
23
|
-
attr_writer :credentials
|
24
|
-
|
25
|
-
#
|
26
|
-
# @return [LucidIntercom::Credentials]
|
27
|
-
#
|
28
|
-
# @raise [LucidIntercom::MissingCredentialsError] if credentials are unset
|
29
|
-
#
|
30
|
-
def credentials
|
31
|
-
raise MissingCredentialsError unless @credentials
|
32
|
-
|
33
|
-
@credentials
|
34
|
-
end
|
35
|
-
end
|
@@ -1,17 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'lucid_intercom/errors/error'
|
4
|
-
|
5
|
-
module LucidIntercom
|
6
|
-
class RequestError < Error
|
7
|
-
#
|
8
|
-
# @param status [Integer] the HTTP response status
|
9
|
-
#
|
10
|
-
def initialize(status)
|
11
|
-
@status = status
|
12
|
-
end
|
13
|
-
|
14
|
-
# @return [Integer]
|
15
|
-
attr_reader :status
|
16
|
-
end
|
17
|
-
end
|
@@ -1,23 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'lucid_intercom/credentials'
|
4
|
-
|
5
|
-
module LucidIntercom
|
6
|
-
module Events
|
7
|
-
class Base
|
8
|
-
#
|
9
|
-
# @param shop_attributes [Hash] shop attributes in format returned by the Shopify API
|
10
|
-
# @param credentials [LucidIntercom::Credentials]
|
11
|
-
#
|
12
|
-
def initialize(shop_attributes, credentials = LucidIntercom.credentials)
|
13
|
-
@credentials = credentials
|
14
|
-
@shop_attributes = shop_attributes
|
15
|
-
end
|
16
|
-
|
17
|
-
# @return [LucidIntercom::Credentials]
|
18
|
-
attr_reader :credentials
|
19
|
-
# @return [Hash]
|
20
|
-
attr_reader :shop_attributes
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
@@ -1,37 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'lucid_intercom/credentials'
|
4
|
-
require 'lucid_intercom/send_event'
|
5
|
-
require 'lucid_intercom/update_user'
|
6
|
-
require 'lucid_intercom/events/base'
|
7
|
-
|
8
|
-
module LucidIntercom
|
9
|
-
module Events
|
10
|
-
class ChangedPlan < Base
|
11
|
-
#
|
12
|
-
# @param plan_name [String, nil] e.g. 'free', or nil to unset
|
13
|
-
#
|
14
|
-
def call(plan_name)
|
15
|
-
LucidIntercom::UpdateUser.new(shop_attributes, {plan: plan_name}, credentials).()
|
16
|
-
LucidIntercom::SendEvent.new(shop_attributes, credentials).(event_name, event_metadata(plan_name))
|
17
|
-
end
|
18
|
-
|
19
|
-
#
|
20
|
-
# @return [String]
|
21
|
-
#
|
22
|
-
private def event_name
|
23
|
-
"#{credentials.app_prefix}_changed_plan"
|
24
|
-
end
|
25
|
-
|
26
|
-
#
|
27
|
-
# @return [Hash]
|
28
|
-
#
|
29
|
-
private def event_metadata(plan_name)
|
30
|
-
{
|
31
|
-
company_id: shop_attributes['myshopify_domain'],
|
32
|
-
new_plan: plan_name,
|
33
|
-
}
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
@@ -1,37 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'lucid_intercom/credentials'
|
4
|
-
require 'lucid_intercom/send_event'
|
5
|
-
require 'lucid_intercom/update_user'
|
6
|
-
require 'lucid_intercom/events/base'
|
7
|
-
|
8
|
-
module LucidIntercom
|
9
|
-
module Events
|
10
|
-
class Installed < Base
|
11
|
-
#
|
12
|
-
# @param plan_name [String] e.g. 'free'
|
13
|
-
#
|
14
|
-
def call(plan_name)
|
15
|
-
LucidIntercom::UpdateUser.new(shop_attributes, {plan: plan_name}, credentials).()
|
16
|
-
LucidIntercom::SendEvent.new(shop_attributes, credentials).(event_name, event_metadata(plan_name))
|
17
|
-
end
|
18
|
-
|
19
|
-
#
|
20
|
-
# @return [String]
|
21
|
-
#
|
22
|
-
private def event_name
|
23
|
-
"#{credentials.app_prefix}_installed"
|
24
|
-
end
|
25
|
-
|
26
|
-
#
|
27
|
-
# @return [Hash]
|
28
|
-
#
|
29
|
-
private def event_metadata(plan_name)
|
30
|
-
{
|
31
|
-
company_id: shop_attributes['myshopify_domain'],
|
32
|
-
new_plan: plan_name,
|
33
|
-
}
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|