Class: Vimaly::Client
- Inherits:
-
Object
- Object
- Vimaly::Client
- Defined in:
- lib/vimaly/client.rb
Constant Summary collapse
- VIMALY_ROOT_URL =
"https://o1.vimaly.com"
- CUSTOM_FIELD_TYPES =
[::NilClass, ::String, ::Float, ::Integer, ::Array, ::Date]
Instance Method Summary collapse
- #add_attachment(ticket_id, file_name, file_content, request_options = {}) ⇒ Object
- #bin(name_or_regex) ⇒ Object
- #bins(options = {}) ⇒ Object
- #create_ticket(title, description, format: 'text', ticket_type_name: 'bug', bin_name: /please panic/i, **others) ⇒ Object
-
#custom_fields ⇒ Object
Get custom fields as a hash of id => name, type.
-
#initialize(company_id, user_credentials: nil, auth_key: nil, logger: nil) ⇒ Client
constructor
A new instance of Client.
- #matching_tickets_in_bin(bin_id, title_matcher) ⇒ Object
- #matching_tickets_in_named_bin(bin_name, title_matcher) ⇒ Object
- #search_tickets(query_string, state: 'active') ⇒ Object
- #ticket_type(name_or_regex) ⇒ Object
- #ticket_types ⇒ Object
- #tickets_in_bin(bin_id) ⇒ Object
- #tickets_in_named_bin(bin_name) ⇒ Object
- #update_ticket(id, other_fields) ⇒ Object
Constructor Details
#initialize(company_id, user_credentials: nil, auth_key: nil, logger: nil) ⇒ Client
Returns a new instance of Client.
11 12 13 14 15 16 17 18 |
# File 'lib/vimaly/client.rb', line 11 def initialize(company_id, user_credentials: nil, auth_key: nil, logger: nil) @company_id = company_id @user_credentials = user_credentials @auth_key = auth_key @logger = logger @bin_tickets = {} end |
Instance Method Details
#add_attachment(ticket_id, file_name, file_content, request_options = {}) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/vimaly/client.rb', line 89 def (ticket_id, file_name, file_content, ={}) response = post("/tickets/#{ticket_id}/attachments?name=#{CGI.escape(file_name)}", file_content, ) case response.status when 200..299 true else log_warn "status: #{response.status}" log_warn " #{response.inspect}" false end end |
#bin(name_or_regex) ⇒ Object
101 102 103 104 105 106 107 108 |
# File 'lib/vimaly/client.rb', line 101 def bin(name_or_regex) case name_or_regex when String bins.detect { |bin| bin.name == name_or_regex } when Regexp bins.detect { |bin| bin.name =~ name_or_regex } end end |
#bins(options = {}) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/vimaly/client.rb', line 110 def bins(={}) @bins ||= begin # 500 is the vimaly limit bins_per_request = [:bins_per_request] || 500 # 5000 is just a random value here; hopefully we never reach this limit max_number_of_bins = [:max_number_of_bins] || 5000 bins = [] current_page = 0 while bins.size < max_number_of_bins # those request parameters are not documented, maybe this gets added at some # point to https://vimaly.com/public/rest-help.html#Bins chunk_of_bins = get("/bins?max-results=#{bins_per_request}&page-token=#{current_page * bins_per_request}") current_page += 1 bins += chunk_of_bins break if chunk_of_bins.size < bins_per_request end bins.map do |bin| Bin.new(bin['_id'], bin['name']) end end end |
#create_ticket(title, description, format: 'text', ticket_type_name: 'bug', bin_name: /please panic/i, **others) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/vimaly/client.rb', line 20 def create_ticket(title, description, format: 'text', ticket_type_name: 'bug', bin_name: /please panic/i, **others) ticket = Ticket.new({ title: title, description: description, format: format, ticket_type: ticket_type(ticket_type_name), bin: bin(bin_name) }. merge(others) ) ticket_id = next_ticket_id ticket_to_json = ticket.to_json(custom_field_name_map) response = post("/tickets/#{ticket_id}", ticket_to_json) case response.status when 200..299 ticket_id else log_warn "status: #{response.status}" log_warn " #{response.inspect}" false end end |
#custom_fields ⇒ Object
Get custom fields as a hash of id => name, type
182 183 184 185 186 187 188 |
# File 'lib/vimaly/client.rb', line 182 def custom_fields @custom_fields ||= begin Hash[get('/custom-fields').map do |cf| [cf['_id'], { id: cf['_id'], name: cf['name'], type: CUSTOM_FIELD_TYPES[cf['type'].to_i] }] end] end end |
#matching_tickets_in_bin(bin_id, title_matcher) ⇒ Object
171 172 173 174 175 176 177 178 179 |
# File 'lib/vimaly/client.rb', line 171 def matching_tickets_in_bin(bin_id, title_matcher) tickets = tickets_in_bin(bin_id) case title_matcher when String tickets.select { |t| t.title == title_matcher } when Regexp tickets.select { |t| t.title =~ title_matcher } end end |
#matching_tickets_in_named_bin(bin_name, title_matcher) ⇒ Object
165 166 167 168 169 |
# File 'lib/vimaly/client.rb', line 165 def matching_tickets_in_named_bin(bin_name, title_matcher) named_bin = bin(bin_name) raise "Bin #{bin_name} not found" unless named_bin matching_tickets_in_bin(named_bin.id, title_matcher) end |
#search_tickets(query_string, state: 'active') ⇒ Object
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 82 83 84 85 86 87 |
# File 'lib/vimaly/client.rb', line 57 def search_tickets(query_string, state: 'active') ticket_type_map = Hash[ticket_types.map { |t| [t.id, t] }] bin_map = Hash[bins.map { |b| [b.id, b] }] tickets = [] i = 0 while (i * 500) <= 5000 # Without max-results the request defaults to 200. # The maximum max-results is 500, so will do 10 requests to get our maximum of 5000 tickets. search_response = get("/ticket-search?text=#{query_string}&state=#{state}&max-results=500&page-token=#{i * 500}") response_tickets = case search_response when Array search_response.map do |ticket_data| Ticket.from_vimaly(ticket_data, ticket_type_map, bin_map, custom_fields) end when Hash # Will be a hash when only 1 ticket is returned. [Ticket.from_vimaly(search_response, ticket_type_map, bin_map, custom_fields)] else # If reponse is nil then we know we've got all the data break end tickets.concat(response_tickets) break if response_tickets.size < 500 # Can finish search because we've returned all there is. i += 1 end tickets end |
#ticket_type(name_or_regex) ⇒ Object
133 134 135 136 137 138 139 140 |
# File 'lib/vimaly/client.rb', line 133 def ticket_type(name_or_regex) case name_or_regex when String ticket_types.detect { |tt| tt.name == name_or_regex } when Regexp ticket_types.detect { |tt| tt.name =~ name_or_regex } end end |
#ticket_types ⇒ Object
142 143 144 145 146 147 148 |
# File 'lib/vimaly/client.rb', line 142 def ticket_types @ticket_types ||= begin get('/ticket-types').map do |bin_data| TicketType.new(bin_data['_id'], bin_data['name']) end end end |
#tickets_in_bin(bin_id) ⇒ Object
156 157 158 159 160 161 162 163 |
# File 'lib/vimaly/client.rb', line 156 def tickets_in_bin(bin_id) ticket_type_map = Hash[ticket_types.map { |t| [t.id, t] }] bin_map = Hash[bins.map { |b| [b.id, b] }] get("/tickets?bin_id=#{bin_id}").map do |ticket_data| Ticket.from_vimaly(ticket_data, ticket_type_map, bin_map, custom_fields) end end |
#tickets_in_named_bin(bin_name) ⇒ Object
150 151 152 153 154 |
# File 'lib/vimaly/client.rb', line 150 def tickets_in_named_bin(bin_name) named_bin = bin(bin_name) raise "Bin #{bin_name} not found" unless named_bin tickets_in_bin(named_bin.id) end |
#update_ticket(id, other_fields) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/vimaly/client.rb', line 43 def update_ticket(id, other_fields) ticket = Ticket.new(other_fields) response = put("/tickets/#{id}", ticket.to_json(custom_field_name_map, true)) case response.status when 200..299 id else log_warn "status: #{response.status}" log_warn " #{response.inspect}" false end end |