Skip to content

Commit

Permalink
begin modeling specific audit events
Browse files Browse the repository at this point in the history
* updates #15 ~feature
  • Loading branch information
lanej committed Jan 26, 2013
1 parent e3bd2d2 commit 1118877
Show file tree
Hide file tree
Showing 11 changed files with 151 additions and 25 deletions.
9 changes: 8 additions & 1 deletion lib/zendesk2/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
41 changes: 19 additions & 22 deletions lib/zendesk2/client/models/audit_event.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion lib/zendesk2/client/models/ticket_audit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 13 additions & 0 deletions lib/zendesk2/client/models/ticket_change.rb
Original file line number Diff line number Diff line change
@@ -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
26 changes: 26 additions & 0 deletions lib/zendesk2/client/models/ticket_comment.rb
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions lib/zendesk2/client/models/ticket_comment_privacy_change.rb
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions lib/zendesk2/client/models/ticket_create.rb
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions lib/zendesk2/client/models/ticket_notification.rb
Original file line number Diff line number Diff line change
@@ -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
36 changes: 36 additions & 0 deletions lib/zendesk2/client/models/ticket_voice_comment.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion lib/zendesk2/model.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# @abstract subclass and implement {#save!}
# @abstract subclass and implement {#save!} and {#destroy!}
class Zendesk2::Model < Cistern::Model
attr_accessor :errors

Expand Down
2 changes: 2 additions & 0 deletions spec/tickets_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1118877

Please sign in to comment.