zammad_api 1.3.1 → 1.4.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e210909fb0d53e8184097f0ac41021ae674a5981b80e4e405631d33d6d07623e
4
- data.tar.gz: 83e7ac5be2b238ce682b9f478d0bc7dbab3a00308367a01b95e5fc3c9ee586f5
3
+ metadata.gz: 60178628e519723ca90e772769e9b61e615d152bdb825210b35e8233b9e15a16
4
+ data.tar.gz: 92fe4e55cf8230790ce87735120f39934ac9313064f67983e052d7b2fde6126f
5
5
  SHA512:
6
- metadata.gz: c8122abe30faebe219bea0e71ac5220a060b6c2f1ac08e2d86783987c4de5280daccd9341627a49da41e2e9eb24d88e7db84350b21d0f9f47feda364066deb8b
7
- data.tar.gz: 72f47ce52a5f80a4a17f56bb652324cebee7b07c32ae944901065b09b5850d634081b0ae7278327074f6e546c9be6a68714ce842c078979c13294650f9b771f9
6
+ metadata.gz: cb06e3507b9598274739a30aea5e109e4965444f521b925c2c0b949929b786a2cd80b20dbce2ac29323614ac48a1d8bb008b993ae75cc31f7733e63adf4e7b24
7
+ data.tar.gz: 7bfe654612c6be190f5270b7fb311986e566458d4b6529683347b89d4a39718751702a61ee71904c1deec0d169278d274febe3d8ec62bcbdd8c31cc34122ebbe
@@ -31,7 +31,7 @@ module ZammadAPI
31
31
  begin
32
32
  class_object = Kernel.const_get(class_name)
33
33
  rescue
34
- raise "Resource for #{method} does not exist"
34
+ raise ResourceNotFoundError, "Resource for #{method} does not exist"
35
35
  end
36
36
  ZammadAPI::Dispatcher.new(@transport, class_object)
37
37
  end
@@ -39,20 +39,20 @@ module ZammadAPI
39
39
  private
40
40
 
41
41
  def check_config
42
- raise 'missing url in config' if !@config[:url]
43
- raise 'config url needs to start with http:// or https://' if !%r{^(http|https)://}.match?(@config[:url])
42
+ raise ConfigurationError, 'missing url in config' if !@config[:url]
43
+ raise ConfigurationError, 'config url needs to start with http:// or https://' if !%r{^(http|https)://}.match?(@config[:url])
44
44
 
45
45
  # check for token auth
46
46
  return if @config[:http_token] && !@config[:http_token].empty?
47
47
  return if @config[:oauth2_token] && !@config[:oauth2_token].empty?
48
48
 
49
49
  if !@config[:user] || @config[:user].empty?
50
- raise 'missing user in config'
50
+ raise ConfigurationError, 'missing user in config'
51
51
  end
52
52
 
53
53
  return if @config[:password] && !@config[:password].empty?
54
54
 
55
- raise 'missing password in config'
55
+ raise ConfigurationError, 'missing password in config'
56
56
  end
57
57
 
58
58
  def modulize(string)
@@ -0,0 +1,62 @@
1
+ require 'json'
2
+
3
+ module ZammadAPI
4
+ class Error < RuntimeError; end
5
+
6
+ class ConfigurationError < Error; end
7
+
8
+ class ResourceNotFoundError < Error; end
9
+
10
+ class ResponseError < Error
11
+ attr_reader :response, :operation, :resource_class
12
+
13
+ def initialize(operation:, response:, resource_class: nil)
14
+ @operation = operation
15
+ @response = response
16
+ @resource_class = resource_class
17
+ super(default_message)
18
+ end
19
+
20
+ def status
21
+ response&.status
22
+ end
23
+
24
+ def body
25
+ response&.body
26
+ end
27
+
28
+ def self.from(response, **kwargs)
29
+ klass = if response.nil?
30
+ self
31
+ elsif response.status >= 500
32
+ ServerError
33
+ else
34
+ ClientError
35
+ end
36
+ klass.new(response: response, **kwargs)
37
+ end
38
+
39
+ private
40
+
41
+ def default_message
42
+ subject = resource_class ? "#{operation} (#{resource_class.name})" : operation
43
+ "Can't #{subject}: #{detail}"
44
+ end
45
+
46
+ def detail
47
+ body_error || "HTTP #{status}"
48
+ end
49
+
50
+ def body_error
51
+ return nil if body.to_s.strip.empty?
52
+
53
+ parsed = JSON.parse(body)
54
+ parsed.is_a?(Hash) ? parsed['error'] : nil
55
+ rescue JSON::ParserError
56
+ nil
57
+ end
58
+ end
59
+
60
+ class ClientError < ResponseError; end
61
+ class ServerError < ResponseError; end
62
+ end
@@ -62,11 +62,12 @@ module ZammadAPI
62
62
  }.join('&')
63
63
 
64
64
  response = @transport.get(url: url)
65
- data = safe_json_parse(response.body)
66
65
  if response.status != 200
67
- raise "Can't get .#{request} of object (#{@resource.class.name}): #{data['error']}"
66
+ raise ResponseError.from(response, operation: "get .#{request} of object", resource_class: @resource.class)
68
67
  end
69
68
 
69
+ data = safe_json_parse(response.body)
70
+
70
71
  list = []
71
72
  data.each do |local_data|
72
73
  item = @resource.new(@transport, local_data)
@@ -77,7 +78,7 @@ module ZammadAPI
77
78
  end
78
79
 
79
80
  def perform_request(_parameter)
80
- raise "no perform_request implementation for #{self.class.name} found"
81
+ raise Error, "no perform_request implementation for #{self.class.name} found"
81
82
  end
82
83
  end
83
84
  end
@@ -38,17 +38,14 @@ module ZammadAPI
38
38
  end
39
39
 
40
40
  def changed?
41
- @changes.present?
41
+ !@changes.to_h.empty?
42
42
  end
43
43
 
44
44
  def destroy
45
45
  response = @transport.delete(url: "#{@url}/#{@attributes[:id]}")
46
- if response.body.to_s != '' && response.body.to_s != ' '
47
- data = safe_json_parse(response.body)
48
- end
49
46
  return true if response.status == 200
50
47
 
51
- raise "Can't destroy object (#{self.class.name}): #{data['error']}"
48
+ raise ResponseError.from(response, operation: 'destroy object', resource_class: self.class)
52
49
  end
53
50
 
54
51
  def save
@@ -79,11 +76,11 @@ module ZammadAPI
79
76
 
80
77
  def self.find(transport, id)
81
78
  response = transport.get(url: "#{@url}/#{id}?expand=true")
82
- data = safe_json_parse(response.body)
83
79
  if response.status != 200
84
- raise "Can't find object (#{self.class.name}): #{data['error']}"
80
+ raise ResponseError.from(response, operation: 'find object', resource_class: self)
85
81
  end
86
82
 
83
+ data = safe_json_parse(response.body)
87
84
  item = new(transport, data)
88
85
  item.new_instance = false
89
86
  item
@@ -110,11 +107,10 @@ module ZammadAPI
110
107
  end
111
108
 
112
109
  def save_new
113
- response = @transport.post(url: "#{@url}?expand=true", params: @attributes)
114
- attributes = safe_json_parse(response.body)
115
- return attributes if response.status == 201
110
+ response = @transport.post(url: "#{@url}?expand=true", params: @attributes)
111
+ return safe_json_parse(response.body) if response.status == 201
116
112
 
117
- save_error(attributes)
113
+ save_error(response)
118
114
  end
119
115
 
120
116
  def save_existing
@@ -122,16 +118,14 @@ module ZammadAPI
122
118
  @changes.each do |name, values|
123
119
  attributes_to_post[name] = values[1]
124
120
  end
125
- response = @transport.put(url: "#{@url}/#{@attributes[:id]}?expand=true", params: attributes_to_post)
126
- attributes = safe_json_parse(response.body)
127
-
128
- return attributes if response.status == 200
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
129
123
 
130
- save_error(attributes)
124
+ save_error(response)
131
125
  end
132
126
 
133
- def save_error(attributes)
134
- raise "Can't save object (#{self.class.name}): #{attributes['error']}"
127
+ def save_error(response)
128
+ raise ResponseError.from(response, operation: 'save object', resource_class: self.class)
135
129
  end
136
130
 
137
131
  def symbolize_keys_deep!(hash)
@@ -3,11 +3,12 @@ class ZammadAPI::Resources::Ticket < ZammadAPI::Resources::Base
3
3
 
4
4
  def articles
5
5
  response = @transport.get(url: "/api/v1/ticket_articles/by_ticket/#{id}?expand=true")
6
- data = safe_json_parse(response.body)
7
6
  if response.status != 200
8
- raise "Can't get articles (#{self.class.name}): #{data['error']}"
7
+ raise ZammadAPI::ResponseError.from(response, operation: 'get articles', resource_class: self.class)
9
8
  end
10
9
 
10
+ data = safe_json_parse(response.body)
11
+
11
12
  data.collect do |raw|
12
13
  item = ZammadAPI::Resources::TicketArticle.new(@transport, raw)
13
14
  item.new_instance = false
@@ -13,7 +13,6 @@ class ZammadAPI::Resources::TicketArticleAttachment < ZammadAPI::Resources::Base
13
13
  response = @transport.get(url: "/api/v1/ticket_attachment/#{ticket_id}/#{article_id}/#{id}")
14
14
  return response.body if response.status == 200
15
15
 
16
- data = safe_json_parse(response.body)
17
- raise "Can't get articles (#{self.class.name}): #{data['error']}"
16
+ raise ZammadAPI::ResponseError.from(response, operation: 'get articles', resource_class: self.class)
18
17
  end
19
18
  end
@@ -50,7 +50,7 @@ module ZammadAPI
50
50
  end
51
51
 
52
52
  if !on_behalf_of.nil?
53
- req.headers['X-On-Behalf-Of'] = on_behalf_of
53
+ req.headers['From'] = on_behalf_of
54
54
  end
55
55
 
56
56
  yield(req) if block_given?
@@ -1,3 +1,3 @@
1
1
  module ZammadAPI
2
- VERSION = '1.3.1'.freeze
2
+ VERSION = '1.4.0'.freeze
3
3
  end
data/lib/zammad_api.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'zammad_api/version'
2
+ require 'zammad_api/errors'
2
3
  require 'zammad_api/client'
3
4
 
4
5
  module ZammadAPI
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zammad_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.1
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Martin Edenhofer
@@ -34,6 +34,7 @@ files:
34
34
  - lib/zammad_api.rb
35
35
  - lib/zammad_api/client.rb
36
36
  - lib/zammad_api/dispatcher.rb
37
+ - lib/zammad_api/errors.rb
37
38
  - lib/zammad_api/json_helper.rb
38
39
  - lib/zammad_api/list_all.rb
39
40
  - lib/zammad_api/list_base.rb