Completed
Push — master ( 68228c...26585d )
by John
01:10
created

Zendesk.base_url()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
require 'base64'
2
require 'faraday'
3
require 'faraday_middleware'
4
5
module Lita
6
  module Handlers
7
    class Zendesk < Handler
8
      is_command = false
9
10
      VERSION_URL = 'api/v2'
11
      QUERY_TICKETS_ALL = 'tickets'
12
      QUERY_TICKETS_ESCALATED = 'search.json?query=tags:escalated+status:open+status:pending+type:ticket'
13
      QUERY_TICKETS_HOLD = 'search.json?query=status:hold+type:ticket'
14
      QUERY_TICKETS_OPEN = 'search.json?query=status:open+type:ticket'
15
      QUERY_TICKETS_NEW = 'search.json?query=status:new+type:ticket'
16
      QUERY_TICKETS_PENDING = 'search.json?query=status:pending+type:ticket'
17
      QUERY_TICKETS_UNSOLVED = 'search.json?query=status<solved+type:ticket'
18
      QUERY_USERS = 'users'
19
20
      config :subdomain, type: String, required: true
21
      config :auth_type, type: String, default: 'password' # or token
22
      config :user, type: String, required: true
23
      config :token, type: String, default: ''
24
      config :password, type: String, default: ''
25
26
      def init
27
        @base_url = base_url
28
        @version_url = "#{@base_url}/#{VERSION_URL}"
29
        @tickets_url = "#{@base_url}/tickets"
30
31
        if config.auth_type == 'password'
32
          @conn = Faraday.new(url: @version_url) do |faraday|
33
            faraday.headers['Authorization'] = "Basic #{basic_credentials}"
34
            faraday.response :json                    # JSON response
35
            faraday.response :logger                  # log requests to STDOUT
36
            faraday.adapter  Faraday.default_adapter  # make requests with Net::HTTP
37
          end
38
        else
39
          @conn = Faraday.new(url: @version_url) do |faraday|
40
            faraday.response :json                    # JSON response
41
            faraday.response :logger                  # log requests to STDOUT
42
            faraday.adapter  Faraday.default_adapter  # make requests with Net::HTTP
43
          end
44
          @conn.basic_auth("#{config.user}/token", config.token) 
45
        end
46
      end
47
48
      def base_url
49
        "https://#{config.subdomain.to_s}.zendesk.com"
50
      end
51
52
      def basic_credentials
53
        Base64.encode64("#{config.user}:#{config.password}").gsub(/\s/,'')
54
      end
55
56
      def zendesk_request(url)
57
        init unless @conn
58
        if url.index('http') != 0
59
          url = "#{@version_url}/#{url}"
60
        end
61
        @conn.get url
62
      end
63
64
      def ticket_count(response, url, ticket_type = '')
65
        res = zendesk_request url
66
        ticket_count = res.body['count']
67
        ticket_word  = ticket_count == 1 ? 'ticket' : 'tickets'
68
        ticket_desc  = ticket_type == '' ? '' : "#{ticket_type} "
69
        response.reply "#{ticket_count} #{ticket_desc}#{ticket_word}."
70
      end
71
72
      def ticket_list(response, url, ticket_type = '')
73
        res = zendesk_request url
74
        tickets = res.body['results']
75
        tickets.each do |ticket|
76
          response.reply "Ticket #{ticket['id']} is #{ticket['status']}: #{@tickets_url}/#{ticket['id']} - #{ticket['subject']}"
77
        end
78
        ticket_length = tickets.length
79
        ticket_count = res.body['count']
80
        ticket_word  = ticket_count == 1 ? 'ticket' : 'tickets'
81
        ticket_desc  = ticket_type == '' ? '' : "#{ticket_type} "
82
        response.reply "Listing #{ticket_length} of #{ticket_count} #{ticket_desc}#{ticket_word}."
83
      end
84
85
      def tickets(count)
86
        count == 1 ? 'ticket' : 'tickets'
87
      end
88
89
      # Info
90
91
      route(/^(?:zd|zendesk)\s+connection\s*$/, :zd_instance_info, command: true, help: { 'zd connection' => 'returns information on the Zendesk connection' })
92
      def zd_instance_info(response)
93
        response.reply "Using Zendesk instance at: #{base_url}"
94
      end
95
96
      # Ticket Counts
97
98
      route(/^(?:zd|zendesk)(\s+unsolved)?\s+tickets?\s*$/, :unsolved_tickets, command: true, help: { 'zd tickets' => 'returns the total count of all unsolved tickets' })
99
      def unsolved_tickets(response)
100
        ticket_count response, QUERY_TICKETS_UNSOLVED, 'unsolved'
101
      end
102
103
      route(/^(?:zd|zendesk)\s+(all|total)\s+tickets?\s*$/, :total_tickets, command: true, help: { 'zd all tickets' => 'returns the count of all tickets' })
104
      def total_tickets(response)
105
        ticket_count response, QUERY_TICKETS_ALL, 'total'
106
      end
107
108
      route(/^(?:zd|zendesk)\s+pending\s+tickets?\s*$/, :pending_tickets, command: true, help: { 'zd pending tickets' => 'returns a count of tickets that are pending' })
109
      def pending_tickets(response)
110
        ticket_count response, QUERY_TICKETS_PENDING, 'pending'
111
      end
112
113
      route(/^(?:zd|zendesk)\s+new\s+tickets?\s*$/, :new_tickets, command: true, help: { 'zd new tickets' => 'returns the count of all new (unassigned) tickets' })
114
      def new_tickets(response)
115
        ticket_count response, QUERY_TICKETS_NEW, 'new'
116
      end
117
118
      route(/^(?:zd|zendesk)\s+escalated\s+tickets?\s*$/, :escalated_tickets, command: true, help: { 'zd escalated tickets' => 'returns a count of tickets with escalated tag that are open or pending' })
119
      def escalated_tickets(response)
120
        ticket_count response, QUERY_TICKETS_ESCALATED, 'escalated'
121
      end
122
123
      route(/^(?:zd|zendesk)\s+open\s+tickets?\s*$/, :open_tickets, command: true, help: { 'zd open tickets' => 'returns the count of all open tickets' })
124
      def open_tickets(response)
125
        ticket_count response, QUERY_TICKETS_OPEN, 'open'
126
      end
127
128
      route(/^(?:zd|zendesk)\s+on\s+hold\s+tickets?\s*$/, :onhold_tickets, command: true, help: { 'zd on hold tickets' => 'returns the count of all on hold tickets' })
129
      def onhold_tickets(response)
130
        ticket_count response, QUERY_TICKETS_HOLD, 'on hold'
131
      end
132
133
      # Ticket Lists
134
135
      route(/^(?:zd|zendesk)\s+list(\s+unsolved)?\s+tickets?\s*$/, :unsolved_tickets_list, command: true, help: { 'zd list tickets' => 'returns a list of unsolved tickets' })
136
      def unsolved_tickets_list(response)
137
        ticket_list response, QUERY_TICKETS_UNSOLVED, 'unsolved'
138
      end
139
140
      route(/^(?:zd|zendesk)\s+list\s+(all|total)\s+tickets?\s*$/, :total_tickets_list, command: true, help: { 'zd list all tickets' => 'returns a list of all tickets' })
141
      def total_tickets_list(response)
142
        ticket_list response, QUERY_TICKETS_ALL, 'total'
143
      end
144
145
      route(/^(?:zd|zendesk)\s+list\s+pending\s+tickets?\s*$/, :pending_tickets_list, command: true, help: { 'zd list pending tickets' => 'returns a list of pending tickets' })
146
      def pending_tickets_list(response)
147
        ticket_list response, QUERY_TICKETS_PENDING, 'pending'
148
      end
149
150
      route(/^(?:zd|zendesk)\s+list\s+new\s+tickets?\s*$/, :new_tickets_list, command: true, help: { 'zd list new tickets' => 'returns a list of new tickets' })
151
      def new_tickets_list(response)
152
        ticket_list response, QUERY_TICKETS_NEW, 'new'
153
      end
154
155
      route(/^(?:zd|zendesk)\s+list\s+escalated\s+tickets?\s*$/, :escalated_tickets_list, command: true, help: { 'zd list esclated tickets' => 'returns a list of escalated tickets' })
156
      def escalated_tickets_list(response)
157
        ticket_list response, QUERY_TICKETS_ESCALATED, 'escalated'
158
      end
159
160
      route(/^(?:zd|zendesk)\s+list\s+open\s+tickets?\s*$/, :open_tickets_list, command: true, help: { 'zd list open tickets' => 'returns a list of open tickets' })
161
      def open_tickets_list(response)
162
        ticket_list response, QUERY_TICKETS_OPEN, 'open'
163
      end
164
165
      route(/^(?:zd|zendesk)\s+list\s+on\s+hold\s+tickets?\s*$/, :onhold_tickets_list, command: true, help: { 'zd list onhold tickets' => 'returns a list of on hold tickets' })
166
      def onhold_tickets_list(response)
167
        ticket_list response, QUERY_TICKETS_HOLD, 'on hold'
168
      end
169
170
      # Ticket Details
171
172
      route(/^(?:zd|zendesk)\s+ticket\s+(\d+)\s*$/, :ticket_details, command: true, help: { 'zd ticket <ID>' => 'returns information about the specified ticket' })
173
      def ticket_details(response)
174
        ticket_id = response.matches[0][0]
175
        url = "#{QUERY_TICKETS_ALL}/#{ticket_id}.json"
176
        res = zendesk_request url
177
        data = res.body
178
179
        message = "Ticket #{data['ticket']['id']}: #{@tickets_url}/#{data['ticket']['id']}"
180
        message += "\nStatus: #{data['ticket']['status'].upcase}"
181
        message += "\nUpdated: " + data['ticket']['updated_at']
182
        message += "\nAdded: #{data['ticket']['created_at']}"
183
        message += "\nSubject: #{data['ticket']['subject']}"
184
        message += "\nDescription:\n-----\n#{data['ticket']['description']}\n-----\n"
185
        response.reply message
186
      end
187
    end
188
189
    Lita.register_handler(Zendesk)
190
  end
191
end