zammad_api 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ad3db8f4610b32b84270108afc10bd8a4881e7fd
4
- data.tar.gz: e4ddc8bcd938de762c1135eb93fc2f89ad9638da
3
+ metadata.gz: 0f34fd120446932dde693b5e5e364c7b3d39c038
4
+ data.tar.gz: f352194c6b650b5522579df5ffa68878e5f6014f
5
5
  SHA512:
6
- metadata.gz: cc9807c9138cefb50ff3946b2637f0a40e2bec5227c3da677f052b0b992e68c2f9e4649510ad9b7ceaab141e8426e5061ab1c816bc031a464a865b1c368cf8b0
7
- data.tar.gz: 7d54671ef307d30e4a180c8b767292d4a4dbf4d350f9cee402de40a168366dafcffa404391b6b84e96654be7711af34b960b064eeceb40818de42e374e4d63d5
6
+ metadata.gz: ff79410da2f92e251a5e8383802b4d1dd1f2d721bf65bb1e4eed1ae35ebb3e2f2dfd5534849c82762a9bfbe0cc9c7d34a50491de87549f93ba9874ce4846b8b2
7
+ data.tar.gz: a539ce959dc74ffcf223669289feca88751379191d9e54c26c2a6c210397f2603e82051d25b837d69bebbf449e8485f5b0e772318d6458407457740efc2172f6
@@ -22,12 +22,11 @@ module ZammadAPI
22
22
  end
23
23
 
24
24
  def method_missing(method, *args)
25
- if method.to_s[-1, 1] == '='
26
- method = method.to_s[0, method.length - 1].to_sym
27
- @changes[method] = [@attributes[method], args[0]]
28
- @attributes[method] = args[0]
29
- end
30
- @attributes[method]
25
+ return @attributes[method] if !method.end_with?('=')
26
+ method = method.to_s[0, method.length - 1].to_sym
27
+ @changes[method] = [@attributes[method], args[0]]
28
+ @attributes[method] = args[0]
29
+ nil
31
30
  end
32
31
 
33
32
  def new_record?
@@ -35,8 +34,7 @@ module ZammadAPI
35
34
  end
36
35
 
37
36
  def changed?
38
- return false if @changes.empty?
39
- true
37
+ @changes.present?
40
38
  end
41
39
 
42
40
  def destroy
@@ -44,35 +42,17 @@ module ZammadAPI
44
42
  if response.body.to_s != '' && response.body.to_s != ' '
45
43
  data = JSON.parse(response.body)
46
44
  end
47
- if response.status != 200
48
- raise "Can't destroy object (#{self.class.name}): #{data['error']}"
49
- end
50
- true
45
+ return true if response.status == 200
46
+ raise "Can't destroy object (#{self.class.name}): #{data['error']}"
51
47
  end
52
48
 
53
49
  def save
54
- if @new_instance
55
- response = @transport.post(url: "#{@url}?expand=true", params: @attributes)
56
- attributes = JSON.parse(response.body)
57
- if response.status != 201
58
- raise "Can't create new object (#{self.class.name}): #{attributes['error']}"
59
- end
60
- else
61
- attributes_to_post = {}
62
- @changes.each { |name, values|
63
- attributes_to_post[name] = values[1]
64
- }
65
- response = @transport.put(url: "#{@url}/#{@attributes[:id]}?expand=true", params: attributes_to_post)
66
- attributes = JSON.parse(response.body)
67
- if response.status != 200
68
- raise "Can't update new object (#{self.class.name}): #{attributes['error']}"
69
- end
70
- end
50
+ attributes = saved_attributes
71
51
  symbolize_keys_deep!(attributes)
72
52
  attributes.delete(:article)
73
- @attributes = attributes
53
+ @attributes = attributes
74
54
  @new_instance = false
75
- @changes = {}
55
+ @changes = {}
76
56
  true
77
57
  end
78
58
 
@@ -117,6 +97,34 @@ module ZammadAPI
117
97
 
118
98
  private
119
99
 
100
+ def saved_attributes
101
+ return save_new if @new_instance
102
+ save_existing
103
+ end
104
+
105
+ def save_new
106
+ response = @transport.post(url: "#{@url}?expand=true", params: @attributes)
107
+ attributes = JSON.parse(response.body)
108
+ return attributes if response.status == 201
109
+ save_error(attributes)
110
+ end
111
+
112
+ def save_existing
113
+ attributes_to_post = {}
114
+ @changes.each { |name, values|
115
+ attributes_to_post[name] = values[1]
116
+ }
117
+ response = @transport.put(url: "#{@url}/#{@attributes[:id]}?expand=true", params: attributes_to_post)
118
+ attributes = JSON.parse(response.body)
119
+
120
+ return attributes if response.status == 200
121
+ save_error(attributes)
122
+ end
123
+
124
+ def save_error(attributes)
125
+ raise "Can't save object (#{self.class.name}): #{attributes['error']}"
126
+ end
127
+
120
128
  def symbolize_keys_deep!(hash)
121
129
  hash.keys.each do |key|
122
130
  key_symbol = key.respond_to?(:to_sym) ? key.to_sym : key
@@ -7,17 +7,16 @@ class ZammadAPI::Resources::Ticket < ZammadAPI::Resources::Base
7
7
  if response.status != 200
8
8
  raise "Can't get articles (#{self.class.name}): #{data['error']}"
9
9
  end
10
- articles = []
11
- data.each { |raw|
10
+
11
+ data.collect { |raw|
12
12
  item = ZammadAPI::Resources::TicketArticle.new(@transport, raw)
13
13
  item.new_instance = false
14
- articles.push item
14
+ item
15
15
  }
16
- articles
17
16
  end
18
17
 
19
18
  def article(data)
20
- data['ticket_id'] = @attributes[:id]
19
+ data[:ticket_id] = @attributes[:id]
21
20
  item = ZammadAPI::Resources::TicketArticle.new(@transport, data)
22
21
  item.save
23
22
  item
@@ -1,3 +1,12 @@
1
1
  class ZammadAPI::Resources::TicketArticle < ZammadAPI::Resources::Base
2
2
  url '/api/v1/ticket_articles'
3
+
4
+ def attachments
5
+ @attributes[:attachments].collect { |raw|
6
+ raw[:ticket_id] = @attributes[:ticket_id]
7
+ raw[:article_id] = @attributes[:id]
8
+ ZammadAPI::Resources::TicketArticleAttachment.new(@transport, raw)
9
+ }
10
+ end
11
+
3
12
  end
@@ -0,0 +1,20 @@
1
+ class ZammadAPI::Resources::TicketArticleAttachment < ZammadAPI::Resources::Base
2
+
3
+ def initialize(transport, attributes = {})
4
+ @transport = transport
5
+ @attributes = attributes
6
+ symbolize_keys_deep!(@attributes)
7
+ end
8
+
9
+ def method_missing(method, *_args)
10
+ @attributes[method.to_sym]
11
+ end
12
+
13
+ def download
14
+ response = @transport.get(url: "/api/v1/ticket_attachment/#{ticket_id}/#{article_id}/#{id}")
15
+ return response.body if response.status == 200
16
+ data = JSON.parse(response.body)
17
+ raise "Can't get articles (#{self.class.name}): #{data['error']}"
18
+ end
19
+
20
+ end
@@ -7,6 +7,7 @@ require 'zammad_api/resources/group'
7
7
  require 'zammad_api/resources/organization'
8
8
  require 'zammad_api/resources/ticket'
9
9
  require 'zammad_api/resources/ticket_article'
10
+ require 'zammad_api/resources/ticket_article_attachment'
10
11
  require 'zammad_api/resources/ticket_state'
11
12
  require 'zammad_api/resources/ticket_priority'
12
13
 
@@ -1,3 +1,3 @@
1
1
  module ZammadAPI
2
- VERSION = '1.0.1'.freeze
2
+ VERSION = '1.0.2'.freeze
3
3
  end
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.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Martin Edenhofer
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2017-02-06 00:00:00.000000000 Z
12
+ date: 2017-03-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: faraday
@@ -101,6 +101,7 @@ files:
101
101
  - lib/zammad_api/resources/organization.rb
102
102
  - lib/zammad_api/resources/ticket.rb
103
103
  - lib/zammad_api/resources/ticket_article.rb
104
+ - lib/zammad_api/resources/ticket_article_attachment.rb
104
105
  - lib/zammad_api/resources/ticket_priority.rb
105
106
  - lib/zammad_api/resources/ticket_state.rb
106
107
  - lib/zammad_api/resources/user.rb