Skip to content

Instantly share code, notes, and snippets.

@tstachl
Created October 29, 2013 16:56
Show Gist options
  • Save tstachl/7218523 to your computer and use it in GitHub Desktop.
Save tstachl/7218523 to your computer and use it in GitHub Desktop.
Simple example of importing customers to desk.com using the API with the API wrapper gem https://github.com/tstachl/desk.
require 'desk_api'
require 'csv'
client = DeskApi::Client.new({
username: '[email protected]', # your desk.com username
password: 'yourpassword', # your desk.com password
subdomain: 'yoursubdomain' # your desk.com subdomain (https://this.desk.com)
})
CSV.foreach('./customers.csv', headers: true) do |row|
emails = []
emails.push({ type: 'home', value: row['email'].strip }) if not row['email'].nil? and not row['email'].empty?
emails.push({ type: 'work', value: row['email_2'].strip }) if not row['email_2'].nil? and not row['email_2'].empty?
phones = []
phones.push({ type: 'home', value: row['phone_home'].strip }) if not row['phone_home'].nil? and not row['phone_home'].empty?
phones.push({ type: 'work', value: row['phone_work'].strip }) if not row['phone_work'].nil? and not row['phone_work'].empty?
phones.push({ type: 'mobile', value: row['phone_mobile'].strip }) if not row['phone_mobile'].nil? and not row['phone_mobile'].empty?
addresses = []
addresses.push({ type: 'home', value: "#{row['street']}\n#{row['city']}, #{row['state']} #{row['zip']}" })
# you need to setup your custom fields
custom_fields = {}
params = {
first_name: (row['first_name'] || '').strip,
last_name: (row['last_name'] || '').strip,
emails: emails,
phone_numbers: phones,
addresses: addresses,
custom_fields: custom_fields
}
begin
client.customers.create params
rescue DeskApi::Error::UnprocessableEntity
# this means the customer already exists
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment