module Rubydesk
Constants
- VERSION
Attributes
base_url[RW]
freshdesk_api_key[RW]
Public Class Methods
doc_name(name)
click to toggle source
match with the root name of xml document that freskdesk uses
# File lib/rubydesk.rb, line 237 def self.doc_name(name) case name when "tickets" then "helpdesk_ticket" when "ticket_fields" then "helpdesk-ticket-fields" when "ticket_notes" then "helpdesk_note" when "users" then "user" when "companies" then "customer" else raise StandardError, "No root object for this call" end end
fd_define_delete(name)
click to toggle source
Freshdesk API client support “DELETE” with the required id parameter
# File lib/rubydesk.rb, line 98 def self.fd_define_delete(name) name = name.to_s method_name = "delete_" + name define_method method_name do |args| uri = mapping(name) raise StandardError, "An ID is required to delete" if args.size.eql? 0 uri.gsub!(/.xml/, "/#{args}.xml") RestClient.delete uri end end
fd_define_get(name)
click to toggle source
Freshdesk API client support “GET” with id parameter optional
Returns nil if there is no response
# File lib/rubydesk.rb, line 46 def self.fd_define_get(name) name = name.to_s method_name = "get_" + name define_method method_name do |*args| uri = mapping(name) uri.gsub!(/\.xml/, "\.#{response_format}") # If we've been passed a string paramter, it means we're fetching # something like domain_URL/helpdesk/tickets/[ticket_id].xml # # If we're supplied with a hash parameter, it means we're fetching # something like domain_URL/helpdesk/tickets.xml?filter_name=all_tickets&page=[value] if args.size > 0 url_args = args.first if url_args.class == Hash uri += '?' + URI.encode_www_form(url_args) else uri.gsub!(/\.#{response_format}/, "/#{url_args}\.#{response_format}") end end begin response = RestClient.get uri rescue Exception response = nil end end end
fd_define_parameterized_get(name)
click to toggle source
Certain GET calls require query strings instead of a more RESTful URI. This method and fd_define_get
are mutually exclusive.
# File lib/rubydesk.rb, line 78 def self.fd_define_parameterized_get(name) name = name.to_s method_name = "get_" + name define_method method_name do |params={}| uri = mapping(name) uri.gsub!(/\.xml/, ".#{response_format}") unless params.empty? uri += '?' + URI.encode_www_form(params) end begin response = RestClient.get uri rescue Exception response = nil end end end
fd_define_post(name)
click to toggle source
Freshdesk API client support “POST” with the optional key, value parameter
Will throw: AlreadyExistedError if there is exact copy of data in the server ConnectionError if there is connection problem with the server
# File lib/rubydesk.rb, line 115 def self.fd_define_post(name) name = name.to_s method_name = "post_" + name define_method method_name do |args, id=nil| raise StandardError, "Arguments are required to modify data" if args.size.eql? 0 uri = mapping(name, id) builder = Nokogiri::XML::Builder.new do |xml| xml.send(doc_name(name)) { if args.has_key? :attachment attachment_name = args[:attachment][:name] or raise StandardError, "Attachment name required" attachment_cdata = args[:attachment][:cdata] or raise StandardError, "Attachment CDATA required" xml.send("attachments", type: "array") { xml.send("attachment") { xml.send("resource", "type" => "file", "name" => attachment_name, "content-type" => "application/octet-stream") { xml.cdata attachment_cdata } } } args.except! :attachment end args.each do |key, value| xml.send(key, value) end } end begin response = RestClient.post uri, builder.to_xml, :content_type => "text/xml" rescue RestClient::UnprocessableEntity raise AlreadyExistedError, "Entry already existed" rescue RestClient::InternalServerError raise ConnectionError, "Connection to the server failed. Please check hostname" rescue RestClient::Found raise ConnectionError, "Connection to the server failed. Please check username/password" rescue Exception raise end response end end
fd_define_put(name)
click to toggle source
Freshdesk API client support “PUT” with key, value parameter
Will throw: ConnectionError if there is connection problem with the server
# File lib/rubydesk.rb, line 168 def self.fd_define_put(name) name = name.to_s method_name = "put_" + name define_method method_name do |args| raise StandardError, "Arguments are required to modify data" if args.size.eql? 0 raise StandardError, "id is required to modify data" if args[:id].nil? uri = mapping(name) builder = Nokogiri::XML::Builder.new do |xml| xml.send(doc_name(name)) { args.each do |key, value| xml.send(key, value) end } end begin uri.gsub!(/.xml/, "/#{args[:id]}.xml") response = RestClient.put uri, builder.to_xml, :content_type => "text/xml" rescue RestClient::InternalServerError raise ConnectionError, "Connection to the server failed. Please check hostname" rescue RestClient::Found raise ConnectionError, "Connection to the server failed. Please check username/password" rescue Exception raise end response end end
initialize(base_url, freshdesk_api_key='X', username='X', password='X')
click to toggle source
# File lib/rubydesk.rb, line 16 def self.initialize(base_url, freshdesk_api_key='X', username='X', password='X') @base_url = base_url @freshdesk_api_key = freshdesk_api_key RestClient.add_before_execution_proc do | req, params | if @freshdesk_api_key req.basic_auth freshdesk_api_key, password else req.basic_auth username, password end end end
mapping(method_name, id = nil)
click to toggle source
Mapping of object name to url:
tickets => helpdesk/tickets.xml ticket_fields => /ticket_fields.xml users => /contacts.xml forums => /categories.xml solutions => /solution/categories.xml companies => /customers.xml
# File lib/rubydesk.rb, line 222 def self.mapping(method_name, id = nil) case method_name when "tickets" then File.join(@base_url + "helpdesk/tickets.xml") when "user_ticket" then File.join(@base_url + "helpdesk/tickets/user_ticket.xml") when "ticket_fields" then File.join(@base_url, "ticket_fields.xml") when "ticket_notes" then File.join(@base_url, "helpdesk/tickets/#{id}/notes.xml") when "users" then File.join(@base_url, "contacts.xml") when "forums" then File.join(@base_url + "categories.xml") when "solutions" then File.join(@base_url + "solution/categories.xml") when "companies" then File.join(@base_url + "customers.xml") when "time_sheets" then File.join(@base_url + "helpdesk/time_sheets.xml") end end
response_format()
click to toggle source
# File lib/rubydesk.rb, line 31 def self.response_format @response_format ||= "xml" end
response_format=(format)
click to toggle source
Specify the response format to use–JSON or XML. Currently JSON is only supported for GETs, so other verbs will still use XML.
# File lib/rubydesk.rb, line 37 def self.response_format=(format) unless format.downcase =~ /json|xml/ raise StandardError "Unsupported format: '#{format}'. Please specify 'xml' or 'json'." end @response_format = format.downcase end