zendesk 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ zendesk.yml
6
+ benchmark.rb
data/CHANGELOG.rdoc CHANGED
@@ -1,3 +1,9 @@
1
+ == 0.1.3
2
+ * enhancements
3
+ * removed requirement active_support
4
+ * work with XML only using ReXML
5
+ * removed jeweler dependecies
6
+
1
7
  == 0.1.2
2
8
  * bugfix
3
9
  * fixed missed field entries
data/Gemfile CHANGED
@@ -1,12 +1,8 @@
1
1
  source "http://rubygems.org"
2
- # Add dependencies required to use your gem here.
3
- # Example:
4
- # gem "activesupport", ">= 2.3.5"
2
+ gemspec
3
+
4
+ gem 'rest-client'
5
5
 
6
- # Add dependencies to develop your gem here.
7
- # Include everything needed to run rake, tests, features, etc.
8
6
  group :development do
9
- gem "bundler", "~> 1.0.0"
10
- gem "jeweler", "~> 1.6.0"
11
- # gem "rcov", ">= 0"
7
+ gem "rspec"
12
8
  end
data/Rakefile CHANGED
@@ -1,25 +1,2 @@
1
- # encoding: utf-8
2
-
3
- require 'rubygems'
4
1
  require 'bundler'
5
- require 'rake/rdoctask'
6
-
7
- begin
8
- Bundler.setup(:default, :development)
9
- rescue Bundler::BundlerError => e
10
- $stderr.puts e.message
11
- $stderr.puts "Run `bundle install` to install missing gems"
12
- exit e.status_code
13
- end
14
- require 'rake'
15
-
16
- require 'jeweler'
17
- Jeweler::Tasks.new do |gem|
18
- gem.name = "zendesk"
19
- gem.homepage = "http://github.com/aderyabin/zendesk"
20
- gem.license = "MIT"
21
- gem.summary = %Q{Ruby wrapper around the Zendesk API}
22
- gem.description = %Q{Ruby wrapper around the Zendesk API}
23
- gem.email = "deriabin@gmail.com"
24
- gem.authors = ["Andrey Deryabin"]
25
- end
2
+ Bundler::GemHelper.install_tasks
data/lib/zendesk.rb CHANGED
@@ -1,17 +1,17 @@
1
1
  # Zendesk
2
2
  require 'rubygems'
3
- require 'active_support'
4
3
  require 'rest_client'
5
4
  require 'yaml'
5
+ require 'rexml/document'
6
6
 
7
7
  module Zendesk
8
-
8
+
9
9
  class ConfigurationNotFound < NameError;
10
10
  end
11
-
12
-
13
- ZENDESK_ROOT = File.expand_path((defined?(Rails) && Rails.root.to_s.length > 0) ? Rails.root : ".") unless defined?(ZENDESK_ROOT)
14
- DEFAULT_CONFIG_PATH = File.join(ZENDESK_ROOT, 'config', 'zendesk.yml')
11
+
12
+
13
+ ZENDESK_ROOT = File.expand_path((defined?(Rails) && Rails.root.to_s.length > 0) ? Rails.root : ".") unless defined?(ZENDESK_ROOT)
14
+ DEFAULT_CONFIG_PATH = File.join(ZENDESK_ROOT, 'config', 'zendesk.yml')
15
15
 
16
16
  def self.load_configuration(config_path)
17
17
  exists = config_path && File.exists?(config_path)
@@ -22,16 +22,63 @@ module Zendesk
22
22
  def self.config
23
23
  @configuration ||= load_configuration(DEFAULT_CONFIG_PATH)
24
24
  end
25
-
25
+
26
26
  def self.resource
27
27
  @resource ||= RestClient::Resource.new config['host'], :user => config['user'], :password => config['password'], :timeout => 20, :open_timeout => 1
28
28
  end
29
+
30
+ def self.xml_in(xml_data)
31
+ xml_elements_to_hash REXML::Document.new(xml_data).root
32
+ end
33
+
34
+ def self.xml_elements_to_hash(element)
35
+ value = element.text
36
+ value = case element.attributes['type']
37
+ when 'integer'
38
+ value.to_i
39
+ when 'datetime'
40
+ DateTime.parse(element.text)
41
+ else
42
+ element.text
43
+ end if value
44
+
45
+ if element.elements.count > 0
46
+ if element.attributes['type'] == 'array'
47
+ value = element.elements.map{|el| xml_elements_to_hash(el) }
48
+ else
49
+ value = {}
50
+ element.elements.each{|el| value.merge! xml_elements_to_hash(el) }
51
+ end
52
+ end
53
+ { element.name.gsub('-', '_').to_sym => value }
54
+ end
55
+
56
+ def self.xml_out(hash)
57
+ doc = REXML::Document.new
58
+ doc.add_element hash_elements_to_xml(hash.keys[0], hash.values[0])
59
+ doc.to_s
60
+ end
29
61
 
62
+ def self.hash_elements_to_xml(key, value)
63
+ element = REXML::Element.new(key.to_s.gsub('_', '-'))
64
+ if value.is_a?(Array)
65
+ element.attributes['type'] = 'array'
66
+ value.each{ |val| element.add_element hash_elements_to_xml(val.keys[0], val.values[0]) }
67
+ elsif value.is_a?(Hash)
68
+ value.each_pair{ |key, v| element.add_element hash_elements_to_xml(key, v) }
69
+ else
70
+ element.text = value
71
+ end
72
+ element
73
+ end
74
+
75
+
76
+
30
77
  autoload :Resource, File.dirname(__FILE__) + '/zendesk/resource.rb'
31
78
  autoload :Comment, File.dirname(__FILE__) + '/zendesk/comment.rb'
32
79
  autoload :Ticket, File.dirname(__FILE__) + '/zendesk/ticket.rb'
33
80
  autoload :User, File.dirname(__FILE__) + '/zendesk/user.rb'
34
-
81
+
35
82
  autoload :Constants, File.dirname(__FILE__) + '/zendesk/lib/constants.rb'
36
83
  autoload :RestObject, File.dirname(__FILE__) + '/zendesk/lib/rest_object.rb'
37
84
  autoload :Properties, File.dirname(__FILE__) + '/zendesk/lib/properties.rb'
@@ -13,12 +13,12 @@ class Zendesk::Comment < Zendesk::Resource
13
13
  end
14
14
 
15
15
  def to_xml
16
- { :value => value, :is_public => is_public }.to_xml(:skip_instruct => true, :root=>:comment)
16
+ Zendesk.xml_out({:comment => { :value => value, :is_public => is_public }})
17
17
  end
18
18
 
19
19
  def save
20
20
  begin
21
- response = resource["tickets/#{ticket_id}.xml"].put self.to_xml, :content_type => 'application/xml'
21
+ response = Zendesk.resource["tickets/#{ticket_id}.xml"].put self.to_xml, :content_type => 'application/xml'
22
22
  return (200..300).include?(response.headers[:status].to_i)
23
23
  rescue Exception => e
24
24
  puts e.message
@@ -1,13 +1,27 @@
1
1
  module Zendesk::Properties
2
2
  module ClassMethods
3
3
 
4
- mattr_accessor :_attributes, :_protected_attributes, :_properties, :_datetimes
5
-
6
4
  @@_attributes = []
7
5
  @@_protected_attributes = []
8
6
  @@_properties = []
9
7
  @@_datetimes = []
10
8
 
9
+ def _attributes
10
+ @@_attributes
11
+ end
12
+
13
+ def _protected_attributes
14
+ @@_protected_attributes
15
+ end
16
+
17
+ def _properties
18
+ @@_properties
19
+ end
20
+
21
+ def _datetimes
22
+ @@_datetimes
23
+ end
24
+
11
25
  def attributes(*vars)
12
26
  @@_attributes.concat vars
13
27
  attr_accessor *vars
@@ -46,7 +60,7 @@ module Zendesk::Properties
46
60
 
47
61
  class_eval <<-END
48
62
  def #{method_name}=(value)
49
- @#{method_name}_id = #{method_name.upcase}.index(value)
63
+ @#{method_name}_id = #{method_name.upcase}.key(value)
50
64
  end
51
65
  END
52
66
  end
@@ -1,10 +1,5 @@
1
1
  module Zendesk::RestObject
2
2
  module ClassMethods
3
- def create(attrs = {})
4
- instance = new(attrs)
5
- instance.save
6
- end
7
-
8
3
  def find(id)
9
4
  begin
10
5
  new().load(id)
@@ -16,16 +11,16 @@ module Zendesk::RestObject
16
11
  end
17
12
 
18
13
  module InstanceMethods
19
- def path
20
- self.class.to_s.demodulize.downcase.pluralize
14
+ def model_name
15
+ self.class.to_s.gsub(/^.*::/, '').downcase
21
16
  end
22
17
 
23
18
  def save
24
19
  begin
25
20
  response = if self.id
26
- Zendesk.resource["#{path}/#{id}.xml"].put self.to_xml, :content_type => 'application/xml'
21
+ Zendesk.resource["#{model_name}s/#{id}.xml"].put self.to_xml, :content_type => 'application/xml'
27
22
  else
28
- Zendesk.resource["#{path}.xml"].post self.to_xml, :content_type => 'application/xml'
23
+ Zendesk.resource["#{model_name}s.xml"].post self.to_xml, :content_type => 'application/xml'
29
24
  end
30
25
  if (200..300).include?(response.headers[:status].to_i)
31
26
  load(id || response.headers[:location].scan(/\d+/).first.to_i)
@@ -51,7 +46,7 @@ module Zendesk::RestObject
51
46
 
52
47
  def load(id)
53
48
  begin
54
- data = load_data(Zendesk.resource["#{path}/#{id}.xml"].get)[path.singularize]
49
+ data = load_data(Zendesk.resource["#{model_name}s/#{id}.xml"].get)[model_name.to_sym]
55
50
  load_attributes(data)
56
51
  load_protected_attributes(data)
57
52
  load_field_entries(data) if respond_to?(:load_field_entries)
@@ -10,6 +10,12 @@ class Zendesk::Resource
10
10
  end
11
11
 
12
12
  def load_data(xml_stream)
13
- Hash.from_xml(xml_stream)
13
+ Zendesk.xml_in(xml_stream)
14
+ end
15
+
16
+ def self.create(attrs = {})
17
+ instance = new(attrs)
18
+ instance.save
19
+ instance
14
20
  end
15
21
  end
@@ -8,8 +8,8 @@ class Zendesk::Ticket < Zendesk::Resource
8
8
  properties :status, :ticket_type, :priority, :via
9
9
 
10
10
 
11
- alias_attribute :id, :nice_id
12
- alias_attribute :set_tags, :current_tags
11
+ alias :id :nice_id
12
+ alias :set_tags :current_tags
13
13
 
14
14
  def initialize(attrs = {})
15
15
  @comments = []
@@ -18,19 +18,21 @@ class Zendesk::Ticket < Zendesk::Resource
18
18
  super
19
19
  end
20
20
 
21
- # Filling field methods from imported data.
21
+ # Filling custom field methods from imported data.
22
22
  # If field is not pointed in config it will be missed
23
23
  def load_field_entries(data)
24
- data['ticket_field_entries'].each do |field_entry|
25
- method_name = @field_ids.index(field_entry['ticket_field_id'])
26
- send("#{method_name}=", field_entry['value']) if method_name
24
+ if data[:ticket_field_entries]
25
+ data[:ticket_field_entries].each do |field_entry|
26
+ method_name = @field_ids.key(field_entry[:ticket_field_id])
27
+ send("#{method_name}=", field_entry[:value]) if method_name
28
+ end
27
29
  end
28
30
  end
29
31
 
30
32
  def load_comments(data)
31
33
  @comments = []
32
- data['comments'].each do |comment|
33
- @comments << Zendesk::Comment.new(comment)
34
+ data[:comments].each do |comment|
35
+ @comments << Zendesk::Comment.new(comment[:comment])
34
36
  end
35
37
  end
36
38
 
@@ -53,20 +55,21 @@ class Zendesk::Ticket < Zendesk::Resource
53
55
  def to_xml
54
56
  result = {}
55
57
  (attributes - @field_ids.keys).each do |obj|
56
- if instance_variable_get(:"@#{obj.to_s}").present?
57
- result[obj.to_s.downcase] = instance_variable_get(:"@#{obj.to_s}")
58
+ if val = instance_variable_get(:"@#{obj.to_s}")
59
+ result[obj.to_s.downcase] = val
58
60
  end
59
61
  end
60
62
 
61
- result[:ticket_field_entries] = []
63
+ ticket_field_entries = []
62
64
  @field_ids.each_pair do |key, value|
63
- if instance_variable_get(:"@#{key.to_s}").present?
64
- result[:ticket_field_entries] << {:ticket_field_id => value, :value => instance_variable_get(:"@#{key.to_s}").to_s}
65
+ if val = instance_variable_get(:"@#{key.to_s}")
66
+ ticket_field_entries << { :ticket_field_id => value, :value => val.to_s }
65
67
  end
66
68
  end
69
+ result[:ticket_field_entries] = ticket_field_entries unless ticket_field_entries.empty?
67
70
 
68
- result[:set_tags] = current_tags
69
- result.to_xml(:skip_instruct => true, :root=>:ticket)
71
+ result[:set_tags] = current_tags if current_tags
72
+ Zendesk.xml_out({:ticket => result})
70
73
  end
71
74
 
72
75
  def tags
@@ -74,14 +77,14 @@ class Zendesk::Ticket < Zendesk::Resource
74
77
  end
75
78
 
76
79
  def tags=(tags)
77
- @current_tags = tags.to_a.join(' ')
80
+ @current_tags = tags.is_a?(Array) ? tags.to_a.join(' ') : tags
78
81
  end
79
82
 
80
83
  def load_fields
81
84
  attr_keys = Zendesk.config['ticket'] || {}
82
85
  @field_ids = {}
83
86
  field_names = attr_keys.keys
84
- unless field_names.blank?
87
+ unless field_names.empty?
85
88
  field_names = field_names.map(&:to_sym)
86
89
  attr_keys.keys.each do |attr_name|
87
90
  @field_ids[attr_name.to_sym] = attr_keys[attr_name]
data/lib/zendesk/user.rb CHANGED
@@ -14,6 +14,6 @@ class Zendesk::User < Zendesk::Resource
14
14
  def to_xml
15
15
  result = {}
16
16
  attributes.each{|attr| result[attr] = instance_variable_get(:"@#{attr.to_s}") }
17
- result.to_xml(:skip_instruct => true, :root=>:user)
17
+ SimpleXML.xml_out result, 'RootName' =>:user, 'AttrPrefix' => true
18
18
  end
19
19
  end
@@ -1,3 +1,3 @@
1
1
  module Zendesk
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.3"
3
3
  end
data/spec/ticket_spec.rb CHANGED
@@ -26,4 +26,9 @@ require File.dirname(__FILE__) + '/../lib/zendesk'
26
26
  ticket.ticket_type_id.should == 3
27
27
  end
28
28
 
29
+ it 'should convert to xml' do
30
+ ticket = Zendesk::Ticket.new( :description => 'ticket_description', :tags => 'one_two' )
31
+ ticket.to_xml.should == "<ticket><description>ticket_description</description><set-tags>one_two</set-tags></ticket>"
32
+ end
33
+
29
34
  end
data/zendesk.gemspec CHANGED
@@ -1,62 +1,21 @@
1
- # Generated by jeweler
2
- # DO NOT EDIT THIS FILE DIRECTLY
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
1
  # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "zendesk/version"
5
4
 
6
5
  Gem::Specification.new do |s|
7
6
  s.name = %q{zendesk}
8
- s.version = "0.1.2"
9
-
10
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
7
+ s.version = Zendesk::VERSION
8
+ s.platform = Gem::Platform::RUBY
11
9
  s.authors = ["Andrey Deryabin"]
12
- s.date = %q{2011-05-19}
10
+ s.date = %q{2011-05-20}
11
+ s.summary = %q{Ruby wrapper around the Zendesk API}
13
12
  s.description = %q{Ruby wrapper around the Zendesk API}
14
13
  s.email = %q{deriabin@gmail.com}
15
- s.extra_rdoc_files = [
16
- "LICENSE.txt",
17
- "README.rdoc"
18
- ]
19
- s.files = [
20
- "CHANGELOG.rdoc",
21
- "Gemfile",
22
- "LICENSE.txt",
23
- "README.rdoc",
24
- "Rakefile",
25
- "VERSION",
26
- "lib/zendesk.rb",
27
- "lib/zendesk/comment.rb",
28
- "lib/zendesk/lib/constants.rb",
29
- "lib/zendesk/lib/properties.rb",
30
- "lib/zendesk/lib/rest_object.rb",
31
- "lib/zendesk/resource.rb",
32
- "lib/zendesk/ticket.rb",
33
- "lib/zendesk/user.rb",
34
- "lib/zendesk/version.rb",
35
- "spec/comment_spec.rb",
36
- "spec/spec_helper.rb",
37
- "spec/ticket_spec.rb",
38
- "zendesk.gemspec"
39
- ]
40
14
  s.homepage = %q{http://github.com/aderyabin/zendesk}
41
- s.licenses = ["MIT"]
42
- s.require_paths = ["lib"]
43
- s.rubygems_version = %q{1.3.7}
44
- s.summary = %q{Ruby wrapper around the Zendesk API}
45
-
46
- if s.respond_to? :specification_version then
47
- current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
48
- s.specification_version = 3
49
15
 
50
- if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
51
- s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
52
- s.add_development_dependency(%q<jeweler>, ["~> 1.6.0"])
53
- else
54
- s.add_dependency(%q<bundler>, ["~> 1.0.0"])
55
- s.add_dependency(%q<jeweler>, ["~> 1.6.0"])
56
- end
57
- else
58
- s.add_dependency(%q<bundler>, ["~> 1.0.0"])
59
- s.add_dependency(%q<jeweler>, ["~> 1.6.0"])
60
- end
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
61
20
  end
62
21
 
metadata CHANGED
@@ -1,13 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zendesk
3
3
  version: !ruby/object:Gem::Version
4
- hash: 31
5
- prerelease: false
6
- segments:
7
- - 0
8
- - 1
9
- - 2
10
- version: 0.1.2
4
+ prerelease:
5
+ version: 0.1.3
11
6
  platform: ruby
12
7
  authors:
13
8
  - Andrey Deryabin
@@ -15,57 +10,25 @@ autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
12
 
18
- date: 2011-05-19 00:00:00 +04:00
13
+ date: 2011-05-20 00:00:00 +04:00
19
14
  default_executable:
20
- dependencies:
21
- - !ruby/object:Gem::Dependency
22
- prerelease: false
23
- type: :development
24
- name: bundler
25
- version_requirements: &id001 !ruby/object:Gem::Requirement
26
- none: false
27
- requirements:
28
- - - ~>
29
- - !ruby/object:Gem::Version
30
- hash: 23
31
- segments:
32
- - 1
33
- - 0
34
- - 0
35
- version: 1.0.0
36
- requirement: *id001
37
- - !ruby/object:Gem::Dependency
38
- prerelease: false
39
- type: :development
40
- name: jeweler
41
- version_requirements: &id002 !ruby/object:Gem::Requirement
42
- none: false
43
- requirements:
44
- - - ~>
45
- - !ruby/object:Gem::Version
46
- hash: 15
47
- segments:
48
- - 1
49
- - 6
50
- - 0
51
- version: 1.6.0
52
- requirement: *id002
15
+ dependencies: []
16
+
53
17
  description: Ruby wrapper around the Zendesk API
54
18
  email: deriabin@gmail.com
55
19
  executables: []
56
20
 
57
21
  extensions: []
58
22
 
59
- extra_rdoc_files:
60
- - LICENSE.txt
61
- - README.rdoc
23
+ extra_rdoc_files: []
24
+
62
25
  files:
26
+ - .gitignore
63
27
  - CHANGELOG.rdoc
64
28
  - Gemfile
65
29
  - LICENSE.txt
66
30
  - README.rdoc
67
31
  - Rakefile
68
- - VERSION
69
32
  - lib/zendesk.rb
70
33
  - lib/zendesk/comment.rb
71
34
  - lib/zendesk/lib/constants.rb
@@ -81,8 +44,8 @@ files:
81
44
  - zendesk.gemspec
82
45
  has_rdoc: true
83
46
  homepage: http://github.com/aderyabin/zendesk
84
- licenses:
85
- - MIT
47
+ licenses: []
48
+
86
49
  post_install_message:
87
50
  rdoc_options: []
88
51
 
@@ -93,25 +56,21 @@ required_ruby_version: !ruby/object:Gem::Requirement
93
56
  requirements:
94
57
  - - ">="
95
58
  - !ruby/object:Gem::Version
96
- hash: 3
97
- segments:
98
- - 0
99
59
  version: "0"
100
60
  required_rubygems_version: !ruby/object:Gem::Requirement
101
61
  none: false
102
62
  requirements:
103
63
  - - ">="
104
64
  - !ruby/object:Gem::Version
105
- hash: 3
106
- segments:
107
- - 0
108
65
  version: "0"
109
66
  requirements: []
110
67
 
111
68
  rubyforge_project:
112
- rubygems_version: 1.3.7
69
+ rubygems_version: 1.6.2
113
70
  signing_key:
114
71
  specification_version: 3
115
72
  summary: Ruby wrapper around the Zendesk API
116
- test_files: []
117
-
73
+ test_files:
74
+ - spec/comment_spec.rb
75
+ - spec/spec_helper.rb
76
+ - spec/ticket_spec.rb
data/VERSION DELETED
@@ -1 +0,0 @@
1
- 0.1.2