intercom-rails 0.2.35 → 0.2.36
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/lib/intercom-rails.rb +0 -1
- data/lib/intercom-rails/config.rb +0 -1
- data/lib/intercom-rails/exceptions.rb +0 -2
- data/lib/intercom-rails/version.rb +1 -1
- data/lib/rails/generators/intercom/config/config_generator.rb +0 -2
- data/lib/rails/generators/intercom/config/intercom.rb.erb +0 -10
- metadata +4 -5
- data/lib/intercom-rails/import.rb +0 -180
- data/lib/intercom-rails/intercom.rake +0 -12
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 442ecf7487fbe99a3fb44e6f14b33ddb002bd70f
|
4
|
+
data.tar.gz: cb7e27a634b175c80e62d07f6689da1c02029400
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cd671fd16f7457d5ae016def0745c4658457fd181527249214a58925e42240bf665a1cbca7e728620c2cd074200e8c2758b289e475e4b278c6761cd84768670b
|
7
|
+
data.tar.gz: b452e1c389e83593c6f76b9b9d5ec2960dcb90e0f45352ca0ba2d66d695cfb285fdc2abf00e0a13f894e04796d0888635d955339e0166d6597b8cbce6cea7c89
|
data/lib/intercom-rails.rb
CHANGED
@@ -9,7 +9,6 @@ require 'intercom-rails/custom_data_helper'
|
|
9
9
|
require 'intercom-rails/shutdown_helper'
|
10
10
|
require 'intercom-rails/auto_include_filter'
|
11
11
|
require 'intercom-rails/config'
|
12
|
-
require 'intercom-rails/import'
|
13
12
|
require 'intercom-rails/railtie' if defined? Rails::Railtie
|
14
13
|
|
15
14
|
module IntercomRails
|
@@ -94,7 +94,6 @@ module IntercomRails
|
|
94
94
|
config_accessor :app_id
|
95
95
|
config_accessor :session_duration
|
96
96
|
config_accessor :api_secret
|
97
|
-
config_accessor :api_key
|
98
97
|
config_accessor :library_url
|
99
98
|
config_accessor :enabled_environments, &ARRAY_VALIDATOR
|
100
99
|
config_accessor :include_for_logged_out_users
|
@@ -8,14 +8,12 @@ module Intercom
|
|
8
8
|
|
9
9
|
argument :app_id, :desc => "Your Intercom app-id, which can be found here: https://app.intercom.io/apps/api_keys"
|
10
10
|
argument :api_secret, :desc => "Your Intercom api-secret, used for secure mode", :optional => true
|
11
|
-
argument :api_key, :desc => "An Intercom API key, for various rake tasks", :optional => true
|
12
11
|
argument :session_duration, :desc => "user session duration, this should match your app", :optional => true
|
13
12
|
|
14
13
|
FALSEY_RESPONSES = ['n', 'no']
|
15
14
|
def create_config_file
|
16
15
|
@app_id = app_id
|
17
16
|
@api_secret = api_secret
|
18
|
-
@api_key = api_key
|
19
17
|
@session_duration = session_duration
|
20
18
|
@include_for_logged_out_users = false
|
21
19
|
|
@@ -20,16 +20,6 @@ IntercomRails.config do |config|
|
|
20
20
|
# config.api_secret = "..."
|
21
21
|
<%- end -%>
|
22
22
|
|
23
|
-
# == Intercom API Key
|
24
|
-
# This is required for some Intercom rake tasks like importing your users;
|
25
|
-
# you can generate one at https://app.intercom.io/apps/api_keys.
|
26
|
-
#
|
27
|
-
<%- if @api_key -%>
|
28
|
-
config.api_key = "<%= @api_key %>"
|
29
|
-
<%- else -%>
|
30
|
-
# config.api_key = "..."
|
31
|
-
<%- end -%>
|
32
|
-
|
33
23
|
# == Enabled Environments
|
34
24
|
# Which environments is auto inclusion of the Javascript enabled for
|
35
25
|
#
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: intercom-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.36
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ben McRedmond
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2016-06-
|
13
|
+
date: 2016-06-23 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: activesupport
|
@@ -172,8 +172,6 @@ files:
|
|
172
172
|
- lib/intercom-rails/custom_data_helper.rb
|
173
173
|
- lib/intercom-rails/date_helper.rb
|
174
174
|
- lib/intercom-rails/exceptions.rb
|
175
|
-
- lib/intercom-rails/import.rb
|
176
|
-
- lib/intercom-rails/intercom.rake
|
177
175
|
- lib/intercom-rails/proxy.rb
|
178
176
|
- lib/intercom-rails/proxy/company.rb
|
179
177
|
- lib/intercom-rails/proxy/user.rb
|
@@ -204,8 +202,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
204
202
|
version: '0'
|
205
203
|
requirements: []
|
206
204
|
rubyforge_project: intercom-rails
|
207
|
-
rubygems_version: 2.
|
205
|
+
rubygems_version: 2.5.1
|
208
206
|
signing_key:
|
209
207
|
specification_version: 4
|
210
208
|
summary: Rails helper for emitting javascript script tags for Intercom
|
211
209
|
test_files: []
|
210
|
+
has_rdoc:
|
@@ -1,180 +0,0 @@
|
|
1
|
-
require 'net/http'
|
2
|
-
require 'json'
|
3
|
-
require 'uri'
|
4
|
-
|
5
|
-
module IntercomRails
|
6
|
-
|
7
|
-
class Import
|
8
|
-
|
9
|
-
def self.bulk_create_api_endpoint
|
10
|
-
host = (ENV['INTERCOM_RAILS_DEV'] ? "http://api.intercom.dev" : "https://api.intercom.io")
|
11
|
-
URI.parse(host + "/v1/users/bulk_create")
|
12
|
-
end
|
13
|
-
|
14
|
-
def self.run(*args)
|
15
|
-
new(*args).run
|
16
|
-
end
|
17
|
-
|
18
|
-
attr_reader :uri, :http, :max_batch_size
|
19
|
-
attr_accessor :failed, :total_sent
|
20
|
-
|
21
|
-
def initialize(options = {})
|
22
|
-
@uri = Import.bulk_create_api_endpoint
|
23
|
-
@http = Net::HTTP.new(@uri.host, @uri.port)
|
24
|
-
@failed = []
|
25
|
-
@total_sent = 0
|
26
|
-
@max_batch_size = [(options[:max_batch_size] || 100), 100].min
|
27
|
-
|
28
|
-
@status_enabled = !!options[:status_enabled]
|
29
|
-
|
30
|
-
if uri.scheme == 'https'
|
31
|
-
http.use_ssl = true
|
32
|
-
http.ca_file = File.join(File.dirname(__FILE__), '../data/cacert.pem')
|
33
|
-
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
# Check for ActiveRecord OR Mongoid
|
38
|
-
def active_record?(user_klass)
|
39
|
-
(defined?(ActiveRecord::Base) && (user_klass < ActiveRecord::Base))
|
40
|
-
end
|
41
|
-
|
42
|
-
def mongoid?(user_klass)
|
43
|
-
(defined?(Mongoid::Document) && (user_klass < Mongoid::Document))
|
44
|
-
end
|
45
|
-
|
46
|
-
def supported_orm?(user_klass)
|
47
|
-
active_record?(user_klass) || mongoid?(user_klass)
|
48
|
-
end
|
49
|
-
|
50
|
-
def assert_runnable
|
51
|
-
raise ImportError, "You can only import your users from your production environment" unless (Rails.env.production? || Rails.env.staging?)
|
52
|
-
raise ImportError, "We couldn't find your user class, please set one in config/initializers/intercom_rails.rb" unless user_klass.present?
|
53
|
-
info "Found user class: #{user_klass}"
|
54
|
-
raise ImportError, "Only ActiveRecord and Mongoid models are supported" unless supported_orm?(user_klass)
|
55
|
-
raise ImportError, "Please add an Intercom API Key to config/initializers/intercom.rb" unless IntercomRails.config.api_key.present?
|
56
|
-
info "Intercom API key found"
|
57
|
-
end
|
58
|
-
|
59
|
-
def run
|
60
|
-
assert_runnable
|
61
|
-
|
62
|
-
info "Sending users in batches of #{max_batch_size}:"
|
63
|
-
batches do |batch, number_in_batch|
|
64
|
-
failures = send_users(batch)['failed']
|
65
|
-
self.failed += failures
|
66
|
-
self.total_sent += number_in_batch
|
67
|
-
|
68
|
-
progress '.' * (number_in_batch - failures.count)
|
69
|
-
progress 'F' * failures.count
|
70
|
-
end
|
71
|
-
info "Successfully created #{self.total_sent - self.failed.count} users", :new_line => true
|
72
|
-
info "Failed to create #{self.failed.count} #{(self.failed.count == 1) ? 'user' : 'users'}, this is likely due to bad data" unless failed.count.zero?
|
73
|
-
|
74
|
-
self
|
75
|
-
end
|
76
|
-
|
77
|
-
def total_failed
|
78
|
-
self.failed.count
|
79
|
-
end
|
80
|
-
|
81
|
-
private
|
82
|
-
|
83
|
-
def batches
|
84
|
-
if active_record?(user_klass)
|
85
|
-
user_klass.find_in_batches(:batch_size => max_batch_size) do |users|
|
86
|
-
users_for_wire = map_to_users_for_wire(users)
|
87
|
-
|
88
|
-
yield(prepare_batch(users_for_wire), users_for_wire.count) unless users_for_wire.count.zero?
|
89
|
-
end
|
90
|
-
elsif mongoid?(user_klass)
|
91
|
-
0.step(user_klass.all.count, max_batch_size) do |offset|
|
92
|
-
users_for_wire = map_to_users_for_wire(user_klass.limit(max_batch_size).skip(offset))
|
93
|
-
yield(prepare_batch(users_for_wire), users_for_wire.count) unless users_for_wire.count.zero?
|
94
|
-
end
|
95
|
-
end
|
96
|
-
end
|
97
|
-
|
98
|
-
def map_to_users_for_wire(users)
|
99
|
-
users.map do |u|
|
100
|
-
user_proxy = Proxy::User.new(u)
|
101
|
-
next unless user_proxy.valid?
|
102
|
-
|
103
|
-
for_wire = user_proxy.to_hash
|
104
|
-
companies = Proxy::Company.companies_for_user(user_proxy)
|
105
|
-
for_wire.merge!(:companies => companies.map(&:to_hash)) unless companies.nil?
|
106
|
-
|
107
|
-
for_wire
|
108
|
-
end.compact
|
109
|
-
end
|
110
|
-
|
111
|
-
def prepare_batch(batch)
|
112
|
-
{:users => batch}.to_json
|
113
|
-
end
|
114
|
-
|
115
|
-
def user_klass
|
116
|
-
if IntercomRails.config.user.model.present?
|
117
|
-
IntercomRails.config.user.model.call
|
118
|
-
else
|
119
|
-
user_klass_name if user_klass_name.class != Module
|
120
|
-
end
|
121
|
-
rescue NameError
|
122
|
-
# Rails lazy loads constants, so this is how we check
|
123
|
-
nil
|
124
|
-
end
|
125
|
-
|
126
|
-
def user_klass_name
|
127
|
-
User
|
128
|
-
end
|
129
|
-
|
130
|
-
def send_users(users)
|
131
|
-
request = Net::HTTP::Post.new(uri.request_uri)
|
132
|
-
request.basic_auth(IntercomRails.config.app_id, IntercomRails.config.api_key)
|
133
|
-
request["Content-Type"] = "application/json"
|
134
|
-
request.body = users
|
135
|
-
|
136
|
-
response = perform_request(request)
|
137
|
-
JSON.parse(response.body)
|
138
|
-
end
|
139
|
-
|
140
|
-
MAX_REQUEST_ATTEMPTS = 3
|
141
|
-
def perform_request(request, attempts = 0, error = {})
|
142
|
-
if (attempts > 0) && (attempts < MAX_REQUEST_ATTEMPTS)
|
143
|
-
sleep(0.5)
|
144
|
-
elsif error.present?
|
145
|
-
raise error[:exception] if error[:exception]
|
146
|
-
raise exception_for_failed_response(error[:failed_response])
|
147
|
-
end
|
148
|
-
|
149
|
-
response = http.request(request)
|
150
|
-
|
151
|
-
return response if successful_response?(response)
|
152
|
-
perform_request(request, attempts + 1, :failed_response => response)
|
153
|
-
rescue Timeout::Error, Errno::ECONNREFUSED, EOFError => e
|
154
|
-
perform_request(request, attempts + 1, :exception => e)
|
155
|
-
end
|
156
|
-
|
157
|
-
def successful_response?(response)
|
158
|
-
raise ImportError, "App ID or API Key are incorrect, please check them in config/initializers/intercom.rb" if response.code == '403'
|
159
|
-
['200', '201'].include?(response.code)
|
160
|
-
end
|
161
|
-
|
162
|
-
def exception_for_failed_response(response)
|
163
|
-
code = response.code
|
164
|
-
IntercomAPIError.new("The Intercom API request failed with the code: #{code}, after #{MAX_REQUEST_ATTEMPTS} attempts.")
|
165
|
-
end
|
166
|
-
|
167
|
-
def status_enabled?
|
168
|
-
@status_enabled
|
169
|
-
end
|
170
|
-
|
171
|
-
def progress(str)
|
172
|
-
print(str) if status_enabled?
|
173
|
-
end
|
174
|
-
|
175
|
-
def info(str, options = {})
|
176
|
-
puts "#{"\n" if options[:new_line]}* #{str}" if status_enabled?
|
177
|
-
end
|
178
|
-
|
179
|
-
end
|
180
|
-
end
|