zendesk2 0.4.4 → 0.4.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (66) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +14 -0
  3. data/lib/zendesk2/client/models/categories.rb +1 -1
  4. data/lib/zendesk2/client/models/forums.rb +1 -1
  5. data/lib/zendesk2/client/models/groups.rb +1 -1
  6. data/lib/zendesk2/client/models/membership.rb +1 -1
  7. data/lib/zendesk2/client/models/organization.rb +2 -0
  8. data/lib/zendesk2/client/models/organizations.rb +1 -1
  9. data/lib/zendesk2/client/models/ticket_audits.rb +1 -1
  10. data/lib/zendesk2/client/models/ticket_comments.rb +1 -1
  11. data/lib/zendesk2/client/models/ticket_metrics.rb +1 -1
  12. data/lib/zendesk2/client/models/tickets.rb +1 -1
  13. data/lib/zendesk2/client/models/topic_comments.rb +1 -1
  14. data/lib/zendesk2/client/models/topics.rb +1 -1
  15. data/lib/zendesk2/client/models/user_field.rb +59 -0
  16. data/lib/zendesk2/client/models/user_fields.rb +11 -0
  17. data/lib/zendesk2/client/models/user_identity.rb +1 -1
  18. data/lib/zendesk2/client/models/users.rb +1 -1
  19. data/lib/zendesk2/client/requests/create_membership.rb +10 -0
  20. data/lib/zendesk2/client/requests/create_user.rb +2 -12
  21. data/lib/zendesk2/client/requests/create_user_field.rb +44 -0
  22. data/lib/zendesk2/client/requests/destroy_user.rb +1 -0
  23. data/lib/zendesk2/client/requests/destroy_user_field.rb +27 -0
  24. data/lib/zendesk2/client/requests/get_category.rb +7 -14
  25. data/lib/zendesk2/client/requests/get_group.rb +6 -14
  26. data/lib/zendesk2/client/requests/get_membership.rb +6 -14
  27. data/lib/zendesk2/client/requests/get_organization.rb +7 -14
  28. data/lib/zendesk2/client/requests/get_organization_memberships.rb +1 -1
  29. data/lib/zendesk2/client/requests/get_ticket.rb +6 -14
  30. data/lib/zendesk2/client/requests/get_ticket_audit.rb +7 -18
  31. data/lib/zendesk2/client/requests/get_ticket_field.rb +6 -15
  32. data/lib/zendesk2/client/requests/get_ticket_fields.rb +1 -1
  33. data/lib/zendesk2/client/requests/get_ticket_metric.rb +6 -17
  34. data/lib/zendesk2/client/requests/get_topic_comment.rb +3 -6
  35. data/lib/zendesk2/client/requests/get_topic_comments.rb +1 -1
  36. data/lib/zendesk2/client/requests/get_user.rb +9 -13
  37. data/lib/zendesk2/client/requests/get_user_field.rb +25 -0
  38. data/lib/zendesk2/client/requests/get_user_fields.rb +18 -0
  39. data/lib/zendesk2/client/requests/get_user_identity.rb +6 -15
  40. data/lib/zendesk2/client/requests/get_user_memberships.rb +1 -1
  41. data/lib/zendesk2/client/requests/mark_membership_default.rb +2 -6
  42. data/lib/zendesk2/client/requests/mark_user_identity_primary.rb +3 -6
  43. data/lib/zendesk2/client/requests/update_group.rb +8 -14
  44. data/lib/zendesk2/client/requests/update_topic_comment.rb +2 -2
  45. data/lib/zendesk2/client/requests/update_user_field.rb +28 -0
  46. data/lib/zendesk2/client.rb +79 -15
  47. data/lib/zendesk2/collection.rb +1 -18
  48. data/lib/zendesk2/paged_collection.rb +100 -0
  49. data/lib/zendesk2/version.rb +1 -1
  50. data/lib/zendesk2.rb +9 -7
  51. data/spec/categories_spec.rb +6 -4
  52. data/spec/forums_spec.rb +7 -4
  53. data/spec/groups_spec.rb +6 -3
  54. data/spec/memberships_spec.rb +31 -5
  55. data/spec/organizations_spec.rb +6 -3
  56. data/spec/shared/zendesk_resource.rb +66 -48
  57. data/spec/ticket_fields_spec.rb +6 -3
  58. data/spec/tickets_spec.rb +6 -3
  59. data/spec/topic_comments_spec.rb +6 -5
  60. data/spec/topics_spec.rb +7 -5
  61. data/spec/user_fields_spec.rb +12 -0
  62. data/spec/user_identities_spec.rb +7 -7
  63. data/spec/users_spec.rb +7 -4
  64. metadata +12 -6
  65. data/spec/config_spec.rb +0 -17
  66. data/spec/shared/resource.rb +0 -66
@@ -12,21 +12,14 @@ class Zendesk2::Client
12
12
 
13
13
  class Mock
14
14
  def get_organization(params={})
15
- id = params["id"]
16
- if body = self.data[:organizations][id]
15
+ id = params["id"]
17
16
 
18
- response(
19
- :path => "/organizations/#{id}.json",
20
- :body => {
21
- "organization" => body
22
- },
23
- )
24
- else
25
- response(
26
- :path => "/organizations/#{id}.json",
27
- :status => 404
28
- )
29
- end
17
+ response(
18
+ :path => "/organizations/#{id}.json",
19
+ :body => {
20
+ "organization" => find!(:organizations, id)
21
+ },
22
+ )
30
23
  end
31
24
  end # Mock
32
25
  end
@@ -14,7 +14,7 @@ class Zendesk2::Client
14
14
  def get_organization_memberships(params={})
15
15
  organization_id = params["organization_id"]
16
16
 
17
- page(params, :memberships, "/organizations/#{organization_id}/memberships.json", "organization_memberships", filter: lambda { |c| c.select { |m| m["organization_id"] == organization_id } })
17
+ resources(:memberships, "/organizations/#{organization_id}/memberships.json", "organization_memberships", filter: lambda { |c| c.select { |m| m["organization_id"] == organization_id } })
18
18
  end
19
19
  end # Mock
20
20
  end
@@ -15,20 +15,12 @@ class Zendesk2::Client
15
15
  id = params["id"]
16
16
  path = "/tickets/#{id}.json"
17
17
 
18
- if body = self.data[:tickets][id]
19
- response(
20
- :path => path,
21
- :body => {
22
- "ticket" => body
23
- },
24
- )
25
- else
26
- response(
27
- :path => path,
28
- :status => 404,
29
- :body => {"error" => "RecordNotFound", "description" => "Not found"},
30
- )
31
- end
18
+ response(
19
+ :path => path,
20
+ :body => {
21
+ "ticket" => self.find!(:tickets, id)
22
+ },
23
+ )
32
24
  end
33
25
  end # Mock
34
26
  end
@@ -13,25 +13,14 @@ class Zendesk2::Client
13
13
 
14
14
  class Mock
15
15
  def get_ticket_audit(params={})
16
- id = params["id"]
17
- ticket_id = params["ticket_id"]
16
+ id = params["id"]
18
17
 
19
- path = "/ticket_audits/#{id}.json"
20
-
21
- if body = self.data[:ticket_audits][id]
22
- response(
23
- :path => path,
24
- :body => {
25
- "ticket_audit" => body
26
- },
27
- )
28
- else
29
- response(
30
- :path => path,
31
- :status => 404,
32
- :body => {"error" => "RecordNotFound", "description" => "Not found"},
33
- )
34
- end
18
+ response(
19
+ :path => "/ticket_audits/#{id}.json",
20
+ :body => {
21
+ "ticket_audit" => find!(:ticket_audits, id)
22
+ },
23
+ )
35
24
  end
36
25
  end # Mock
37
26
  end
@@ -13,22 +13,13 @@ class Zendesk2::Client
13
13
  class Mock
14
14
  def get_ticket_field(params={})
15
15
  id = params["id"]
16
- path = "/ticket_fields/#{id}.json"
17
16
 
18
- if body = self.data[:ticket_fields][id]
19
- response(
20
- :path => path,
21
- :body => {
22
- "ticket_field" => body
23
- },
24
- )
25
- else
26
- response(
27
- :path => path,
28
- :status => 404,
29
- :body => {"error" => "RecordNotFound", "description" => "Not found"},
30
- )
31
- end
17
+ response(
18
+ :path => "/ticket_fields/#{id}.json",
19
+ :body => {
20
+ "ticket_field" => find!(:ticket_fields, id)
21
+ },
22
+ )
32
23
  end
33
24
  end # Mock
34
25
  end
@@ -12,7 +12,7 @@ class Zendesk2::Client
12
12
  end
13
13
  class Mock
14
14
  def get_ticket_fields(params={})
15
- page(params, :ticket_fields, "/ticket_fields.json", "ticket_fields")
15
+ collection(:ticket_fields, "/ticket_fields.json", "ticket_fields")
16
16
  end
17
17
  end
18
18
  end
@@ -20,24 +20,13 @@ class Zendesk2::Client
20
20
  class Mock
21
21
  def get_ticket_metric(params={})
22
22
  id = params["id"]
23
- ticket_id = params["ticket_id"]
24
-
25
- path = "/tickets_metrics/#{id}.json"
26
23
 
27
- if body = self.data[:ticket_metrics][id]
28
- response(
29
- :path => path,
30
- :body => {
31
- "ticket_metric" => body
32
- },
33
- )
34
- else
35
- response(
36
- :path => path,
37
- :status => 404,
38
- :body => {"error" => "RecordNotFound", "description" => "Not found"},
39
- )
40
- end
24
+ response(
25
+ :path => "/tickets_metrics/#{id}.json",
26
+ :body => {
27
+ "ticket_metric" => find!(:ticket_metrics, id)
28
+ },
29
+ )
41
30
  end
42
31
  end # Mock
43
32
  end
@@ -16,15 +16,12 @@ class Zendesk2::Client
16
16
  def get_topic_comment(params={})
17
17
  id = params["id"]
18
18
  topic_id = params["topic_id"]
19
- path = "/topics/#{topic_id}/comments/#{id}.json"
20
-
21
- body = self.data[:topic_comments][id]
22
19
 
23
- unless (topic_comment = self.data[:topic_comments][id]) && topic_comment["topic_id"] == topic_id
24
- response(status: 404)
20
+ unless (topic_comment = self.find!(:topic_comments, id)) && topic_comment["topic_id"] == topic_id
21
+ error!(:not_found)
25
22
  else
26
23
  response(
27
- :path => "/topic_comments/#{id}.json",
24
+ :path => "/topics/#{topic_id}/comments/#{id}.json",
28
25
  :body => {
29
26
  "topic_comment" => topic_comment,
30
27
  },
@@ -13,7 +13,7 @@ class Zendesk2::Client
13
13
  class Mock
14
14
  def get_topic_comments(params={})
15
15
  topic_id = params["topic_id"]
16
- topic = self.data[:topics][topic_id] # TODO: 404 if !topic
16
+ self.find!(:topics, topic_id)
17
17
  filter = lambda { |comments| comments.select { |c| c["topic_id"] == topic_id } }
18
18
 
19
19
  page(params, :topic_comments, "/topics/#{topic_id}/comments.json", "topic_comments", filter: filter).tap do
@@ -13,22 +13,18 @@ class Zendesk2::Client
13
13
  class Mock
14
14
  def get_user(params={})
15
15
  id = params["id"]
16
- if body = self.data[:users][id]
17
16
 
18
- identities = self.data[:identities].values.select{|i| i["user_id"] == id}
19
- identity = identities.find{|i| i["type"] == "email" && i["primary"]} || identities.find{|i| i["type"] == "email"}
17
+ identities = self.data[:identities].values.select{|i| i["user_id"] == id}
18
+ identity = identities.find { |i| i["type"] == "email" && i["primary"] } || identities.find { |i| i["type"] == "email" }
20
19
 
21
- # @todo what happens if no identity?
20
+ # @todo what happens if no identity?
22
21
 
23
- response(
24
- :path => "/users/#{id}.json",
25
- :body => {
26
- "user" => body.merge("email" => identity["value"]),
27
- },
28
- )
29
- else
30
- response(status: 404)
31
- end
22
+ response(
23
+ :path => "/users/#{id}.json",
24
+ :body => {
25
+ "user" => find!(:users, id).merge("email" => identity["value"]),
26
+ },
27
+ )
32
28
  end
33
29
  end # Mock
34
30
  end
@@ -0,0 +1,25 @@
1
+ class Zendesk2::Client
2
+ class Real
3
+ def get_user_field(params={})
4
+ id = params["id"]
5
+
6
+ request(
7
+ :method => :get,
8
+ :path => "/user_fields/#{id}.json"
9
+ )
10
+ end
11
+ end # Real
12
+
13
+ class Mock
14
+ def get_user_field(params={})
15
+ id = params["id"]
16
+
17
+ response(
18
+ :path => "/user_fields/#{id}.json",
19
+ :body => {
20
+ "user_field" => find!(:user_fields, id)
21
+ },
22
+ )
23
+ end
24
+ end # Mock
25
+ end
@@ -0,0 +1,18 @@
1
+ class Zendesk2::Client
2
+ class Real
3
+ def get_user_fields(params={})
4
+ page_params = Zendesk2.paging_parameters(params)
5
+
6
+ request(
7
+ :params => page_params,
8
+ :method => :get,
9
+ :path => "/user_fields.json",
10
+ )
11
+ end
12
+ end
13
+ class Mock
14
+ def get_user_fields(params={})
15
+ collection(:user_fields, "/user_fields.json", "user_fields")
16
+ end
17
+ end
18
+ end
@@ -13,22 +13,13 @@ class Zendesk2::Client
13
13
  def get_user_identity(params={})
14
14
  id = params["id"]
15
15
  user_id = params["user_id"]
16
- path = "/users/#{user_id}/identities/#{id}.json"
17
16
 
18
- if body = self.data[:identities][id]
19
- response(
20
- :path => path,
21
- :body => {
22
- "identity" => body
23
- },
24
- )
25
- else
26
- response(
27
- :path => path,
28
- :status => 404,
29
- :body => {"error" => "RecordNotFound", "description" => "Not found"},
30
- )
31
- end
17
+ response(
18
+ :path => "/users/#{user_id}/identities/#{id}.json",
19
+ :body => {
20
+ "identity" => find!(:identities, id)
21
+ },
22
+ )
32
23
  end
33
24
  end # Mock
34
25
  end
@@ -16,7 +16,7 @@ class Zendesk2::Client
16
16
  def get_user_memberships(params={})
17
17
  user_id = params["user_id"]
18
18
 
19
- page(params, :memberships, "/users/#{user_id}/organization_memberships.json", "organization_memberships", filter: lambda{|c| c.select { |a| a["user_id"] == user_id }})
19
+ resources(:memberships, "/users/#{user_id}/organization_memberships.json", "organization_memberships", filter: lambda{|c| c.select { |a| a["user_id"] == user_id }})
20
20
  end
21
21
  end # Mock
22
22
  end
@@ -19,7 +19,7 @@ class Zendesk2::Client
19
19
 
20
20
  path = "/users/#{user_id}/organization_memberships/#{id}/make_default.json"
21
21
 
22
- if (membership = self.data[:memberships][id]) && membership["user_id"] == user_id
22
+ if (membership = self.find!(:memberships, id)) && membership["user_id"] == user_id
23
23
  # only one user can be default
24
24
  other_user_memberships = self.data[:memberships].values.select { |m| m["user_id"] == user_id }
25
25
  other_user_memberships.each { |i| i["default"] = false }
@@ -29,11 +29,7 @@ class Zendesk2::Client
29
29
  :method => :put,
30
30
  :path => path
31
31
  )
32
- else
33
- response(
34
- :path => path,
35
- :status => 404
36
- )
32
+ else error!(:not_found)
37
33
  end
38
34
  end
39
35
  end
@@ -17,7 +17,7 @@ class Zendesk2::Client
17
17
  user_id = params.delete("user_id")
18
18
  path = "/users/#{user_id}/identities/#{id}/make_primary.json"
19
19
 
20
- user_identity = self.data[:identities][id]
20
+ user_identity = self.find!(:identities, id)
21
21
 
22
22
  if user_identity && user_identity["user_id"] == user_id
23
23
  # only one user can be primary
@@ -29,11 +29,8 @@ class Zendesk2::Client
29
29
  :method => :put,
30
30
  :path => path
31
31
  )
32
- else
33
- response(
34
- :path => path,
35
- :status => 404
36
- )
32
+ else
33
+ error!(:not_found)
37
34
  end
38
35
  end
39
36
  end
@@ -15,21 +15,15 @@ class Zendesk2::Client
15
15
  end
16
16
  class Mock
17
17
  def update_group(params={})
18
- id = params.delete("id")
19
- path = "/groups/#{id}.json"
20
-
21
- if group = self.data[:groups][id]
22
- group.merge!(params)
23
- response(
24
- :method => :put,
25
- :path => "/groups/#{id}.json",
26
- :body => {
27
- "group" => group
28
- },
29
- )
30
- else response(status: 404)
31
- end
18
+ id = params.delete("id")
32
19
 
20
+ response(
21
+ :method => :put,
22
+ :path => "/groups/#{id}.json",
23
+ :body => {
24
+ "group" => find!(:groups, id).merge!(params)
25
+ },
26
+ )
33
27
  end
34
28
  end
35
29
  end
@@ -20,8 +20,8 @@ class Zendesk2::Client
20
20
  topic_id = params.delete("topic_id")
21
21
  path = "/topics/#{topic_id}/comments/#{id}.json"
22
22
 
23
- unless (topic_comment = self.data[:topic_comments][id]) && topic_comment["topic_id"] == topic_id
24
- response(status: 404)
23
+ unless (topic_comment = self.find!(:topic_comments, id)) && topic_comment["topic_id"] == topic_id
24
+ error!(:not_found)
25
25
  else
26
26
  body = topic_comment.merge!(params)
27
27
  response(
@@ -0,0 +1,28 @@
1
+ class Zendesk2::Client
2
+ class Real
3
+ def update_user_field(params={})
4
+ id = params.delete("id")
5
+
6
+ request(
7
+ :method => :put,
8
+ :path => "/user_fields/#{id}.json",
9
+ :body => {
10
+ "user_field" => params
11
+ },
12
+ )
13
+ end
14
+ end
15
+ class Mock
16
+ def update_user_field(params={})
17
+ user_field_id = params.delete("id")
18
+
19
+ response(
20
+ :method => :put,
21
+ :path => "/user_fields/#{user_field_id}.json",
22
+ :body => {
23
+ "user_field" => find!(:user_fields, user_field_id).merge!(params),
24
+ },
25
+ )
26
+ end
27
+ end
28
+ end
@@ -18,8 +18,9 @@ class Zendesk2::Client < Cistern::Service
18
18
  collection :ticket_comments
19
19
  collection :topic_comments
20
20
  collection :topics
21
- collection :users
21
+ collection :user_fields
22
22
  collection :user_identities
23
+ collection :users
23
24
  model :category
24
25
  model :forum
25
26
  model :group
@@ -38,9 +39,9 @@ class Zendesk2::Client < Cistern::Service
38
39
  model :topic
39
40
  model :topic_comment
40
41
  model :user
42
+ model :user_field
41
43
  model :user_identity
42
44
 
43
- request :get_assignable_groups
44
45
  request :create_category
45
46
  request :create_forum
46
47
  request :create_group
@@ -51,6 +52,7 @@ class Zendesk2::Client < Cistern::Service
51
52
  request :create_topic
52
53
  request :create_topic_comment
53
54
  request :create_user
55
+ request :create_user_field
54
56
  request :create_user_identity
55
57
  request :destroy_category
56
58
  request :destroy_forum
@@ -62,7 +64,9 @@ class Zendesk2::Client < Cistern::Service
62
64
  request :destroy_topic
63
65
  request :destroy_topic_comment
64
66
  request :destroy_user
67
+ request :destroy_user_field
65
68
  request :destroy_user_identity
69
+ request :get_assignable_groups
66
70
  request :get_audits
67
71
  request :get_categories
68
72
  request :get_category
@@ -73,10 +77,9 @@ class Zendesk2::Client < Cistern::Service
73
77
  request :get_group
74
78
  request :get_groups
75
79
  request :get_membership
76
- request :get_user_memberships
77
- request :get_organization_memberships
78
80
  request :get_organization
79
81
  request :get_organization_by_external_id
82
+ request :get_organization_memberships
80
83
  request :get_organization_tickets
81
84
  request :get_organization_users
82
85
  request :get_organizations
@@ -95,8 +98,11 @@ class Zendesk2::Client < Cistern::Service
95
98
  request :get_topic_comments
96
99
  request :get_topics
97
100
  request :get_user
101
+ request :get_user_field
102
+ request :get_user_fields
98
103
  request :get_user_identities
99
104
  request :get_user_identity
105
+ request :get_user_memberships
100
106
  request :get_users
101
107
  request :mark_membership_default
102
108
  request :mark_user_identity_primary
@@ -112,24 +118,34 @@ class Zendesk2::Client < Cistern::Service
112
118
  request :update_topic
113
119
  request :update_topic_comment
114
120
  request :update_user
121
+ request :update_user_field
115
122
  request :update_user_identity
116
123
 
117
124
  recognizes :url, :subdomain, :host, :port, :path, :scheme, :logger, :adapter, :username, :password, :token, :jwt_token
118
125
 
126
+ module Shared
127
+ def require_parameters(params, *requirements)
128
+ if (missing = requirements - params.keys).any?
129
+ raise ArgumentError, "missing parameters: #{missing.join(", ")}"
130
+ end
131
+ end
132
+ end
133
+
119
134
  class Real
135
+ include Shared
120
136
 
121
137
  attr_accessor :username, :url, :token, :logger, :jwt_token
122
138
 
123
139
  def initialize(options={})
124
140
  url = options[:url] || Zendesk2.defaults[:url] || begin
125
- host = options[:host]
126
- subdomain = options[:subdomain] || Zendesk2.defaults[:subdomain]
141
+ host = options[:host]
142
+ subdomain = options[:subdomain] || Zendesk2.defaults[:subdomain]
127
143
 
128
- host ||= "#{subdomain}.zendesk.com"
129
- scheme = options[:scheme] || "https"
130
- port = options[:port] || (scheme == "https" ? 443 : 80)
144
+ host ||= "#{subdomain}.zendesk.com"
145
+ scheme = options[:scheme] || "https"
146
+ port = options[:port] || (scheme == "https" ? 443 : 80)
131
147
 
132
- "#{scheme}://#{host}:#{port}"
148
+ "#{scheme}://#{host}:#{port}"
133
149
  end
134
150
 
135
151
  @url = URI.parse(url).to_s
@@ -181,6 +197,7 @@ class Zendesk2::Client < Cistern::Service
181
197
  end
182
198
 
183
199
  class Mock
200
+ include Shared
184
201
 
185
202
  attr_reader :username, :url, :token, :jwt_token
186
203
 
@@ -199,6 +216,7 @@ class Zendesk2::Client < Cistern::Service
199
216
  :tickets => {},
200
217
  :topic_comments => {},
201
218
  :topics => {},
219
+ :user_fields => {},
202
220
  :users => {},
203
221
  }
204
222
  end
@@ -218,12 +236,12 @@ class Zendesk2::Client < Cistern::Service
218
236
 
219
237
  def initialize(options={})
220
238
  url = options[:url] || begin
221
- host = options[:host]
222
- host ||= "#{options[:subdomain] || "mock"}.zendesk.com"
223
- scheme = options[:scheme] || "https"
224
- port = options[:port] || (scheme == "https" ? 443 : 80)
239
+ host = options[:host]
240
+ host ||= "#{options[:subdomain] || "mock"}.zendesk.com"
241
+ scheme = options[:scheme] || "https"
242
+ port = options[:port] || (scheme == "https" ? 443 : 80)
225
243
 
226
- "#{scheme}://#{host}:#{port}"
244
+ "#{scheme}://#{host}:#{port}"
227
245
  end
228
246
 
229
247
  @url = url
@@ -249,6 +267,21 @@ class Zendesk2::Client < Cistern::Service
249
267
  File.join(@url, "/api/v2", path.to_s)
250
268
  end
251
269
 
270
+ def resources(collection, path, collection_root, options={})
271
+ filter = options[:filter]
272
+ resources = self.data[collection].values
273
+ resources = filter.call(resources) if filter
274
+ count = resources.size
275
+
276
+ response(
277
+ :body => {
278
+ collection_root => resources,
279
+ "count" => count,
280
+ },
281
+ :path => path
282
+ )
283
+ end
284
+
252
285
  def page(params, collection, path, collection_root, options={})
253
286
  page_params = Zendesk2.paging_parameters(params)
254
287
  page_size = (page_params["per_page"] || 50).to_i
@@ -288,6 +321,37 @@ class Zendesk2::Client < Cistern::Service
288
321
  pluralized
289
322
  end
290
323
 
324
+ def self.error_map
325
+ @@error_map ||= {
326
+ :invalid => [422, {
327
+ "error" => "RecordInvalid",
328
+ "description" => "Record validation errors",
329
+ }],
330
+ :not_found => [404, {
331
+ "error" => "RecordNotFound",
332
+ "description" => "Not found",
333
+ }],
334
+ }
335
+ end
336
+
337
+ def find!(collection, identity, options={})
338
+ if resource = self.data[collection][identity]
339
+ resource
340
+ else
341
+ error!(options[:error] || :not_found)
342
+ end
343
+ end
344
+
345
+ def error!(type, options={})
346
+ status, body = self.class.error_map[type]
347
+ body.merge!("details" => options[:details]) if options[:details]
348
+
349
+ response(
350
+ :status => status,
351
+ :body => body,
352
+ )
353
+ end
354
+
291
355
  def response(options={})
292
356
  method = options[:method] || :get
293
357
  status = options[:status] || 200