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.
- checksums.yaml +4 -4
- data/.yardopts +11 -0
- data/CHANGELOG.md +235 -0
- data/LICENSE.AGPL.txt +661 -0
- data/LICENSE.MIT.txt +7 -0
- data/LICENSE.md +6 -0
- data/README.md +794 -0
- data/UPGRADING.md +158 -0
- data/lib/zammad_api/attribute_access.rb +121 -0
- data/lib/zammad_api/client.rb +243 -43
- data/lib/zammad_api/collection.rb +233 -0
- data/lib/zammad_api/config.rb +239 -0
- data/lib/zammad_api/deep_copy.rb +43 -0
- data/lib/zammad_api/errors.rb +97 -35
- data/lib/zammad_api/resource_proxy.rb +195 -0
- data/lib/zammad_api/resources/base.rb +196 -93
- data/lib/zammad_api/resources/group.rb +12 -2
- data/lib/zammad_api/resources/organization.rb +12 -2
- data/lib/zammad_api/resources/ticket.rb +42 -19
- data/lib/zammad_api/resources/ticket_article.rb +14 -7
- data/lib/zammad_api/resources/ticket_article_attachment.rb +29 -13
- data/lib/zammad_api/resources/ticket_priority.rb +10 -2
- data/lib/zammad_api/resources/ticket_state.rb +9 -2
- data/lib/zammad_api/resources/user.rb +16 -2
- data/lib/zammad_api/resources.rb +13 -13
- data/lib/zammad_api/response.rb +52 -0
- data/lib/zammad_api/transport.rb +212 -40
- data/lib/zammad_api/version.rb +4 -1
- data/lib/zammad_api.rb +16 -3
- data/sig/zammad_api/attribute_access.rbs +31 -0
- data/sig/zammad_api/client.rbs +42 -0
- data/sig/zammad_api/collection.rbs +46 -0
- data/sig/zammad_api/config.rbs +69 -0
- data/sig/zammad_api/deep_copy.rbs +7 -0
- data/sig/zammad_api/errors.rbs +61 -0
- data/sig/zammad_api/resource_proxy.rbs +41 -0
- data/sig/zammad_api/resources/base.rbs +66 -0
- data/sig/zammad_api/resources/resources.rbs +201 -0
- data/sig/zammad_api/response.rbs +16 -0
- data/sig/zammad_api/transport.rbs +47 -0
- data/sig/zammad_api.rbs +3 -0
- metadata +69 -15
- data/lib/zammad_api/dispatcher.rb +0 -12
- data/lib/zammad_api/json_helper.rb +0 -11
- data/lib/zammad_api/list_all.rb +0 -11
- data/lib/zammad_api/list_base.rb +0 -84
- data/lib/zammad_api/list_search.rb +0 -11
- 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
|
-
|
|
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
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
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
|
-
@
|
|
16
|
-
@
|
|
17
|
-
@
|
|
18
|
-
@
|
|
19
|
-
|
|
20
|
-
|
|
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
|
-
|
|
28
|
-
|
|
102
|
+
# @return [Boolean] whether this record has not been stored yet
|
|
103
|
+
def new_record? = @new_record
|
|
29
104
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
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
|
-
|
|
37
|
-
|
|
38
|
-
end
|
|
111
|
+
# @return [Boolean] whether {#destroy} removed this record from Zammad
|
|
112
|
+
def destroyed? = @destroyed
|
|
39
113
|
|
|
40
|
-
|
|
41
|
-
|
|
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
|
-
|
|
45
|
-
|
|
46
|
-
return true if response.status == 200
|
|
121
|
+
# @return [Boolean] whether there are staged changes
|
|
122
|
+
def changed? = !changes.empty?
|
|
47
123
|
|
|
48
|
-
|
|
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
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
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
|
-
|
|
66
|
-
|
|
161
|
+
load_response!(response)
|
|
162
|
+
true
|
|
67
163
|
end
|
|
68
164
|
|
|
69
|
-
|
|
70
|
-
|
|
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
|
-
|
|
74
|
-
|
|
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
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
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
|
-
|
|
84
|
-
|
|
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
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
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
|
-
|
|
96
|
-
|
|
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
|
-
|
|
104
|
-
return save_new if @new_instance
|
|
211
|
+
attr_reader :transport
|
|
105
212
|
|
|
106
|
-
|
|
213
|
+
def mark_persisted!
|
|
214
|
+
@new_record = false
|
|
107
215
|
end
|
|
108
216
|
|
|
109
|
-
def
|
|
110
|
-
|
|
111
|
-
|
|
217
|
+
def load_response!(response)
|
|
218
|
+
@attributes = frozen_attributes(response.decoded(:object))
|
|
219
|
+
@baseline = @attributes
|
|
220
|
+
@new_record = false
|
|
221
|
+
end
|
|
112
222
|
|
|
113
|
-
|
|
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
|
|
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
|
-
|
|
125
|
-
|
|
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
|
-
|
|
128
|
-
|
|
232
|
+
staged = frozen_attributes(value)
|
|
233
|
+
@attributes = @attributes.merge(key => staged).freeze
|
|
234
|
+
staged
|
|
129
235
|
end
|
|
130
236
|
|
|
131
|
-
def
|
|
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
|
-
|
|
137
|
-
|
|
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
|
-
|
|
2
|
-
|
|
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
|
-
|
|
2
|
-
|
|
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
|
-
|
|
2
|
-
url '/api/v1/tickets'
|
|
1
|
+
# frozen_string_literal: true
|
|
3
2
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
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
|
-
|
|
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
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
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
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
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
|
-
|
|
2
|
-
url '/api/v1/ticket_articles'
|
|
1
|
+
# frozen_string_literal: true
|
|
3
2
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
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
|