Class: Zdpop::Populator

Inherits:
Object
  • Object
show all
Defined in:
lib/zdpop/populator.rb

Instance Method Summary collapse

Constructor Details

#initialize(configfile, datafile) ⇒ Populator

Returns a new instance of Populator.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/zdpop/populator.rb', line 13

def initialize(configfile,datafile)
  begin
    # Set configuration
    raise "Configuration file #{configfile} missing" unless File.file? configfile
    config = YAML.load_file(configfile)
    @site = config[:zendesk_site]
    @user = config[:zendesk_user]
    @password = config[:zendesk_password]
    raise "Missing Zendesk site, user or password" if [ @site, @user, @password ].include?(nil)

    # Load data
    raise "Data file #{datafile} missing" unless File.file? datafile
    data = CSV.read(datafile)
    raise "No Zendesk data loaded - file empty" unless data != []
    create(data)
  rescue Exception => e
    puts e.message
  end
end

Instance Method Details

#create(data) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/zdpop/populator.rb', line 33

def create(data)
  orglist = list_orgs
  userlist = list_users
  data.each { |d|
    org = d[0]
    name = d[1]
    email = d[2]
    domain = email.gsub(/^.*\@(.*)$/, '\1')
    unless orglist.include?(org)
      create_org(org,domain)
    end
    unless userlist.include?(name)
      create_user(name,email,org)
    end
  }
end

#create_org(org, domain) ⇒ Object



79
80
81
82
83
84
85
86
87
# File 'lib/zdpop/populator.rb', line 79

def create_org(org,domain)
  payload = { :organization => { :name => org, :is_shared => 'true', :is_shared_comments => 'true', :default => domain } }
  response = HTTParty.post("#{@site}api/v1/organizations.json", :basic_auth => {:username=>"#{@user}", :password=> "#{@password}" }, :body => payload )
  if response.code == 201
    puts "Created organization #{org}"
  else
    puts "Response code: #{response.code} - #{response.body}"
  end
end

#create_user(name, email, org) ⇒ Object



89
90
91
92
93
94
95
96
97
98
# File 'lib/zdpop/populator.rb', line 89

def create_user(name,email,org)
  id = lookup_org_id(org)
  payload = { :user => { :name => name, :email => email, :roles => '0', :restriction_id => '2', :organization_id => id } }
  response = HTTParty.post("#{@site}users.json", :basic_auth => {:username=>"#{@user}", :password=> "#{@password}" }, :body => payload )
  if response.code == 200
    puts "Created user #{name}"
  else
    puts "Response code: #{response.code} - #{response.body}"
  end
end

#list_orgsObject



50
51
52
53
54
55
56
57
58
# File 'lib/zdpop/populator.rb', line 50

def list_orgs
  orglist = []
  organizations = HTTParty.get("#{@site}api/v1/organizations.json", :basic_auth => {:username=>"#{@user}", :password=> "#{@password}" } )

  organizations.each { |o|
      orglist << o["name"]
  }
  return orglist
end

#list_usersObject



70
71
72
73
74
75
76
77
# File 'lib/zdpop/populator.rb', line 70

def list_users
  userlist = []
  users = HTTParty.get("#{@site}users.json", :basic_auth => {:username=>"#{@user}", :password=> "#{@password}" } )
  users.each { |u|
    userlist << u["name"]
  }
  return userlist
end

#lookup_org_id(org) ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'lib/zdpop/populator.rb', line 60

def lookup_org_id(org)
  orgtable = {}
  organizations = HTTParty.get("#{@site}api/v1/organizations.json", :basic_auth => {:username=>"#{@user}", :password=> "#{@password}" } )
  organizations.each { |o|
    orgtable["#{o["name"]}"] = o["id"]
  }
  id = orgtable["#{org}"]
  return id
end