Class: ZendeskRails::Testing::Ticket

Inherits:
Resource
  • Object
show all
Defined in:
lib/zendesk_rails/testing.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Resource

all, clear!, #method_missing

Constructor Details

#initialize(attributes) ⇒ Ticket

Returns a new instance of Ticket.



39
40
41
42
43
44
45
46
47
48
# File 'lib/zendesk_rails/testing.rb', line 39

def initialize(attributes)
  attributes.reverse_merge!(
    created_at: Time.now,
    updated_at: Time.now,
    priority: 'low',
    status: 'new',
    comments: []
  )
  super(attributes)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class ZendeskRails::Testing::Resource

Class Method Details

.create(client_or_opts = {}, opts = nil) ⇒ Object



78
79
80
81
82
83
84
85
# File 'lib/zendesk_rails/testing.rb', line 78

def create(client_or_opts = {}, opts = nil)
  opts ||= client_or_opts
  id = (all.map(&:id).max || 0) + 1
  opts.merge!(id: id, description: opts[:comment][:value])
  ticket = new opts.slice(:subject, :requester, :description, :id)
  @all.unshift ticket
  ticket
end

.find(opts = {}) ⇒ Object



87
88
89
90
91
92
93
# File 'lib/zendesk_rails/testing.rb', line 87

def find(opts = {})
  all.find do |ticket|
    opts.map do |key, value|
      ticket.send(key) == value || ticket.send(key).to_s == value
    end.all?
  end
end

Instance Method Details

#as_commentObject



58
59
60
61
62
63
# File 'lib/zendesk_rails/testing.rb', line 58

def as_comment
  @attributes.slice(:created_at, :updated_at).merge(
    html_body: description,
    author: { name: requester[:name] }
  )
end

#comment=(opts) ⇒ Object



70
71
72
73
74
75
# File 'lib/zendesk_rails/testing.rb', line 70

def comment=(opts)
  @attributes[:comments].unshift(
    html_body: opts[:body],
    author: requester
  )
end

#comments(_opts = {}) ⇒ Object



65
66
67
68
# File 'lib/zendesk_rails/testing.rb', line 65

def comments(_opts = {})
  list = (@attributes[:comments] + [as_comment]).map { |c| Comment.new(c) }
  list.sort { |a, b| b.created_at <=> a.created_at }
end

#requester_idObject



54
55
56
# File 'lib/zendesk_rails/testing.rb', line 54

def requester_id
  @requester_id ||= rand(1..1000)
end

#saveObject



50
51
52
# File 'lib/zendesk_rails/testing.rb', line 50

def save
  true
end