desk 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (74) hide show
  1. data/.gemtest +0 -0
  2. data/.gitignore +11 -0
  3. data/.rspec +3 -0
  4. data/.yardopts +9 -0
  5. data/Gemfile +12 -0
  6. data/HISTORY.mkd +44 -0
  7. data/LICENSE.mkd +20 -0
  8. data/README.mkd +267 -0
  9. data/Rakefile +23 -0
  10. data/desk.gemspec +44 -0
  11. data/lib/desk.rb +26 -0
  12. data/lib/desk/api.rb +28 -0
  13. data/lib/desk/authentication.rb +25 -0
  14. data/lib/desk/client.rb +28 -0
  15. data/lib/desk/client/article.rb +92 -0
  16. data/lib/desk/client/case.rb +55 -0
  17. data/lib/desk/client/customer.rb +146 -0
  18. data/lib/desk/client/interaction.rb +75 -0
  19. data/lib/desk/client/macro.rb +142 -0
  20. data/lib/desk/client/topic.rb +90 -0
  21. data/lib/desk/client/user.rb +38 -0
  22. data/lib/desk/configuration.rb +98 -0
  23. data/lib/desk/connection.rb +39 -0
  24. data/lib/desk/error.rb +67 -0
  25. data/lib/desk/request.rb +44 -0
  26. data/lib/desk/version.rb +4 -0
  27. data/lib/faraday/request/multipart_with_file.rb +30 -0
  28. data/lib/faraday/request/oauth.rb +23 -0
  29. data/lib/faraday/response/raise_http_4xx.rb +45 -0
  30. data/lib/faraday/response/raise_http_5xx.rb +24 -0
  31. data/spec/desk/api_spec.rb +70 -0
  32. data/spec/desk/client/article_spec.rb +134 -0
  33. data/spec/desk/client/case_spec.rb +99 -0
  34. data/spec/desk/client/customer_spec.rb +158 -0
  35. data/spec/desk/client/interaction_spec.rb +191 -0
  36. data/spec/desk/client/macro_spec.rb +204 -0
  37. data/spec/desk/client/topic_spec.rb +135 -0
  38. data/spec/desk/client/user_spec.rb +58 -0
  39. data/spec/desk/client_spec.rb +10 -0
  40. data/spec/desk_spec.rb +127 -0
  41. data/spec/faraday/response_spec.rb +34 -0
  42. data/spec/fixtures/article.json +50 -0
  43. data/spec/fixtures/article_create.json +54 -0
  44. data/spec/fixtures/article_destroy.json +3 -0
  45. data/spec/fixtures/article_update.json +54 -0
  46. data/spec/fixtures/articles.json +58 -0
  47. data/spec/fixtures/case.json +59 -0
  48. data/spec/fixtures/case_update.json +59 -0
  49. data/spec/fixtures/cases.json +182 -0
  50. data/spec/fixtures/customer.json +58 -0
  51. data/spec/fixtures/customer_create.json +56 -0
  52. data/spec/fixtures/customer_create_email.json +15 -0
  53. data/spec/fixtures/customer_update.json +47 -0
  54. data/spec/fixtures/customer_update_email.json +15 -0
  55. data/spec/fixtures/customers.json +98 -0
  56. data/spec/fixtures/interaction_create.json +126 -0
  57. data/spec/fixtures/interactions.json +139 -0
  58. data/spec/fixtures/macro.json +8 -0
  59. data/spec/fixtures/macro_action.json +9 -0
  60. data/spec/fixtures/macro_action_update.json +12 -0
  61. data/spec/fixtures/macro_actions.json +69 -0
  62. data/spec/fixtures/macro_create.json +13 -0
  63. data/spec/fixtures/macro_destroy.json +3 -0
  64. data/spec/fixtures/macro_update.json +13 -0
  65. data/spec/fixtures/macros.json +24 -0
  66. data/spec/fixtures/topic.json +9 -0
  67. data/spec/fixtures/topic_create.json +14 -0
  68. data/spec/fixtures/topic_destroy.json +3 -0
  69. data/spec/fixtures/topic_update.json +14 -0
  70. data/spec/fixtures/topics.json +35 -0
  71. data/spec/fixtures/user.json +15 -0
  72. data/spec/fixtures/users.json +24 -0
  73. data/spec/helper.rb +55 -0
  74. metadata +464 -0
@@ -0,0 +1,158 @@
1
+ require 'helper'
2
+
3
+ describe Desk::Client do
4
+ Desk::Configuration::VALID_FORMATS.each do |format|
5
+ context ".new(:format => '#{format}')" do
6
+ before do
7
+ @client = Desk::Client.new(:subdomain => "example", :format => format, :consumer_key => 'CK', :consumer_secret => 'CS', :oauth_token => 'OT', :oauth_token_secret => 'OS')
8
+ end
9
+
10
+ describe ".customers" do
11
+
12
+ context "lookup" do
13
+
14
+ before do
15
+ stub_get("customers.#{format}").
16
+ to_return(:body => fixture("customers.#{format}"), :headers => {:content_type => "application/#{format}; charset=utf-8"})
17
+ end
18
+
19
+ it "should get the correct resource" do
20
+ @client.customers
21
+ a_get("customers.#{format}").
22
+ should have_been_made
23
+ end
24
+
25
+ it "should return up to 100 customers worth of extended information" do
26
+ customers = @client.customers
27
+
28
+ customers.results.should be_a Array
29
+ customers.results.first.customer.first_name.should == "Jeremy"
30
+ end
31
+
32
+ end
33
+ end
34
+
35
+ describe ".customer" do
36
+
37
+ context "lookup" do
38
+
39
+ before do
40
+ stub_get("customers/1.#{format}").
41
+ to_return(:body => fixture("customer.#{format}"), :headers => {:content_type => "application/#{format}; charset=utf-8"})
42
+ end
43
+
44
+ it "should get the correct resource" do
45
+ @client.customer(1)
46
+ a_get("customers/1.#{format}").
47
+ should have_been_made
48
+ end
49
+
50
+ it "should return up to 100 customers worth of extended information" do
51
+ customer = @client.customer(1)
52
+
53
+ customer.first_name.should == "Jeremy"
54
+ customer.addresses.first.address.city.should == "Commack"
55
+ end
56
+
57
+ end
58
+ end
59
+
60
+ describe ".create_customer" do
61
+
62
+ context "create" do
63
+
64
+ before do
65
+ stub_post("customers.#{format}").
66
+ to_return(:body => fixture("customer_create.#{format}"), :headers => {:content_type => "application/#{format}; charset=utf-8"})
67
+ end
68
+
69
+ it "should get the correct resource" do
70
+ @client.create_customer(:name => "Chris Warren", :twitter => "cdwarren")
71
+ a_post("customers.#{format}").
72
+ should have_been_made
73
+ end
74
+
75
+ it "should return the information about this user" do
76
+ customer = @client.create_customer(:name => "John Smith", :twitter => "cdwarren")
77
+
78
+ customer.first_name.should == "John"
79
+ customer.phones.first.phone.phone.should == "123-456-7890"
80
+ end
81
+
82
+ end
83
+ end
84
+
85
+ describe ".update_customer" do
86
+
87
+ context "update" do
88
+
89
+ before do
90
+ stub_put("customers/1.#{format}").
91
+ to_return(:body => fixture("customer_update.#{format}"), :headers => {:content_type => "application/#{format}; charset=utf-8"})
92
+ end
93
+
94
+ it "should get the correct resource" do
95
+ @client.update_customer(1, :name => "Chris Warren", :twitter => "cdwarren")
96
+ a_put("customers/1.#{format}").
97
+ should have_been_made
98
+ end
99
+
100
+ it "should return the information about this user" do
101
+ customer = @client.update_customer(1, :name => "Joslyn Esser")
102
+
103
+ customer.first_name.should == "Joslyn"
104
+ end
105
+
106
+ end
107
+ end
108
+
109
+ describe ".create_customer_email" do
110
+
111
+ context "create" do
112
+
113
+ before do
114
+ stub_post("customers/1/emails.#{format}").
115
+ to_return(:body => fixture("customer_create_email.#{format}"), :headers => {:content_type => "application/#{format}; charset=utf-8"})
116
+ end
117
+
118
+ it "should get the correct resource" do
119
+ @client.create_customer_email(1, :email => "foo@example.com")
120
+ a_post("customers/1/emails.#{format}").
121
+ should have_been_made
122
+ end
123
+
124
+ it "should return the information about this user" do
125
+ email = @client.create_customer_email(1, :email => "api@example.com")
126
+
127
+ email.email.should == "api@example.com"
128
+ end
129
+
130
+ end
131
+ end
132
+
133
+ describe ".update_customer_email" do
134
+
135
+ context "update" do
136
+
137
+ before do
138
+ stub_put("customers/1/emails/2.#{format}").
139
+ to_return(:body => fixture("customer_update_email.#{format}"), :headers => {:content_type => "application/#{format}; charset=utf-8"})
140
+ end
141
+
142
+ it "should get the correct resource" do
143
+ @client.update_customer_email(1, 2, :email => "foo@example.com")
144
+ a_put("customers/1/emails/2.#{format}").
145
+ should have_been_made
146
+ end
147
+
148
+ it "should return the information about this user" do
149
+ email = @client.update_customer_email(1, 2, :email => "api@example.com")
150
+
151
+ email.email.should == "api@example.com"
152
+ end
153
+
154
+ end
155
+ end
156
+ end
157
+ end
158
+ end
@@ -0,0 +1,191 @@
1
+ require 'helper'
2
+
3
+ describe Desk::Client do
4
+ include EmailSpec::Helpers
5
+ include EmailSpec::Matchers
6
+
7
+ Desk::Configuration::VALID_FORMATS.each do |format|
8
+ context ".new(:format => '#{format}')" do
9
+ before do
10
+ @client = Desk::Client.new(:subdomain => "example", :format => format, :consumer_key => 'CK', :consumer_secret => 'CS', :oauth_token => 'OT', :oauth_token_secret => 'OS', :support_email => "help@example.com")
11
+ end
12
+
13
+ describe ".create_interaction" do
14
+ context "create a new interaction without specifying direction should default to inbound" do
15
+ before do
16
+ stub_post("interactions.#{format}").
17
+ to_return(:body => fixture("interaction_create.#{format}"), :headers => {:content_type => "application/#{format}; charset=utf-8"})
18
+ end
19
+
20
+ it "should get the correct resource" do
21
+ @client.create_interaction(:interaction_subject => "this is an api test", :customer_email => "foo@example.com")
22
+ a_post("interactions.#{format}").
23
+ should have_been_made
24
+ end
25
+
26
+ it "should create an interaction" do
27
+ interaction = @client.create_interaction(:interaction_subject => "this is an api test", :customer_email => "foo@example.com")
28
+
29
+ interaction.customer.emails.first.email.email.should == "customer@zencoder.com"
30
+ interaction.interaction.interactionable.email.subject.should == "this is an api test"
31
+ end
32
+ end
33
+
34
+ context "create a new interaction and specify inbound" do
35
+ before do
36
+ stub_post("interactions.#{format}").
37
+ to_return(:body => fixture("interaction_create.#{format}"), :headers => {:content_type => "application/#{format}; charset=utf-8"})
38
+ end
39
+
40
+ it "should get the correct resource" do
41
+ @client.create_interaction(:interaction_subject => "this is an api test", :customer_email => "foo@example.com", :direction => "in")
42
+ a_post("interactions.#{format}").
43
+ should have_been_made
44
+ end
45
+
46
+ it "should create an interaction" do
47
+ interaction = @client.create_interaction(:interaction_subject => "this is an api test", :customer_email => "foo@example.com", :direction => "in")
48
+
49
+ interaction.customer.emails.first.email.email.should == "customer@zencoder.com"
50
+ interaction.interaction.interactionable.email.subject.should == "this is an api test"
51
+ end
52
+ end
53
+
54
+ context "create a new interaction and specify outbound" do
55
+ before do
56
+ @email = @client.create_interaction(:customer_email => "customer@example.com", :interaction_subject => "Need help?", :interaction_body => "Sorry we missed you in chat today.", :direction => "outbound")
57
+ end
58
+
59
+ it "should deliver the email to the customer" do
60
+ @email.last.should deliver_to("customer@example.com")
61
+ end
62
+
63
+ it "should be from the support email" do
64
+ @email.last.should deliver_from(@client.support_email)
65
+ end
66
+
67
+ it "should contain the message in the mail body" do
68
+ @email.last.should have_body_text(/Sorry we missed you in chat today/)
69
+ end
70
+
71
+ it "should bcc to the support email" do
72
+ @email.last.should bcc_to(@client.support_email)
73
+ end
74
+
75
+ it "should set the Desk headers" do
76
+ @email.last.should have_header("x-assistly-customer-email","customer@example.com")
77
+ @email.last.should have_header("x-assistly-interaction-direction","out")
78
+ @email.last.should have_header("x-assistly-case-status","open")
79
+ end
80
+ end
81
+ end
82
+
83
+ describe ".create_inbound_interaction" do
84
+ context "create a new interaction" do
85
+ before do
86
+ stub_post("interactions.#{format}").
87
+ to_return(:body => fixture("interaction_create.#{format}"), :headers => {:content_type => "application/#{format}; charset=utf-8"})
88
+ end
89
+
90
+ it "should get the correct resource" do
91
+ @client.create_inbound_interaction(:interaction_subject => "this is an api test", :customer_email => "foo@example.com")
92
+ a_post("interactions.#{format}").
93
+ should have_been_made
94
+ end
95
+
96
+ it "should create an interaction" do
97
+ interaction = @client.create_inbound_interaction(:interaction_subject => "this is an api test", :customer_email => "foo@example.com")
98
+
99
+ interaction.customer.emails.first.email.email.should == "customer@zencoder.com"
100
+ interaction.interaction.interactionable.email.subject.should == "this is an api test"
101
+ end
102
+ end
103
+ end
104
+
105
+ describe ".create_outbound_interaction" do
106
+ context "create" do
107
+
108
+ before do
109
+ @email = @client.create_outbound_interaction("customer@example.com", "Need help?", "Sorry we missed you in chat today.")
110
+ end
111
+
112
+ it "should deliver the email to the customer" do
113
+ @email.last.should deliver_to("customer@example.com")
114
+ end
115
+
116
+ it "should be from the support email" do
117
+ @email.last.should deliver_from(@client.support_email)
118
+ end
119
+
120
+ it "should contain the message in the mail body" do
121
+ @email.last.should have_body_text(/Sorry we missed you in chat today/)
122
+ end
123
+
124
+ it "should bcc to the support email" do
125
+ @email.last.should bcc_to(@client.support_email)
126
+ end
127
+
128
+ it "should set the Assitly headers" do
129
+ @email.last.should have_header("x-assistly-customer-email","customer@example.com")
130
+ @email.last.should have_header("x-assistly-interaction-direction","out")
131
+ @email.last.should have_header("x-assistly-case-status","open")
132
+ end
133
+
134
+ end
135
+
136
+ context "without support_email defined" do
137
+
138
+ before do
139
+ @client_without_support_email = Desk::Client.new(:subdomain => "example", :format => format, :consumer_key => 'CK', :consumer_secret => 'CS', :oauth_token => 'OT', :oauth_token_secret => 'OS')
140
+ end
141
+
142
+ it "should raise an error" do
143
+ lambda do
144
+ @client_without_support_email.create_outbound_interaction("customer@example.com", "Need help?", "Sorry we missed you in chat today.")
145
+ end.should raise_error(Desk::SupportEmailNotSet)
146
+ end
147
+
148
+ end
149
+
150
+ context "with customer headers set" do
151
+ before do
152
+ @custom_email = @client.create_outbound_interaction("customer@example.com", "Need help?", "Sorry we missed you in chat today.", :headers => { "x-assistly-interaction-user-agent" => "12345"})
153
+ end
154
+
155
+ it "should merge the custom headers" do
156
+ @custom_email.last.should have_header("x-assistly-interaction-user-agent","12345")
157
+ end
158
+
159
+ it "should preserve the existing headers" do
160
+ @custom_email.last.should have_header("x-assistly-customer-email","customer@example.com")
161
+ end
162
+ end
163
+ end
164
+
165
+ describe ".interactions" do
166
+
167
+ context "lookup" do
168
+
169
+ before do
170
+ stub_get("interactions.#{format}").
171
+ to_return(:body => fixture("interactions.#{format}"), :headers => {:content_type => "application/#{format}; charset=utf-8"})
172
+ end
173
+
174
+ it "should get the correct resource" do
175
+ @client.interactions
176
+ a_get("interactions.#{format}").
177
+ should have_been_made
178
+ end
179
+
180
+ it "should return up to 100 users worth of extended information" do
181
+ interactions = @client.interactions
182
+
183
+ interactions.results.should be_a Array
184
+ interactions.results.last.interaction.user.name.should == "Agent Jeremy"
185
+ end
186
+
187
+ end
188
+ end
189
+ end
190
+ end
191
+ end
@@ -0,0 +1,204 @@
1
+ require 'helper'
2
+
3
+ describe Desk::Client do
4
+ Desk::Configuration::VALID_FORMATS.each do |format|
5
+ context ".new(:format => '#{format}')" do
6
+ before do
7
+ @client = Desk::Client.new(:subdomain => "example", :format => format, :consumer_key => 'CK', :consumer_secret => 'CS', :oauth_token => 'OT', :oauth_token_secret => 'OS')
8
+ end
9
+
10
+ describe ".macros" do
11
+
12
+ context "lookup" do
13
+
14
+ before do
15
+ stub_get("macros.#{format}").
16
+ to_return(:body => fixture("macros.#{format}"), :headers => {:content_type => "application/#{format}; charset=utf-8"})
17
+ end
18
+
19
+ it "should get the correct resource" do
20
+ @client.macros
21
+ a_get("macros.#{format}").
22
+ should have_been_made
23
+ end
24
+
25
+ it "should return up to 100 macros worth of extended information" do
26
+ macros = @client.macros
27
+
28
+ macros.results.should be_a Array
29
+ macros.results.first.macro.id.should == 11
30
+ end
31
+
32
+ end
33
+ end
34
+
35
+ describe ".macro" do
36
+
37
+ context "lookup" do
38
+
39
+ before do
40
+ stub_get("macros/13.#{format}").
41
+ to_return(:body => fixture("macro.#{format}"), :headers => {:content_type => "application/#{format}; charset=utf-8"})
42
+ end
43
+
44
+ it "should get the correct resource" do
45
+ @client.macro(13)
46
+ a_get("macros/13.#{format}").
47
+ should have_been_made
48
+ end
49
+
50
+ it "should return up to 100 cases worth of extended information" do
51
+ macro = @client.macro(13)
52
+
53
+ macro.id.should == 13
54
+ macro.name.should == "API Macro"
55
+ end
56
+
57
+ end
58
+ end
59
+
60
+ describe ".create_macro" do
61
+
62
+ context "create" do
63
+
64
+ before do
65
+ stub_post("macros.#{format}").
66
+ to_return(:body => fixture("macro_create.#{format}"), :headers => {:content_type => "application/#{format}; charset=utf-8"})
67
+ end
68
+
69
+ it "should post to the correct resource" do
70
+ @client.create_macro("API Macro", :description => "Everything belongs here")
71
+ a_post("macros.#{format}").
72
+ should have_been_made
73
+ end
74
+
75
+ it "should return the new macro" do
76
+ macro = @client.create_macro("API Macro", :description => "Everything belongs here")
77
+
78
+ macro.id.should == 12
79
+ macro.name.should == "API Macro"
80
+ end
81
+
82
+ end
83
+ end
84
+
85
+ describe ".update_macro" do
86
+
87
+ context "update" do
88
+
89
+ before do
90
+ stub_put("macros/13.#{format}").
91
+ to_return(:body => fixture("macro_update.#{format}"), :headers => {:content_type => "application/#{format}; charset=utf-8"})
92
+ end
93
+
94
+ it "should post to the correct resource" do
95
+ @client.update_macro(13, :name => "Updated")
96
+ a_put("macros/13.#{format}").
97
+ should have_been_made
98
+ end
99
+
100
+ it "should return the new macro" do
101
+ macro = @client.update_macro(13, :name => "Updated")
102
+
103
+ macro.name.should == "Updated"
104
+ end
105
+
106
+ end
107
+ end
108
+
109
+ describe ".delete_macro" do
110
+
111
+ context "delete" do
112
+
113
+ before do
114
+ stub_delete("macros/1.#{format}").
115
+ to_return(:body => fixture("macro_destroy.#{format}"), :headers => {:content_type => "application/#{format}; charset=utf-8"})
116
+ end
117
+
118
+ it "should post to the correct resource" do
119
+ @client.delete_macro(1)
120
+ a_delete("macros/1.#{format}").
121
+ should have_been_made
122
+ end
123
+
124
+ it "should return a successful response" do
125
+ macro = @client.delete_macro(1)
126
+ macro.success.should == true
127
+ end
128
+
129
+ end
130
+ end
131
+
132
+ describe ".macro_actions" do
133
+
134
+ context "lookup" do
135
+
136
+ before do
137
+ stub_get("macros/1/actions.#{format}").
138
+ to_return(:body => fixture("macro_actions.#{format}"), :headers => {:content_type => "application/#{format}; charset=utf-8"})
139
+ end
140
+
141
+ it "should get the correct resource" do
142
+ @client.macro_actions(1)
143
+ a_get("macros/1/actions.#{format}").
144
+ should have_been_made
145
+ end
146
+
147
+ it "should return up to 100 macro actions worth of extended information" do
148
+ macro_actions = @client.macro_actions(1)
149
+
150
+ macro_actions.should be_a Array
151
+ macro_actions.first.action.slug.should == "set-case-description"
152
+ end
153
+
154
+ end
155
+ end
156
+
157
+ describe ".macro_action" do
158
+
159
+ context "lookup" do
160
+
161
+ before do
162
+ stub_get("macros/1/actions/set-case-description.#{format}").
163
+ to_return(:body => fixture("macro_action.#{format}"), :headers => {:content_type => "application/#{format}; charset=utf-8"})
164
+ end
165
+
166
+ it "should get the correct resource" do
167
+ @client.macro_action(1,"set-case-description")
168
+ a_get("macros/1/actions/set-case-description.#{format}").
169
+ should have_been_made
170
+ end
171
+
172
+ it "should return up to 100 macro actions worth of extended information" do
173
+ macro_action = @client.macro_action(1,"set-case-description")
174
+ macro_action.slug.should == "set-case-description"
175
+ end
176
+
177
+ end
178
+ end
179
+
180
+ describe ".update_macro_action" do
181
+
182
+ context "update" do
183
+
184
+ before do
185
+ stub_put("macros/1/actions/set-case-description.#{format}").
186
+ to_return(:body => fixture("macro_action_update.#{format}"), :headers => {:content_type => "application/#{format}; charset=utf-8"})
187
+ end
188
+
189
+ it "should post to the correct resource" do
190
+ @client.update_macro_action(1, "set-case-description", :value => "This is my case description")
191
+ a_put("macros/1/actions/set-case-description.#{format}").
192
+ should have_been_made
193
+ end
194
+
195
+ it "should return the new macro" do
196
+ macro_action = @client.update_macro_action(1, "set-case-description", :value => "This is my case description")
197
+ macro_action.value.should == "Description to be applied"
198
+ end
199
+
200
+ end
201
+ end
202
+ end
203
+ end
204
+ end