KayakoClient 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -18,6 +18,7 @@ require 'kayako_client/mixins/user_visibility_api'
18
18
  require 'kayako_client/mixins/staff_visibility_api'
19
19
 
20
20
  require 'kayako_client/base'
21
+ require 'kayako_client/custom_field'
21
22
  require 'kayako_client/department'
22
23
  require 'kayako_client/staff'
23
24
  require 'kayako_client/staff_group'
@@ -0,0 +1,73 @@
1
+ module KayakoClient
2
+
3
+ class CustomFieldOption < KayakoClient::Base
4
+ supports :all
5
+
6
+ property :custom_field_option_id, :integer
7
+ property :custom_field_id, :integer
8
+ property :option_value, :string
9
+ property :display_order, :integer
10
+ property :is_selected, :boolean
11
+ property :parent_custom_field_option_id, :integer
12
+
13
+ def has_parent_custom_field_option?
14
+ !parent_custom_field_option_id.nil? && parent_custom_field_option_id > 0
15
+ end
16
+
17
+ def self.all(custom_field_id, options = {})
18
+ unless custom_field_id.to_i > 0
19
+ logger.error "invalid :custom_field_id - #{custom_field_id}" if logger
20
+ raise ArgumentError, "invalid custom field ID"
21
+ end
22
+ super(options.merge(:e => "/#{self.superclass.name.split('::').last}/CustomField/ListOptions/#{custom_field_id.to_i}"))
23
+ end
24
+
25
+ end
26
+
27
+ class CustomField < KayakoClient::Base
28
+ supports :all
29
+
30
+ TYPE_TEXT = 1
31
+ TYPE_TEXT_AREA = 2
32
+ TYPE_PASSWORD = 3
33
+ TYPE_CHECKBOX = 4
34
+ TYPE_RADIO = 5
35
+ TYPE_SELECT = 6
36
+ TYPE_MULTI_SELECT = 7
37
+ TYPE_CUSTOM = 8
38
+ TYPE_LINKED_SELECT = 9
39
+ TYPE_DATE = 10
40
+ TYPE_FILE = 11
41
+
42
+ property :custom_field_id, :integer
43
+ property :field_name, :string
44
+ property :title, :string
45
+ property :description, :string
46
+ property :field_type, :integer, :range => 1..11
47
+ property :is_required, :boolean
48
+ property :default_value, :string
49
+ property :regexp_validate, :string
50
+ property :custom_field_group_id, :integer
51
+ property :encrypt_in_db, :boolean
52
+ property :display_order, :integer
53
+ property :user_editable, :boolean
54
+ property :staff_editable, :boolean
55
+
56
+ def options
57
+ if instance_variable_defined?(:@options)
58
+ instance_variable_get(:@options)
59
+ elsif !new? && custom_field_id && custom_field_id > 0
60
+ options = KayakoClient::CustomFieldOption.all(custom_field_id, inherited_options)
61
+ if options && !options.empty?
62
+ instance_variable_set(:@options, options)
63
+ else
64
+ instance_variable_set(:@options, nil)
65
+ end
66
+ else
67
+ nil
68
+ end
69
+ end
70
+
71
+ end
72
+
73
+ end
@@ -7,6 +7,7 @@ module KayakoClient
7
7
 
8
8
  def initialize(options = {})
9
9
  @client = ::HTTPClient.new
10
+ @client.ssl_config.verify_mode = OpenSSL::SSL::VERIFY_NONE
10
11
  if options[:host]
11
12
  @client.proxy = "http://#{options[:host]}:#{options[:port] || 80}"
12
13
  @client.set_proxy_auth(options[:user], options[:pass])
@@ -1,4 +1,4 @@
1
- require 'net/http'
1
+ require 'net/https'
2
2
 
3
3
  module KayakoClient
4
4
  class NetHTTP
@@ -13,8 +13,9 @@ module KayakoClient
13
13
  def get(base, params = {})
14
14
  uri = URI.parse(base)
15
15
  log = params.delete(:logger) || logger
16
- @client.use_ssl = true if uri.is_a?(URI::HTTPS)
17
- resp = @client.start(uri.host, uri.port) do |request|
16
+ session = @client.new(uri.host, uri.port)
17
+ session.use_ssl = true if uri.is_a?(URI::HTTPS)
18
+ resp = session.start do |request|
18
19
  query = url(uri.path, params)
19
20
  log.debug ":get => #{query}" if log
20
21
  request.get(query)
@@ -26,8 +27,9 @@ module KayakoClient
26
27
  uri = URI.parse(base)
27
28
  e = params.delete(:e)
28
29
  log = params.delete(:logger) || logger
29
- @client.use_ssl = true if uri.is_a?(URI::HTTPS)
30
- resp = @client.start(uri.host, uri.port) do |request|
30
+ session = @client.new(uri.host, uri.port)
31
+ session.use_ssl = true if uri.is_a?(URI::HTTPS)
32
+ resp = session.start do |request|
31
33
  query = url(uri.path, { :e => e })
32
34
  data = form_data(params)
33
35
  if log
@@ -43,8 +45,9 @@ module KayakoClient
43
45
  uri = URI.parse(base)
44
46
  e = params.delete(:e)
45
47
  log = params.delete(:logger) || logger
46
- @client.use_ssl = true if uri.is_a?(URI::HTTPS)
47
- resp = @client.start(uri.host, uri.port) do |request|
48
+ session = @client.new(uri.host, uri.port)
49
+ session.use_ssl = true if uri.is_a?(URI::HTTPS)
50
+ resp = session.start do |request|
48
51
  query = url(uri.path, { :e => e })
49
52
  data = form_data(params)
50
53
  if log
@@ -59,8 +62,9 @@ module KayakoClient
59
62
  def delete(base, params = {})
60
63
  uri = URI.parse(base)
61
64
  log = params.delete(:logger) || logger
62
- @client.use_ssl = true if uri.is_a?(URI::HTTPS)
63
- resp = @client.start(uri.host, uri.port) do |request|
65
+ session = @client.new(uri.host, uri.port)
66
+ session.use_ssl = true if uri.is_a?(URI::HTTPS)
67
+ resp = session.start do |request|
64
68
  query = url(uri.path, params)
65
69
  log.debug ":delete => #{query}" if log
66
70
  request.delete(query)
@@ -133,6 +133,12 @@ module KayakoClient
133
133
  KayakoClient::TicketCustomField.get(ticket, options.merge(inherited_options))
134
134
  end
135
135
 
136
+ def post_ticket_custom_fields(options = {})
137
+ KayakoClient::TicketCustomField.post(options.merge(inherited_options))
138
+ end
139
+
140
+ alias_method :create_ticket_custom_fields, :post_ticket_custom_fields
141
+
136
142
 
137
143
  def ticket_notes(ticket, options = {})
138
144
  KayakoClient::TicketNote.all(ticket, options.merge(inherited_options))
@@ -304,5 +310,14 @@ module KayakoClient
304
310
 
305
311
  alias_method :destroy_user_organization, :delete_user_organization
306
312
 
313
+
314
+ def custom_fields(options = {})
315
+ KayakoClient::CustomField.all(options.merge(inherited_options))
316
+ end
317
+
318
+ def custom_field_options(custom_field_id, options = {})
319
+ KayakoClient::CustomFieldOption.all(custom_field_id, options.merge(inherited_options))
320
+ end
321
+
307
322
  end
308
323
  end
@@ -27,6 +27,10 @@ module KayakoClient
27
27
  base.extend(ClassMethods)
28
28
  end
29
29
 
30
+ def id
31
+ nil
32
+ end
33
+
30
34
  def to_i
31
35
  instance_variable_defined?(:@id) ? instance_variable_get(:@id).to_i : 0
32
36
  end
@@ -39,6 +39,18 @@ module KayakoClient
39
39
  KayakoClient::TicketCustomField.get(id, options.merge(inherited_options)) if id
40
40
  end
41
41
 
42
+ def post_custom_fields(options = {})
43
+ if id
44
+ if logger && options[:ticket_id] && options[:ticket_id].to_i != id
45
+ logger.warn "overwriting :ticket_id"
46
+ end
47
+ options[:ticket_id] = id
48
+ KayakoClient::TicketCustomField.post(options.merge(inherited_options))
49
+ end
50
+ end
51
+
52
+ alias_method :create_custom_fields, :post_custom_fields
53
+
42
54
 
43
55
  def notes(options = {})
44
56
  KayakoClient::TicketNote.all(id, options.merge(inherited_options)) if id
@@ -92,6 +92,7 @@ module KayakoClient
92
92
  property :auto_user_id, :boolean, :new => true
93
93
  property :staff_id, :integer, :new => true
94
94
  property :type, :symbol, :new => true, :in => TICKET_TYPES
95
+ property :template_group_id, :integer
95
96
 
96
97
  property :work_flow, :object, :class => TicketWorkflow
97
98
  property :watcher, [ :object ], :class => TicketWatcher
@@ -4,25 +4,25 @@ require 'time'
4
4
  module KayakoClient
5
5
 
6
6
  class TicketCustomFieldValue < KayakoClient::Tickets
7
- embedded
8
7
 
9
- property :id, :integer
10
- property :type, :integer, :range => 1..11
11
- property :title, :string
12
- property :file_name, :string
8
+ property :id, :integer, :readonly => true
9
+ property :type, :integer, :readonly => true, :range => 1..11
10
+ property :title, :string, :readonly => true
11
+ property :name, :string, :readonly => true
12
+ property :file_name, :string, :readonly => true
13
13
  property :contents, :string
14
14
 
15
15
  def initialize(*args)
16
16
  super(*args)
17
17
  if defined?(@type)
18
- if @type == KayakoClient::TicketCustomField::TYPE_FILE
18
+ if @type == KayakoClient::CustomField::TYPE_FILE
19
19
  self.class.send(:include, KayakoClient::Attachment)
20
20
  if defined?(@contents)
21
21
  logger.debug "decoding base64 :contents" if logger
22
22
  @contents = Base64.decode64(@contents)
23
23
  @contents = Base64.decode64(@contents)
24
24
  end
25
- elsif @type == KayakoClient::TicketCustomField::TYPE_DATE
25
+ elsif @type == KayakoClient::CustomField::TYPE_DATE
26
26
  @contents = Time.parse(@contents) if defined?(@contents)
27
27
  end
28
28
  end
@@ -31,36 +31,64 @@ module KayakoClient
31
31
  end
32
32
 
33
33
  class TicketCustomFieldGroup < KayakoClient::Tickets
34
- embedded
35
34
 
36
- property :id, :integer
37
- property :title, :string
35
+ property :id, :integer, :readonly => true
36
+ property :title, :string, :readonly => true
37
+
38
38
  property :fields, [ :object ], :class => TicketCustomFieldValue, :get => :field
39
39
 
40
40
  end
41
41
 
42
42
  class TicketCustomField < KayakoClient::Tickets
43
- supports :get
44
-
45
- TYPE_TEXT = 1
46
- TYPE_TEXT_AREA = 2
47
- TYPE_PASSWORD = 3
48
- TYPE_CHECKBOX = 4
49
- TYPE_RADIO = 5
50
- TYPE_SELECT = 6
51
- TYPE_MULTI_SELECT = 7
52
- TYPE_CUSTOM = 8
53
- TYPE_LINKED_SELECT = 9
54
- TYPE_DATE = 10
55
- TYPE_FILE = 11
43
+ supports :get, :post
44
+
45
+ TYPE_TEXT = KayakoClient::CustomField::TYPE_TEXT
46
+ TYPE_TEXT_AREA = KayakoClient::CustomField::TYPE_TEXT_AREA
47
+ TYPE_PASSWORD = KayakoClient::CustomField::TYPE_PASSWORD
48
+ TYPE_CHECKBOX = KayakoClient::CustomField::TYPE_CHECKBOX
49
+ TYPE_RADIO = KayakoClient::CustomField::TYPE_RADIO
50
+ TYPE_SELECT = KayakoClient::CustomField::TYPE_SELECT
51
+ TYPE_MULTI_SELECT = KayakoClient::CustomField::TYPE_MULTI_SELECT
52
+ TYPE_CUSTOM = KayakoClient::CustomField::TYPE_CUSTOM
53
+ TYPE_LINKED_SELECT = KayakoClient::CustomField::TYPE_LINKED_SELECT
54
+ TYPE_DATE = KayakoClient::CustomField::TYPE_DATE
55
+ TYPE_FILE = KayakoClient::CustomField::TYPE_FILE
56
+
57
+ attr_accessor :ticket_id
56
58
 
57
59
  property :groups, [ :object ], :class => TicketCustomFieldGroup, :get => :group
58
60
 
59
61
  # NOTE: when a new field is added returns (for old tickets):
60
62
  # [Notice]: Undefined offset: 11 (api/class.Controller_TicketCustomField.php:279)
61
63
 
64
+ def initialize(*args)
65
+ properties = {}
66
+ if args.last.is_a?(Hash)
67
+ args.last.keys.each do |property|
68
+ if property.is_a?(String)
69
+ properties[property] = args.last.delete(property)
70
+ end
71
+ end
72
+ if args.last.has_key?(:ticket_id)
73
+ self.ticket_id = args.last.delete(:ticket_id)
74
+ end
75
+ end
76
+ super
77
+ if properties.size > 0
78
+ properties.each do |property, value|
79
+ self[property] = value
80
+ end
81
+ end
82
+ end
83
+
62
84
  def empty?
63
- !(groups && groups.size > 0)
85
+ if groups && groups.size > 0
86
+ false
87
+ elsif defined?(@new_group) && @new_group.size > 0
88
+ false
89
+ else
90
+ true
91
+ end
64
92
  end
65
93
 
66
94
  def custom_field(name)
@@ -85,8 +113,27 @@ module KayakoClient
85
113
  end
86
114
  end
87
115
  end
116
+ @groups.each do |group|
117
+ next unless group.fields && !group.fields.empty?
118
+ group.fields.each do |field|
119
+ if field.name == name
120
+ return field.contents
121
+ end
122
+ end
123
+ end
88
124
  end
89
125
  end
126
+
127
+ if defined?(@new_group) && @new_group.size > 0
128
+ if name.is_a?(String)
129
+ @new_group.each do |field|
130
+ if field.name == name
131
+ return field.contents
132
+ end
133
+ end
134
+ end
135
+ end
136
+
90
137
  nil
91
138
  end
92
139
 
@@ -97,12 +144,104 @@ module KayakoClient
97
144
 
98
145
  def each(&block)
99
146
  if defined?(@groups) && @groups.size > 0
100
- @custom_fields ||= @groups.inject([]) do |array, group|
101
- array.concat(group.fields) if group.fields && group.fields.size > 0
102
- array
147
+ @groups.each do |group|
148
+ group.fields.each(&block)
149
+ end
150
+ end
151
+
152
+ if defined?(@new_group) && @new_group.size > 0
153
+ @new_group.each(&block)
154
+ end
155
+ end
156
+
157
+ def []=(name, value)
158
+ if defined?(@groups) && @groups.size > 0
159
+ if name.is_a?(Numeric)
160
+ if name.to_i > 0
161
+ @groups.each do |group|
162
+ next unless group.fields && !group.fields.empty?
163
+ group.fields.each do |field|
164
+ if field.id == name.to_i
165
+ field.contents = value.to_s
166
+ return field.contents
167
+ end
168
+ end
169
+ end
170
+ raise ArgumentError, "use custom field name"
171
+ else
172
+ raise ArgumentError, "invalid custom field ID"
173
+ end
174
+ elsif name.is_a?(String)
175
+ @groups.each do |group|
176
+ next unless group.fields && !group.fields.empty?
177
+ group.fields.each do |field|
178
+ if field.title == name
179
+ field.contents = value.to_s
180
+ return field.contents
181
+ end
182
+ end
183
+ end
184
+ @groups.each do |group|
185
+ next unless group.fields && !group.fields.empty?
186
+ group.fields.each do |field|
187
+ if field.name == name
188
+ field.contents = value.to_s
189
+ return field.contents
190
+ end
191
+ end
192
+ end
103
193
  end
104
- @custom_fields.each(&block) unless @custom_fields.empty?
105
194
  end
195
+
196
+ if name =~ %r{^[a-z0-9]{12}$}
197
+ @new_group ||= []
198
+ @new_group << TicketCustomFieldValue.new(:name => name.to_s, :contents => value.to_s)
199
+ return value.to_s
200
+ end
201
+
202
+ super
203
+ end
204
+
205
+ def post
206
+ raise RuntimeError, "client not configured" unless configured?
207
+ raise RuntimeError, "missing ticket ID" unless ticket_id.to_i > 0
208
+ params = {}
209
+ each do |field|
210
+ params[field.name] = field.contents
211
+ end
212
+ random = self.class.salt
213
+ e = "#{self.class.path}/#{ticket_id.to_i}"
214
+ response = client.post(api_url, params.merge(:e => e,
215
+ :apikey => api_key,
216
+ :salt => random,
217
+ :signature => self.class.signature(random, secret_key)))
218
+ if response.is_a?(KayakoClient::HTTPOK)
219
+ if logger
220
+ logger.debug "Response:"
221
+ logger.debug response.body
222
+ end
223
+ payload = xml_backend.new(response.body, { :logger => logger })
224
+ clean
225
+ import(payload.to_hash)
226
+ loaded!
227
+ logger.info ":post successful" if logger
228
+ true
229
+ else
230
+ unless response.is_a?(Hash)
231
+ logger.error "Response: #{response.status} #{response.body}" if logger
232
+ raise StandardError, "server returned #{response.status}: #{response.body}"
233
+ end
234
+ false
235
+ end
236
+ end
237
+
238
+ def self.get(*args)
239
+ object = super
240
+ if object && object.respond_to?(:ticket_id=)
241
+ object.ticket_id = args[0]
242
+ object.loaded!
243
+ end
244
+ object
106
245
  end
107
246
 
108
247
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: KayakoClient
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
8
  - 0
9
- - 1
10
- version: 1.0.1
9
+ - 2
10
+ version: 1.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Kayako
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-01-26 00:00:00 Z
18
+ date: 2012-03-16 00:00:00 Z
19
19
  dependencies: []
20
20
 
21
21
  description: Kayako's official Ruby interface library for the REST API.
@@ -65,6 +65,7 @@ files:
65
65
  - lib/kayako_client/user.rb
66
66
  - lib/kayako_client/user_group.rb
67
67
  - lib/kayako_client/user_organization.rb
68
+ - lib/kayako_client/custom_field.rb
68
69
  homepage: http://forge.kayako.com/projects/kayako-ruby-api-library
69
70
  licenses: []
70
71
 
@@ -94,7 +95,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
94
95
  requirements: []
95
96
 
96
97
  rubyforge_project:
97
- rubygems_version: 1.8.10
98
+ rubygems_version: 1.8.15
98
99
  signing_key:
99
100
  specification_version: 3
100
101
  summary: Kayako official Ruby REST API library.