-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfaster-csv-pull.rb
More file actions
81 lines (73 loc) · 1.82 KB
/
Copy pathfaster-csv-pull.rb
File metadata and controls
81 lines (73 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
require 'rubygems'
require 'CSV'
require 'httparty'
require 'FileUtils'
require 'nokogiri'
require 'crack'
require 'uri'
require 'json'
login = ARGV[0]
password = ARGV[1]
infile = ARGV[2]
outfile = infile.split(".").first
def is_a_number?(s)
s.to_s.match(/\A[+-]?\d+?(\.\d+)?\Z/) == nil ? false : true
end
class Zenbody
include JSON
def self.email(data)
newJSON = JSON.parse(data)
if newJSON.length > 0
return newJSON['user']['name']
else
return 'none found
'
end
end
def comments(data)
login = ARGV[0]
password = ARGV[1]
a = Zenuser.new(login, password)
newJSON = JSON.parse(data.body)
totalComment = ''
newJSON['comments'].each do |comment|
name = a.requester(comment['author_id'])
totalComment << Zenbody.email(name) + ": " + comment['value'] + "\r\n"
end
return totalComment
end
end
class Zenuser
include HTTParty
base_uri 'https://something.zendesk.com'
headers 'content-type' => 'application/json'
def initialize(u, p)
@auth = {:username => u, :password => p}
end
def tickets(ticket_number)
options = {:basic_auth => @auth}
self.class.get('/tickets/' + ticket_number +'.json', options)
end
def requester(user_id)
options = {:basic_auth => @auth}
json = JSON 'name' =>'System User'
results = self.class.get('/api/v2/users/' + user_id.to_s, options)
if results.headers['status'] == '200 OK'
return results.body
else
return json
end
end
end
x = Zenuser.new(login, password)
y = Zenbody.new()
arr_of_arrs = CSV.read(infile)
CSV.open(outfile + '-with-comments.csv', 'w', :force_quotes => true, :col_sep =>'|') do |csv|
arr_of_arrs.each do |row|
if is_a_number?(row[1])
ticketComment = y.comments(x.tickets(row[1]))
row.push(ticketComment)
end
csv << row
end
end