Module: Rubydesk

Defined in:
lib/rubydesk.rb,
lib/rubydesk/version.rb

Defined Under Namespace

Classes: AlreadyExistedError, ConnectionError

Constant Summary collapse

VERSION =
"0.0.3"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.base_urlObject

Returns the value of attribute base_url.



13
14
15
# File 'lib/rubydesk.rb', line 13

def base_url
  @base_url
end

.freshdesk_api_keyObject

Returns the value of attribute freshdesk_api_key.



13
14
15
# File 'lib/rubydesk.rb', line 13

def freshdesk_api_key
  @freshdesk_api_key
end

Class Method Details

.doc_name(name) ⇒ Object

match with the root name of xml document that freskdesk uses



237
238
239
240
241
242
243
244
245
246
# 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) ⇒ Object

Freshdesk API client support “DELETE” with the required id parameter



98
99
100
101
102
103
104
105
106
107
108
# 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) ⇒ Object

Freshdesk API client support “GET” with id parameter optional

Returns nil if there is no response


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# 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) ⇒ Object

Certain GET calls require query strings instead of a more RESTful URI. This method and fd_define_get are mutually exclusive.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# 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) ⇒ Object

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


115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# 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) ⇒ Object

Freshdesk API client support “PUT” with key, value parameter

Will throw:
  ConnectionError     if there is connection problem with the server


168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# 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') ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 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) ⇒ Object

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


222
223
224
225
226
227
228
229
230
231
232
233
234
# 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_formatObject



31
32
33
# File 'lib/rubydesk.rb', line 31

def self.response_format
  @response_format ||= "xml"
end

.response_format=(format) ⇒ Object

Specify the response format to use–JSON or XML. Currently JSON is only supported for GETs, so other verbs will still use XML.



37
38
39
40
41
42
# 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