├── .gitignore
├── Gemfile
├── Gemfile.lock
├── MIT-LICENSE.txt
├── Manifest
├── README.markdown
├── Rakefile
├── geminstaller.yml
├── lib
├── console.rb
├── zendesk.rb
├── zendesk
│ ├── attachment.rb
│ ├── comment.rb
│ ├── entry.rb
│ ├── forum.rb
│ ├── group.rb
│ ├── main.rb
│ ├── organization.rb
│ ├── search.rb
│ ├── tag.rb
│ ├── ticket.rb
│ ├── user.rb
│ └── user_identity.rb
└── zendesk_api.rb
├── spec
└── zendesk
│ ├── main_spec.rb
│ └── user_spec.rb
└── zendesk-api.gemspec
/.gitignore:
--------------------------------------------------------------------------------
1 | *.swp
2 | *.gem
3 | *.swo
4 | .idea/*
5 | *.orig
6 | pkg/*
7 | .rvmrc
8 |
9 | test.rb
10 |
11 |
--------------------------------------------------------------------------------
/Gemfile:
--------------------------------------------------------------------------------
1 | source "https://rubygems.org"
2 |
3 | gemspec
4 |
5 | gem "curb", "0.6.7"
6 | gem "rspec-rails", "1.3.2"
7 | gem "rspec", "1.3.0"
8 |
9 |
--------------------------------------------------------------------------------
/Gemfile.lock:
--------------------------------------------------------------------------------
1 | PATH
2 | remote: .
3 | specs:
4 | zendesk-api (0.3.3)
5 | activesupport (>= 2.3)
6 | crack (>= 0.1.8)
7 |
8 | GEM
9 | remote: https://rubygems.org/
10 | specs:
11 | activesupport (2.3.14)
12 | crack (0.3.1)
13 | curb (0.6.7)
14 | rack (1.4.1)
15 | rspec (1.3.0)
16 | rspec-rails (1.3.2)
17 | rack (>= 1.0.0)
18 | rspec (>= 1.3.0)
19 |
20 | PLATFORMS
21 | ruby
22 |
23 | DEPENDENCIES
24 | curb (= 0.6.7)
25 | rspec (= 1.3.0)
26 | rspec-rails (= 1.3.2)
27 | zendesk-api!
28 |
--------------------------------------------------------------------------------
/MIT-LICENSE.txt:
--------------------------------------------------------------------------------
1 | Copyright (c) 2010 Peter Ericson
2 |
3 | Permission is hereby granted, free of charge, to any person obtaining
4 | a copy of this software and associated documentation files (the
5 | "Software"), to deal in the Software without restriction, including
6 | without limitation the rights to use, copy, modify, merge, publish,
7 | distribute, sublicense, and/or sell copies of the Software, and to
8 | permit persons to whom the Software is furnished to do so, subject to
9 | the following conditions:
10 |
11 | The above copyright notice and this permission notice shall be
12 | included in all copies or substantial portions of the Software.
13 |
14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
--------------------------------------------------------------------------------
/Manifest:
--------------------------------------------------------------------------------
1 | Manifest
2 | README.markdown
3 | Rakefile
4 | geminstaller.yml
5 | lib/console.rb
6 | lib/zendesk_api.rb
7 | lib/zendesk.rb
8 | lib/zendesk/attachment.rb
9 | lib/zendesk/entry.rb
10 | lib/zendesk/forum.rb
11 | lib/zendesk/group.rb
12 | lib/zendesk/main.rb
13 | lib/zendesk/organization.rb
14 | lib/zendesk/search.rb
15 | lib/zendesk/tag.rb
16 | lib/zendesk/ticket.rb
17 | lib/zendesk/user.rb
18 | lib/zendesk/user_identity.rb
19 | spec/zendesk/main_spec.rb
20 | spec/zendesk/user_spec.rb
21 |
--------------------------------------------------------------------------------
/README.markdown:
--------------------------------------------------------------------------------
1 | THIS GEM IS DEPRECATED!!!
2 | ------------
3 | USE THE OFFICIAL GEM FROM ZENDESK
4 | ------------
5 | [zendesk_api gem from zendesk](https://github.com/zendesk/zendesk_api_client_rb)
6 | ------------
7 |
8 |
9 | THIS GEM WILL NOT BE UPDATED
10 | ------------
11 |
12 | old documentation below
13 |
14 | Zendesk API
15 | ------------
16 |
17 | The unofficial Ruby Library for interacting with the [Zendesk REST API](http://www.zendesk.com/api)
18 |
19 | Looking for a new head maintainer, since I do not use zendesk anymore.
20 |
21 | ## Documentation & Requirements
22 | * ActiveResource gem
23 | * Curl
24 | * Curb gem
25 |
26 | ## What
27 | * Ruby wrapper around the Zendesk REST API
28 |
29 | ## Install Instructions
30 | Normal install:
31 |
32 | gem install zendesk-api
33 |
34 | Bundler install:
35 |
36 | gem "zendesk-api", "latestversion"
37 |
38 | ## Supported v1 Endpoints
39 |
40 | * Attachments
41 | * Comments
42 | * Entries
43 | * Forums
44 | * Groups
45 | * Organizations
46 | * Search
47 | * Tags
48 | * Tickets
49 | * Users
50 |
51 | ## How to use it
52 | ### Basic
53 | Below outputs xml
54 |
55 | z = Zendesk::Main.new('subdomain', 'username', 'password')
56 | and outputs json
57 |
58 | z = Zendesk::Main.new('subdomain', 'username', 'password', :format => 'json')
59 |
60 | ### Show
61 |
62 | z.get_function_name(user_id)
63 |
64 | where function_name is one the following:
65 | user, organization, group, ticket, attachement, tag, forum, entries, search
66 |
67 | e.g.
68 |
69 | z.get_user(121)
70 | ### List
71 |
72 | z.get_function_names #with a s in the end, for plural
73 | e.g.
74 |
75 | z.get_users
76 |
77 | ### Create
78 | with string
79 |
80 | z.create_user("email@company.comJohn Doe")
81 | with hash(array is not supported yet)
82 |
83 | z.create_user({:email => 'email@company.com', :name => 'John Doe'})
84 |
85 | ### Update
86 | Not supported yet
87 |
88 | ### Destroy
89 |
90 | z.destroy_function_name(id)
91 | e.g.
92 |
93 | z.destroy_user(234)
94 |
95 |
96 | ## Using The Zendesk Console
97 |
98 | The Zendesk library comes with a convenient console for testing and quick commands (or whatever else you want to use it for).
99 |
100 | From /
101 |
102 | irb -r lib/zendesk/console
103 | z = Zendesk::Main.new('accountname', 'username', 'password')
104 | z.get_users
105 |
106 | ## License
107 |
108 | The Zendesk library is released under the MIT license:
109 |
110 | * http://www.opensource.org/licenses/MIT
111 |
--------------------------------------------------------------------------------
/Rakefile:
--------------------------------------------------------------------------------
1 | require 'rubygems'
2 | require 'rake'
3 | require 'echoe'
4 |
5 | Echoe.new('zendesk-api', '0.3.2') do |p|
6 | p.description = "RubyGem wrapper for REST API to http://zendesk.com"
7 | p.author = "Peter Ericson"
8 | p.url = "http://github.com/pgericson/zendesk-api"
9 | p.email = "pg.ericson@gmail.com"
10 | p.ignore_pattern = ["tmp/*", "script/*"]
11 | p.development_dependencies = []
12 | p.runtime_dependencies = ["crack >=0.1.8", "activesupport >=2.3"]
13 | end
14 | # how to build the new gem ( I can't remember so here it is for all time)
15 | # rake build_gemspec
16 |
17 | Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
18 |
--------------------------------------------------------------------------------
/geminstaller.yml:
--------------------------------------------------------------------------------
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 |
12 |
--------------------------------------------------------------------------------
/lib/console.rb:
--------------------------------------------------------------------------------
1 | require 'lib/zendesk_api'
2 | puts <<-TXT
3 | Stuff here to explain what it does
4 | TXT
5 |
6 | include Zendesk
7 |
--------------------------------------------------------------------------------
/lib/zendesk.rb:
--------------------------------------------------------------------------------
1 | require File.dirname(__FILE__) + "/zendesk"
2 |
--------------------------------------------------------------------------------
/lib/zendesk/attachment.rb:
--------------------------------------------------------------------------------
1 | module Zendesk
2 | module Attachment
3 |
4 | end
5 | end
6 |
--------------------------------------------------------------------------------
/lib/zendesk/comment.rb:
--------------------------------------------------------------------------------
1 | module Zendesk
2 | module Comment
3 | #example input {"value" => "my comment", "public" => true}
4 | def create_comment(ticket_id, input, options = {})
5 | make_request("tickets/#{ticket_id}", ({:update => Zendesk::Main.to_xml('comment', input)}), options)
6 | end
7 | end
8 | end
--------------------------------------------------------------------------------
/lib/zendesk/entry.rb:
--------------------------------------------------------------------------------
1 | module Zendesk
2 | module Entry
3 |
4 | def get_entries(forum_id)
5 | make_request("forums/#{forum_id}/entries")
6 | end
7 |
8 | def get_entry(id)
9 | make_request("entries/#{id}")
10 | end
11 |
12 | def create_entrie(input)
13 | make_request("entries", :create => Zendesk::Main.to_xml('entrie', input))
14 | end
15 |
16 | def update_entrie(input)
17 | make_request("entries", :update => Zendesk::Main.to_xml('entrie', input))
18 | end
19 |
20 | def delete_entrie(id)
21 | make_request("entries/#{id}", :destroy => true)
22 | end
23 | end
24 | end
25 |
--------------------------------------------------------------------------------
/lib/zendesk/forum.rb:
--------------------------------------------------------------------------------
1 | module Zendesk
2 | module Forum
3 |
4 | def get_forums
5 | make_request("forums")
6 | end
7 |
8 | def get_forum(id)
9 | make_request("forums/#{id}")
10 | end
11 |
12 | def create_forum(input)
13 | make_request("forums", :create => Zendesk::Main.to_xml('forum', input))
14 | end
15 |
16 | def update_forum(input)
17 | make_request("forums", :update => Zendesk::Main.to_xml('forum', input))
18 | end
19 |
20 | def delete_forum(id)
21 | make_request("forums/#{id}", :destroy => true)
22 | end
23 | end
24 | end
25 |
--------------------------------------------------------------------------------
/lib/zendesk/group.rb:
--------------------------------------------------------------------------------
1 | module Zendesk
2 | module Group
3 |
4 | def get_groups
5 | make_request("groups")
6 | end
7 |
8 | def get_group(id)
9 | make_request("groups/#{id}")
10 | end
11 |
12 | def create_group(input)
13 | make_request("groups", :create => Zendesk::Main.to_xml('group', input))
14 | end
15 |
16 | def update_group(input)
17 | make_request("groups", :update => Zendesk::Main.to_xml('group', input))
18 | end
19 |
20 | def delete_group(id)
21 | make_request("groups/#{id}", :destroy => true)
22 | end
23 | end
24 | end
25 |
--------------------------------------------------------------------------------
/lib/zendesk/main.rb:
--------------------------------------------------------------------------------
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_postfix << "api/v#{@options[:api_version]}/" if @options[:api_version]
22 |
23 | url = url_prefix + @account + url_postfix
24 | end
25 |
26 | def self.to_xml(function_name, input)
27 | if input.is_a?(String)
28 | input
29 | else
30 | input.to_xml({:root => function_name})
31 | end
32 | end
33 |
34 |
35 | def params_list(list)
36 | params = "?" + list.map do |k, v|
37 | if v.is_a?(Array)
38 | v.map do |val|
39 | "#{k}[]=#{val}"
40 | end.join("&")
41 | else
42 | "#{k}=#{v}"
43 | end
44 | end.join("&")
45 | end
46 |
47 | def string_body(body)
48 | if body.values.first.is_a?(Hash)
49 | body.values.first.to_xml.strip
50 | elsif body.values.first.is_a?(String)
51 | body.values.first
52 | end
53 | end
54 |
55 | def make_request(end_url, body = {}, options = {})
56 | options.reverse_merge!({:on_behalf_of => nil})
57 |
58 | curl = Curl::Easy.new(main_url + end_url + ".#{@format}")
59 | curl.userpwd = "#{@username}:#{@password}"
60 |
61 | curl.headers={}
62 | curl.headers.merge!({"X-On-Behalf-Of" => options[:on_behalf_of]}) if options[:on_behalf_of].present?
63 |
64 | if body.empty? or body[:list]
65 | curl.url = curl.url + params_list(body[:list]) if body[:list]
66 | curl.perform
67 | elsif body[:post]
68 | curl.headers.merge!({"Content-Type" => "application/xml"})
69 | curl.http_post
70 | elsif body[:create]
71 | curl.headers.merge!({"Content-Type" => "application/xml"})
72 | curl.http_post(string_body(body))
73 | elsif body[:update]
74 | # PUT seems badly broken, at least I can't get it to work without always
75 | # raising an exception about rewinding the data stream
76 | # curl.http_put(final_body)
77 | curl.headers.merge!({ "Content-Type" => "application/xml", "X-Http-Method-Override" => "put" })
78 | curl.http_post(string_body(body))
79 | elsif body[:destroy]
80 | curl.http_delete
81 | end
82 |
83 | if curl.body_str == "Couldn't authenticate you"
84 | return "string" #raise CouldNotAuthenticateYou
85 | end
86 | Response.new(curl, format)
87 | end
88 |
89 | class Response
90 |
91 | attr_reader :status, :body, :headers_raw, :headers, :curl, :url, :data
92 |
93 | def initialize(curl, format)
94 | @format=format
95 | @curl = curl
96 | @url = curl.url
97 | @status = curl.response_code
98 | @body = curl.body_str
99 | @headers_raw = curl.header_str
100 | parse_headers
101 | # parse the data coming back
102 | @data = Crack::XML.parse(@body || "") if @format == "xml"
103 | end
104 |
105 | def parse_headers
106 | hs={}
107 | return hs if headers_raw.nil? or headers_raw==""
108 | headers_raw.split("\r\n")[1..-1].each do |h|
109 | # Rails.logger.info h
110 | m=h.match(/([^:]+):\s?(.*)/)
111 | next if m.nil? or m[2].nil?
112 | # Rails.logger.info m.inspect
113 | hs[m[1]]=m[2]
114 | end
115 | @headers=hs
116 | end
117 |
118 | end
119 |
120 | include Zendesk::User
121 | include Zendesk::UserIdentity
122 | include Zendesk::Organization
123 | include Zendesk::Group
124 | include Zendesk::Ticket
125 | include Zendesk::Attachment
126 | include Zendesk::Tag
127 | include Zendesk::Forum
128 | include Zendesk::Entry
129 | include Zendesk::Search
130 | include Zendesk::Comment
131 | end
132 | end
133 |
--------------------------------------------------------------------------------
/lib/zendesk/organization.rb:
--------------------------------------------------------------------------------
1 | module Zendesk
2 | module Organization
3 |
4 | def get_organizations
5 | make_request("organizations")
6 | end
7 |
8 | def get_organization(id)
9 | make_request("organizations/#{id}")
10 | end
11 |
12 | def create_organization(input)
13 | make_request("organizations", :create => Zendesk::Main.to_xml('organization', input))
14 | end
15 |
16 | def update_organization(input)
17 | make_request("organizations", :update => Zendesk::Main.to_xml('organization', input))
18 | end
19 |
20 | def delete_organization(id)
21 | make_request("organizations/#{id}", :destroy => true)
22 | end
23 | end
24 | end
25 |
--------------------------------------------------------------------------------
/lib/zendesk/search.rb:
--------------------------------------------------------------------------------
1 | module Zendesk
2 | module Search
3 | def search(params)
4 | make_request("search", :list => params)
5 | end
6 | end
7 | end
8 |
--------------------------------------------------------------------------------
/lib/zendesk/tag.rb:
--------------------------------------------------------------------------------
1 | module Zendesk
2 | module Tag
3 |
4 | def get_tags(params = nil)
5 | if params
6 | make_request("tags", :list => params)
7 | else
8 | make_request("tags")
9 | end
10 | end
11 |
12 | end
13 | end
14 |
--------------------------------------------------------------------------------
/lib/zendesk/ticket.rb:
--------------------------------------------------------------------------------
1 | module Zendesk
2 | module Ticket
3 |
4 | def get_tickets(rule_id)
5 | make_request("rules/#{rule_id}")
6 | end
7 |
8 | def get_ticket(id)
9 | make_request("tickets/#{id}")
10 | end
11 |
12 | def create_ticket(input, options = {})
13 | make_request("tickets", {:create => Zendesk::Main.to_xml('ticket', input)}, options)
14 | end
15 |
16 | def update_ticket(id, input, options = {})
17 | make_request("tickets/#{id}", {:update => Zendesk::Main.to_xml('ticket', input)}, options)
18 | end
19 |
20 | def delete_ticket(id)
21 | make_request("tickets/#{id}", :destroy => true)
22 | end
23 | end
24 | end
25 |
--------------------------------------------------------------------------------
/lib/zendesk/user.rb:
--------------------------------------------------------------------------------
1 | module Zendesk
2 | module User
3 |
4 | def get_users(params = nil)
5 | if params
6 | make_request("users", :list => params)
7 | else
8 | make_request("users")
9 | end
10 | end
11 |
12 | def get_user(id)
13 | make_request("users/#{id}")
14 | end
15 |
16 | def create_user(input)
17 | make_request("users", :create => Zendesk::Main.to_xml('user', input))
18 | end
19 |
20 | def update_user(id, input)
21 | make_request("users/#{id}", :update => Zendesk::Main.to_xml('user', input))
22 | end
23 |
24 | def delete_user(id)
25 | make_request("users/#{id}", :destroy => true)
26 | end
27 | end
28 | end
29 |
--------------------------------------------------------------------------------
/lib/zendesk/user_identity.rb:
--------------------------------------------------------------------------------
1 | module Zendesk
2 | module UserIdentity
3 |
4 | def user_add_twitter(user_id, twitter)
5 | make_request("users/#{user_id}/user_identities", :create => "#{twitter}")
6 | end
7 |
8 | def user_primary_identity(user_id, id)
9 | make_request("users/#{user_id}/user_identities/#{id}/make_primary", :post => true)
10 | end
11 |
12 | def user_add_email(user_id, email)
13 | make_request("users/#{user_id}/user_identities", :create => "#{email}")
14 | end
15 |
16 | def user_delete_identity(user_id, id)
17 | make_request("users/#{user_id}/user_identities/#{id}", :destroy => true)
18 | end
19 |
20 | def user_identities(user_id)
21 | make_request("users/#{user_id}/user_identities")
22 | end
23 |
24 | end
25 | end
26 |
--------------------------------------------------------------------------------
/lib/zendesk_api.rb:
--------------------------------------------------------------------------------
1 | require 'rubygems'
2 | require 'curb'
3 | require 'crack'
4 | gem 'activesupport'
5 | require 'active_support'
6 | require 'active_support/version'
7 | # need to pull in the pieces we want with Rails 3
8 | require 'active_support/core_ext' if ActiveSupport::VERSION::MAJOR == 3
9 |
10 | module Zendesk
11 | class Error < StandardError; end
12 | class CouldNotAuthenticateYou < StandardError; end
13 | end
14 |
15 | require 'zendesk/user'
16 | require 'zendesk/user_identity'
17 | require 'zendesk/organization'
18 | require 'zendesk/group'
19 | require 'zendesk/ticket'
20 | require 'zendesk/attachment'
21 | require 'zendesk/tag'
22 | require 'zendesk/forum'
23 | require 'zendesk/entry'
24 | require 'zendesk/search'
25 | require 'zendesk/comment'
26 | require 'zendesk/main'
27 |
--------------------------------------------------------------------------------
/spec/zendesk/main_spec.rb:
--------------------------------------------------------------------------------
1 | require "spec"
2 | require 'zendesk_api'
3 |
4 | describe Zendesk::Main do
5 | describe "basic" do
6 | before(:each) do
7 | @account = "this_account"
8 | @username = "this_username"
9 | @password = "this_password"
10 | @zendesk = Zendesk::Main.new(@account, @username, @password)
11 | end
12 |
13 | it "should have the correct mail_url with no ssl options" do
14 | @zendesk.main_url.should == "http://#{@account}.zendesk.com/"
15 | end
16 |
17 | it "should have the correct mail_url with ssl options" do
18 | @zendesk = Zendesk::Main.new(@account, @username, @password, :ssl => true)
19 | @zendesk.main_url.should == "https://#{@account}.zendesk.com/"
20 | end
21 |
22 | it "should add the api version to the URL if one is specified" do
23 | @zendesk = Zendesk::Main.new(@account, @username, @password, :ssl => true, :api_version => 1)
24 | @zendesk.main_url.should == "https://#{@account}.zendesk.com/api/v1/"
25 | end
26 | end
27 |
28 | describe 'make_request' do
29 | context "searching" do
30 | before do
31 | curl_object = Curl::Easy.method(:new)
32 | Curl::Easy.stub!(:new).and_return do |* args|
33 | curl = curl_object.call(* args)
34 | curl.should_receive(:perform)
35 | curl
36 | end
37 | end
38 |
39 | it "should construct array url for search" do
40 | zendesk = Zendesk::Main.new('my_company', "some_login", "some_password")
41 | params = {
42 | :foo => "bar",
43 | :foo_list => [1, 2, 3]
44 | }
45 |
46 | response = zendesk.make_request("search", :list => params)
47 | response.url.should =~ /foo=bar/
48 | response.url.should =~ /foo_list\[\]=1/
49 | response.url.should =~ /foo_list\[\]=2/
50 | response.url.should =~ /foo_list\[\]=3/
51 |
52 | end
53 | end
54 |
55 | context "headers" do
56 | before(:each) do
57 | @zendesk = Zendesk::Main.new('my_company', "some_login", "some_password")
58 | @mock_object = mock('curl_connection')
59 | @mock_object.stub!(:userpwd=).and_return(true)
60 | @mock_object.stub!(:headers=).and_return({})
61 | @mock_object.stub!(:headers).and_return({})
62 | @mock_object.stub!(:body_str).and_return("body")
63 | @mock_object.stub!(:header_str).and_return("header")
64 | @mock_object.stub!(:url).and_return("url")
65 | @mock_object.stub!(:response_code).and_return("response_code")
66 | @mock_object.should_receive(:http_post).and_return(true)
67 | @curl_object = Curl::Easy.should_receive(:new).and_return(@mock_object)
68 | end
69 | it "should use default options if none are passed" do
70 | params = {"subject" => "rspec rulez", "description" => "description", "status_id"=>1}
71 | options_hash = {:on_behalf_of => "test@test.com"}
72 |
73 | options_hash.should_receive(:reverse_merge!).once.with({:on_behalf_of => nil})
74 | @mock_object.headers.should_receive(:merge!).once.with({"Content-Type" => "application/xml"}).and_return(true)
75 | @mock_object.headers.should_receive(:merge!).once.with({"X-On-Behalf-Of" => "test@test.com"}).and_return(true)
76 |
77 | @zendesk.make_request("ticket", {:create => params}, options_hash)
78 | end
79 |
80 | it "should format headers for creation" do
81 | params = {"subject" => "rspec rulez", "description" => "description", "status_id"=>1}
82 |
83 | @mock_object.headers.should_receive(:merge!).with({"Content-Type" => "application/xml"}).any_number_of_times.and_return(true)
84 |
85 | @zendesk.make_request("ticket", :create => params)
86 | end
87 | it "should add X-On-Behalf-Of if passed in options" do
88 | params = {"subject" => "rspec rulez", "description" => "description", "status_id"=>1}
89 |
90 | @mock_object.headers.should_receive(:merge!).once.with({"Content-Type" => "application/xml"}).and_return(true)
91 | @mock_object.headers.should_receive(:merge!).once.with({"X-On-Behalf-Of" => "test@test.com"}).and_return(true)
92 |
93 | @zendesk.make_request("ticket", {:create => params}, {:on_behalf_of => "test@test.com"})
94 | end
95 | it "should not add X-On-Behalf-Of if not passed in options" do
96 | params = {"subject" => "rspec rulez", "description" => "description", "status_id"=>1}
97 |
98 | @mock_object.headers.should_receive(:merge!).once.with({"Content-Type" => "application/xml"}).and_return(true)
99 | @mock_object.headers.should_not_receive(:merge!).with({"X-On-Behalf-Of" => ""}).and_return(true)
100 |
101 | @zendesk.make_request("ticket", {:create => params}, {:on_behalf_of => ""})
102 | end
103 | end
104 | end
105 |
106 | describe "#self.to_xml" do
107 | context "comments" do
108 | it "should output correctly formatted XML" do
109 | input = {"public" => true,"value" => "new comment"}
110 | xml = Zendesk::Main.to_xml('comment', input)
111 | xml.should =~ /\s*true<\/public>\s*new comment<\/value>\s*<\/comment>/
112 | end
113 | end
114 |
115 | context "tickets" do
116 | it "should output correctly formatted XML" do
117 | input = {"subject" => "rspec rulez","status_id"=>1,"description" => "description"}
118 | xml = Zendesk::Main.to_xml('ticket', input)
119 | xml.should =~ /\s*rspec rulez<\/subject>\s*1<\/status-id>\s*description<\/description>\s*<\/ticket>/
120 | end
121 | end
122 | end
123 | end
124 |
--------------------------------------------------------------------------------
/spec/zendesk/user_spec.rb:
--------------------------------------------------------------------------------
1 | require "spec"
2 | require '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
93 |
--------------------------------------------------------------------------------
/zendesk-api.gemspec:
--------------------------------------------------------------------------------
1 | # -*- encoding: utf-8 -*-
2 |
3 | Gem::Specification.new do |s|
4 | s.name = %q{zendesk-api}
5 | s.version = "0.3.4"
6 |
7 | s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8 | s.authors = ["Peter Ericson"]
9 | s.date = %q{2010-12-27}
10 | s.description = %q{RubyGem wrapper for REST API to http://zendesk.com}
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/comment.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", "lib/zendesk/user_identity.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/comment.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", "lib/zendesk/user_identity.rb", "spec/zendesk/main_spec.rb", "spec/zendesk/user_spec.rb", "zendesk-api.gemspec"]
14 | s.homepage = %q{http://github.com/pgericson/zendesk-api}
15 | s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Zendesk-api", "--main", "README.markdown"]
16 | s.require_paths = ["lib"]
17 | s.rubyforge_project = %q{zendesk-api}
18 | s.rubygems_version = %q{1.3.7}
19 | s.summary = %q{RubyGem wrapper for REST API to http://zendesk.com}
20 |
21 | if s.respond_to? :specification_version then
22 | current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
23 | s.specification_version = 3
24 |
25 | if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
26 | s.add_runtime_dependency(%q, [">= 0.1.8"])
27 | s.add_runtime_dependency(%q, [">= 2.3"])
28 | else
29 | s.add_dependency(%q, [">= 0.1.8"])
30 | s.add_dependency(%q, [">= 2.3"])
31 | end
32 | else
33 | s.add_dependency(%q, [">= 0.1.8"])
34 | s.add_dependency(%q, [">= 2.3"])
35 | end
36 | end
37 |
--------------------------------------------------------------------------------