zammad_api 1.3.0 → 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: 42c75fd97c167f27bec8282ccd8214f3885e051691c042ad0e8e491651814d81
4
- data.tar.gz: 949a756e4310f53f9ca929a9e973dcb30af4a80e494e55059da6cac345a8bc28
3
+ metadata.gz: 60178628e519723ca90e772769e9b61e615d152bdb825210b35e8233b9e15a16
4
+ data.tar.gz: 92fe4e55cf8230790ce87735120f39934ac9313064f67983e052d7b2fde6126f
5
5
  SHA512:
6
- metadata.gz: b6625e7abed25c87ae3fe46bf543c9095cc092f61eacc23fded2af1a9807a0fc8ebf589221d6afbb57414704e3c732e9f629f89bf460791cffecd91448398a38
7
- data.tar.gz: '0379ee29e881afe691eb7d47b0c2c3dbe01ae0a6eddd73e783d24ec8c07fc0dd635dca03148c9a539c006e5b2ad6c87173b944e5511024849727ffcff58be454'
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
@@ -0,0 +1,11 @@
1
+ require 'json'
2
+
3
+ module ZammadAPI
4
+ module JsonHelper
5
+ def safe_json_parse(string)
6
+ JSON.parse(string)
7
+ rescue JSON::ParserError
8
+ {}
9
+ end
10
+ end
11
+ end
@@ -1,6 +1,9 @@
1
+ require 'zammad_api/json_helper'
2
+
1
3
  module ZammadAPI
2
4
  class ListBase
3
5
  include Enumerable
6
+ include ZammadAPI::JsonHelper
4
7
 
5
8
  def initialize(resource, transport, parameter = {})
6
9
  @resource = resource
@@ -59,11 +62,12 @@ module ZammadAPI
59
62
  }.join('&')
60
63
 
61
64
  response = @transport.get(url: url)
62
- data = JSON.parse(response.body)
63
65
  if response.status != 200
64
- 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)
65
67
  end
66
68
 
69
+ data = safe_json_parse(response.body)
70
+
67
71
  list = []
68
72
  data.each do |local_data|
69
73
  item = @resource.new(@transport, local_data)
@@ -74,7 +78,7 @@ module ZammadAPI
74
78
  end
75
79
 
76
80
  def perform_request(_parameter)
77
- raise "no perform_request implementation for #{self.class.name} found"
81
+ raise Error, "no perform_request implementation for #{self.class.name} found"
78
82
  end
79
83
  end
80
84
  end
@@ -1,10 +1,13 @@
1
1
  require 'cgi'
2
- require 'json'
2
+ require 'zammad_api/json_helper'
3
3
  require 'zammad_api/transport'
4
4
 
5
5
  module ZammadAPI
6
6
  module Resources
7
7
  class Base
8
+ include ZammadAPI::JsonHelper
9
+ extend ZammadAPI::JsonHelper
10
+
8
11
  attr_accessor :new_instance, :url, :attributes
9
12
  attr_reader :changes
10
13
 
@@ -35,17 +38,14 @@ module ZammadAPI
35
38
  end
36
39
 
37
40
  def changed?
38
- @changes.present?
41
+ !@changes.to_h.empty?
39
42
  end
40
43
 
41
44
  def destroy
42
45
  response = @transport.delete(url: "#{@url}/#{@attributes[:id]}")
43
- if response.body.to_s != '' && response.body.to_s != ' '
44
- data = JSON.parse(response.body)
45
- end
46
46
  return true if response.status == 200
47
47
 
48
- raise "Can't destroy object (#{self.class.name}): #{data['error']}"
48
+ raise ResponseError.from(response, operation: 'destroy object', resource_class: self.class)
49
49
  end
50
50
 
51
51
  def save
@@ -76,11 +76,11 @@ module ZammadAPI
76
76
 
77
77
  def self.find(transport, id)
78
78
  response = transport.get(url: "#{@url}/#{id}?expand=true")
79
- data = JSON.parse(response.body)
80
79
  if response.status != 200
81
- raise "Can't find object (#{self.class.name}): #{data['error']}"
80
+ raise ResponseError.from(response, operation: 'find object', resource_class: self)
82
81
  end
83
82
 
83
+ data = safe_json_parse(response.body)
84
84
  item = new(transport, data)
85
85
  item.new_instance = false
86
86
  item
@@ -107,11 +107,10 @@ module ZammadAPI
107
107
  end
108
108
 
109
109
  def save_new
110
- response = @transport.post(url: "#{@url}?expand=true", params: @attributes)
111
- attributes = JSON.parse(response.body)
112
- 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
113
112
 
114
- save_error(attributes)
113
+ save_error(response)
115
114
  end
116
115
 
117
116
  def save_existing
@@ -119,16 +118,14 @@ module ZammadAPI
119
118
  @changes.each do |name, values|
120
119
  attributes_to_post[name] = values[1]
121
120
  end
122
- response = @transport.put(url: "#{@url}/#{@attributes[:id]}?expand=true", params: attributes_to_post)
123
- attributes = JSON.parse(response.body)
124
-
125
- 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
126
123
 
127
- save_error(attributes)
124
+ save_error(response)
128
125
  end
129
126
 
130
- def save_error(attributes)
131
- 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)
132
129
  end
133
130
 
134
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 = 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 = 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.0'.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.0
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Martin Edenhofer
@@ -34,6 +34,8 @@ 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
38
+ - lib/zammad_api/json_helper.rb
37
39
  - lib/zammad_api/list_all.rb
38
40
  - lib/zammad_api/list_base.rb
39
41
  - lib/zammad_api/list_search.rb