zendesk2 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -18,6 +18,8 @@ class Zendesk2::Client < Cistern::Service
18
18
  request :destroy_user
19
19
  request :get_current_user
20
20
  request :get_organization
21
+ request :get_organization_tickets
22
+ request :get_organization_users
21
23
  request :get_ticket
22
24
  request :get_user
23
25
  request :get_organizations
@@ -148,12 +150,15 @@ class Zendesk2::Client < Cistern::Service
148
150
  File.join(@url, path)
149
151
  end
150
152
 
151
- def page(params, collection, path, collection_root)
153
+ def page(params, collection, path, collection_root, options={})
152
154
  page_params = Zendesk2.paging_parameters(params)
153
155
  page_size = (page_params["per_page"] || 50).to_i
154
156
  page_index = (page_params["page"] || 1).to_i
155
- count = self.data[collection].size
156
157
  offset = (page_index - 1) * page_size
158
+ filter = options[:filter]
159
+ resources = self.data[collection].values
160
+ resources = filter.call(resources) if filter
161
+ count = resources.size
157
162
  total_pages = (count / page_size) + 1
158
163
 
159
164
  next_page = if page_index < total_pages
@@ -167,7 +172,7 @@ class Zendesk2::Client < Cistern::Service
167
172
  nil
168
173
  end
169
174
 
170
- resource_page = self.data[collection].values.slice(offset, page_size)
175
+ resource_page = resources.slice(offset, page_size)
171
176
 
172
177
  body = {
173
178
  collection_root => resource_page,
@@ -13,6 +13,18 @@ class Zendesk2::Client::Organization < Cistern::Model
13
13
  attribute :shared_tickets
14
14
  attribute :updated_at, type: :time
15
15
 
16
+ def destroy
17
+ requires :identity
18
+
19
+ response = connection.destroy_organization("id" => self.identity)
20
+ end
21
+
22
+ def destroyed?
23
+ self.reload
24
+ rescue not_found
25
+ true
26
+ end
27
+
16
28
  def save
17
29
  if new_record?
18
30
  requires :name
@@ -21,7 +33,7 @@ class Zendesk2::Client::Organization < Cistern::Model
21
33
  else
22
34
  requires :identity
23
35
  params = {
24
- "id" => self.identity,
36
+ "id" => self.identity,
25
37
  "name" => self.name,
26
38
  }
27
39
  data = connection.update_organization(params).body["organization"]
@@ -29,15 +41,16 @@ class Zendesk2::Client::Organization < Cistern::Model
29
41
  end
30
42
  end
31
43
 
32
- def destroy
44
+ def users
33
45
  requires :identity
34
-
35
- response = connection.destroy_organization("id" => self.identity)
46
+ data = connection.get_organization_users("id" => self.identity).body["users"]
47
+ connection.users.load(data)
36
48
  end
37
49
 
38
- def destroyed?
39
- self.reload
40
- rescue not_found
41
- true
50
+ def tickets
51
+ requires :identity
52
+ data = connection.get_organization_tickets("id" => self.identity).body["tickets"]
53
+
54
+ connection.tickets.load(data)
42
55
  end
43
56
  end
@@ -20,23 +20,18 @@ class Zendesk2::Client::Ticket < Cistern::Model
20
20
  attribute :forum_topic_id
21
21
  attribute :problem_id
22
22
  attribute :has_incidents
23
- attribute :due_at
24
- attribute :tags
23
+ attribute :due_at, type: :time
24
+ attribute :tags, type: :array
25
25
  attribute :fields
26
26
 
27
27
  def save
28
28
  if new_record?
29
29
  requires :subject, :description
30
- data = connection.create_ticket(attributes).body["ticket"]
30
+ data = connection.create_ticket(params).body["ticket"]
31
31
  merge_attributes(data)
32
32
  else
33
33
  requires :identity
34
- params = {
35
- "id" => self.identity,
36
- "subject" => self.subject,
37
- "description" => self.description,
38
- }
39
- data = connection.update_ticket(params).body["ticket"]
34
+ data = connection.update_ticket(params.merge("id" => self.identity)).body["ticket"]
40
35
  merge_attributes(data)
41
36
  end
42
37
  end
@@ -52,4 +47,20 @@ class Zendesk2::Client::Ticket < Cistern::Model
52
47
  rescue not_found
53
48
  true
54
49
  end
50
+
51
+ def params
52
+ Cistern::Hash.slice(Zendesk2.stringify_keys(attributes), "external_id", "via", "requester_id", "submitter_id", "assignee_id", "organization_id", "subject", "description", "fields", "recipient", "status")
53
+ end
54
+
55
+ def submitter
56
+ self.connection.users.get(submitter_id)
57
+ end
58
+
59
+ def requester=(requester)
60
+ self.requester_id= requester.id
61
+ end
62
+
63
+ def requester
64
+ self.connection.users.get(self.requester_id)
65
+ end
55
66
  end
@@ -32,16 +32,11 @@ class Zendesk2::Client::User < Cistern::Model
32
32
  def save
33
33
  if new_record?
34
34
  requires :name, :email
35
- data = connection.create_user(attributes).body["user"]
35
+ data = connection.create_user(params).body["user"]
36
36
  merge_attributes(data)
37
37
  else
38
38
  requires :identity
39
- params = {
40
- "id" => self.identity,
41
- "name" => self.name,
42
- "email" => self.email,
43
- }
44
- data = connection.update_user(params).body["user"]
39
+ data = connection.update_user(params.merge("id" => self.identity)).body["user"]
45
40
  merge_attributes(data)
46
41
  end
47
42
  end
@@ -57,4 +52,10 @@ class Zendesk2::Client::User < Cistern::Model
57
52
  def destroyed?
58
53
  !self.active
59
54
  end
55
+
56
+ private
57
+
58
+ def params
59
+ Cistern::Hash.slice(Zendesk2.stringify_keys(attributes), "name", "email", "organization_id", "external_id", "alias", "active", "verified", "locate_id", "time_zone", "phone", "signature", "details", "notes", "role", "custom_role_id", "moderator", "ticket_restriction", "only_private_comments")
60
+ end
60
61
  end
@@ -20,6 +20,11 @@ class Zendesk2::Client
20
20
  "updated_at" => Time.now.iso8601,
21
21
  }.merge(params)
22
22
 
23
+ record["requester_id"] ||= @current_user_id
24
+ record["submitter_id"]= @current_user_id
25
+ requester = self.data[:users][record["requester_id"]]
26
+ # TODO: throw error if user doesn't exist?
27
+ record["organization_id"]= requester["organization_id"]
23
28
  self.data[:tickets][identity]= record
24
29
 
25
30
  response(
@@ -14,10 +14,10 @@ class Zendesk2::Client
14
14
  identity = self.class.new_id
15
15
 
16
16
  record = {
17
- "id" => identity,
18
- "url" => url_for("/users/#{identity}.json"),
19
- "created_at" => Time.now.iso8601,
20
- "updated_at" => Time.now.iso8601,
17
+ "id" => identity,
18
+ "url" => url_for("/users/#{identity}.json"),
19
+ "created_at" => Time.now.iso8601,
20
+ "updated_at" => Time.now.iso8601
21
21
  }.merge(params)
22
22
 
23
23
  self.data[:users][identity]= record
@@ -0,0 +1,23 @@
1
+ class Zendesk2::Client
2
+ class Real
3
+ def get_organization_tickets(params={})
4
+ id = params["id"]
5
+ page_params = Zendesk2.paging_parameters(params)
6
+
7
+ request(
8
+ :params => page_params,
9
+ :method => :get,
10
+ :path => "/organizations/#{id}/tickets.json",
11
+ )
12
+ end
13
+ end # Real
14
+
15
+ class Mock
16
+ def get_organization_tickets(params={})
17
+ id = params["id"]
18
+
19
+ requesters = self.data[:users].values.select{|u| u["organization_id"].to_s == id.to_s}.map{|s| s["organization_id"].to_s}
20
+ page(params, :tickets, "/organizations/#{id}/tickets.json", "tickets", filter: lambda{|c| c.select{|u| requesters.include?(u["organization_id"].to_s)}})
21
+ end
22
+ end # Mock
23
+ end
@@ -0,0 +1,21 @@
1
+ class Zendesk2::Client
2
+ class Real
3
+ def get_organization_users(params={})
4
+ id = params["id"]
5
+ page_params = Zendesk2.paging_parameters(params)
6
+
7
+ request(
8
+ :params => page_params,
9
+ :method => :get,
10
+ :path => "/organizations/#{id}/users.json",
11
+ )
12
+ end
13
+ end # Real
14
+
15
+ class Mock
16
+ def get_organization_users(params={})
17
+ id = params["id"]
18
+ page(params, :users, "/organizations/#{id}/users.json", "users", filter: lambda{|c| c.select{|u| u["organization_id"].to_s == id.to_s}})
19
+ end
20
+ end # Mock
21
+ end
@@ -1,3 +1,3 @@
1
1
  module Zendesk2
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
data/lib/zendesk2.rb CHANGED
@@ -33,6 +33,10 @@ module Zendesk2
33
33
  def self.uuid
34
34
  UUIDTools::UUID.random_create.to_s
35
35
  end
36
+
37
+ def self.stringify_keys(hash)
38
+ hash.inject({}){|r,(k,v)| r.merge(k.to_s => v)}
39
+ end
36
40
  end
37
41
 
38
42
  Zendesk = Zendesk2
@@ -6,4 +6,18 @@ describe "organizations" do
6
6
  :organizations,
7
7
  lambda { {name: Zendesk2.uuid} },
8
8
  lambda { {name: Zendesk2.uuid} }
9
+
10
+ describe do
11
+ let(:organization) { client.organizations.create(name: Zendesk2.uuid) }
12
+ it "should get #users" do
13
+ user = client.users.create(email: "#{Zendesk2.uuid}@example.org", name: Zendesk2.uuid, organization_id: organization.id)
14
+ organization.users.should include user
15
+ end
16
+
17
+ it "should get #tickets" do
18
+ user = client.users.create(email: "#{Zendesk2.uuid}@example.org", name: Zendesk2.uuid, organization_id: organization.id)
19
+ ticket = client.tickets.create(subject: "#{Zendesk2.uuid}@example.org", description: Zendesk2.uuid, requester: user)
20
+ organization.tickets.should include ticket
21
+ end
22
+ end
9
23
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zendesk2
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-18 00:00:00.000000000 Z
12
+ date: 2012-06-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: cistern
@@ -123,6 +123,8 @@ files:
123
123
  - lib/zendesk2/requests/destroy_user.rb
124
124
  - lib/zendesk2/requests/get_current_user.rb
125
125
  - lib/zendesk2/requests/get_organization.rb
126
+ - lib/zendesk2/requests/get_organization_tickets.rb
127
+ - lib/zendesk2/requests/get_organization_users.rb
126
128
  - lib/zendesk2/requests/get_organizations.rb
127
129
  - lib/zendesk2/requests/get_ticket.rb
128
130
  - lib/zendesk2/requests/get_tickets.rb