zammad_api 1.4.0 → 2.0.0.alpha1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +11 -0
  3. data/CHANGELOG.md +235 -0
  4. data/LICENSE.AGPL.txt +661 -0
  5. data/LICENSE.MIT.txt +7 -0
  6. data/LICENSE.md +6 -0
  7. data/README.md +794 -0
  8. data/UPGRADING.md +158 -0
  9. data/lib/zammad_api/attribute_access.rb +121 -0
  10. data/lib/zammad_api/client.rb +243 -43
  11. data/lib/zammad_api/collection.rb +233 -0
  12. data/lib/zammad_api/config.rb +239 -0
  13. data/lib/zammad_api/deep_copy.rb +43 -0
  14. data/lib/zammad_api/errors.rb +97 -35
  15. data/lib/zammad_api/resource_proxy.rb +195 -0
  16. data/lib/zammad_api/resources/base.rb +196 -93
  17. data/lib/zammad_api/resources/group.rb +12 -2
  18. data/lib/zammad_api/resources/organization.rb +12 -2
  19. data/lib/zammad_api/resources/ticket.rb +42 -19
  20. data/lib/zammad_api/resources/ticket_article.rb +14 -7
  21. data/lib/zammad_api/resources/ticket_article_attachment.rb +29 -13
  22. data/lib/zammad_api/resources/ticket_priority.rb +10 -2
  23. data/lib/zammad_api/resources/ticket_state.rb +9 -2
  24. data/lib/zammad_api/resources/user.rb +16 -2
  25. data/lib/zammad_api/resources.rb +13 -13
  26. data/lib/zammad_api/response.rb +52 -0
  27. data/lib/zammad_api/transport.rb +212 -40
  28. data/lib/zammad_api/version.rb +4 -1
  29. data/lib/zammad_api.rb +16 -3
  30. data/sig/zammad_api/attribute_access.rbs +31 -0
  31. data/sig/zammad_api/client.rbs +42 -0
  32. data/sig/zammad_api/collection.rbs +46 -0
  33. data/sig/zammad_api/config.rbs +69 -0
  34. data/sig/zammad_api/deep_copy.rbs +7 -0
  35. data/sig/zammad_api/errors.rbs +61 -0
  36. data/sig/zammad_api/resource_proxy.rbs +41 -0
  37. data/sig/zammad_api/resources/base.rbs +66 -0
  38. data/sig/zammad_api/resources/resources.rbs +201 -0
  39. data/sig/zammad_api/response.rbs +16 -0
  40. data/sig/zammad_api/transport.rbs +47 -0
  41. data/sig/zammad_api.rbs +3 -0
  42. metadata +69 -15
  43. data/lib/zammad_api/dispatcher.rb +0 -12
  44. data/lib/zammad_api/json_helper.rb +0 -11
  45. data/lib/zammad_api/list_all.rb +0 -11
  46. data/lib/zammad_api/list_base.rb +0 -84
  47. data/lib/zammad_api/list_search.rb +0 -11
  48. data/lib/zammad_api/log.rb +0 -18
@@ -0,0 +1,195 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ZammadAPI
4
+ # Entry point for working with one kind of Zammad record.
5
+ #
6
+ # Obtained from {Client}, e.g. +client.tickets+, and exposes the operations
7
+ # that are not tied to an individual record.
8
+ #
9
+ # @example
10
+ # client.groups.find(1)
11
+ # client.groups.create(name: 'Support')
12
+ # client.groups.all.each { |group| puts group.name }
13
+ # client.groups.order(:name).first(5)
14
+ # client.groups.search('support').first
15
+ #
16
+ # A proxy is +Enumerable+ over {#all}, so the collection surface is reachable
17
+ # without naming it:
18
+ #
19
+ # @example
20
+ # client.groups.each { |group| puts group.name }
21
+ # client.groups.first(5)
22
+ # client.groups.map(&:name)
23
+ # client.groups.find_each(batch_size: 500) { |group| archive(group) }
24
+ #
25
+ # {#find} takes an id and is not +Enumerable#find+; +detect+ is still the
26
+ # block form.
27
+ class ResourceProxy
28
+ include Enumerable
29
+
30
+ # @return [Class] the resource class this proxy operates on
31
+ attr_reader :resource_class
32
+
33
+ # @api private
34
+ def initialize(transport, resource_class)
35
+ @transport = transport
36
+ @resource_class = resource_class
37
+ end
38
+
39
+ # Builds an unsaved record.
40
+ #
41
+ # @param attributes [Hash]
42
+ # @return [Resources::Base]
43
+ def new(attributes = {})
44
+ resource_class.new(@transport, attributes)
45
+ end
46
+
47
+ # Builds and immediately saves a record.
48
+ #
49
+ # This raises when Zammad rejects the attributes. Use +new+ and
50
+ # {Resources::Base#save} to branch on a validation failure instead.
51
+ #
52
+ # @param attributes [Hash]
53
+ # @return [Resources::Base]
54
+ # @raise [ResponseError] when Zammad rejected the request
55
+ def create(attributes = {})
56
+ record = new(attributes)
57
+ record.save!
58
+ record
59
+ end
60
+
61
+ # Fetches a single record by id.
62
+ #
63
+ # @param id [Integer, String]
64
+ # @return [Resources::Base]
65
+ # @raise [NotFoundError] when no such record exists
66
+ def find(id)
67
+ response = @transport.get(resource_class.member_path(id), query: { expand: true })
68
+
69
+ resource_class.from_response(@transport, response.decoded(:object))
70
+ end
71
+
72
+ # The first record carrying all of these attribute values, or nil.
73
+ #
74
+ # An +id:+ is read with {#find}, in one request. Otherwise, where the
75
+ # resource can be searched, the longest String value is the
76
+ # search term and the hits are checked against every value. Otherwise
77
+ # the index is walked until a record matches. Values are compared
78
+ # exactly, and a record that does not carry an attribute does not match it.
79
+ #
80
+ # @example
81
+ # client.users.find_by(email: 'someone@example.com')
82
+ # client.ticket_states.find_by(name: 'open')
83
+ #
84
+ # @param params [Hash{Symbol => Object}]
85
+ # @return [Resources::Base, nil]
86
+ # @raise [ArgumentError] when no attribute was given
87
+ def find_by(**params)
88
+ raise ArgumentError, 'find_by needs at least one attribute to match' if params.empty?
89
+ return find_by_id(params) if params.key?(:id)
90
+
91
+ term = params.values.grep(String).reject { it.strip.empty? }.max_by(&:length)
92
+ candidates = resource_class.searchable? && term ? search(term) : all
93
+
94
+ candidates.detect { |record| params.all? { |key, value| record.key?(key) && record[key] == value } }
95
+ end
96
+
97
+ # Like {#find_by}, but raises when nothing matched.
98
+ #
99
+ # @param params [Hash{Symbol => Object}]
100
+ # @return [Resources::Base]
101
+ # @raise [NotFoundError] when no record matched
102
+ def find_by!(**params)
103
+ find_by(**params) || raise(NotFoundError, "no #{resource_class.name} matched #{params.keys.join(' and ')}")
104
+ end
105
+
106
+ # @param id [Integer, String]
107
+ # @return [Boolean] whether a record with that id exists and can be read
108
+ def exists?(id)
109
+ find(id)
110
+ true
111
+ rescue NotFoundError
112
+ false
113
+ end
114
+
115
+ # Deletes a record by id, without reading it first.
116
+ #
117
+ # @param id [Integer, String]
118
+ # @return [true]
119
+ # @raise [NotFoundError] when no such record exists
120
+ def destroy(id)
121
+ @transport.delete(resource_class.member_path(id))
122
+ true
123
+ end
124
+
125
+ # Every record of this kind, as a lazily paginated collection.
126
+ #
127
+ # @return [Collection]
128
+ def all = Collection.new(@transport, resource_class)
129
+
130
+ # @!group Collection shorthands, each forwarded to {#all}
131
+
132
+ # A forward hands on a block or a count that may be nil, which is what picks
133
+ # the overload on the collection, and Steep cannot follow that through.
134
+
135
+ # (see Collection#order)
136
+ def order(*attributes, **directions) = all.order(*attributes, **directions)
137
+
138
+ # (see Collection#each)
139
+ def each(&block) = all.each(&block) # steep:ignore BlockTypeMismatch
140
+
141
+ # (see Collection#find_each)
142
+ def find_each(batch_size: nil, &block) = all.find_each(batch_size: batch_size, &block) # steep:ignore BlockTypeMismatch
143
+
144
+ # (see Collection#in_batches)
145
+ def in_batches(of: nil, &block) = all.in_batches(of: of, &block) # steep:ignore BlockTypeMismatch
146
+
147
+ # (see Collection#page)
148
+ def page(number, per_page: nil) = all.page(number, per_page: per_page)
149
+
150
+ # (see Collection#pluck)
151
+ def pluck(*keys) = all.pluck(*keys)
152
+
153
+ # (see Collection#empty?)
154
+ def empty? = all.empty?
155
+
156
+ # (see Collection#first)
157
+ def first(count = nil) = all.first(count) # steep:ignore ArgumentTypeMismatch
158
+
159
+ # @!endgroup
160
+
161
+ # Records matching a Zammad search term, as a lazily paginated collection.
162
+ #
163
+ # Keywords are passed to the search endpoint as they are, for what it
164
+ # narrows by besides the term, such as +condition:+ and +ids:+, or
165
+ # +role_ids:+ for users.
166
+ #
167
+ # @example
168
+ # client.tickets.search('state.name:open').first(10)
169
+ # client.users.search('smith', role_ids: [2])
170
+ #
171
+ # @param term [String] a Zammad search query; +'*'+ matches every record
172
+ # @param filters [Hash{Symbol => Object}] further search parameters
173
+ # @return [Collection]
174
+ # @raise [Error] for a resource Zammad routes no search endpoint for
175
+ # @raise [ArgumentError] for a blank term, which Zammad reads as no term
176
+ # and answers with every record
177
+ def search(term, **filters)
178
+ raise Error, "Zammad routes no search endpoint for #{resource_class.name}: look a record up with find_by, which walks the records instead" if !resource_class.searchable?
179
+ raise ArgumentError, "search needs a non-empty query string. Search for '*' to ask for every record." if !term.is_a?(String) || term.strip.empty?
180
+
181
+ Collection.new(@transport, resource_class, search: true, query: { query: term, **filters })
182
+ end
183
+
184
+ def inspect = "#<#{self.class.name} #{resource_class.name}>"
185
+
186
+ private
187
+
188
+ def find_by_id(params)
189
+ record = find(params.fetch(:id))
190
+ record if params.except(:id).all? { |key, value| record.key?(key) && record[key] == value }
191
+ rescue NotFoundError
192
+ nil
193
+ end
194
+ end
195
+ end
@@ -1,140 +1,243 @@
1
- require 'cgi'
2
- require 'zammad_api/json_helper'
3
- require 'zammad_api/transport'
1
+ # frozen_string_literal: true
4
2
 
5
3
  module ZammadAPI
6
4
  module Resources
5
+ # Shared behaviour for every Zammad record.
6
+ #
7
+ # Attributes are read and written through +method_missing+, because Zammad
8
+ # records can carry administrator-defined custom attributes:
9
+ #
10
+ # group = client.groups.find(1)
11
+ # group.name # read
12
+ # group.name = 'Support' # stage a change
13
+ # group.changed? # => true
14
+ # group.save # persist
7
15
  class Base
8
- include ZammadAPI::JsonHelper
9
- extend ZammadAPI::JsonHelper
10
-
11
- attr_accessor :new_instance, :url, :attributes
12
- attr_reader :changes
16
+ include AttributeAccess
17
+
18
+ # Largest page size Zammad's generic index endpoints serve. Resources
19
+ # whose endpoint caps lower declare +max_per_page+.
20
+ DEFAULT_MAX_PER_PAGE = 1000
21
+
22
+ # The validation failure from the most recent {#save}, so that a +false+
23
+ # return value can be acted on.
24
+ #
25
+ # @return [ValidationError, nil]
26
+ attr_reader :error
27
+
28
+ class << self
29
+ # Each declaration defines the reader of the same setting on this
30
+ # class, which a subclass inherits and can declare again.
31
+
32
+ # Declares the API path of this resource, relative to the instance URL.
33
+ #
34
+ # @param value [String]
35
+ # @return [void]
36
+ def path(value) = define_singleton_method(:resource_path) { value }
37
+
38
+ # Declares whether Zammad routes a +/search+ endpoint for this resource.
39
+ #
40
+ # @param value [Boolean]
41
+ # @return [void]
42
+ def searchable(value) = define_singleton_method(:searchable?) { value }
43
+
44
+ # Declares the largest page size this resource's index endpoint serves.
45
+ #
46
+ # @param value [Integer]
47
+ # @return [void]
48
+ def max_per_page(value) = define_singleton_method(:page_limit) { value }
49
+
50
+ # Declares whether this resource's index endpoint reads +sort_by+ and
51
+ # +order_by+. One that does not is refused by {Collection#order}.
52
+ #
53
+ # @param value [Boolean]
54
+ # @return [void]
55
+ def sortable(value) = define_singleton_method(:sortable?) { value }
56
+
57
+ # @return [String] the API path of this resource
58
+ # @raise [Error] when the resource declares none
59
+ def resource_path = raise(Error, "#{name} does not declare an API path")
60
+
61
+ # @return [Boolean] whether Zammad routes a +/search+ endpoint here
62
+ def searchable? = false
63
+
64
+ # @return [Integer] the largest page size this endpoint serves
65
+ def page_limit = DEFAULT_MAX_PER_PAGE
66
+
67
+ # @return [Boolean] whether the index endpoint reads +sort_by+ and +order_by+
68
+ def sortable? = true
69
+
70
+ # The API path of one record of this kind, with the id escaped.
71
+ #
72
+ # @api private
73
+ # @param id [Integer, String]
74
+ # @return [String]
75
+ def member_path(id) = "#{resource_path}/#{Transport.escape_path_segment(id)}"
76
+
77
+ # Builds a record that is already stored in Zammad.
78
+ #
79
+ # @api private
80
+ # @param transport [Transport]
81
+ # @param attributes [Hash]
82
+ # @return [Base]
83
+ def from_response(transport, attributes)
84
+ record = new(transport, attributes)
85
+ record.send(:mark_persisted!)
86
+ record
87
+ end
88
+ end
13
89
 
90
+ # @param transport [Transport]
91
+ # @param attributes [Hash, nil] or what converts to a Hash, such as
92
+ # permitted `ActionController::Parameters`
14
93
  def initialize(transport, attributes = {})
15
- @new_instance = true
16
- @transport = transport
17
- @changes = {}
18
- @url = self.class.get_url
19
-
20
- if attributes.nil?
21
- attributes = {}
22
- end
23
- @attributes = attributes
24
- symbolize_keys_deep!(@attributes)
94
+ @transport = transport
95
+ @attributes = frozen_attributes(Hash(attributes)) # steep:ignore UnresolvedOverloading
96
+ @baseline = @attributes
97
+ @new_record = true
98
+ @destroyed = false
99
+ @error = nil
25
100
  end
26
101
 
27
- def method_missing(method, *args)
28
- return @attributes[method] if !method.to_s.end_with?('=')
102
+ # @return [Boolean] whether this record has not been stored yet
103
+ def new_record? = @new_record
29
104
 
30
- method = method.to_s[0, method.length - 1].to_sym
31
- @changes[method] = [@attributes[method], args[0]]
32
- @attributes[method] = args[0]
33
- nil
34
- end
105
+ # Whether this record exists in Zammad: false both before the first save
106
+ # and after {#destroy}.
107
+ #
108
+ # @return [Boolean]
109
+ def persisted? = !@new_record && !@destroyed
35
110
 
36
- def new_record?
37
- @new_instance
38
- end
111
+ # @return [Boolean] whether {#destroy} removed this record from Zammad
112
+ def destroyed? = @destroyed
39
113
 
40
- def changed?
41
- !@changes.to_h.empty?
114
+ # Staged changes as +attribute => [old_value, new_value]+.
115
+ #
116
+ # @return [Hash{Symbol => Array(Object, Object)}]
117
+ def changes
118
+ @attributes.filter_map { |key, value| [key, [@baseline[key], value].freeze] if !@baseline.key?(key) || @baseline[key] != value }.to_h.freeze
42
119
  end
43
120
 
44
- def destroy
45
- response = @transport.delete(url: "#{@url}/#{@attributes[:id]}")
46
- return true if response.status == 200
121
+ # @return [Boolean] whether there are staged changes
122
+ def changed? = !changes.empty?
47
123
 
48
- raise ResponseError.from(response, operation: 'destroy object', resource_class: self.class)
124
+ # Stages several changes at once.
125
+ #
126
+ # @param attributes [Hash]
127
+ # @return [self]
128
+ def assign_attributes(attributes)
129
+ Hash(attributes).each { |key, value| write_attribute(DeepCopy.symbolize(key), value) }
130
+ self
49
131
  end
50
132
 
133
+ # Persists this record, creating it if it is new.
134
+ #
135
+ # @return [Boolean] false when Zammad rejected the attributes; see {#error}
136
+ # @raise [ResponseError] for any other failure
51
137
  def save
52
- attributes = saved_attributes
53
- symbolize_keys_deep!(attributes)
54
- attributes.delete(:article)
55
- @attributes = attributes
56
- @new_instance = false
57
- @changes = {}
58
- true
59
- end
60
-
61
- def self.get_url
62
- @url
63
- end
138
+ save!
139
+ rescue ValidationError => e
140
+ @error = e
141
+ false
142
+ end
143
+
144
+ # Persists this record, creating it if it is new. An existing record
145
+ # sends only the attributes that changed, and nothing when none did.
146
+ #
147
+ # @return [true]
148
+ # @raise [ValidationError] when Zammad rejected the attributes
149
+ def save!
150
+ @error = nil
151
+
152
+ if new_record?
153
+ response = transport.post(self.class.resource_path, query: { expand: true }, body: @attributes)
154
+ else
155
+ unsaved = changes.transform_values(&:last)
156
+ return true if unsaved.empty?
157
+
158
+ response = transport.put(member_path, query: { expand: true }, body: unsaved)
159
+ end
64
160
 
65
- def self.url(value)
66
- @url = value
161
+ load_response!(response)
162
+ true
67
163
  end
68
164
 
69
- def self.all(transport, _)
70
- ZammadAPI::ListAll.new(self, transport, per_page: 100)
165
+ # Stages the attributes and saves.
166
+ #
167
+ # @param attributes [Hash]
168
+ # @return [Boolean] see {#save}
169
+ def update(attributes)
170
+ assign_attributes(attributes)
171
+ save
71
172
  end
72
173
 
73
- def self.search(transport, parameter)
74
- ZammadAPI::ListSearch.new(self, transport, parameter)
174
+ # Stages the attributes and saves.
175
+ #
176
+ # @param attributes [Hash]
177
+ # @return [true]
178
+ # @raise [ValidationError] see {#save!}
179
+ def update!(attributes)
180
+ assign_attributes(attributes)
181
+ save!
75
182
  end
76
183
 
77
- def self.find(transport, id)
78
- response = transport.get(url: "#{@url}/#{id}?expand=true")
79
- if response.status != 200
80
- raise ResponseError.from(response, operation: 'find object', resource_class: self)
81
- end
184
+ # Reads this record again, dropping staged changes.
185
+ #
186
+ # @return [self]
187
+ # @raise [Error] when the record is new
188
+ def reload
189
+ raise_if_new!('reload')
82
190
 
83
- data = safe_json_parse(response.body)
84
- item = new(transport, data)
85
- item.new_instance = false
86
- item
191
+ load_response!(transport.get(member_path, query: { expand: true }))
192
+ self
87
193
  end
88
194
 
89
- def self.create(transport, data)
90
- item = new(transport, data)
91
- item.save
92
- item
93
- end
195
+ # Deletes this record in Zammad.
196
+ #
197
+ # @return [true]
198
+ # @raise [Error] when the record is new
199
+ def destroy
200
+ raise_if_new!('destroy')
94
201
 
95
- def self.destroy(transport, id)
96
- item = find(transport, id)
97
- item.destroy
202
+ transport.delete(member_path)
203
+ @destroyed = true
98
204
  true
99
205
  end
100
206
 
207
+ def inspect = "#<#{self.class.name} id=#{id.inspect} new_record=#{new_record?}#{' destroyed=true' if destroyed?} attributes=#{Transport.redact(attributes).inspect}>"
208
+
101
209
  private
102
210
 
103
- def saved_attributes
104
- return save_new if @new_instance
211
+ attr_reader :transport
105
212
 
106
- save_existing
213
+ def mark_persisted!
214
+ @new_record = false
107
215
  end
108
216
 
109
- def save_new
110
- response = @transport.post(url: "#{@url}?expand=true", params: @attributes)
111
- return safe_json_parse(response.body) if response.status == 201
217
+ def load_response!(response)
218
+ @attributes = frozen_attributes(response.decoded(:object))
219
+ @baseline = @attributes
220
+ @new_record = false
221
+ end
112
222
 
113
- save_error(response)
223
+ def raise_if_new!(operation)
224
+ raise Error, "#{self.class.name} has not been saved, so there is nothing to #{operation}" if new_record?
114
225
  end
115
226
 
116
- def save_existing
117
- attributes_to_post = {}
118
- @changes.each do |name, values|
119
- attributes_to_post[name] = values[1]
120
- end
121
- response = @transport.put(url: "#{@url}/#{@attributes[:id]}?expand=true", params: attributes_to_post)
122
- return safe_json_parse(response.body) if response.status == 200
227
+ def writable_attributes? = true
123
228
 
124
- save_error(response)
125
- end
229
+ def write_attribute(key, value)
230
+ raise Error, "#{self.class.name}#id cannot be changed: it is what addresses this record" if key == :id
126
231
 
127
- def save_error(response)
128
- raise ResponseError.from(response, operation: 'save object', resource_class: self.class)
232
+ staged = frozen_attributes(value)
233
+ @attributes = @attributes.merge(key => staged).freeze
234
+ staged
129
235
  end
130
236
 
131
- def symbolize_keys_deep!(hash)
132
- hash.keys.each do |key|
133
- key_symbol = key.respond_to?(:to_sym) ? key.to_sym : key
134
- hash[key_symbol] = hash.delete key # Preserve order even when key == key_symbol
237
+ def member_path = self.class.member_path(require_id!)
135
238
 
136
- symbolize_keys_deep! hash[key_symbol] if hash[key_symbol].is_a? Hash
137
- end
239
+ def require_id!
240
+ id || raise(Error, "#{self.class.name} has no id, save it first")
138
241
  end
139
242
  end
140
243
  end
@@ -1,3 +1,13 @@
1
- class ZammadAPI::Resources::Group < ZammadAPI::Resources::Base
2
- url '/api/v1/groups'
1
+ # frozen_string_literal: true
2
+
3
+ module ZammadAPI
4
+ module Resources
5
+ # A group: what a ticket is assigned to, and what agents are given access by.
6
+ # Reached through {Client#groups}.
7
+ class Group < Base
8
+ path 'api/v1/groups'
9
+
10
+ searchable true
11
+ end
12
+ end
3
13
  end
@@ -1,3 +1,13 @@
1
- class ZammadAPI::Resources::Organization < ZammadAPI::Resources::Base
2
- url '/api/v1/organizations'
1
+ # frozen_string_literal: true
2
+
3
+ module ZammadAPI
4
+ module Resources
5
+ # An organization that customers belong to. Reached through
6
+ # {Client#organizations}.
7
+ class Organization < Base
8
+ path 'api/v1/organizations'
9
+
10
+ searchable true
11
+ end
12
+ end
3
13
  end
@@ -1,25 +1,48 @@
1
- class ZammadAPI::Resources::Ticket < ZammadAPI::Resources::Base
2
- url '/api/v1/tickets'
1
+ # frozen_string_literal: true
3
2
 
4
- def articles
5
- response = @transport.get(url: "/api/v1/ticket_articles/by_ticket/#{id}?expand=true")
6
- if response.status != 200
7
- raise ZammadAPI::ResponseError.from(response, operation: 'get articles', resource_class: self.class)
8
- end
3
+ module ZammadAPI
4
+ module Resources
5
+ # A ticket. Reached through {Client#tickets}, and its articles through
6
+ # {#articles} and {#add_article}.
7
+ class Ticket < Base
8
+ path 'api/v1/tickets'
9
9
 
10
- data = safe_json_parse(response.body)
10
+ # TicketsController#index hardcodes `reorder(id: :asc)`, so it reads nothing
11
+ # but the paging - not even sort_by.
12
+ sortable false
11
13
 
12
- data.collect do |raw|
13
- item = ZammadAPI::Resources::TicketArticle.new(@transport, raw)
14
- item.new_instance = false
15
- item
16
- end
17
- end
14
+ # /api/v1/tickets caps the page size at 100, unlike the generic index
15
+ # endpoints, see TicketsController#index.
16
+ max_per_page 100
17
+
18
+ searchable true
18
19
 
19
- def article(data)
20
- data[:ticket_id] = @attributes[:id]
21
- item = ZammadAPI::Resources::TicketArticle.new(@transport, data)
22
- item.save
23
- item
20
+ # Every article of this ticket, read in one request on each call.
21
+ #
22
+ # @return [Array<TicketArticle>]
23
+ # @raise [Error] when the ticket has no id yet
24
+ # @raise [ResponseError] when Zammad rejected the request
25
+ def articles
26
+ path = "#{TicketArticle.resource_path}/by_ticket/#{Transport.escape_path_segment(require_id!)}"
27
+ response = transport.get(path, query: { expand: true })
28
+
29
+ response.decoded(:array).map { TicketArticle.from_response(transport, it) }
30
+ end
31
+
32
+ # Adds an article to this ticket.
33
+ #
34
+ # @example
35
+ # ticket.add_article(subject: 'Update', body: 'Reset link sent.', type: 'note')
36
+ #
37
+ # @param attributes [Hash] article attributes, e.g. +body:+, +type:+
38
+ # @return [TicketArticle] the created article
39
+ # @raise [Error] when the ticket has no id yet
40
+ # @raise [ResponseError] when Zammad rejected the request
41
+ def add_article(attributes = {})
42
+ record = TicketArticle.new(transport, Hash(attributes).merge(ticket_id: require_id!))
43
+ record.save!
44
+ record
45
+ end
46
+ end
24
47
  end
25
48
  end
@@ -1,11 +1,18 @@
1
- class ZammadAPI::Resources::TicketArticle < ZammadAPI::Resources::Base
2
- url '/api/v1/ticket_articles'
1
+ # frozen_string_literal: true
3
2
 
4
- def attachments
5
- @attributes[:attachments].collect do |raw|
6
- raw[:ticket_id] = @attributes[:ticket_id]
7
- raw[:article_id] = @attributes[:id]
8
- ZammadAPI::Resources::TicketArticleAttachment.new(@transport, raw)
3
+ module ZammadAPI
4
+ module Resources
5
+ # An article of a ticket: an e-mail, a note, a phone call. Reached through
6
+ # {Ticket#articles}, or by id through {Client#ticket_articles}.
7
+ class TicketArticle < Base
8
+ path 'api/v1/ticket_articles'
9
+
10
+ # @return [Array<TicketArticleAttachment>] the article's attachments
11
+ def attachments
12
+ (attributes[:attachments] || []).map do |raw|
13
+ TicketArticleAttachment.new(transport, raw.merge(ticket_id: attributes[:ticket_id], article_id: id))
14
+ end
15
+ end
9
16
  end
10
17
  end
11
18
  end