class TicketAbstractorClient::ServiceNow::Ticket

Attributes

sys_id[RW]

Public Class Methods

fetch_all_tickets(opts) click to toggle source
# File lib/ticket_abstractor_client/service_now/ticket.rb, line 10
def self.fetch_all_tickets(opts)
  fetch(:all, opts)
end
fetch_by_id(opts) click to toggle source
# File lib/ticket_abstractor_client/service_now/ticket.rb, line 6
def self.fetch_by_id(opts)
  fetch(:by_id, opts).first
end
fetch_tickets_by_query(opts) click to toggle source
# File lib/ticket_abstractor_client/service_now/ticket.rb, line 14
def self.fetch_tickets_by_query(opts)
  fetch(:by_query, opts)
end
new(opts = {}) click to toggle source
# File lib/ticket_abstractor_client/service_now/ticket.rb, line 18
def initialize(opts = {})
  super(opts)

  @project = opts[:project] || raise(Errors::TicketArgumentError, 'Project is not given')
  @sys_id = @fields['sys_id']
  @status = @fields['state']
end

Private Class Methods

fetch(selektor, opts) click to toggle source

selektor – all, by_id, by_query Ticket.fetch(:all, opts) Ticket.fetch(:by_id, opts) Ticket.fetch(:by_query, opts)

# File lib/ticket_abstractor_client/service_now/ticket.rb, line 90
def self.fetch(selektor, opts)
  opts = opts.with_indifferent_access
  endpoint = opts.delete(:endpoint)
  method_map = { all: :get_all_tickets, by_id: :get_ticket_by_id, by_query: :get_tickets_by_query }
  client = Client.new(endpoint)
  client_response = client.public_send(method_map.fetch(selektor.to_sym), opts)

  Array.wrap(client_response).map do |ticket_hash|
    ticket_id = opts[:ticket_id] || ticket_hash['number']
    ticket = new(ticket_id: ticket_id, fields: ticket_hash, endpoint: endpoint, project: opts[:project])
    filtered_ticket = filter_ticket(ticket)
    filtered_ticket.reset_changes!
    filtered_ticket.communications_stack = client.communications_stack
    filtered_ticket
  end
end
filter_ticket(raw_ticket) click to toggle source
# File lib/ticket_abstractor_client/service_now/ticket.rb, line 107
def self.filter_ticket(raw_ticket)
  tickets_filter_class = TicketAbstractorClient.configuration.snow_tickets_filter_class

  return raw_ticket if tickets_filter_class.blank?

  tickets_filter_class.new(raw_ticket).filter_ticket
end

Public Instance Methods

add_attachment(attachment) click to toggle source
# File lib/ticket_abstractor_client/service_now/ticket.rb, line 26
def add_attachment(attachment)
  @attachments << Attachment.new(attachment.merge(ticket_id: @ticket_id, endpoint: @endpoint, project: @project))
  @changes[:new_attachments] += 1

  self
end
add_comment(comment) click to toggle source
# File lib/ticket_abstractor_client/service_now/ticket.rb, line 33
def add_comment(comment)
  @comments << Comment.new(comment.merge(ticket_id: @ticket_id, endpoint: @endpoint, project: @project))
  @changes[:new_comments] += 1

  self
end
attachments() click to toggle source
# File lib/ticket_abstractor_client/service_now/ticket.rb, line 40
def attachments
  return @attachments if @attachments.present?

  return [] if @ticket_id.blank? && @attachments.blank?

  @attachments, attachments_communications_stack = Attachment.fetch(@ticket_id, @endpoint, @project)
  @communications_stack += attachments_communications_stack

  @attachments
end
comments() click to toggle source
# File lib/ticket_abstractor_client/service_now/ticket.rb, line 51
def comments
  return @comments if @comments.present?

  return [] if @ticket_id.blank? && @comments.blank?

  @comments, comments_communications_stack = Comment.fetch(@ticket_id, @endpoint, @project)
  @communications_stack += comments_communications_stack

  @comments
end
sync!() click to toggle source
# File lib/ticket_abstractor_client/service_now/ticket.rb, line 66
def sync!
  raise(Errors::TicketArgumentError, 'No changes to apply') unless self.any_changes?

  return create_ticket if @changes[:create]

  update_ticket if @changes[:update]
  update_status if @changes[:new_status]
  sync_comments unless @changes[:new_comments].zero?
  sync_attachments unless @changes[:new_attachments].zero?
  reset_changes!

  self
end
to_hash() click to toggle source
# File lib/ticket_abstractor_client/service_now/ticket.rb, line 62
def to_hash
  { sys_id: @sys_id }.merge(super())
end
updated_at() click to toggle source
# File lib/ticket_abstractor_client/service_now/ticket.rb, line 80
def updated_at
  @fields['sys_updated_on']
end

Private Instance Methods

create_ticket() click to toggle source
# File lib/ticket_abstractor_client/service_now/ticket.rb, line 115
def create_ticket
  client = Client.new(@endpoint)
  response = client.create_ticket(self)
  @communications_stack += client.communications_stack

  ticket = Ticket.fetch_by_id(ticket_id: response['number'], project: @project, endpoint: @endpoint)

  @sys_id = ticket.sys_id
  @fields = ticket.fields
  @status = ticket.status
  @ticket_id = ticket.ticket_id
  @communications_stack += ticket.communications_stack
  reset_changes!

  self
end
sync_attachments() click to toggle source
# File lib/ticket_abstractor_client/service_now/ticket.rb, line 144
def sync_attachments
  @communications_stack +=
    @attachments.last(@changes[:new_attachments]).flat_map do |attachment|
      attachment.sync!
      attachment.communications_stack
    end
end
sync_comments() click to toggle source
# File lib/ticket_abstractor_client/service_now/ticket.rb, line 152
def sync_comments
  @communications_stack +=
    @comments.last(@changes[:new_comments]).flat_map do |comment|
      comment.sync!
      comment.communications_stack
    end
end
update_status() click to toggle source
# File lib/ticket_abstractor_client/service_now/ticket.rb, line 132
def update_status
  @fields['state'] = @status
  update_ticket
end
update_ticket() click to toggle source
# File lib/ticket_abstractor_client/service_now/ticket.rb, line 137
def update_ticket
  client = Client.new(@endpoint)
  response = client.update_ticket(self)
  @communications_stack += client.communications_stack
  @fields['sys_updated_on'] = response['sys_updated_on']
end