Class: RamenRails::Import

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Import

Returns a new instance of Import.



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

def initialize(opts = {})
  self.user = opts[:user]
  self.company = opts[:company]
end

Instance Attribute Details

#companyObject

Returns the value of attribute company.



15
16
17
# File 'lib/ramen-rails/import.rb', line 15

def company
  @company
end

#userObject

Returns the value of attribute user.



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

def user
  @user
end

Class Method Details

.all_usersObject



6
7
8
9
10
11
12
# File 'lib/ramen-rails/import.rb', line 6

def self.all_users
  User.all.each do |u|
    new(user: u).import
  end

  nil
end

Instance Method Details

#can_get_company_labels?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/ramen-rails/import.rb', line 22

def can_get_company_labels?
  RamenRails.config.import_company_labels.present?
end

#can_get_labels?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/ramen-rails/import.rb', line 26

def can_get_labels?
  RamenRails.config.import_user_labels.present?
end

#can_get_value?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/ramen-rails/import.rb', line 30

def can_get_value?
  RamenRails.config.import_user_value.present?
end

#generate_hashObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/ramen-rails/import.rb', line 67

def generate_hash
  obj = {}
  [:email, :name, :id].each do |attr|
    obj[attr] = user.send(attr)
  end
 
  company_obj = {}
  if company
    [:url, :name, :id].each do |attr|
      company_obj[attr] = user.send(attr)
    end
    
    company_obj[:labels] = get_company_labels if can_get_company_labels?
  end

  obj[:labels] = get_labels if can_get_labels?
  obj[:value] = get_value if can_get_value?

  if user.respond_to?(:created_at) && user.send(:created_at).present?
    obj[:created_at] = user.send(:created_at).to_i
  end
  
  ts = Time.now.to_i

  auth_hash = (Digest::SHA256.new << "#{obj[:email]}:#{obj[:id]}:#{obj[:name]}:#{ts}:#{RamenRails.config.organization_secret}").to_s
  
  h = {
    organization_id: RamenRails.config.organization_id,
    user: obj,
    timestamp: ts,
    auth_hash: auth_hash,
    import: true
  }

  h[:company] = company_obj if company_obj.present?

  h
end

#get_company_labelsObject



34
35
36
37
# File 'lib/ramen-rails/import.rb', line 34

def get_company_labels
  company.
    instance_eval(&RamenRails.config.import_company_labels)
end

#get_labelsObject



39
40
41
42
# File 'lib/ramen-rails/import.rb', line 39

def get_labels
  user.
    instance_eval(&RamenRails.config.import_user_labels)
end

#get_valueObject



44
45
46
47
# File 'lib/ramen-rails/import.rb', line 44

def get_value
  user.
    instance_eval(&RamenRails.config.import_user_value)
end

#importObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/ramen-rails/import.rb', line 49

def import
  h = generate_hash
  json = h.to_json

  endpoint = ENV['RAMEN_IMPORT_URI'] || "https://ramen.is/po.json"
  uri = URI("#{endpoint}?json_payload=#{CGI.escape(json)}")
  start = Time.now.to_f
  resp = Net::HTTP.get_response(uri)
  total = Time.now.to_f - start

  if resp.code == "200"
    puts "Imported #{obj[:name]} <#{obj[:email]}> in #{total} seconds"
  else
    puts "ERROR (#{resp.code}) Importing #{obj[:name]} <#{obj[:email]}>. Continuing...."
    puts resp.body.to_s
  end
end