Module: Zendesk

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

Defined Under Namespace

Modules: Constants, Properties, RestObject Classes: Comment, ConfigurationNotFound, Resource, Ticket, User

Constant Summary collapse

ZENDESK_ROOT =
File.expand_path((defined?(Rails) && Rails.root.to_s.length > 0) ? Rails.root : ".")
DEFAULT_CONFIG_PATH =
File.join(ZENDESK_ROOT, 'config', 'zendesk.yml')
VERSION =
"0.1.5"

Class Method Summary collapse

Class Method Details

.configObject



22
23
24
# File 'lib/zendesk.rb', line 22

def self.config
  @configuration ||= load_configuration(DEFAULT_CONFIG_PATH)
end

.hash_elements_to_xml(key, value) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/zendesk.rb', line 62

def self.hash_elements_to_xml(key, value)
  element = REXML::Element.new(key.to_s.gsub('_', '-'))
  if value.is_a?(Array)
    element.attributes['type'] = 'array'
    value.each{ |val| element.add_element hash_elements_to_xml(val.keys[0], val.values[0]) }
  elsif value.is_a?(Hash)
    value.each_pair{ |key, v| element.add_element hash_elements_to_xml(key, v) }
  else
    element.text = value
  end
  element
end

.load_configuration(config_path) ⇒ Object



16
17
18
19
20
# File 'lib/zendesk.rb', line 16

def self.load_configuration(config_path)
  exists = config_path && File.exists?(config_path)
  raise ConfigurationNotFound, "could not find the \"#{config_path}\" configuration file" unless exists
  YAML.load_file(config_path)
end

.resourceObject



26
27
28
# File 'lib/zendesk.rb', line 26

def self.resource
  @resource ||= RestClient::Resource.new config['host'], :user => config['user'], :password => config['password'], :timeout => 20, :open_timeout => 1
end

.xml_elements_to_hash(element) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/zendesk.rb', line 34

def self.xml_elements_to_hash(element)
  value = element.text
  value = case element.attributes['type']
  when 'integer'
    value.to_i
  when 'datetime'
    DateTime.parse(element.text)
  else
    element.text
  end if value

  if element.elements.count > 0
    if element.attributes['type'] == 'array'
      value = element.elements.map{|el| xml_elements_to_hash(el) }
    else
      value = {}
      element.elements.each{|el| value.merge! xml_elements_to_hash(el) }
    end      
  end
  { element.name.gsub('-', '_').to_sym => value }      
end

.xml_in(xml_data) ⇒ Object



30
31
32
# File 'lib/zendesk.rb', line 30

def self.xml_in(xml_data)
  xml_elements_to_hash REXML::Document.new(xml_data).root
end

.xml_out(hash) ⇒ Object



56
57
58
59
60
# File 'lib/zendesk.rb', line 56

def self.xml_out(hash)
  doc = REXML::Document.new 
  doc.add_element hash_elements_to_xml(hash.keys[0], hash.values[0])
  doc.to_s
end