Permalink
Browse files

- removed requirement active_support

- work with XML only using ReXML
- removed jeweler dependencies
  • Loading branch information...
1 parent 772badf commit eb4882d7aaab80a4475e2bb87dc76d484fec550d @aderyabin aderyabin committed May 20, 2011
Showing with 126 additions and 125 deletions.
  1. +4 −8 Gemfile
  2. +1 −24 Rakefile
  3. +56 −8 lib/zendesk.rb
  4. +2 −2 lib/zendesk/comment.rb
  5. +17 −3 lib/zendesk/lib/properties.rb
  6. +5 −11 lib/zendesk/lib/rest_object.rb
  7. +7 −1 lib/zendesk/resource.rb
  8. +18 −16 lib/zendesk/ticket.rb
  9. +1 −1 lib/zendesk/user.rb
  10. +5 −0 spec/ticket_spec.rb
  11. +10 −51 zendesk.gemspec
View
12 Gemfile
@@ -1,12 +1,8 @@
source "http://rubygems.org"
-# Add dependencies required to use your gem here.
-# Example:
-# gem "activesupport", ">= 2.3.5"
+gemspec
+
+gem 'rest-client'
-# Add dependencies to develop your gem here.
-# Include everything needed to run rake, tests, features, etc.
group :development do
- gem "bundler", "~> 1.0.0"
- gem "jeweler", "~> 1.6.0"
- # gem "rcov", ">= 0"
+ gem "rspec"
end
View
@@ -1,25 +1,2 @@
-# encoding: utf-8
-
-require 'rubygems'
require 'bundler'
-require 'rake/rdoctask'
-
-begin
- Bundler.setup(:default, :development)
-rescue Bundler::BundlerError => e
- $stderr.puts e.message
- $stderr.puts "Run `bundle install` to install missing gems"
- exit e.status_code
-end
-require 'rake'
-
-require 'jeweler'
-Jeweler::Tasks.new do |gem|
- gem.name = "zendesk"
- gem.homepage = "http://github.com/aderyabin/zendesk"
- gem.license = "MIT"
- gem.summary = %Q{Ruby wrapper around the Zendesk API}
- gem.description = %Q{Ruby wrapper around the Zendesk API}
- gem.email = "deriabin@gmail.com"
- gem.authors = ["Andrey Deryabin"]
-end
+Bundler::GemHelper.install_tasks
View
@@ -1,17 +1,17 @@
# Zendesk
require 'rubygems'
-require 'active_support'
require 'rest_client'
require 'yaml'
+require 'rexml/document'
module Zendesk
-
+
class ConfigurationNotFound < NameError;
end
-
-
- ZENDESK_ROOT = File.expand_path((defined?(Rails) && Rails.root.to_s.length > 0) ? Rails.root : ".") unless defined?(ZENDESK_ROOT)
- DEFAULT_CONFIG_PATH = File.join(ZENDESK_ROOT, 'config', 'zendesk.yml')
+
+
+ ZENDESK_ROOT = File.expand_path((defined?(Rails) && Rails.root.to_s.length > 0) ? Rails.root : ".") unless defined?(ZENDESK_ROOT)
+ DEFAULT_CONFIG_PATH = File.join(ZENDESK_ROOT, 'config', 'zendesk.yml')
def self.load_configuration(config_path)
exists = config_path && File.exists?(config_path)
@@ -22,16 +22,64 @@ def self.load_configuration(config_path)
def self.config
@configuration ||= load_configuration(DEFAULT_CONFIG_PATH)
end
-
+
def self.resource
@resource ||= RestClient::Resource.new config['host'], :user => config['user'], :password => config['password'], :timeout => 20, :open_timeout => 1
end
+
+ def self.xml_in(xml_data)
+ xml_elements_to_hash REXML::Document.new(xml_data).root
+ end
+
+ 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
+
+ def self.xml_out(hash)
+ doc = REXML::Document.new
+ doc << REXML::XMLDecl.new('1.0', 'UTF-8')
+ doc.add_element hash_elements_to_xml(hash.keys[0], hash.values[0])
+ doc.to_s
+ end
+ 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
+
+
+
autoload :Resource, File.dirname(__FILE__) + '/zendesk/resource.rb'
autoload :Comment, File.dirname(__FILE__) + '/zendesk/comment.rb'
autoload :Ticket, File.dirname(__FILE__) + '/zendesk/ticket.rb'
autoload :User, File.dirname(__FILE__) + '/zendesk/user.rb'
-
+
autoload :Constants, File.dirname(__FILE__) + '/zendesk/lib/constants.rb'
autoload :RestObject, File.dirname(__FILE__) + '/zendesk/lib/rest_object.rb'
autoload :Properties, File.dirname(__FILE__) + '/zendesk/lib/properties.rb'
View
@@ -13,12 +13,12 @@ def initialize(attrs = {})
end
def to_xml
- { :value => value, :is_public => is_public }.to_xml(:skip_instruct => true, :root=>:comment)
+ Zendesk.xml_out({:comment => { :value => value, :is_public => is_public }})
end
def save
begin
- response = resource["tickets/#{ticket_id}.xml"].put self.to_xml, :content_type => 'application/xml'
+ response = Zendesk.resource["tickets/#{ticket_id}.xml"].put self.to_xml, :content_type => 'application/xml'
return (200..300).include?(response.headers[:status].to_i)
rescue Exception => e
puts e.message
@@ -1,13 +1,27 @@
module Zendesk::Properties
module ClassMethods
- mattr_accessor :_attributes, :_protected_attributes, :_properties, :_datetimes
-
@@_attributes = []
@@_protected_attributes = []
@@_properties = []
@@_datetimes = []
+ def _attributes
+ @@_attributes
+ end
+
+ def _protected_attributes
+ @@_protected_attributes
+ end
+
+ def _properties
+ @@_properties
+ end
+
+ def _datetimes
+ @@_datetimes
+ end
+
def attributes(*vars)
@@_attributes.concat vars
attr_accessor *vars
@@ -46,7 +60,7 @@ def #{method_name}
class_eval <<-END
def #{method_name}=(value)
- @#{method_name}_id = #{method_name.upcase}.index(value)
+ @#{method_name}_id = #{method_name.upcase}.key(value)
end
END
end
@@ -1,11 +1,5 @@
module Zendesk::RestObject
module ClassMethods
- def create(attrs = {})
- instance = new(attrs)
- instance.save
- instance
- end
-
def find(id)
begin
new().load(id)
@@ -17,16 +11,16 @@ def find(id)
end
module InstanceMethods
- def path
- self.class.to_s.demodulize.downcase.pluralize
+ def model_name
+ self.class.to_s.gsub(/^.*::/, '').downcase
end
def save
begin
response = if self.id
- Zendesk.resource["#{path}/#{id}.xml"].put self.to_xml, :content_type => 'application/xml'
+ Zendesk.resource["#{model_name}s/#{id}.xml"].put self.to_xml, :content_type => 'application/xml'
else
- Zendesk.resource["#{path}.xml"].post self.to_xml, :content_type => 'application/xml'
+ Zendesk.resource["#{model_name}s.xml"].post self.to_xml, :content_type => 'application/xml'
end
if (200..300).include?(response.headers[:status].to_i)
load(id || response.headers[:location].scan(/\d+/).first.to_i)
@@ -52,7 +46,7 @@ def reload
def load(id)
begin
- data = load_data(Zendesk.resource["#{path}/#{id}.xml"].get)[path.singularize]
+ data = load_data(Zendesk.resource["#{model_name}s/#{id}.xml"].get)[model_name.to_sym]
load_attributes(data)
load_protected_attributes(data)
load_field_entries(data) if respond_to?(:load_field_entries)
View
@@ -10,6 +10,12 @@ def initialize(attrs = {})
end
def load_data(xml_stream)
- Hash.from_xml(xml_stream)
+ Zendesk.xml_in(xml_stream)
+ end
+
+ def self.create(attrs = {})
+ instance = new(attrs)
+ instance.save
+ instance
end
end
View
@@ -8,8 +8,8 @@ class Zendesk::Ticket < Zendesk::Resource
properties :status, :ticket_type, :priority, :via
- alias_attribute :id, :nice_id
- alias_attribute :set_tags, :current_tags
+ alias :id :nice_id
+ alias :set_tags :current_tags
def initialize(attrs = {})
@comments = []
@@ -18,19 +18,21 @@ def initialize(attrs = {})
super
end
- # Filling field methods from imported data.
+ # Filling custom field methods from imported data.
# If field is not pointed in config it will be missed
def load_field_entries(data)
- data['ticket_field_entries'].each do |field_entry|
- method_name = @field_ids.index(field_entry['ticket_field_id'])
- send("#{method_name}=", field_entry['value']) if method_name
+ if data[:ticket_field_entries]
+ data[:ticket_field_entries].each do |field_entry|
+ method_name = @field_ids.key(field_entry[:ticket_field_id])
+ send("#{method_name}=", field_entry[:value]) if method_name
+ end
end
end
def load_comments(data)
@comments = []
- data['comments'].each do |comment|
- @comments << Zendesk::Comment.new(comment)
+ data[:comments].each do |comment|
+ @comments << Zendesk::Comment.new(comment[:comment])
end
end
@@ -53,35 +55,35 @@ def create_comment(value, is_public = true)
def to_xml
result = {}
(attributes - @field_ids.keys).each do |obj|
- if instance_variable_get(:"@#{obj.to_s}").present?
- result[obj.to_s.downcase] = instance_variable_get(:"@#{obj.to_s}")
+ if val = instance_variable_get(:"@#{obj.to_s}")
+ result[obj.to_s.downcase] = val
end
end
result[:ticket_field_entries] = []
@field_ids.each_pair do |key, value|
- if instance_variable_get(:"@#{key.to_s}").present?
- result[:ticket_field_entries] << {:ticket_field_id => value, :value => instance_variable_get(:"@#{key.to_s}").to_s}
+ if val = instance_variable_get(:"@#{key.to_s}")
+ result[:ticket_field_entries] << { :ticket_field_id => value, :value => val.to_s }
end
end
- result[:set_tags] = current_tags
- result.to_xml(:skip_instruct => true, :root=>:ticket)
+ result[:set_tags] = current_tags if current_tags
+ Zendesk.xml_out({:ticket => result})
end
def tags
@current_tags.try(:split)
end
def tags=(tags)
- @current_tags = tags.to_a.join(' ')
+ @current_tags = tags.is_a?(Array) ? tags.to_a.join(' ') : tags
end
def load_fields
attr_keys = Zendesk.config['ticket'] || {}
@field_ids = {}
field_names = attr_keys.keys
- unless field_names.blank?
+ unless field_names.empty?
field_names = field_names.map(&:to_sym)
attr_keys.keys.each do |attr_name|
@field_ids[attr_name.to_sym] = attr_keys[attr_name]
View
@@ -14,6 +14,6 @@ def initialize(attrs = {})
def to_xml
result = {}
attributes.each{|attr| result[attr] = instance_variable_get(:"@#{attr.to_s}") }
- result.to_xml(:skip_instruct => true, :root=>:user)
+ SimpleXML.xml_out result, 'RootName' =>:user, 'AttrPrefix' => true
end
end
View
@@ -26,4 +26,9 @@
ticket.ticket_type_id.should == 3
end
+ it 'should convert to xml' do
+ ticket = Zendesk::Ticket.new( :description => 'ticket_description', :tags => 'one two three' )
+ ticket.to_xml.should == "<ticket>\n <description>ticket_description</description>\n <set_tags>one two three</set_tags>\n</ticket>\n"
+ end
+
end
Oops, something went wrong.

0 comments on commit eb4882d

Please sign in to comment.