freshservice 0.0.1
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.
- data/lib/freshservice.rb +258 -0
- metadata +78 -0
data/lib/freshservice.rb
ADDED
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
require 'rest_client'
|
|
2
|
+
require 'nokogiri'
|
|
3
|
+
require 'uri'
|
|
4
|
+
|
|
5
|
+
class Freshservice
|
|
6
|
+
|
|
7
|
+
# custom errors
|
|
8
|
+
class AlreadyExistedError < StandardError; end
|
|
9
|
+
class ConnectionError < StandardError; end
|
|
10
|
+
|
|
11
|
+
attr_accessor :base_url
|
|
12
|
+
|
|
13
|
+
def initialize(base_url, username, password='X')
|
|
14
|
+
|
|
15
|
+
@base_url = base_url
|
|
16
|
+
@auth = {:user => username, :password => password}
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# Freshdesk API client support "GET" with id parameter optional
|
|
20
|
+
# Returns nil if there is no response
|
|
21
|
+
def self.fd_define_get(name)
|
|
22
|
+
name = name.to_s
|
|
23
|
+
method_name = "get_" + name
|
|
24
|
+
|
|
25
|
+
define_method method_name do |*args|
|
|
26
|
+
uri = mapping(name)
|
|
27
|
+
# If we've been passed a string paramter, it means we're fetching
|
|
28
|
+
# something like domain_URL/helpdesk/tickets/[ticket_id].json
|
|
29
|
+
#
|
|
30
|
+
# If we're supplied with a hash parameter, it means we're fetching
|
|
31
|
+
# something like domain_URL/helpdesk/tickets.json?filter_name=all_tickets&page=[value]
|
|
32
|
+
if args.size > 0
|
|
33
|
+
url_args = args.first
|
|
34
|
+
if url_args.class == Hash
|
|
35
|
+
uri += '?' + URI.encode_www_form(url_args)
|
|
36
|
+
else
|
|
37
|
+
uri.gsub!(/\.json/, "/#{url_args}\.json")
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
begin
|
|
42
|
+
response = RestClient::Request.execute(@auth.merge(:method => :get, :url => uri))
|
|
43
|
+
rescue Exception
|
|
44
|
+
response = nil
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# Certain GET calls require query strings instead of a more
|
|
50
|
+
# RESTful URI. This method and fd_define_get are mutually exclusive.
|
|
51
|
+
# def self.fd_define_parameterized_get(name)
|
|
52
|
+
# name = name.to_s
|
|
53
|
+
# method_name = "get_" + name
|
|
54
|
+
|
|
55
|
+
# define_method method_name do |params={}|
|
|
56
|
+
# uri = mapping(name)
|
|
57
|
+
# uri.gsub!(/\.json/, ".json")
|
|
58
|
+
# unless params.empty?
|
|
59
|
+
# uri += '?' + URI.encode_www_form(params)
|
|
60
|
+
# end
|
|
61
|
+
|
|
62
|
+
# begin
|
|
63
|
+
# response = RestClient::Request.execute(@auth.merge(:method => :get, :url => uri))
|
|
64
|
+
# rescue Exception
|
|
65
|
+
# response = nil
|
|
66
|
+
# end
|
|
67
|
+
# end
|
|
68
|
+
# end
|
|
69
|
+
|
|
70
|
+
# Freshdesk API client support "DELETE" with the required id parameter
|
|
71
|
+
def self.fd_define_delete(name)
|
|
72
|
+
name = name.to_s
|
|
73
|
+
method_name = "delete_" + name
|
|
74
|
+
define_method method_name do |args|
|
|
75
|
+
uri = mapping(name)
|
|
76
|
+
raise StandardError, "An ID is required to delete" if args.size.eql? 0
|
|
77
|
+
uri.gsub!(/.json/, "/#{args}.json")
|
|
78
|
+
RestClient::Request.execute(@auth.merge(:method => :delete, :url => uri))
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# Freshdesk API client support "POST" with the optional key, value parameter
|
|
83
|
+
#
|
|
84
|
+
# Will throw:
|
|
85
|
+
# AlreadyExistedError if there is exact copy of data in the server
|
|
86
|
+
# ConnectionError if there is connection problem with the server
|
|
87
|
+
def self.fd_define_post(name)
|
|
88
|
+
name = name.to_s
|
|
89
|
+
method_name = "post_" + name
|
|
90
|
+
|
|
91
|
+
define_method method_name do |args, id=nil|
|
|
92
|
+
debugger
|
|
93
|
+
raise StandardError, "Arguments are required to modify data" if args.size.eql? 0
|
|
94
|
+
id = args[:id]
|
|
95
|
+
uri = mapping(name, id)
|
|
96
|
+
|
|
97
|
+
builder = Nokogiri::XML::Builder.new do |json|
|
|
98
|
+
json.send(doc_name(name)) {
|
|
99
|
+
if args.has_key? :attachment
|
|
100
|
+
attachment_name = args[:attachment][:name] or raise StandardError, "Attachment name required"
|
|
101
|
+
attachment_cdata = args[:attachment][:cdata] or raise StandardError, "Attachment CDATA required"
|
|
102
|
+
json.send("attachments", type: "array") {
|
|
103
|
+
json.send("attachment") {
|
|
104
|
+
json.send("resource", "type" => "file", "name" => attachment_name, "content-type" => "application/octet-stream") {
|
|
105
|
+
json.cdata attachment_cdata
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
args.except! :attachment
|
|
110
|
+
end
|
|
111
|
+
args.each do |key, value|
|
|
112
|
+
json.send(key, value)
|
|
113
|
+
end
|
|
114
|
+
}
|
|
115
|
+
end
|
|
116
|
+
data = Hash.from_xml(builder.to_xml)
|
|
117
|
+
begin
|
|
118
|
+
options = @auth.merge(
|
|
119
|
+
:method => :post,
|
|
120
|
+
:payload => data,
|
|
121
|
+
:headers => {:content_type => "text/json"},
|
|
122
|
+
:url => uri
|
|
123
|
+
)
|
|
124
|
+
response = RestClient::Request.execute(options)
|
|
125
|
+
rescue RestClient::UnprocessableEntity
|
|
126
|
+
raise AlreadyExistedError, "Entry already existed"
|
|
127
|
+
|
|
128
|
+
rescue RestClient::InternalServerError
|
|
129
|
+
raise ConnectionError, "Connection to the server failed. Please check hostname"
|
|
130
|
+
|
|
131
|
+
rescue RestClient::Found
|
|
132
|
+
raise ConnectionError, "Connection to the server failed. Please check username/password"
|
|
133
|
+
|
|
134
|
+
rescue Exception
|
|
135
|
+
raise
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
response
|
|
139
|
+
end
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
# Freshdesk API client support "PUT" with key, value parameter
|
|
143
|
+
#
|
|
144
|
+
# Will throw:
|
|
145
|
+
# ConnectionError if there is connection problem with the server
|
|
146
|
+
def self.fd_define_put(name)
|
|
147
|
+
name = name.to_s
|
|
148
|
+
method_name = "put_" + name
|
|
149
|
+
|
|
150
|
+
define_method method_name do |args|
|
|
151
|
+
raise StandardError, "Arguments are required to modify data" if args.size.eql? 0
|
|
152
|
+
raise StandardError, "id is required to modify data" if args[:id].nil?
|
|
153
|
+
uri = mapping(name)
|
|
154
|
+
|
|
155
|
+
builder = Nokogiri::XML::Builder.new do |json|
|
|
156
|
+
json.send(doc_name(name)) {
|
|
157
|
+
args.each do |key, value|
|
|
158
|
+
json.send(key, value)
|
|
159
|
+
end
|
|
160
|
+
}
|
|
161
|
+
end
|
|
162
|
+
data = Hash.from_xml(builder.to_xml)
|
|
163
|
+
begin
|
|
164
|
+
uri.gsub!(/.json/, "/#{args[:id]}.json")
|
|
165
|
+
options = @auth.merge(
|
|
166
|
+
:method => :put,
|
|
167
|
+
:payload => data,
|
|
168
|
+
:headers => {:content_type => "text/json"},
|
|
169
|
+
:url => uri
|
|
170
|
+
)
|
|
171
|
+
response = RestClient::Request.execute(options)
|
|
172
|
+
rescue RestClient::InternalServerError
|
|
173
|
+
raise ConnectionError, "Connection to the server failed. Please check hostname"
|
|
174
|
+
|
|
175
|
+
rescue RestClient::Found
|
|
176
|
+
raise ConnectionError, "Connection to the server failed. Please check username/password"
|
|
177
|
+
|
|
178
|
+
rescue Exception
|
|
179
|
+
raise
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
response
|
|
183
|
+
end
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
[:tickets, :problems, :changes, :releases, :users, :solutions, :departments, :config_items].each do |a|
|
|
187
|
+
fd_define_get a
|
|
188
|
+
fd_define_post a
|
|
189
|
+
fd_define_delete a
|
|
190
|
+
fd_define_put a
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
[:ticket_fields, :problem_fields, :change_fields, :release_fields, :ci_types, :ci_type_fields].each do |a|
|
|
194
|
+
fd_define_get a
|
|
195
|
+
end
|
|
196
|
+
|
|
197
|
+
[:ticket_notes, :problem_notes, :change_notes, :release_notes].each do |a|
|
|
198
|
+
fd_define_post a
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
# [:user_ticket].each do |resource|
|
|
202
|
+
# fd_define_parameterized_get resource
|
|
203
|
+
# end
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
# Mapping of object name to url:
|
|
207
|
+
# tickets => helpdesk/tickets.json
|
|
208
|
+
# ticket_fields => /ticket_fields.json
|
|
209
|
+
# users => /contacts.json
|
|
210
|
+
# forums => /categories.json
|
|
211
|
+
# solutions => /solution/categories.json
|
|
212
|
+
# companies => /customers.json
|
|
213
|
+
def mapping(method_name, id = nil)
|
|
214
|
+
case method_name
|
|
215
|
+
when "tickets" then File.join(@base_url + "helpdesk/tickets.json")
|
|
216
|
+
when "ticket_fields" then File.join(@base_url, "ticket_fields.json")
|
|
217
|
+
when "ticket_notes" then File.join(@base_url, "helpdesk/tickets/#{id}/notes.json")
|
|
218
|
+
when "problems" then File.join(@base_url + "itil/problems.json")
|
|
219
|
+
when "problem_fields" then File.join(@base_url, "itil/problem_fields.json")
|
|
220
|
+
when "problem_notes" then File.join(@base_url, "itil/problems/#{id}/notes.json")
|
|
221
|
+
when "changes" then File.join(@base_url + "itil/changes.json")
|
|
222
|
+
when "change_fields" then File.join(@base_url, "itil/change_fields.json")
|
|
223
|
+
when "change_notes" then File.join(@base_url, "itil/changes/#{id}/notes.json")
|
|
224
|
+
when "releases" then File.join(@base_url + "itil/releases.json")
|
|
225
|
+
when "release_fields" then File.join(@base_url, "itil/release_fields.json")
|
|
226
|
+
when "release_notes" then File.join(@base_url, "itil/releases/#{id}/notes.json")
|
|
227
|
+
when "users" then File.join(@base_url, "itil/requesters.json")
|
|
228
|
+
when "solutions" then File.join(@base_url + "solution/categories.json")
|
|
229
|
+
when "departments" then File.join(@base_url + "itil/departments.json")
|
|
230
|
+
when "config_items" then File.join(@base_url + "cmdb/items.json")
|
|
231
|
+
when "ci_types" then File.join(@base_url + "cmdb/ci_types.json")
|
|
232
|
+
when "ci_type_fields" then File.join(@base_url + "cmdb/ci_types.json")
|
|
233
|
+
end
|
|
234
|
+
end
|
|
235
|
+
|
|
236
|
+
# match with the root name of json document that freskdesk uses
|
|
237
|
+
def doc_name(name)
|
|
238
|
+
case name
|
|
239
|
+
when "tickets" then "helpdesk_ticket"
|
|
240
|
+
# when "ticket_fields" then "helpdesk-ticket-fields"
|
|
241
|
+
when "ticket_notes" then "helpdesk_note"
|
|
242
|
+
when "problems" then "itil_problem"
|
|
243
|
+
# when "problem_fields" then "itil_problem_fields"
|
|
244
|
+
when "problem_notes" then "itil_note"
|
|
245
|
+
when "changes" then "itil_change"
|
|
246
|
+
# when "change_fields" then "itil_change_fields"
|
|
247
|
+
when "change_notes" then "itil_note"
|
|
248
|
+
when "releases" then "itil_release"
|
|
249
|
+
# when "release_fields" then "itil_release_fields"
|
|
250
|
+
when "release_notes" then "itil_note"
|
|
251
|
+
when "users" then "user"
|
|
252
|
+
when "departments" then "itil_department"
|
|
253
|
+
when "config_items" then "cmdb_config_item"
|
|
254
|
+
when "solutions" then "solution_category"
|
|
255
|
+
else raise StandardError, "No root object for this call"
|
|
256
|
+
end
|
|
257
|
+
end
|
|
258
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: freshservice
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
prerelease:
|
|
6
|
+
platform: ruby
|
|
7
|
+
authors:
|
|
8
|
+
- David Liman
|
|
9
|
+
- Tim Macdonald
|
|
10
|
+
autorequire:
|
|
11
|
+
bindir: bin
|
|
12
|
+
cert_chain: []
|
|
13
|
+
date: 2014-02-24 00:00:00.000000000 Z
|
|
14
|
+
dependencies:
|
|
15
|
+
- !ruby/object:Gem::Dependency
|
|
16
|
+
name: rest-client
|
|
17
|
+
requirement: !ruby/object:Gem::Requirement
|
|
18
|
+
none: false
|
|
19
|
+
requirements:
|
|
20
|
+
- - ! '>='
|
|
21
|
+
- !ruby/object:Gem::Version
|
|
22
|
+
version: '0'
|
|
23
|
+
type: :runtime
|
|
24
|
+
prerelease: false
|
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
26
|
+
none: false
|
|
27
|
+
requirements:
|
|
28
|
+
- - ! '>='
|
|
29
|
+
- !ruby/object:Gem::Version
|
|
30
|
+
version: '0'
|
|
31
|
+
- !ruby/object:Gem::Dependency
|
|
32
|
+
name: nokogiri
|
|
33
|
+
requirement: !ruby/object:Gem::Requirement
|
|
34
|
+
none: false
|
|
35
|
+
requirements:
|
|
36
|
+
- - ! '>='
|
|
37
|
+
- !ruby/object:Gem::Version
|
|
38
|
+
version: '0'
|
|
39
|
+
type: :runtime
|
|
40
|
+
prerelease: false
|
|
41
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
42
|
+
none: false
|
|
43
|
+
requirements:
|
|
44
|
+
- - ! '>='
|
|
45
|
+
- !ruby/object:Gem::Version
|
|
46
|
+
version: '0'
|
|
47
|
+
description: Ruby Gem for interfacing with the Freshservice API
|
|
48
|
+
email:
|
|
49
|
+
executables: []
|
|
50
|
+
extensions: []
|
|
51
|
+
extra_rdoc_files: []
|
|
52
|
+
files:
|
|
53
|
+
- lib/freshservice.rb
|
|
54
|
+
homepage: https://github.com/PremSundar/freshservice-api
|
|
55
|
+
licenses: []
|
|
56
|
+
post_install_message:
|
|
57
|
+
rdoc_options: []
|
|
58
|
+
require_paths:
|
|
59
|
+
- lib
|
|
60
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
61
|
+
none: false
|
|
62
|
+
requirements:
|
|
63
|
+
- - ! '>='
|
|
64
|
+
- !ruby/object:Gem::Version
|
|
65
|
+
version: '0'
|
|
66
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
67
|
+
none: false
|
|
68
|
+
requirements:
|
|
69
|
+
- - ! '>='
|
|
70
|
+
- !ruby/object:Gem::Version
|
|
71
|
+
version: '0'
|
|
72
|
+
requirements: []
|
|
73
|
+
rubyforge_project:
|
|
74
|
+
rubygems_version: 1.8.23
|
|
75
|
+
signing_key:
|
|
76
|
+
specification_version: 3
|
|
77
|
+
summary: Ruby Gem for interfacing with the Freshservice API
|
|
78
|
+
test_files: []
|