rubydesk 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +2 -0
- data/lib/rubydesk/version.rb +3 -0
- data/lib/rubydesk.rb +245 -0
- data/rubydesk.gemspec +23 -0
- metadata +80 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 284c6c625c53ed16cdd44bcb3d65766e07da8d5c
|
4
|
+
data.tar.gz: a9fd0abfe356b0ded3050426a1ad9d0024e08659
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3318291eb242687c9062a0d56bfb8783f8ddce14190d77e7f363a6ddba73e0d394af9368a8f5af90b36d816ec1b8733e9a10af116fe0ae8550eb33cc8c0e9964
|
7
|
+
data.tar.gz: 1e6f23db74ec2fd6b3f4e6ce8026a577ebf69cdfdfe139d4d479bd471b8d1053eb37f8aeae10084d5ed8a5e4928042d3ac414b76600f6fb9139d68c8605aeb2e
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Trey Caliva
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# Rubydesk
|
2
|
+
|
3
|
+
Ruby Gem to connect to Freshdesk API
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'rubydesk'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install rubydesk
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it ( https://github.com/[my-github-username]/rubydesk/fork )
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/lib/rubydesk.rb
ADDED
@@ -0,0 +1,245 @@
|
|
1
|
+
require "rubydesk/version"
|
2
|
+
require 'rest_client'
|
3
|
+
require 'nokogiri'
|
4
|
+
require 'uri'
|
5
|
+
|
6
|
+
module Rubydesk
|
7
|
+
|
8
|
+
# custom errors
|
9
|
+
class AlreadyExistedError < StandardError; end
|
10
|
+
class ConnectionError < StandardError; end
|
11
|
+
|
12
|
+
attr_accessor :base_url, :freshdesk_api_key
|
13
|
+
|
14
|
+
def initialize(base_url, freshdesk_api_key='X', username='X', password='X')
|
15
|
+
|
16
|
+
@base_url = base_url
|
17
|
+
@freshdesk_api_key = freshdesk_api_key
|
18
|
+
|
19
|
+
RestClient.add_before_execution_proc do | req, params |
|
20
|
+
|
21
|
+
if @freshdesk_api_key
|
22
|
+
req.basic_auth freshdesk_api_key, password
|
23
|
+
else
|
24
|
+
req.basic_auth username, password
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def response_format
|
30
|
+
@response_format ||= "xml"
|
31
|
+
end
|
32
|
+
|
33
|
+
# Specify the response format to use--JSON or XML. Currently JSON is only
|
34
|
+
# supported for GETs, so other verbs will still use XML.
|
35
|
+
def response_format=(format)
|
36
|
+
unless format.downcase =~ /json|xml/
|
37
|
+
raise StandardError "Unsupported format: '#{format}'. Please specify 'xml' or 'json'."
|
38
|
+
end
|
39
|
+
@response_format = format.downcase
|
40
|
+
end
|
41
|
+
|
42
|
+
# Freshdesk API client support "GET" with id parameter optional
|
43
|
+
# Returns nil if there is no response
|
44
|
+
def self.fd_define_get(name)
|
45
|
+
name = name.to_s
|
46
|
+
method_name = "get_" + name
|
47
|
+
|
48
|
+
define_method method_name do |*args|
|
49
|
+
uri = mapping(name)
|
50
|
+
uri.gsub!(/\.xml/, "\.#{response_format}")
|
51
|
+
|
52
|
+
# If we've been passed a string paramter, it means we're fetching
|
53
|
+
# something like domain_URL/helpdesk/tickets/[ticket_id].xml
|
54
|
+
#
|
55
|
+
# If we're supplied with a hash parameter, it means we're fetching
|
56
|
+
# something like domain_URL/helpdesk/tickets.xml?filter_name=all_tickets&page=[value]
|
57
|
+
if args.size > 0
|
58
|
+
url_args = args.first
|
59
|
+
if url_args.class == Hash
|
60
|
+
uri += '?' + URI.encode_www_form(url_args)
|
61
|
+
else
|
62
|
+
uri.gsub!(/\.#{response_format}/, "/#{url_args}\.#{response_format}")
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
begin
|
67
|
+
response = RestClient.get uri
|
68
|
+
rescue Exception
|
69
|
+
response = nil
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
# Certain GET calls require query strings instead of a more
|
75
|
+
# RESTful URI. This method and fd_define_get are mutually exclusive.
|
76
|
+
def self.fd_define_parameterized_get(name)
|
77
|
+
name = name.to_s
|
78
|
+
method_name = "get_" + name
|
79
|
+
|
80
|
+
define_method method_name do |params={}|
|
81
|
+
uri = mapping(name)
|
82
|
+
uri.gsub!(/\.xml/, ".#{response_format}")
|
83
|
+
unless params.empty?
|
84
|
+
uri += '?' + URI.encode_www_form(params)
|
85
|
+
end
|
86
|
+
|
87
|
+
begin
|
88
|
+
response = RestClient.get uri
|
89
|
+
rescue Exception
|
90
|
+
response = nil
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
# Freshdesk API client support "DELETE" with the required id parameter
|
96
|
+
def self.fd_define_delete(name)
|
97
|
+
name = name.to_s
|
98
|
+
method_name = "delete_" + name
|
99
|
+
|
100
|
+
define_method method_name do |args|
|
101
|
+
uri = mapping(name)
|
102
|
+
raise StandardError, "An ID is required to delete" if args.size.eql? 0
|
103
|
+
uri.gsub!(/.xml/, "/#{args}.xml")
|
104
|
+
RestClient.delete uri
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
# Freshdesk API client support "POST" with the optional key, value parameter
|
109
|
+
#
|
110
|
+
# Will throw:
|
111
|
+
# AlreadyExistedError if there is exact copy of data in the server
|
112
|
+
# ConnectionError if there is connection problem with the server
|
113
|
+
def self.fd_define_post(name)
|
114
|
+
name = name.to_s
|
115
|
+
method_name = "post_" + name
|
116
|
+
|
117
|
+
define_method method_name do |args, id=nil|
|
118
|
+
raise StandardError, "Arguments are required to modify data" if args.size.eql? 0
|
119
|
+
uri = mapping(name, id)
|
120
|
+
|
121
|
+
builder = Nokogiri::XML::Builder.new do |xml|
|
122
|
+
xml.send(doc_name(name)) {
|
123
|
+
if args.has_key? :attachment
|
124
|
+
attachment_name = args[:attachment][:name] or raise StandardError, "Attachment name required"
|
125
|
+
attachment_cdata = args[:attachment][:cdata] or raise StandardError, "Attachment CDATA required"
|
126
|
+
xml.send("attachments", type: "array") {
|
127
|
+
xml.send("attachment") {
|
128
|
+
xml.send("resource", "type" => "file", "name" => attachment_name, "content-type" => "application/octet-stream") {
|
129
|
+
xml.cdata attachment_cdata
|
130
|
+
}
|
131
|
+
}
|
132
|
+
}
|
133
|
+
args.except! :attachment
|
134
|
+
end
|
135
|
+
|
136
|
+
args.each do |key, value|
|
137
|
+
xml.send(key, value)
|
138
|
+
end
|
139
|
+
}
|
140
|
+
end
|
141
|
+
|
142
|
+
begin
|
143
|
+
response = RestClient.post uri, builder.to_xml, :content_type => "text/xml"
|
144
|
+
|
145
|
+
rescue RestClient::UnprocessableEntity
|
146
|
+
raise AlreadyExistedError, "Entry already existed"
|
147
|
+
|
148
|
+
rescue RestClient::InternalServerError
|
149
|
+
raise ConnectionError, "Connection to the server failed. Please check hostname"
|
150
|
+
|
151
|
+
rescue RestClient::Found
|
152
|
+
raise ConnectionError, "Connection to the server failed. Please check username/password"
|
153
|
+
|
154
|
+
rescue Exception
|
155
|
+
raise
|
156
|
+
end
|
157
|
+
|
158
|
+
response
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
# Freshdesk API client support "PUT" with key, value parameter
|
163
|
+
#
|
164
|
+
# Will throw:
|
165
|
+
# ConnectionError if there is connection problem with the server
|
166
|
+
def self.fd_define_put(name)
|
167
|
+
name = name.to_s
|
168
|
+
method_name = "put_" + name
|
169
|
+
|
170
|
+
define_method method_name do |args|
|
171
|
+
raise StandardError, "Arguments are required to modify data" if args.size.eql? 0
|
172
|
+
raise StandardError, "id is required to modify data" if args[:id].nil?
|
173
|
+
uri = mapping(name)
|
174
|
+
|
175
|
+
builder = Nokogiri::XML::Builder.new do |xml|
|
176
|
+
xml.send(doc_name(name)) {
|
177
|
+
args.each do |key, value|
|
178
|
+
xml.send(key, value)
|
179
|
+
end
|
180
|
+
}
|
181
|
+
end
|
182
|
+
|
183
|
+
begin
|
184
|
+
uri.gsub!(/.xml/, "/#{args[:id]}.xml")
|
185
|
+
response = RestClient.put uri, builder.to_xml, :content_type => "text/xml"
|
186
|
+
|
187
|
+
rescue RestClient::InternalServerError
|
188
|
+
raise ConnectionError, "Connection to the server failed. Please check hostname"
|
189
|
+
|
190
|
+
rescue RestClient::Found
|
191
|
+
raise ConnectionError, "Connection to the server failed. Please check username/password"
|
192
|
+
|
193
|
+
rescue Exception
|
194
|
+
raise
|
195
|
+
end
|
196
|
+
|
197
|
+
response
|
198
|
+
end
|
199
|
+
end
|
200
|
+
|
201
|
+
[:tickets, :ticket_fields, :ticket_notes, :users, :forums, :solutions, :companies, :time_sheets].each do |a|
|
202
|
+
fd_define_get a
|
203
|
+
fd_define_post a
|
204
|
+
fd_define_delete a
|
205
|
+
fd_define_put a
|
206
|
+
end
|
207
|
+
|
208
|
+
[:user_ticket].each do |resource|
|
209
|
+
fd_define_parameterized_get resource
|
210
|
+
end
|
211
|
+
|
212
|
+
|
213
|
+
# Mapping of object name to url:
|
214
|
+
# tickets => helpdesk/tickets.xml
|
215
|
+
# ticket_fields => /ticket_fields.xml
|
216
|
+
# users => /contacts.xml
|
217
|
+
# forums => /categories.xml
|
218
|
+
# solutions => /solution/categories.xml
|
219
|
+
# companies => /customers.xml
|
220
|
+
def mapping(method_name, id = nil)
|
221
|
+
case method_name
|
222
|
+
when "tickets" then File.join(@base_url + "helpdesk/tickets.xml")
|
223
|
+
when "user_ticket" then File.join(@base_url + "helpdesk/tickets/user_ticket.xml")
|
224
|
+
when "ticket_fields" then File.join(@base_url, "ticket_fields.xml")
|
225
|
+
when "ticket_notes" then File.join(@base_url, "helpdesk/tickets/#{id}/notes.xml")
|
226
|
+
when "users" then File.join(@base_url, "contacts.xml")
|
227
|
+
when "forums" then File.join(@base_url + "categories.xml")
|
228
|
+
when "solutions" then File.join(@base_url + "solution/categories.xml")
|
229
|
+
when "companies" then File.join(@base_url + "customers.xml")
|
230
|
+
when "time_sheets" then File.join(@base_url + "helpdesk/time_sheets.xml")
|
231
|
+
end
|
232
|
+
end
|
233
|
+
|
234
|
+
# match with the root name of xml document that freskdesk uses
|
235
|
+
def doc_name(name)
|
236
|
+
case name
|
237
|
+
when "tickets" then "helpdesk_ticket"
|
238
|
+
when "ticket_fields" then "helpdesk-ticket-fields"
|
239
|
+
when "ticket_notes" then "helpdesk_note"
|
240
|
+
when "users" then "user"
|
241
|
+
when "companies" then "customer"
|
242
|
+
else raise StandardError, "No root object for this call"
|
243
|
+
end
|
244
|
+
end
|
245
|
+
end
|
data/rubydesk.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'rubydesk/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "rubydesk"
|
8
|
+
spec.version = Rubydesk::VERSION
|
9
|
+
spec.authors = ["Trey Caliva"]
|
10
|
+
spec.email = ["trey@directorschoice.travel"]
|
11
|
+
spec.summary = "Ruby Gem to connect to Freshdesk API"
|
12
|
+
spec.description = "Ruby Gem to connect to Freshdesk API"
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rubydesk
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Trey Caliva
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-04-30 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
description: Ruby Gem to connect to Freshdesk API
|
42
|
+
email:
|
43
|
+
- trey@directorschoice.travel
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- Gemfile
|
50
|
+
- LICENSE.txt
|
51
|
+
- README.md
|
52
|
+
- Rakefile
|
53
|
+
- lib/rubydesk.rb
|
54
|
+
- lib/rubydesk/version.rb
|
55
|
+
- rubydesk.gemspec
|
56
|
+
homepage: ''
|
57
|
+
licenses:
|
58
|
+
- MIT
|
59
|
+
metadata: {}
|
60
|
+
post_install_message:
|
61
|
+
rdoc_options: []
|
62
|
+
require_paths:
|
63
|
+
- lib
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
requirements: []
|
75
|
+
rubyforge_project:
|
76
|
+
rubygems_version: 2.2.2
|
77
|
+
signing_key:
|
78
|
+
specification_version: 4
|
79
|
+
summary: Ruby Gem to connect to Freshdesk API
|
80
|
+
test_files: []
|