diff --git a/lib/zendesk2/client.rb b/lib/zendesk2/client.rb index 2e23703..e2615cf 100644 --- a/lib/zendesk2/client.rb +++ b/lib/zendesk2/client.rb @@ -4,6 +4,8 @@ class Zendesk2::Client < Cistern::Service model_path "zendesk2/client/models" request_path "zendesk2/client/requests" + require 'zendesk2/client/models/audit_event' # so special + collection :categories collection :forums collection :groups @@ -14,13 +16,18 @@ class Zendesk2::Client < Cistern::Service collection :topics collection :users collection :user_identities - model :audit_event model :category model :forum model :group model :organization model :ticket model :ticket_audit + model :ticket_change + model :ticket_comment + model :ticket_comment_privacy_change + model :ticket_create + model :ticket_notification + model :ticket_voice_comment model :topic model :topic_comment model :user diff --git a/lib/zendesk2/client/models/audit_event.rb b/lib/zendesk2/client/models/audit_event.rb index 16d2f8a..ef32367 100644 --- a/lib/zendesk2/client/models/audit_event.rb +++ b/lib/zendesk2/client/models/audit_event.rb @@ -1,33 +1,30 @@ +# @abstract subclass and implement audit event specific attributes class Zendesk2::Client::AuditEvent < Cistern::Model extend Zendesk2::Attributes extend Forwardable - # @return [Integer] Automatically assigned when creating events - identity :id, type: :integer - # @return [String] Has the value Comment + def self.all + @all ||= [] + end + + def self.inherited(klass) + all << klass + end + + def self.for(attributes) + event_class = "Zendesk2::Client::Ticket#{attributes["type"]}" + if klass = all.find{|k| k.name == event_class} + klass.new(attributes) + else # handle unrecognized audit events + attributes.reject{|k,v| k == :connection} + end + end + + # @return [String] has the event value attribute :type, type: :string - # @return [String] The actual comment made by the author - attribute :body, type: :string - # @return [String] The actual comment made by the author formatted to HTML - attribute :html_body, type: :string - # @return [Boolean] If this is a public comment or an internal agents only note - attribute :public, type: :boolean - # @return [Boolean] If this comment is trusted or marked as being potentially fraudulent - attribute :trusted, type: :boolean - # @return [Integer] The id of the author of this comment - attribute :author_id, type: :integer - # @return [Array] The attachments on this comment as Attachment objects - attribute :attachments, type: :array # @return [Zendesk2::Client::TicketAudit] audit that includes this event attr_accessor :ticket_audit def_delegators :ticket_audit, :created_at - - # @return [Zendesk2::Client::User] event author - def author - requires :author_id - - self.connection.users.get(self.author_id) - end end diff --git a/lib/zendesk2/client/models/ticket_audit.rb b/lib/zendesk2/client/models/ticket_audit.rb index 9e8be0f..fb65210 100644 --- a/lib/zendesk2/client/models/ticket_audit.rb +++ b/lib/zendesk2/client/models/ticket_audit.rb @@ -23,6 +23,6 @@ def ticket end def events - (self.attributes[:events] || []).map{|ae| self.connection.audit_event(ae.merge(ticket_audit: self))} + (self.attributes[:events] || []).map{|ae| Zendesk2::Client::AuditEvent.for(ae.merge(ticket_audit: self, connection: self.connection))} end end diff --git a/lib/zendesk2/client/models/ticket_change.rb b/lib/zendesk2/client/models/ticket_change.rb new file mode 100644 index 0000000..3e726e7 --- /dev/null +++ b/lib/zendesk2/client/models/ticket_change.rb @@ -0,0 +1,13 @@ +class Zendesk2::Client + class TicketChange < AuditEvent + # @return [Integer] Automatically assigned when creating events + identity :id, type: :integer + + # @return [String] The name of the field that was changed + attribute :field_name, type: :string + # @return [String] The name of the field that was changed + attribute :value, type: :string + # @return [Array] The previous value of the field that was changed + attribute :previous_value, type: :array, parser: lambda{|v, _| [*v]} + end +end diff --git a/lib/zendesk2/client/models/ticket_comment.rb b/lib/zendesk2/client/models/ticket_comment.rb new file mode 100644 index 0000000..d7606eb --- /dev/null +++ b/lib/zendesk2/client/models/ticket_comment.rb @@ -0,0 +1,26 @@ +class Zendesk2::Client + class TicketComment < AuditEvent + # @return [Integer] Automatically assigned when creating events + identity :id, type: :integer + + # @return [String] The actual comment made by the author + attribute :body, type: :string + # @return [String] The actual comment made by the author formatted to HTML + attribute :html_body, type: :string + # @return [Boolean] If this is a public comment or an internal agents only note + attribute :public, type: :boolea + # @return [Boolean] If this comment is trusted or marked as being potentially fraudulent + attribute :trusted, type: :boolean + # @return [Integer] The id of the author of this comment + attribute :author_id, type: :integer + # @return [Array] The attachments on this comment as Attachment objects + attribute :attachments, type: :array + + # @return [Zendesk2::Client::User] event author + def author + requires :author_id + + self.connection.users.get(self.author_id) + end + end +end diff --git a/lib/zendesk2/client/models/ticket_comment_privacy_change.rb b/lib/zendesk2/client/models/ticket_comment_privacy_change.rb new file mode 100644 index 0000000..7e41687 --- /dev/null +++ b/lib/zendesk2/client/models/ticket_comment_privacy_change.rb @@ -0,0 +1,19 @@ +class Zendesk2::Client + class TicketCommentPrivacyChange < AuditEvent + + # @return [integer] Automatically assigned when creating events + identity :id, type: :integer + + # @return [Integer] The id if the comment that changed privacy + attribute :comment_id, type: :integer + # @return [Boolean] Tells if the comment was made public or private + attribute :public, type: :boolean + + # @return [Zendesk2::Client::TicketComment] ticket comment pertaining to this privacy change + def comment + requires :comment_id + + self.connection.ticket_comments.get(self.comment_id) + end + end +end diff --git a/lib/zendesk2/client/models/ticket_create.rb b/lib/zendesk2/client/models/ticket_create.rb new file mode 100644 index 0000000..4346626 --- /dev/null +++ b/lib/zendesk2/client/models/ticket_create.rb @@ -0,0 +1,11 @@ +class Zendesk2::Client + class TicketCreate < AuditEvent + # @return [integer] Automatically assigned when creating events + identity :id, type: :integer + + # @return [string] The name of the field that was set + attribute :field_name, type: :string + # @return [Array] The value of the field that was set + attribute :value, parser: lambda{|v, _| [*v]} + end +end diff --git a/lib/zendesk2/client/models/ticket_notification.rb b/lib/zendesk2/client/models/ticket_notification.rb new file mode 100644 index 0000000..d0bbf1d --- /dev/null +++ b/lib/zendesk2/client/models/ticket_notification.rb @@ -0,0 +1,15 @@ +class Zendesk2::Client + class TicketNotification < AuditEvent + # @return [Integer] Automatically assigned when creating events + identity :id, type: :integer + + # @return [String] The message sent to the recipients + attribute :body, type: :string + # @return [Array] A array of simple object holding the ids and names of the recipients of this notification + attribute :recipients, type: :array + # @return [String] The subject of the message sent to the recipients + attribute :subject, type: :string + # @return [Hash] A reference to the trigger that created this notification + attribute :via + end +end diff --git a/lib/zendesk2/client/models/ticket_voice_comment.rb b/lib/zendesk2/client/models/ticket_voice_comment.rb new file mode 100644 index 0000000..20bdfdb --- /dev/null +++ b/lib/zendesk2/client/models/ticket_voice_comment.rb @@ -0,0 +1,36 @@ +class Zendesk2::Client + class TicketVoiceComment < AuditEvent + # @return [integer] Automatically assigned when creating events + identity :id, type: :integer + + # @return [Array] The attachments on this comment as Attachment objects + attribute :attachments, type: :array + # @return [Integer] The id of the author of this comment + attribute :author_id, type: :integer + # @return [String] The actual comment made by the author + attribute :body, type: :string + # @return [String] A hash of properties about the call + attribute :data, type: :string + # @return [String] A formatted version of the phone number which dialed the call + attribute :formatted_from, type: :string + # @return [String] A formatted version of the phone number which answered the call + attribute :formatted_to, type: :string + # @return [String] The actual comment made by the author formatted to HTML + attribute :html_body, type: :string + # @return [Boolean] If this is a public comment or an internal agents only note + attribute :public, type: :boolean + # @return [Boolean] If true, the ticket requester can see this comment + attribute :public, type: :boolean + # @return [Boolean] If this comment is trusted or marked as being potentially fraudulent + attribute :trusted, type: :boolean + # @return [String] Has the value VoiceComment + attribute :type, type: :string + + # @return [Zendesk2::Client::User] event author + def author + requires :author_id + + self.connection.users.get(self.author_id) + end + end +end diff --git a/lib/zendesk2/model.rb b/lib/zendesk2/model.rb index 24bda96..c403680 100644 --- a/lib/zendesk2/model.rb +++ b/lib/zendesk2/model.rb @@ -1,4 +1,4 @@ -# @abstract subclass and implement {#save!} +# @abstract subclass and implement {#save!} and {#destroy!} class Zendesk2::Model < Cistern::Model attr_accessor :errors diff --git a/spec/tickets_spec.rb b/spec/tickets_spec.rb index 924c5b3..05c807e 100644 --- a/spec/tickets_spec.rb +++ b/spec/tickets_spec.rb @@ -33,6 +33,8 @@ event = events.first event.body.should == body + event.should be_a(Zendesk2::Client::TicketComment) + event.ticket_audit.should == audit end it "lists comments" do