zendesk-api 0.2.2 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Manifest CHANGED
@@ -1,6 +1,7 @@
1
1
  Manifest
2
2
  README.markdown
3
3
  Rakefile
4
+ geminstaller.yml
4
5
  lib/console.rb
5
6
  lib/zendesk-api.rb
6
7
  lib/zendesk.rb
@@ -9,9 +10,11 @@ lib/zendesk/entry.rb
9
10
  lib/zendesk/forum.rb
10
11
  lib/zendesk/group.rb
11
12
  lib/zendesk/main.rb
13
+ lib/zendesk/main.rb.orig
12
14
  lib/zendesk/organization.rb
13
15
  lib/zendesk/search.rb
14
16
  lib/zendesk/tag.rb
15
17
  lib/zendesk/ticket.rb
16
18
  lib/zendesk/user.rb
19
+ spec/zendesk/main_spec.rb
17
20
  zendesk-api.gemspec
data/Rakefile CHANGED
@@ -2,7 +2,7 @@ require 'rubygems'
2
2
  require 'rake'
3
3
  require 'echoe'
4
4
 
5
- Echoe.new('zendesk-api', '0.2.2') do |p|
5
+ Echoe.new('zendesk-api', '0.2.3') do |p|
6
6
  p.description = "RubyGem wrapper for REST API to http://zendesk.com"
7
7
  p.author = "Peter Ericson"
8
8
  p.url = "http://github.com/pgericson/zendesk-api"
data/geminstaller.yml ADDED
@@ -0,0 +1,11 @@
1
+ defaults:
2
+ install_options: --source http://gems.github.com --source http://gems.rubyforge.org --no-ri --no-rdoc
3
+
4
+ gems:
5
+ - name: rspec
6
+ version: '=1.3.0'
7
+ - name: rspec-rails
8
+ version: '=1.3.2'
9
+ - name: curb
10
+ version: '=0.6.7'
11
+
data/lib/zendesk/main.rb CHANGED
@@ -8,13 +8,13 @@ module Zendesk
8
8
  @username = username
9
9
  @password = password
10
10
  @options = options
11
- if options[:format] && ['xml','json'].any?{|f| f == options[:format]}
11
+ if options[:format] && ['xml', 'json'].any?{|f| f == options[:format]}
12
12
  @format = options[:format]
13
13
  else
14
14
  @format = 'xml'
15
15
  end
16
16
  end
17
-
17
+
18
18
  def main_url
19
19
  url_prefix = @options[:ssl] ? "https://" : "http://"
20
20
  url_postfix = ".zendesk.com/"
@@ -26,32 +26,40 @@ module Zendesk
26
26
  input
27
27
  else
28
28
  input.to_xml({:root => function_name})
29
- end
29
+ end
30
30
  end
31
31
 
32
32
  def make_request(end_url, body = {})
33
33
  curl = Curl::Easy.new(main_url + end_url + ".#{@format}")
34
34
  curl.userpwd = "#{@username}:#{@password}"
35
35
  unless body.empty?
36
- if body.values.first.is_a?(Hash)
37
- final_body = body.values.first.to_xml
38
- elsif body.values.first.is_a?(String)
39
- final_body = body.values.first
40
- end
41
-
42
- if body[:create]
43
- curl.headers = "Content-Type: application/xml"
44
- curl.http_post(final_body)
45
- elsif body[:update]
46
- curl.headers = "Content-Type: application/xml"
47
- curl.http_put(final_body)
48
- elsif body[:destroy]
49
- curl.http_delete(final_body)
50
- elsif body[:list]
51
- params = "?" + body[:list].to_a.map do |p|
52
- "#{p[0]}=#{p[1]}"
36
+ if body[:list]
37
+ params = "?" + body[:list].map do |k, v|
38
+ if v.is_a?(Array)
39
+ v.map do |val|
40
+ "#{k}[]=#{val}"
41
+ end.join("&")
42
+ else
43
+ "#{k}=#{v}"
44
+ end
53
45
  end.join("&")
54
46
  curl.url = curl.url + params
47
+ else
48
+ if body.values.first.is_a?(Hash)
49
+ final_body = body.values.first.to_xml
50
+ elsif body.values.first.is_a?(String)
51
+ final_body = body.values.first
52
+ end
53
+
54
+ if body[:create]
55
+ curl.headers = "Content-Type: application/xml"
56
+ curl.http_post(final_body)
57
+ elsif body[:update]
58
+ curl.headers = "Content-Type: application/xml"
59
+ curl.http_put(final_body)
60
+ elsif body[:destroy]
61
+ curl.http_delete(final_body)
62
+ end
55
63
  end
56
64
  end
57
65
  curl.perform
@@ -61,20 +69,22 @@ module Zendesk
61
69
  end
62
70
  Response.new(curl)
63
71
  end
64
-
72
+
65
73
  class Response
66
-
67
- attr_reader :status, :body, :headers_raw, :headers
68
-
74
+
75
+ attr_reader :status, :body, :headers_raw, :headers, :curl
76
+
69
77
  def initialize(curl)
78
+ @curl = curl
70
79
  @status=curl.response_code
71
80
  @body=curl.body_str
72
81
  @headers_raw=curl.header_str
73
82
  parse_headers
74
83
  end
75
-
84
+
76
85
  def parse_headers
77
86
  hs={}
87
+ return hs if headers_raw.nil? or headers_raw==""
78
88
  headers_raw.split("\r\n")[1..-1].each do |h|
79
89
  # Rails.logger.info h
80
90
  m=h.match(/([^:]+):\s?(.*)/)
@@ -84,7 +94,7 @@ module Zendesk
84
94
  end
85
95
  @headers=hs
86
96
  end
87
-
97
+
88
98
  end
89
99
 
90
100
  include Zendesk::User
@@ -0,0 +1,118 @@
1
+ module Zendesk
2
+ class Main
3
+ attr_accessor :main_url, :format
4
+ attr_reader :response_raw, :response
5
+
6
+ def initialize(account, username, password, options = {})
7
+ @account = account
8
+ @username = username
9
+ @password = password
10
+ @options = options
11
+ if options[:format] && ['xml', 'json'].any?{|f| f == options[:format]}
12
+ @format = options[:format]
13
+ else
14
+ @format = 'xml'
15
+ end
16
+ end
17
+
18
+ def main_url
19
+ url_prefix = @options[:ssl] ? "https://" : "http://"
20
+ url_postfix = ".zendesk.com/"
21
+ url = url_prefix + @account + url_postfix
22
+ end
23
+
24
+ def self.to_xml(function_name, input)
25
+ if input.is_a?(String)
26
+ input
27
+ else
28
+ input.to_xml({:root => function_name})
29
+ end
30
+ end
31
+
32
+ def make_request(end_url, body = {})
33
+ curl = Curl::Easy.new(main_url + end_url + ".#{@format}")
34
+ curl.userpwd = "#{@username}:#{@password}"
35
+ unless body.empty?
36
+ if body[:list]
37
+ params = "?" + body[:list].map do |k, v|
38
+ if v.is_a?(Array)
39
+ v.map do |val|
40
+ "#{k}[]=#{val}"
41
+ end.join("&")
42
+ else
43
+ "#{k}=#{v}"
44
+ end
45
+ end.join("&")
46
+ curl.url = curl.url + params
47
+ else
48
+ if body.values.first.is_a?(Hash)
49
+ final_body = body.values.first.to_xml
50
+ elsif body.values.first.is_a?(String)
51
+ final_body = body.values.first
52
+ end
53
+
54
+ if body[:create]
55
+ curl.headers = "Content-Type: application/xml"
56
+ curl.http_post(final_body)
57
+ elsif body[:update]
58
+ curl.headers = "Content-Type: application/xml"
59
+ curl.http_put(final_body)
60
+ elsif body[:destroy]
61
+ curl.http_delete(final_body)
62
+ end
63
+ end
64
+ end
65
+ curl.perform
66
+
67
+ if curl.body_str == "<error>Couldn't authenticate you</error>"
68
+ return "string" #raise CouldNotAuthenticateYou
69
+ end
70
+ Response.new(curl)
71
+ end
72
+
73
+ class Response
74
+ <<<<<<< HEAD:lib/zendesk/main.rb
75
+
76
+ attr_reader :status, :body, :headers_raw, :headers, :curl
77
+
78
+ def initialize(curl)
79
+ @curl=curl
80
+ =======
81
+
82
+ attr_reader :status, :body, :headers_raw, :headers, :curl
83
+
84
+ def initialize(curl)
85
+ @curl = curl
86
+ >>>>>>> ungulation/master:lib/zendesk/main.rb
87
+ @status=curl.response_code
88
+ @body=curl.body_str
89
+ @headers_raw=curl.header_str
90
+ parse_headers
91
+ end
92
+
93
+ def parse_headers
94
+ hs={}
95
+ return hs if headers_raw.nil? or headers_raw==""
96
+ headers_raw.split("\r\n")[1..-1].each do |h|
97
+ # Rails.logger.info h
98
+ m=h.match(/([^:]+):\s?(.*)/)
99
+ next if m.nil? or m[2].nil?
100
+ # Rails.logger.info m.inspect
101
+ hs[m[1]]=m[2]
102
+ end
103
+ @headers=hs
104
+ end
105
+
106
+ end
107
+
108
+ include Zendesk::User
109
+ include Zendesk::Organization
110
+ include Zendesk::Group
111
+ include Zendesk::Ticket
112
+ include Zendesk::Attachment
113
+ include Zendesk::Tag
114
+ include Zendesk::Forum
115
+ include Zendesk::Entry
116
+ include Zendesk::Search
117
+ end
118
+ end
@@ -0,0 +1,47 @@
1
+ require "spec"
2
+ require 'lib/zendesk-api'
3
+
4
+ describe Zendesk::Main, "basic" do
5
+ before(:each) do
6
+ @account = "this_account"
7
+ @username = "this_username"
8
+ @password = "this_password"
9
+ @zendesk = Zendesk::Main.new(@account, @username, @password)
10
+ end
11
+
12
+ it "should have the correct mail_url with no ssl options" do
13
+ @zendesk.main_url.should == "http://#{@account}.zendesk.com/"
14
+ end
15
+
16
+ it "should have the correct mail_url with ssl options" do
17
+ @zendesk = Zendesk::Main.new(@account, @username, @password, :ssl => true)
18
+ @zendesk.main_url.should == "https://#{@account}.zendesk.com/"
19
+ end
20
+ end
21
+
22
+ describe Zendesk::Main, 'make_request' do
23
+ before do
24
+ curl_object = Curl::Easy.method(:new)
25
+ Curl::Easy.stub!(:new).and_return do |*args|
26
+ curl = curl_object.call(*args)
27
+ curl.stub!(:perform)
28
+ curl.stub!(:header_str) { "adsf\r\ndsaf"}
29
+ curl
30
+ end
31
+ end
32
+
33
+ it "should construct array url for search" do
34
+ zendesk = Zendesk::Main.new('my_company', "some_login", "some_password")
35
+ params = {
36
+ :foo => "bar",
37
+ :foo_list => [1, 2, 3]
38
+ }
39
+
40
+ response = zendesk.make_request("search", :list => params)
41
+ response.curl.url.should =~ /foo=bar/
42
+ response.curl.url.should =~ /foo_list\[\]=1/
43
+ response.curl.url.should =~ /foo_list\[\]=2/
44
+ response.curl.url.should =~ /foo_list\[\]=3/
45
+
46
+ end
47
+ end
data/zendesk-api.gemspec CHANGED
@@ -2,15 +2,15 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{zendesk-api}
5
- s.version = "0.2.2"
5
+ s.version = "0.2.3"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Peter Ericson"]
9
- s.date = %q{2010-04-09}
9
+ s.date = %q{2010-04-12}
10
10
  s.description = %q{RubyGem wrapper for REST API to http://zendesk.com}
11
11
  s.email = %q{pg.ericson@gmail.com}
12
- s.extra_rdoc_files = ["README.markdown", "lib/console.rb", "lib/zendesk-api.rb", "lib/zendesk.rb", "lib/zendesk/attachment.rb", "lib/zendesk/entry.rb", "lib/zendesk/forum.rb", "lib/zendesk/group.rb", "lib/zendesk/main.rb", "lib/zendesk/organization.rb", "lib/zendesk/search.rb", "lib/zendesk/tag.rb", "lib/zendesk/ticket.rb", "lib/zendesk/user.rb"]
13
- s.files = ["Manifest", "README.markdown", "Rakefile", "lib/console.rb", "lib/zendesk-api.rb", "lib/zendesk.rb", "lib/zendesk/attachment.rb", "lib/zendesk/entry.rb", "lib/zendesk/forum.rb", "lib/zendesk/group.rb", "lib/zendesk/main.rb", "lib/zendesk/organization.rb", "lib/zendesk/search.rb", "lib/zendesk/tag.rb", "lib/zendesk/ticket.rb", "lib/zendesk/user.rb", "zendesk-api.gemspec"]
12
+ s.extra_rdoc_files = ["README.markdown", "lib/console.rb", "lib/zendesk-api.rb", "lib/zendesk.rb", "lib/zendesk/attachment.rb", "lib/zendesk/entry.rb", "lib/zendesk/forum.rb", "lib/zendesk/group.rb", "lib/zendesk/main.rb", "lib/zendesk/main.rb.orig", "lib/zendesk/organization.rb", "lib/zendesk/search.rb", "lib/zendesk/tag.rb", "lib/zendesk/ticket.rb", "lib/zendesk/user.rb"]
13
+ s.files = ["Manifest", "README.markdown", "Rakefile", "geminstaller.yml", "lib/console.rb", "lib/zendesk-api.rb", "lib/zendesk.rb", "lib/zendesk/attachment.rb", "lib/zendesk/entry.rb", "lib/zendesk/forum.rb", "lib/zendesk/group.rb", "lib/zendesk/main.rb", "lib/zendesk/main.rb.orig", "lib/zendesk/organization.rb", "lib/zendesk/search.rb", "lib/zendesk/tag.rb", "lib/zendesk/ticket.rb", "lib/zendesk/user.rb", "spec/zendesk/main_spec.rb", "zendesk-api.gemspec"]
14
14
  s.homepage = %q{http://github.com/pgericson/zendesk-api}
15
15
  s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Zendesk-api", "--main", "README.markdown"]
16
16
  s.require_paths = ["lib"]
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 2
8
- - 2
9
- version: 0.2.2
8
+ - 3
9
+ version: 0.2.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - Peter Ericson
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-04-09 00:00:00 +02:00
17
+ date: 2010-04-12 00:00:00 +02:00
18
18
  default_executable:
19
19
  dependencies: []
20
20
 
@@ -34,6 +34,7 @@ extra_rdoc_files:
34
34
  - lib/zendesk/forum.rb
35
35
  - lib/zendesk/group.rb
36
36
  - lib/zendesk/main.rb
37
+ - lib/zendesk/main.rb.orig
37
38
  - lib/zendesk/organization.rb
38
39
  - lib/zendesk/search.rb
39
40
  - lib/zendesk/tag.rb
@@ -43,6 +44,7 @@ files:
43
44
  - Manifest
44
45
  - README.markdown
45
46
  - Rakefile
47
+ - geminstaller.yml
46
48
  - lib/console.rb
47
49
  - lib/zendesk-api.rb
48
50
  - lib/zendesk.rb
@@ -51,11 +53,13 @@ files:
51
53
  - lib/zendesk/forum.rb
52
54
  - lib/zendesk/group.rb
53
55
  - lib/zendesk/main.rb
56
+ - lib/zendesk/main.rb.orig
54
57
  - lib/zendesk/organization.rb
55
58
  - lib/zendesk/search.rb
56
59
  - lib/zendesk/tag.rb
57
60
  - lib/zendesk/ticket.rb
58
61
  - lib/zendesk/user.rb
62
+ - spec/zendesk/main_spec.rb
59
63
  - zendesk-api.gemspec
60
64
  has_rdoc: true
61
65
  homepage: http://github.com/pgericson/zendesk-api