Class: ZendeskTicket

Inherits:
Object
  • Object
show all
Defined in:
lib/zentool/zendesk_ticket.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username, password, domain) ⇒ ZendeskTicket

Returns a new instance of ZendeskTicket.



15
16
17
18
19
20
# File 'lib/zentool/zendesk_ticket.rb', line 15

def initialize(username, password, domain)
  @root_uri = "https://#{domain}.zendesk.com/api/v2/"
  @start_time = Time.new('2016-01-01').to_i
  @tickets_uri = @root_uri + "incremental/tickets.json?start_time=#{@start_time}"
  @username, @password, @domain = username, password, domain
end

Instance Attribute Details

#domainObject

Returns the value of attribute domain.



13
14
15
# File 'lib/zentool/zendesk_ticket.rb', line 13

def domain
  @domain
end

#passwordObject

Returns the value of attribute password.



13
14
15
# File 'lib/zentool/zendesk_ticket.rb', line 13

def password
  @password
end

#root_uriObject

Returns the value of attribute root_uri.



13
14
15
# File 'lib/zentool/zendesk_ticket.rb', line 13

def root_uri
  @root_uri
end

#start_timeObject

Returns the value of attribute start_time.



13
14
15
# File 'lib/zentool/zendesk_ticket.rb', line 13

def start_time
  @start_time
end

#tickets_uriObject

Returns the value of attribute tickets_uri.



13
14
15
# File 'lib/zentool/zendesk_ticket.rb', line 13

def tickets_uri
  @tickets_uri
end

#usernameObject

Returns the value of attribute username.



13
14
15
# File 'lib/zentool/zendesk_ticket.rb', line 13

def username
  @username
end

Instance Method Details

#basic_authObject



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

def basic_auth
  {
    basic_auth: {
      username: @username,
      password: @password
    }
  }
end

#check_authObject



79
80
81
82
83
84
85
# File 'lib/zentool/zendesk_ticket.rb', line 79

def check_auth
  response = HTTParty.get(@tickets_uri, basic_auth)
  unless response.code == 200
    puts "Error #{response.code}: #{response.message}"
    abort
  end
end

#download_ticketsObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/zentool/zendesk_ticket.rb', line 39

def download_tickets
  @tickets ||= begin

    first_page = HTTParty.get(@tickets_uri, basic_auth)
    # puts first_page
    tickets = first_page['tickets']
    next_url = first_page['next_page']
    count = first_page['count']
    puts "   Got: #{count}"

    while count == 1000
      next_page = HTTParty.get(next_url, basic_auth)
      tickets += next_page['tickets']
      next_url = next_page['next_page']
      count = next_page['count']
      puts "   Got: #{count}"
    end
  end
  tickets
end

#export_columnsObject



60
61
62
63
# File 'lib/zentool/zendesk_ticket.rb', line 60

def export_columns
  %w(id type subject status user_priority development_priority company project
     platform function satisfaction_rating created_at updated_at)
end

#metric_columnsObject



65
66
67
68
# File 'lib/zentool/zendesk_ticket.rb', line 65

def metric_columns
  %w(initially_assigned_at solved_at full_resolution_time_in_minutes
     requester_wait_time_in_minutes reply_time_in_minutes)
end

#retrieve_fields(tickets_in) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/zentool/zendesk_ticket.rb', line 87

def retrieve_fields(tickets_in)

  puts '   Total tickets = ' + tickets_in.count.to_s
  puts

  CSV.open('all_tickets.csv', 'wb') do |csv|
    csv << self.export_columns + self.metric_columns
  end

  tickets = Array.new

  print "Enter number of tickets (max of #{tickets_in.count.to_s}): "
  number_of_tickets = gets.chomp.to_i
  puts

  progressbar = ProgressBar.create(title: "#{number_of_tickets} Tickets",
    starting_at: 0, format: '%a |%b>>%i| %p%% %t', total: number_of_tickets)

  tickets_in.first(number_of_tickets).each do |ticket|
    CSV.open('all_tickets.csv', 'a') do |csv|
      row = []
      info = Hash.new
      metrics_info = Hash.new
      self.export_columns.each do |column|
        case column
        when 'type'
          info['type'] = ticket['custom_fields'][0]['value']
          row << info['type']
        when 'user_priority'
          info['user_priority'] = ticket['custom_fields'][1]['value']
          row << info['user_priority']
        when 'development_priority'
          value = ticket['custom_fields'][2]['value']
          if value
            info['development_priority'] = "d#{value[-1]}" if value[-1].to_i > 0
          else
            info['development_priority'] = value
          end
          row << info['development_priority']
        when 'company'
          info['company'] = ticket['custom_fields'][3]['value']
          row << info['company']
        when 'project'
          info['project'] = ticket['custom_fields'][4]['value']
          row << info['project']
        when 'platform'
          info['platform'] = ticket['custom_fields'][5]['value']
          row << info['platform']
        when 'function'
          info['function'] = ticket['custom_fields'][6]['value']
          row << info['function']
        when 'satisfaction_rating'
          info['satisfaction_rating'] = ticket['satisfaction_rating']['score']
          row << info['satisfaction_rating']
        else
          info[column] = ticket[column]
          row << info[column]
        end
      end

      begin
        metrics = HTTParty.get("#{@root_uri}tickets/#{ticket['id']}/metrics.json", self.basic_auth)
        self.metric_columns.each do |column|
          if metrics['ticket_metric']
            case column
            when 'solved_at', 'initially_assigned_at'
              metrics_info[column] = metrics['ticket_metric'][column]
            else
              metrics_info[column] = metrics['ticket_metric'][column]['business']
            end
            row << metrics_info[column]
          end
        end
      rescue
        retry
      end

      this = Ticket.new(info, metrics_info)
      tickets << this
      csv << row
      progressbar.increment
    end
  end
  tickets
end

#runObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/zentool/zendesk_ticket.rb', line 22

def run

  puts 'Checking authentication...'
  check_auth
  puts 'Authentication successful!'
  puts
  puts 'Envision Zendesk Tickets'
  puts '------------------------'
  puts '-> Retrieving Tickets...'

  tickets_in = self.download_tickets
  tickets = self.retrieve_fields(tickets_in)
  metrics = Metrics.new(tickets)
  metrics.graph
  metrics.save
end