lucid_intercom 0.4.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +76 -0
- data/lib/lucid_intercom.rb +10 -0
- data/lib/lucid_intercom/attributes.rb +57 -0
- data/lib/lucid_intercom/attributes/base.rb +63 -0
- data/lib/lucid_intercom/attributes/company.rb +22 -0
- data/lib/lucid_intercom/attributes/custom.rb +47 -0
- data/lib/lucid_intercom/attributes/user.rb +28 -0
- data/lib/lucid_intercom/credentials.rb +35 -0
- data/lib/lucid_intercom/errors.rb +3 -0
- data/lib/lucid_intercom/errors/error.rb +11 -0
- data/lib/lucid_intercom/errors/missing_credentials_error.rb +7 -0
- data/lib/lucid_intercom/errors/request_error.rb +17 -0
- data/lib/lucid_intercom/events.rb +5 -0
- data/lib/lucid_intercom/events/base.rb +23 -0
- data/lib/lucid_intercom/events/changed_plan.rb +37 -0
- data/lib/lucid_intercom/events/installed.rb +37 -0
- data/lib/lucid_intercom/events/uninstalled.rb +33 -0
- data/lib/lucid_intercom/render_snippet.rb +46 -0
- data/lib/lucid_intercom/send_event.rb +80 -0
- data/lib/lucid_intercom/snippet.html.erb +14 -0
- data/lib/lucid_intercom/update_user.rb +74 -0
- data/lib/lucid_intercom/version.rb +5 -0
- metadata +93 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: e093b7cbc34689c682b5f4f1d3fd4fdef7ac033c9cf0fba38b0404e54f50c9a8
|
4
|
+
data.tar.gz: e08a13e91779a38d92d076bcfad7a7e6003ad5b3ea4de3086cfc0ca9432d162e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b02dbad3e5c7284f146b53f02e2bac8ed500edb95662ed5cfc1a709330b80db817c035973a451f2d9af7a58f3d62d2dcb33df4f1b1b04a188d71fb186ce90703
|
7
|
+
data.tar.gz: e2de118f2b5767081d4f8bea3d75ec8e359644799f94e1542c69917d248d12635a3af5d0348ac1f98674242c90f992d33437ee7316ac07d3657e80d83d2e0c33
|
data/README.md
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
lucid_intercom
|
2
|
+
==============
|
3
|
+
|
4
|
+
Installation
|
5
|
+
------------
|
6
|
+
|
7
|
+
Add the gem to your ‘Gemfile’:
|
8
|
+
|
9
|
+
gem 'lucid_intercom'
|
10
|
+
|
11
|
+
|
12
|
+
Usage
|
13
|
+
-----
|
14
|
+
|
15
|
+
### Configure the default API credentials
|
16
|
+
|
17
|
+
LucidIntercom.credentials = LucidIntercom::Credentials.new(
|
18
|
+
'...', # access_token
|
19
|
+
'...', # secret
|
20
|
+
'...', # app_id
|
21
|
+
'...' # app_prefix
|
22
|
+
)
|
23
|
+
|
24
|
+
Here, ‘app_prefix’ is the snakecased app name, e.g. ‘smart_order_tags’
|
25
|
+
|
26
|
+
Alternatively, a credentials object may optionally be passed as a
|
27
|
+
keyword argument to any of the classes listed below.
|
28
|
+
|
29
|
+
|
30
|
+
### Render the browser snippet
|
31
|
+
|
32
|
+
LucidIntercom::RenderSnippet.new(shop_attributes, app_attributes).()
|
33
|
+
|
34
|
+
This returns an HTML string which you can use in your view layout.
|
35
|
+
|
36
|
+
See the source code for documentation of arguments.
|
37
|
+
|
38
|
+
|
39
|
+
### Send an event
|
40
|
+
|
41
|
+
When a user installs/uninstalls the app, or changes their plan:
|
42
|
+
|
43
|
+
LucidIntercom::Events::Installed.new(shop_attributes).(plan_name)
|
44
|
+
LucidIntercom::Events::Uninstalled.new(shop_attributes).()
|
45
|
+
LucidIntercom::Events::ChangedPlan.new(shop_attributes).(plan_name)
|
46
|
+
|
47
|
+
Note that the `shop_attributes` hash for uninstalled events cannot
|
48
|
+
be read from the API (as the access token is invalid at this stage).
|
49
|
+
You should use the data hash provided with Shopify’s ‘app/uninstalled’
|
50
|
+
webhook instead.
|
51
|
+
|
52
|
+
|
53
|
+
### Plan names
|
54
|
+
|
55
|
+
For installed apps, yet to subscribe to a plan, use ‘pending’.
|
56
|
+
|
57
|
+
For apps with a single paid plan, use ‘subscribed’.
|
58
|
+
|
59
|
+
For free apps, use ‘free’.
|
60
|
+
|
61
|
+
For partner-friendly app installs, use ‘partner’.
|
62
|
+
|
63
|
+
|
64
|
+
### Send a custom event
|
65
|
+
|
66
|
+
LucidIntercom::SendEvent.new(shop_attributes).(event_name, event_metadata)
|
67
|
+
|
68
|
+
See the source code for documentation of arguments.
|
69
|
+
|
70
|
+
|
71
|
+
### Update a user
|
72
|
+
|
73
|
+
LucidIntercom::UpdateUser.new(shop_attributes, app_attributes).()
|
74
|
+
|
75
|
+
When this is called, and the user did not previously exist, the
|
76
|
+
user will be created.
|
@@ -0,0 +1,10 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Primarily for Bundler.
|
4
|
+
|
5
|
+
require 'lucid_intercom/attributes'
|
6
|
+
require 'lucid_intercom/credentials'
|
7
|
+
require 'lucid_intercom/events'
|
8
|
+
require 'lucid_intercom/render_snippet'
|
9
|
+
require 'lucid_intercom/send_event'
|
10
|
+
require 'lucid_intercom/update_user'
|
@@ -0,0 +1,57 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'lucid_intercom/attributes/user'
|
4
|
+
require 'lucid_intercom/attributes/company'
|
5
|
+
require 'lucid_intercom/attributes/custom'
|
6
|
+
|
7
|
+
module LucidIntercom
|
8
|
+
class Attributes
|
9
|
+
#
|
10
|
+
# @see LucidIntercom::Attributes::Base#initialize
|
11
|
+
#
|
12
|
+
def initialize(*args)
|
13
|
+
@args = args
|
14
|
+
end
|
15
|
+
|
16
|
+
#
|
17
|
+
# @return [Hash]
|
18
|
+
#
|
19
|
+
def user
|
20
|
+
user_browser.reject { |k, _| k == :user_hash }
|
21
|
+
end
|
22
|
+
|
23
|
+
#
|
24
|
+
# User attributes for browser (with 'user_hash').
|
25
|
+
#
|
26
|
+
# @return [Hash]
|
27
|
+
#
|
28
|
+
def user_browser
|
29
|
+
User.new(*@args).()
|
30
|
+
end
|
31
|
+
|
32
|
+
#
|
33
|
+
# @return [Hash]
|
34
|
+
#
|
35
|
+
def company
|
36
|
+
Company.new(*@args).()
|
37
|
+
end
|
38
|
+
|
39
|
+
#
|
40
|
+
# Company attributes for browser (expects 'id', not 'company_id').
|
41
|
+
#
|
42
|
+
# @return [Hash]
|
43
|
+
#
|
44
|
+
def company_browser
|
45
|
+
company2 = company
|
46
|
+
company2[:id] = company2.delete(:company_id)
|
47
|
+
company2
|
48
|
+
end
|
49
|
+
|
50
|
+
#
|
51
|
+
# @return [Hash]
|
52
|
+
#
|
53
|
+
def custom
|
54
|
+
Custom.new(*@args).()
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,63 @@
|
|
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
|
@@ -0,0 +1,22 @@
|
|
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
|
@@ -0,0 +1,47 @@
|
|
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
|
@@ -0,0 +1,28 @@
|
|
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
|
@@ -0,0 +1,35 @@
|
|
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
|
@@ -0,0 +1,17 @@
|
|
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
|
@@ -0,0 +1,23 @@
|
|
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
|
@@ -0,0 +1,37 @@
|
|
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
|
@@ -0,0 +1,37 @@
|
|
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
|
@@ -0,0 +1,33 @@
|
|
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 Uninstalled < Base
|
11
|
+
def call
|
12
|
+
LucidIntercom::UpdateUser.new(shop_attributes, {plan: nil}, credentials).()
|
13
|
+
LucidIntercom::SendEvent.new(shop_attributes, credentials).(event_name, event_metadata)
|
14
|
+
end
|
15
|
+
|
16
|
+
#
|
17
|
+
# @return [String]
|
18
|
+
#
|
19
|
+
private def event_name
|
20
|
+
"#{credentials.app_prefix}_uninstalled"
|
21
|
+
end
|
22
|
+
|
23
|
+
#
|
24
|
+
# @return [Hash]
|
25
|
+
#
|
26
|
+
private def event_metadata
|
27
|
+
{
|
28
|
+
company_id: shop_attributes['myshopify_domain'],
|
29
|
+
}
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'erb'
|
4
|
+
require 'lucid_intercom/attributes'
|
5
|
+
require 'lucid_intercom/credentials'
|
6
|
+
|
7
|
+
module LucidIntercom
|
8
|
+
class RenderSnippet
|
9
|
+
TEMPLATE = ERB.new(File.read("#{__dir__}/snippet.html.erb")).freeze
|
10
|
+
|
11
|
+
#
|
12
|
+
# Leave arguments unset for unauthenticated visitors.
|
13
|
+
#
|
14
|
+
# @param credentials [LucidIntercom::Credentials]
|
15
|
+
#
|
16
|
+
# @see LucidIntercom::Attributes#initialize
|
17
|
+
#
|
18
|
+
def initialize(shop_attributes = {}, app_attributes = {}, credentials = LucidIntercom.credentials)
|
19
|
+
@credentials = credentials
|
20
|
+
@attributes = Attributes.new(shop_attributes, app_attributes, credentials) if shop_attributes.empty?
|
21
|
+
end
|
22
|
+
|
23
|
+
# @return [LucidIntercom::Attributes]
|
24
|
+
attr_reader :attributes
|
25
|
+
# @return [LucidIntercom::Credentials]
|
26
|
+
attr_reader :credentials
|
27
|
+
|
28
|
+
#
|
29
|
+
# @return [String] the rendered HTML
|
30
|
+
#
|
31
|
+
def call
|
32
|
+
TEMPLATE.result(binding)
|
33
|
+
end
|
34
|
+
|
35
|
+
#
|
36
|
+
# Quote and escape a value for the window.intercomSettings object.
|
37
|
+
#
|
38
|
+
private def h(v)
|
39
|
+
v.is_a?(String) ? ?" + v.gsub(/./) { |c| escape_char(c) } + ?" : v
|
40
|
+
end
|
41
|
+
|
42
|
+
private def escape_char(c)
|
43
|
+
%w(" ' / < > \\).include?(c) ? '\%s' % c : c
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'json'
|
4
|
+
require 'net/http'
|
5
|
+
require 'uri'
|
6
|
+
require 'lucid_intercom/attributes'
|
7
|
+
require 'lucid_intercom/credentials'
|
8
|
+
require 'lucid_intercom/errors'
|
9
|
+
|
10
|
+
module LucidIntercom
|
11
|
+
class SendEvent
|
12
|
+
#
|
13
|
+
# @param shop_attributes [Hash] shop attributes in format returned by the Shopify API
|
14
|
+
# @param credentials [LucidIntercom::Credentials]
|
15
|
+
#
|
16
|
+
def initialize(shop_attributes, credentials = LucidIntercom.credentials)
|
17
|
+
@attributes = Attributes.new(shop_attributes, {}, credentials)
|
18
|
+
@credentials = credentials
|
19
|
+
end
|
20
|
+
|
21
|
+
# @return [LucidIntercom::Attributes]
|
22
|
+
attr_reader :attributes
|
23
|
+
# @return [LucidIntercom::Credentials]
|
24
|
+
attr_reader :credentials
|
25
|
+
|
26
|
+
#
|
27
|
+
# Send event for user identified by attributes.
|
28
|
+
#
|
29
|
+
# @param event_name [String]
|
30
|
+
# @param event_metadata [Hash]
|
31
|
+
#
|
32
|
+
# @raise [LucidIntercom::RequestError] if the response status >= 400
|
33
|
+
#
|
34
|
+
def call(event_name, event_metadata)
|
35
|
+
res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
|
36
|
+
http.request(req, data(event_name, event_metadata))
|
37
|
+
end
|
38
|
+
|
39
|
+
status = res.code.to_i
|
40
|
+
|
41
|
+
if status >= 400 # rubocop:disable Style/GuardClause
|
42
|
+
raise LucidIntercom::RequestError.new(status), 'invalid response code %s' % status
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
#
|
47
|
+
# @return [URI::HTTPS]
|
48
|
+
#
|
49
|
+
private def uri
|
50
|
+
URI('https://api.intercom.io/events')
|
51
|
+
end
|
52
|
+
|
53
|
+
#
|
54
|
+
# @return [Net::HTTP::Post]
|
55
|
+
#
|
56
|
+
private def req
|
57
|
+
req = Net::HTTP::Post.new(uri)
|
58
|
+
req['Authorization'] = "Bearer #{credentials.access_token}"
|
59
|
+
req['Content-Type'] = 'application/json'
|
60
|
+
|
61
|
+
req
|
62
|
+
end
|
63
|
+
|
64
|
+
#
|
65
|
+
# @param event_name [String]
|
66
|
+
# @param event_metadata [Hash]
|
67
|
+
#
|
68
|
+
# @return [Hash]
|
69
|
+
#
|
70
|
+
private def data(event_name, event_metadata)
|
71
|
+
event = {}
|
72
|
+
event[:email] = attributes.user[:email]
|
73
|
+
event[:event_name] = event_name
|
74
|
+
event[:created_at] = Time.now.utc.to_i
|
75
|
+
event[:metadata] = event_metadata
|
76
|
+
|
77
|
+
event.to_json
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
<script>
|
2
|
+
window.intercomSettings = {
|
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>
|
@@ -0,0 +1,74 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'json'
|
4
|
+
require 'net/http'
|
5
|
+
require 'uri'
|
6
|
+
require 'lucid_intercom/attributes'
|
7
|
+
require 'lucid_intercom/credentials'
|
8
|
+
require 'lucid_intercom/errors'
|
9
|
+
|
10
|
+
module LucidIntercom
|
11
|
+
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
|
21
|
+
|
22
|
+
# @return [LucidIntercom::Attributes]
|
23
|
+
attr_reader :attributes
|
24
|
+
# @return [LucidIntercom::Credentials]
|
25
|
+
attr_reader :credentials
|
26
|
+
|
27
|
+
#
|
28
|
+
# Create or update user identified by attributes.
|
29
|
+
#
|
30
|
+
# @raise [LucidIntercom::RequestError] if the response status >= 400
|
31
|
+
#
|
32
|
+
def call
|
33
|
+
res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
|
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
|
+
|
44
|
+
#
|
45
|
+
# @return [URI::HTTPS]
|
46
|
+
#
|
47
|
+
private def uri
|
48
|
+
URI('https://api.intercom.io/users')
|
49
|
+
end
|
50
|
+
|
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
|
61
|
+
end
|
62
|
+
|
63
|
+
#
|
64
|
+
# @return [Hash]
|
65
|
+
#
|
66
|
+
private def data
|
67
|
+
user = attributes.user
|
68
|
+
user[:companies] = [attributes.company]
|
69
|
+
user[:companies][0]['custom_attributes'] = attributes.custom
|
70
|
+
|
71
|
+
user.to_json
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
metadata
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: lucid_intercom
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.4.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Kelsey Judson
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-03-02 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.6'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rubocop
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 0.52.0
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 0.52.0
|
41
|
+
description:
|
42
|
+
email: kelsey@lucid.nz
|
43
|
+
executables: []
|
44
|
+
extensions: []
|
45
|
+
extra_rdoc_files: []
|
46
|
+
files:
|
47
|
+
- README.md
|
48
|
+
- lib/lucid_intercom.rb
|
49
|
+
- lib/lucid_intercom/attributes.rb
|
50
|
+
- lib/lucid_intercom/attributes/base.rb
|
51
|
+
- lib/lucid_intercom/attributes/company.rb
|
52
|
+
- lib/lucid_intercom/attributes/custom.rb
|
53
|
+
- lib/lucid_intercom/attributes/user.rb
|
54
|
+
- lib/lucid_intercom/credentials.rb
|
55
|
+
- lib/lucid_intercom/errors.rb
|
56
|
+
- lib/lucid_intercom/errors/error.rb
|
57
|
+
- lib/lucid_intercom/errors/missing_credentials_error.rb
|
58
|
+
- lib/lucid_intercom/errors/request_error.rb
|
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
|
64
|
+
- lib/lucid_intercom/render_snippet.rb
|
65
|
+
- lib/lucid_intercom/send_event.rb
|
66
|
+
- lib/lucid_intercom/snippet.html.erb
|
67
|
+
- lib/lucid_intercom/update_user.rb
|
68
|
+
- lib/lucid_intercom/version.rb
|
69
|
+
homepage: https://github.com/lucidnz/gem-lucid_intercom
|
70
|
+
licenses:
|
71
|
+
- ISC
|
72
|
+
metadata: {}
|
73
|
+
post_install_message:
|
74
|
+
rdoc_options: []
|
75
|
+
require_paths:
|
76
|
+
- lib
|
77
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - ">="
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0'
|
82
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
requirements: []
|
88
|
+
rubyforge_project:
|
89
|
+
rubygems_version: 2.7.3
|
90
|
+
signing_key:
|
91
|
+
specification_version: 4
|
92
|
+
summary: Lucid integration for Intercom
|
93
|
+
test_files: []
|