Class: IntercomRails::Import

Inherits:
Object
  • Object
show all
Defined in:
lib/intercom-rails/import.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Import

Returns a new instance of Import.


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/intercom-rails/import.rb', line 21

def initialize(options = {})
  @uri = Import.bulk_create_api_endpoint
  @http = Net::HTTP.new(@uri.host, @uri.port)
  @failed = []
  @total_sent = 0
  @max_batch_size = [(options[:max_batch_size] || 100), 100].min

  @status_enabled = !!options[:status_enabled]

  if uri.scheme == 'https'
    http.use_ssl = true
    http.ca_file = File.join(File.dirname(__FILE__), '../data/cacert.pem')
    http.verify_mode = OpenSSL::SSL::VERIFY_PEER
  end
end

Instance Attribute Details

#failedObject

Returns the value of attribute failed


19
20
21
# File 'lib/intercom-rails/import.rb', line 19

def failed
  @failed
end

#httpObject (readonly)

Returns the value of attribute http


18
19
20
# File 'lib/intercom-rails/import.rb', line 18

def http
  @http
end

#max_batch_sizeObject (readonly)

Returns the value of attribute max_batch_size


18
19
20
# File 'lib/intercom-rails/import.rb', line 18

def max_batch_size
  @max_batch_size
end

#total_sentObject

Returns the value of attribute total_sent


19
20
21
# File 'lib/intercom-rails/import.rb', line 19

def total_sent
  @total_sent
end

#uriObject (readonly)

Returns the value of attribute uri


18
19
20
# File 'lib/intercom-rails/import.rb', line 18

def uri
  @uri
end

Class Method Details

.bulk_create_api_endpointObject


9
10
11
12
# File 'lib/intercom-rails/import.rb', line 9

def self.bulk_create_api_endpoint
  host = (ENV['INTERCOM_RAILS_DEV'] ? "http://api.intercom.dev" : "https://api.intercom.io")
  URI.parse(host + "/v1/users/bulk_create")
end

.run(*args) ⇒ Object


14
15
16
# File 'lib/intercom-rails/import.rb', line 14

def self.run(*args)
  new(*args).run
end

Instance Method Details

#active_record?(user_klass) ⇒ Boolean

Check for ActiveRecord OR Mongoid

Returns:

  • (Boolean)

38
39
40
# File 'lib/intercom-rails/import.rb', line 38

def active_record?(user_klass)
  (defined?(ActiveRecord::Base) && (user_klass < ActiveRecord::Base))
end

#assert_runnableObject

Raises:


50
51
52
53
54
55
56
57
# File 'lib/intercom-rails/import.rb', line 50

def assert_runnable
  raise ImportError, "You can only import your users from your production environment" unless (Rails.env.production? || Rails.env.staging?)
  raise ImportError, "We couldn't find your user class, please set one in config/initializers/intercom_rails.rb" unless user_klass.present?
  info "Found user class: #{user_klass}"
  raise ImportError, "Only ActiveRecord and Mongoid models are supported" unless supported_orm?(user_klass)
  raise ImportError, "Please add an Intercom API Key to config/initializers/intercom.rb" unless IntercomRails.config.api_key.present?
  info "Intercom API key found"
end

#mongoid?(user_klass) ⇒ Boolean

Returns:

  • (Boolean)

42
43
44
# File 'lib/intercom-rails/import.rb', line 42

def mongoid?(user_klass)
  (defined?(Mongoid::Document) && (user_klass < Mongoid::Document))
end

#runObject


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/intercom-rails/import.rb', line 59

def run
  assert_runnable

  info "Sending users in batches of #{max_batch_size}:"
  batches do |batch, number_in_batch|
    failures = send_users(batch)['failed']
    self.failed += failures
    self.total_sent += number_in_batch

    progress '.' * (number_in_batch - failures.count)
    progress 'F' * failures.count
  end
  info "Successfully created #{self.total_sent - self.failed.count} users", :new_line => true
  info "Failed to create #{self.failed.count} #{(self.failed.count == 1) ? 'user' : 'users'}, this is likely due to bad data" unless failed.count.zero?

  self
end

#supported_orm?(user_klass) ⇒ Boolean

Returns:

  • (Boolean)

46
47
48
# File 'lib/intercom-rails/import.rb', line 46

def supported_orm?(user_klass)
  active_record?(user_klass) || mongoid?(user_klass)
end

#total_failedObject


77
78
79
# File 'lib/intercom-rails/import.rb', line 77

def total_failed
  self.failed.count
end