desk 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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
data/lib/desk.rb ADDED
@@ -0,0 +1,26 @@
1
+ require 'desk/error'
2
+ require 'desk/configuration'
3
+ require 'desk/api'
4
+ require 'desk/client'
5
+ require 'pony'
6
+
7
+ module Desk
8
+ extend Configuration
9
+
10
+ # Alias for Desk::Client.new
11
+ #
12
+ # @return [Desk::Client]
13
+ def self.client(options={})
14
+ Desk::Client.new(options)
15
+ end
16
+
17
+ # Delegate to Desk::Client
18
+ def self.method_missing(method, *args, &block)
19
+ return super unless client.respond_to?(method)
20
+ client.send(method, *args, &block)
21
+ end
22
+
23
+ def self.respond_to?(method)
24
+ client.respond_to?(method) || super
25
+ end
26
+ end
data/lib/desk/api.rb ADDED
@@ -0,0 +1,28 @@
1
+ require 'desk/connection'
2
+ require 'desk/request'
3
+ require 'desk/authentication'
4
+
5
+ module Desk
6
+ # @private
7
+ class API
8
+ # @private
9
+ attr_accessor *Configuration::VALID_OPTIONS_KEYS
10
+
11
+ # Creates a new API
12
+ def initialize(options={})
13
+ options = Desk.options.merge(options)
14
+
15
+ Configuration::VALID_OPTIONS_KEYS.each do |key|
16
+ send("#{key}=", options[key])
17
+ end
18
+ end
19
+
20
+ def endpoint
21
+ "https://#{self.subdomain}.desk.com/api/#{self.version}/"
22
+ end
23
+
24
+ include Connection
25
+ include Request
26
+ include Authentication
27
+ end
28
+ end
@@ -0,0 +1,25 @@
1
+ module Desk
2
+ # @private
3
+ module Authentication
4
+ private
5
+
6
+ # Authentication hash
7
+ #
8
+ # @return [Hash]
9
+ def authentication
10
+ {
11
+ :consumer_key => consumer_key,
12
+ :consumer_secret => consumer_secret,
13
+ :token => oauth_token,
14
+ :token_secret => oauth_token_secret
15
+ }
16
+ end
17
+
18
+ # Check whether user is authenticated
19
+ #
20
+ # @return [Boolean]
21
+ def authenticated?
22
+ authentication.values.all?
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,28 @@
1
+ module Desk
2
+ # Wrapper for the Desk.com REST API
3
+ #
4
+ # @note All methods have been separated into modules and follow the same grouping used in {http://dev.desk.com/doc the Desk.com API Documentation}.
5
+ # @see http://dev.desk.com/pages/every_developer
6
+ class Client < API
7
+ # Require client method modules after initializing the Client class in
8
+ # order to avoid a superclass mismatch error, allowing those modules to be
9
+ # Client-namespaced.
10
+ require 'desk/client/user'
11
+ require 'desk/client/interaction'
12
+ require 'desk/client/case'
13
+ require 'desk/client/customer'
14
+ require 'desk/client/topic'
15
+ require 'desk/client/article'
16
+ require 'desk/client/macro'
17
+
18
+ alias :api_endpoint :endpoint
19
+
20
+ include Desk::Client::User
21
+ include Desk::Client::Interaction
22
+ include Desk::Client::Case
23
+ include Desk::Client::Customer
24
+ include Desk::Client::Topic
25
+ include Desk::Client::Article
26
+ include Desk::Client::Macro
27
+ end
28
+ end
@@ -0,0 +1,92 @@
1
+ module Desk
2
+ class Client
3
+ # Defines methods related to articles
4
+ module Article
5
+ # Returns extended information of articles for a topic
6
+ #
7
+ # @param id [Integer] a article ID
8
+ # @option options [Boolean, String, Integer]
9
+ # @example Return extended information for 12345
10
+ # Desk.articles
11
+ # Desk.articles(:count => 5)
12
+ # Desk.articles(:count => 5, :page => 3)
13
+ # @format :json
14
+ # @authenticated true
15
+ # @see http://dev.desk.com/docs/api/topics/articles
16
+ def articles(id, *args)
17
+ options = args.last.is_a?(Hash) ? args.pop : {}
18
+ response = get("topics/#{id}/articles",options)
19
+ response
20
+ end
21
+
22
+ # Returns extended information on a single article
23
+ #
24
+ # @param id [Integer] a article ID
25
+ # @option options [Hash]
26
+ # @example Return extended information for 12345
27
+ # Desk.article(12345)
28
+ # Desk.article(12345, :by => "external_id")
29
+ # @format :json
30
+ # @authenticated true
31
+ # @see http://dev.desk.com/docs/api/articles/show
32
+ def article(id, *args)
33
+ options = args.last.is_a?(Hash) ? args.pop : {}
34
+ response = get("articles/#{id}",options)
35
+ response.article
36
+ end
37
+
38
+ # Creates a new article
39
+ #
40
+ # @param id [Integer] a article ID
41
+ # @param id [Integer] a article ID
42
+ # @param id [Integer] a article ID
43
+ # @option options [Hash]
44
+ # @example Creates a new article
45
+ # Desk.create_article(1, :subject => "API Tips", :main_content => "Tips on using our API")
46
+ # @format :json
47
+ # @authenticated true
48
+ # @see http://dev.desk.com/docs/api/articles/create
49
+ def create_article(topic_id, *args)
50
+ options = args.last.is_a?(Hash) ? args.pop : {}
51
+ response = post("topics/#{topic_id}/articles",options)
52
+ if response['success']
53
+ return response['results']['article']
54
+ else
55
+ return response
56
+ end
57
+ end
58
+
59
+ # Updates a single article
60
+ #
61
+ # @param id [Integer] a article ID
62
+ # @option options [String]
63
+ # @example Updates information for article 12345
64
+ # Desk.update_article(12345, :subject => "New Subject")
65
+ # @format :json
66
+ # @authenticated true
67
+ # @see http://dev.desk.com/docs/api/articles/update
68
+ def update_article(id, *args)
69
+ options = args.last.is_a?(Hash) ? args.pop : {}
70
+ response = put("articles/#{id}",options)
71
+ if response['success']
72
+ return response['results']['article']
73
+ else
74
+ return response
75
+ end
76
+ end
77
+
78
+ # Deletes a single article
79
+ #
80
+ # @param id [Integer] a article ID
81
+ # @example Deletes article 12345
82
+ # Desk.update_article(12345, :subject => "New Subject")
83
+ # @format :json
84
+ # @authenticated true
85
+ # @see http://dev.desk.com/docs/api/articles/update
86
+ def delete_article(id)
87
+ response = delete("articles/#{id}")
88
+ response
89
+ end
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,55 @@
1
+ module Desk
2
+ class Client
3
+ # Defines methods related to cases
4
+ module Case
5
+ # Returns extended information of cases
6
+ #
7
+ # @option options [Boolean, String, Integer]
8
+ # @example Return extended information for 12345
9
+ # Desk.cases(:case_id => 12345)
10
+ # Desk.cases(:email => "customer@example.com", :count => 5)
11
+ # Desk.cases(:since_id => 12345)
12
+ # @format :json
13
+ # @authenticated true
14
+ # @see http://dev.desk.com/docs/api/cases/show
15
+ def cases(*args)
16
+ options = args.last.is_a?(Hash) ? args.pop : {}
17
+ response = get("cases",options)
18
+ response
19
+ end
20
+
21
+ # Returns extended information on a single case
22
+ #
23
+ # @option options [String]
24
+ # @example Return extended information for 12345
25
+ # Desk.case(12345)
26
+ # Desk.case(12345, :by => "external_id")
27
+ # @format :json
28
+ # @authenticated true
29
+ # @see http://dev.desk.com/docs/api/cases/show
30
+ def case(id, *args)
31
+ options = args.last.is_a?(Hash) ? args.pop : {}
32
+ response = get("cases/#{id}",options)
33
+ response.case
34
+ end
35
+
36
+ # Updates a single case
37
+ #
38
+ # @option options [String]
39
+ # @example Return extended information for 12345
40
+ # Desk.update_case(12345, :subject => "New Subject")
41
+ # @format :json
42
+ # @authenticated true
43
+ # @see http://dev.desk.com/docs/api/cases/update
44
+ def update_case(id, *args)
45
+ options = args.last.is_a?(Hash) ? args.pop : {}
46
+ response = put("cases/#{id}",options)
47
+ response.case
48
+ end
49
+
50
+ def case_url(id)
51
+ "https://#{subdomain}.desk.com/agent/case/#{id}"
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,146 @@
1
+ module Desk
2
+ class Client
3
+ # Defines methods related to customers
4
+ module Customer
5
+ # Returns extended information of customers
6
+ #
7
+ # @option options [Boolean, String, Integer]
8
+ # @example Return extended information for customers
9
+ # Desk.customers
10
+ # Desk.customers(:since_id => 12345, :count => 5)
11
+ # @format :json
12
+ # @authenticated true
13
+ # @see http://dev.desk.com/docs/api/customers
14
+ def customers(*args)
15
+ options = args.last.is_a?(Hash) ? args.pop : {}
16
+ response = get("customers",options)
17
+ response
18
+ end
19
+
20
+ # Returns extended information on a single customer
21
+ #
22
+ # @option options [String]
23
+ # @example Return extended information for customer 12345
24
+ # Desk.customer(12345)
25
+ # @format :json
26
+ # @authenticated true
27
+ # @see http://dev.desk.com/docs/api/customers/show
28
+ def customer(id)
29
+ response = get("customers/#{id}")
30
+ response.customer
31
+ end
32
+
33
+ # Create a new customer
34
+ #
35
+ # @option options [String]
36
+ # @example Return extended information for 12345
37
+ # Desk.create_customer(:name => "Chris Warren", :twitter => "cdwarren")
38
+ # @format :json
39
+ # @authenticated true
40
+ # @see http://dev.desk.com/docs/api/customers/create
41
+ def create_customer(*args)
42
+ options = args.last.is_a?(Hash) ? args.pop : {}
43
+ response = post("customers",options)
44
+ if response['success']
45
+ return response['results']['customer']
46
+ else
47
+ return response
48
+ end
49
+ end
50
+
51
+ # Update a customer
52
+ #
53
+ # @option options [String]
54
+ # @example Return extended information for 12345
55
+ # Desk.update_customer(12345, :name => "Christopher Warren")
56
+ # @format :json
57
+ # @authenticated true
58
+ # @see http://dev.desk.com/docs/api/customers/update
59
+ def update_customer(id, *args)
60
+ options = args.last.is_a?(Hash) ? args.pop : {}
61
+ response = put("customers/#{id}",options)
62
+ if response['success']
63
+ return response['results']['customer']
64
+ else
65
+ return response
66
+ end
67
+ end
68
+
69
+ # Create a new customer email
70
+ #
71
+ # @option options [String]
72
+ # @example Return extended information for 12345
73
+ # Desk.create_customer_email(12345, "foo@example.com")
74
+ # @format :json
75
+ # @authenticated true
76
+ # @see http://dev.desk.com/docs/api/customers/emails/create
77
+ def create_customer_email(id, email, *args)
78
+ options = args.last.is_a?(Hash) ? args.pop : {}
79
+ options.merge!({:email => email})
80
+ response = post("customers/#{id}/emails",options)
81
+ if response['success']
82
+ return response['results']['email']
83
+ else
84
+ return response
85
+ end
86
+ end
87
+
88
+ # Update a customer's email
89
+ #
90
+ # @option options [String]
91
+ # @example Return extended information for 12345
92
+ # Desk.update_customer_email(12345, 12345, :email => "foo@example.com")
93
+ # Desk.update_customer_email(12345, 12345, :customer_contact_type => "work")
94
+ # @format :json
95
+ # @authenticated true
96
+ # @see http://dev.desk.com/docs/api/customers/emails/update
97
+ def update_customer_email(id, email_id, *args)
98
+ options = args.last.is_a?(Hash) ? args.pop : {}
99
+ response = put("customers/#{id}/emails/#{email_id}",options)
100
+ if response['success']
101
+ return response['results']['email']
102
+ else
103
+ return response
104
+ end
105
+ end
106
+
107
+ # Create a new customer phone number
108
+ #
109
+ # @option options [String]
110
+ # @example Return extended information for 12345
111
+ # Desk.create_customer_phone(12345, "555-368-7147")
112
+ # @format :json
113
+ # @authenticated true
114
+ # @see http://dev.desk.com/docs/api/customers/phones/create
115
+ def create_customer_phone(id, phone, *args)
116
+ options = args.last.is_a?(Hash) ? args.pop : {}
117
+ options.merge!({:phone => phone})
118
+ response = post("customers/#{id}/phones",options)
119
+ if response['success']
120
+ return response['results']['phone']
121
+ else
122
+ return response
123
+ end
124
+ end
125
+
126
+ # Update a customer's phone number
127
+ #
128
+ # @option options [String]
129
+ # @example Return extended information for 12345
130
+ # Desk.update_customer_phone(12345, 12345, :phone => "555-368-7147")
131
+ # Desk.update_customer_phone(12345, 12345, :customer_contact_type => "work")
132
+ # @format :json
133
+ # @authenticated true
134
+ # @see http://dev.desk.com/docs/api/customers/phones/update
135
+ def update_customer_phone(id, phone_id, *args)
136
+ options = args.last.is_a?(Hash) ? args.pop : {}
137
+ response = put("customers/#{id}/phones/#{phone_id}",options)
138
+ if response['success']
139
+ return response['results']['phone']
140
+ else
141
+ return response
142
+ end
143
+ end
144
+ end
145
+ end
146
+ end
@@ -0,0 +1,75 @@
1
+ module Desk
2
+ class Client
3
+ # Defines methods related to interactions
4
+ module Interaction
5
+
6
+ # Returns extended information of up to 100 interactions
7
+ #
8
+ # @option options [Boolean, String, Integer]
9
+ # @example Return extended information for 12345
10
+ # Desk.interactions(:since_id => 12345)
11
+ # Desk.interactions(:since_id => 12345, :count => 5)
12
+ # @format :json
13
+ # @authenticated true
14
+ # @see http://dev.desk.com/docs/api/interactions
15
+ def interactions(*args)
16
+ options = args.last.is_a?(Hash) ? args.pop : {}
17
+ response = get("interactions",options)
18
+ response
19
+ end
20
+
21
+ def create_interaction(*args)
22
+ options = args.last.is_a?(Hash) ? args.pop : {}
23
+ if options[:direction].to_s == "outbound"
24
+ options.delete(:direction)
25
+ to = options.delete(:customer_email)
26
+ subject = options.delete(:interaction_subject)
27
+ body = options.delete(:interaction_body)
28
+
29
+ create_outbound_interaction(to, subject, body, options)
30
+ else
31
+ create_inbound_interaction(options)
32
+ end
33
+ end
34
+
35
+ # Creates an interaction from a customer
36
+ #
37
+ # @format :json
38
+ # @authenticated true
39
+ # @rate_limited true
40
+ # @return [Array] The requested users.
41
+ # @see http://dev.desk.com/docs/api/interactions/create
42
+ # @example Create a new interaction
43
+ # Desk.create_interaction(:interaction_subject => "this is an api test", :customer_email => "foo@example.com")
44
+ def create_inbound_interaction(*args)
45
+ options = args.last.is_a?(Hash) ? args.pop : {}
46
+ response = post('interactions', options)
47
+ if response['success']
48
+ return response['results']
49
+ else
50
+ return response
51
+ end
52
+ end
53
+
54
+ # Create an interaction from an agent
55
+ #
56
+ # Desk's API doesn't support creating a new case/interaction initiated by an agent
57
+ # so we'll use send an email to the customer directly that is BCC'd to the support email address
58
+ # which will create the ticket
59
+ #
60
+ # @see http://support.desk.com/customer/portal/articles/4180
61
+ # @see http://support.desk.com/customer/portal/articles/6728
62
+ def create_outbound_interaction(to, subject, body, *args)
63
+ raise Desk::SupportEmailNotSet if support_email.blank?
64
+ options = args.last.is_a?(Hash) ? args.pop : {}
65
+ options.merge!(:to => to, :subject => subject, :body => body, :from => support_email, :bcc => support_email)
66
+ headers = { "x-assistly-customer-email" => to,
67
+ "x-assistly-interaction-direction" => "out",
68
+ "x-assistly-case-status" => options[:status]||"open"}
69
+ headers.merge!(options[:headers]) if options[:headers]
70
+ options.merge!(:headers => headers)
71
+ Pony.mail(options)
72
+ end
73
+ end
74
+ end
75
+ end