zendesk-api 0.2.4 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/Manifest CHANGED
@@ -10,11 +10,10 @@ lib/zendesk/entry.rb
10
10
  lib/zendesk/forum.rb
11
11
  lib/zendesk/group.rb
12
12
  lib/zendesk/main.rb
13
- lib/zendesk/main.rb.orig
14
13
  lib/zendesk/organization.rb
15
14
  lib/zendesk/search.rb
16
15
  lib/zendesk/tag.rb
17
16
  lib/zendesk/ticket.rb
18
17
  lib/zendesk/user.rb
19
18
  spec/zendesk/main_spec.rb
20
- zendesk-api.gemspec
19
+ spec/zendesk/user_spec.rb
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.4') do |p|
5
+ Echoe.new('zendesk-api', '0.3.0') 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"
@@ -29,40 +29,45 @@ module Zendesk
29
29
  end
30
30
  end
31
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
32
+
33
+ def params_list(list)
34
+ params = "?" + list.map do |k, v|
35
+ if v.is_a?(Array)
36
+ v.map do |val|
37
+ "#{k}[]=#{val}"
45
38
  end.join("&")
46
- curl.url = curl.url + params
47
39
  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
62
- end
40
+ "#{k}=#{v}"
63
41
  end
42
+ end.join("&")
43
+ end
44
+
45
+ def string_body(body)
46
+ if body.values.first.is_a?(Hash)
47
+ body.values.first.to_xml.strip
48
+ elsif body.values.first.is_a?(String)
49
+ body.values.first
50
+ end
51
+ end
52
+
53
+ def make_request(end_url, body = {})
54
+ curl = Curl::Easy.new(main_url + end_url + ".#{@format}")
55
+ curl.userpwd = "#{@username}:#{@password}"
56
+ if body.empty? or body[:list]
57
+ curl.url = curl.url + params_list(body[:list]) if body[:list]
58
+ curl.perform
59
+ elsif body[:create]
60
+ curl.headers = "Content-Type: application/xml"
61
+ curl.http_post(string_body(body))
62
+ elsif body[:update]
63
+ # PUT seems badly broken, at least I can't get it to work without always
64
+ # raising an exception about rewinding the data stream
65
+ # curl.http_put(final_body)
66
+ curl.headers = { "Content-Type" => "application/xml", "X-Http-Method-Override" => "put" }
67
+ curl.http_post(string_body(body))
68
+ elsif body[:destroy]
69
+ curl.http_delete
64
70
  end
65
- curl.perform
66
71
 
67
72
  if curl.body_str == "<error>Couldn't authenticate you</error>"
68
73
  return "string" #raise CouldNotAuthenticateYou
@@ -72,13 +77,14 @@ module Zendesk
72
77
 
73
78
  class Response
74
79
 
75
- attr_reader :status, :body, :headers_raw, :headers, :curl
80
+ attr_reader :status, :body, :headers_raw, :headers, :curl, :url
76
81
 
77
82
  def initialize(curl)
78
83
  @curl = curl
79
- @status=curl.response_code
80
- @body=curl.body_str
81
- @headers_raw=curl.header_str
84
+ @url = curl.url
85
+ @status = curl.response_code
86
+ @body = curl.body_str
87
+ @headers_raw = curl.header_str
82
88
  parse_headers
83
89
  end
84
90
 
@@ -17,8 +17,8 @@ module Zendesk
17
17
  make_request("users", :create => Zendesk::Main.to_xml('user', input))
18
18
  end
19
19
 
20
- def update_user(input)
21
- make_request("users", :update => Zendesk::Main.to_xml('user', input))
20
+ def update_user(id, input)
21
+ make_request("users/#{id}", :update => Zendesk::Main.to_xml('user', input))
22
22
  end
23
23
 
24
24
  def delete_user(id)
@@ -24,7 +24,7 @@ describe Zendesk::Main, 'make_request' do
24
24
  curl_object = Curl::Easy.method(:new)
25
25
  Curl::Easy.stub!(:new).and_return do |*args|
26
26
  curl = curl_object.call(*args)
27
- curl.stub!(:perform)
27
+ curl.should_receive(:perform)
28
28
  curl.stub!(:header_str) { "adsf\r\ndsaf"}
29
29
  curl
30
30
  end
@@ -38,10 +38,10 @@ describe Zendesk::Main, 'make_request' do
38
38
  }
39
39
 
40
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/
41
+ response.url.should =~ /foo=bar/
42
+ response.url.should =~ /foo_list\[\]=1/
43
+ response.url.should =~ /foo_list\[\]=2/
44
+ response.url.should =~ /foo_list\[\]=3/
45
45
 
46
46
  end
47
47
  end
@@ -0,0 +1,92 @@
1
+ require "spec"
2
+ require 'lib/zendesk-api'
3
+
4
+ describe Zendesk::Main, 'user api' do
5
+ # before do
6
+ # end
7
+
8
+ it "should be able to create" do
9
+ data={"email" => "bob@aol.com", "name" => "Bob Jones", "roles" => 0, "restriction-id" => 4}
10
+ curl_object = Curl::Easy.method(:new)
11
+ Curl::Easy.stub!(:new).and_return do |*args|
12
+ curl = curl_object.call(*args)
13
+ # curl.stub!(:http_post)
14
+ body=Zendesk::Main.to_xml('user', data)
15
+ curl.should_receive(:http_post).with(body)
16
+ curl.stub!(:perform)
17
+ curl.stub!(:header_str) { "\r\nLocation: http://account.zendesk.com/users/23.xml\r\ndsaf: smoke"}
18
+ curl.stub!(:response_code).and_return(201)
19
+ curl
20
+ end
21
+ zendesk = Zendesk::Main.new('my_company', "some_login", "some_password")
22
+ response = zendesk.create_user(data)
23
+ response.url.should =~ %r{/users.xml}
24
+ response.headers["Location"].should =~ %r{/users/23.xml}
25
+ response.status.should == 201
26
+ end
27
+
28
+ it "should be able to update" do
29
+ data={"email" => "bob@aol.com", "name" => "Bob Jones", "roles" => 0, "restriction-id" => 4}
30
+ curl_object = Curl::Easy.method(:new)
31
+ Curl::Easy.stub!(:new).and_return do |*args|
32
+ curl = curl_object.call(*args)
33
+ curl.stub!(:perform)
34
+ body=Zendesk::Main.to_xml('user', data)
35
+ curl.should_receive(:http_post).with(body)
36
+ curl.stub!(:header_str) { "\r\ntest: blah"}
37
+ curl.stub!(:response_code).and_return(200)
38
+ curl
39
+ end
40
+ zendesk = Zendesk::Main.new('my_company', "some_login", "some_password")
41
+ response = zendesk.update_user(39,data)
42
+ response.curl.headers["X-Http-Method-Override"].should == "put"
43
+ response.url.should =~ %r{/users/39.xml$}
44
+ response.status.should == 200
45
+ end
46
+
47
+ it "should be able to get" do
48
+ curl_object = Curl::Easy.method(:new)
49
+ Curl::Easy.stub!(:new).and_return do |*args|
50
+ curl = curl_object.call(*args)
51
+ curl.stub!(:perform)
52
+ curl.stub!(:header_str) { "\r\ntest: blah"}
53
+ curl.stub!(:response_code).and_return(200)
54
+ curl
55
+ end
56
+ zendesk = Zendesk::Main.new('my_company', "some_login", "some_password")
57
+ response = zendesk.get_user(13)
58
+ response.url.should =~ %r{/users/13.xml$}
59
+ response.status.should == 200
60
+ end
61
+
62
+ it "should be able to list all" do
63
+ curl_object = Curl::Easy.method(:new)
64
+ Curl::Easy.stub!(:new).and_return do |*args|
65
+ curl = curl_object.call(*args)
66
+ curl.stub!(:perform)
67
+ curl.stub!(:header_str) { "\r\ntest: blah"}
68
+ curl.stub!(:response_code).and_return(200)
69
+ curl
70
+ end
71
+ zendesk = Zendesk::Main.new('my_company', "some_login", "some_password")
72
+ response = zendesk.get_users
73
+ response.url.should =~ %r{/users.xml$}
74
+ response.status.should == 200
75
+ end
76
+
77
+ it "should be able to delete" do
78
+ curl_object = Curl::Easy.method(:new)
79
+ Curl::Easy.stub!(:new).and_return do |*args|
80
+ curl = curl_object.call(*args)
81
+ curl.stub!(:http_delete)
82
+ curl.stub!(:perform)
83
+ curl.stub!(:response_code).and_return(200)
84
+ curl.stub!(:header_str) { "\r\nadsf\r\ndsaf"}
85
+ curl
86
+ end
87
+ zendesk = Zendesk::Main.new('my_company', "some_login", "some_password")
88
+ response = zendesk.delete_user(12)
89
+ response.url.should =~ %r{/users/12.xml}
90
+ response.status.should == 200
91
+ end
92
+ end
@@ -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.4"
5
+ s.version = "0.3.0"
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-13}
9
+ s.date = %q{2010-05-06}
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/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"]
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", "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/organization.rb", "lib/zendesk/search.rb", "lib/zendesk/tag.rb", "lib/zendesk/ticket.rb", "lib/zendesk/user.rb", "spec/zendesk/main_spec.rb", "spec/zendesk/user_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
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 2
8
- - 4
9
- version: 0.2.4
7
+ - 3
8
+ - 0
9
+ version: 0.3.0
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-13 00:00:00 +02:00
17
+ date: 2010-05-06 00:00:00 +02:00
18
18
  default_executable:
19
19
  dependencies: []
20
20
 
@@ -34,7 +34,6 @@ 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
38
37
  - lib/zendesk/organization.rb
39
38
  - lib/zendesk/search.rb
40
39
  - lib/zendesk/tag.rb
@@ -53,13 +52,13 @@ files:
53
52
  - lib/zendesk/forum.rb
54
53
  - lib/zendesk/group.rb
55
54
  - lib/zendesk/main.rb
56
- - lib/zendesk/main.rb.orig
57
55
  - lib/zendesk/organization.rb
58
56
  - lib/zendesk/search.rb
59
57
  - lib/zendesk/tag.rb
60
58
  - lib/zendesk/ticket.rb
61
59
  - lib/zendesk/user.rb
62
60
  - spec/zendesk/main_spec.rb
61
+ - spec/zendesk/user_spec.rb
63
62
  - zendesk-api.gemspec
64
63
  has_rdoc: true
65
64
  homepage: http://github.com/pgericson/zendesk-api
@@ -1,118 +0,0 @@
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