desk_api 0.1.3 → 0.5.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.
- checksums.yaml +4 -4
- data/.travis.yml +1 -2
- data/Gemfile +0 -6
- data/README.md +38 -31
- data/desk_api.gemspec +1 -4
- data/lib/desk_api.rb +5 -7
- data/lib/desk_api/client.rb +44 -57
- data/lib/desk_api/configuration.rb +89 -95
- data/lib/desk_api/default.rb +51 -55
- data/lib/desk_api/error.rb +42 -44
- data/lib/desk_api/rate_limit.rb +20 -22
- data/lib/desk_api/resource.rb +142 -65
- data/lib/desk_api/version.rb +1 -1
- data/spec/cassettes/DeskApi_Client/using_Basic_Authentication/_delete/deletes_a_resource.yml +22 -22
- data/spec/cassettes/DeskApi_Client/using_Basic_Authentication/_get/fetches_resources.yml +26 -26
- data/spec/cassettes/DeskApi_Client/using_Basic_Authentication/_patch/updates_a_resource.yml +30 -30
- data/spec/cassettes/DeskApi_Client/using_Basic_Authentication/_post/creates_a_resource.yml +27 -27
- data/spec/cassettes/DeskApi_Client/using_OAuth/_delete/deletes_a_resource.yml +22 -22
- data/spec/cassettes/DeskApi_Client/using_OAuth/_get/fetches_resources.yml +31 -286
- data/spec/cassettes/DeskApi_Client/using_OAuth/_patch/updates_a_resource.yml +35 -35
- data/spec/cassettes/DeskApi_Client/using_OAuth/_post/creates_a_resource.yml +28 -28
- data/spec/cassettes/DeskApi_Error/_from_response/can_be_created_from_a_faraday_response.yml +22 -22
- data/spec/cassettes/DeskApi_Error/_from_response/uses_the_body_message_if_present.yml +22 -22
- data/spec/cassettes/DeskApi_Error/on_validation_error/allows_access_to_error_hash.yml +22 -22
- data/spec/cassettes/DeskApi_Resource/_by_url/finds_resources_by_url.yml +37 -286
- data/spec/cassettes/DeskApi_Resource/_create/creates_a_new_topic.yml +27 -27
- data/spec/cassettes/DeskApi_Resource/_create/throws_an_error_creating_a_user.yml +54 -0
- data/spec/cassettes/DeskApi_Resource/_delete/deletes_a_resource.yml +83 -79
- data/spec/cassettes/DeskApi_Resource/_delete/throws_an_error_deleting_a_non_deletalbe_resource.yml +78 -28
- data/spec/cassettes/DeskApi_Resource/_exec_/can_be_forced_to_reload.yml +34 -285
- data/spec/cassettes/DeskApi_Resource/_exec_/loads_the_current_resource.yml +34 -285
- data/spec/cassettes/DeskApi_Resource/_find/has_an_alias_by_id.yml +54 -0
- data/spec/cassettes/DeskApi_Resource/_find/loads_the_requested_resource.yml +54 -0
- data/spec/cassettes/DeskApi_Resource/_get_linked_resource/returns_linked_resources.yml +113 -0
- data/spec/cassettes/DeskApi_Resource/_get_linked_resource/returns_nil_if_link_is_nil.yml +62 -0
- data/spec/cassettes/DeskApi_Resource/_get_linked_resource/saves_the_linked_resource_instead_of_the_url.yml +113 -0
- data/spec/cassettes/DeskApi_Resource/_method_missing/loads_the_resource_to_find_a_suitable_method.yml +34 -285
- data/spec/cassettes/DeskApi_Resource/_method_missing/raises_an_error_if_method_does_not_exist.yml +34 -285
- data/spec/cassettes/DeskApi_Resource/_page/keeps_the_resource_as_loaded.yml +113 -0
- data/spec/cassettes/DeskApi_Resource/_page/returns_the_current_page_and_loads_if_page_not_defined.yml +62 -0
- data/spec/cassettes/DeskApi_Resource/_page/sets_the_resource_to_not_loaded.yml +113 -0
- data/spec/cassettes/DeskApi_Resource/_search/allows_searching_on_search_enabled_resources.yml +23 -23
- data/spec/cassettes/DeskApi_Resource/_search/throws_an_error_if_search_is_not_enabled.yml +52 -0
- data/spec/cassettes/DeskApi_Resource/_update/can_update_without_a_hash.yml +178 -58
- data/spec/cassettes/DeskApi_Resource/_update/throws_an_error_updating_a_user.yml +86 -28
- data/spec/cassettes/DeskApi_Resource/_update/updates_a_topic.yml +61 -57
- data/spec/desk_api/client_spec.rb +9 -16
- data/spec/desk_api/resource_spec.rb +118 -43
- data/spec/spec_helper.rb +1 -1
- metadata +24 -102
- data/lib/desk_api/action/create.rb +0 -15
- data/lib/desk_api/action/delete.rb +0 -9
- data/lib/desk_api/action/embeddable.rb +0 -47
- data/lib/desk_api/action/field.rb +0 -33
- data/lib/desk_api/action/link.rb +0 -29
- data/lib/desk_api/action/resource.rb +0 -14
- data/lib/desk_api/action/search.rb +0 -15
- data/lib/desk_api/action/update.rb +0 -17
- data/lib/desk_api/error/method_not_supported.rb +0 -9
- data/lib/desk_api/error/not_embeddable.rb +0 -8
- data/lib/desk_api/error/not_updateable.rb +0 -10
- data/lib/desk_api/error/parse_error.rb +0 -9
- data/lib/desk_api/resource/article.rb +0 -10
- data/lib/desk_api/resource/article_translation.rb +0 -8
- data/lib/desk_api/resource/attachment.rb +0 -8
- data/lib/desk_api/resource/case.rb +0 -11
- data/lib/desk_api/resource/company.rb +0 -8
- data/lib/desk_api/resource/customer.rb +0 -9
- data/lib/desk_api/resource/filter.rb +0 -7
- data/lib/desk_api/resource/inbound_mailbox.rb +0 -7
- data/lib/desk_api/resource/integration_url.rb +0 -9
- data/lib/desk_api/resource/job.rb +0 -9
- data/lib/desk_api/resource/label.rb +0 -9
- data/lib/desk_api/resource/macro.rb +0 -9
- data/lib/desk_api/resource/macro_action.rb +0 -7
- data/lib/desk_api/resource/note.rb +0 -7
- data/lib/desk_api/resource/page.rb +0 -64
- data/lib/desk_api/resource/reply.rb +0 -10
- data/lib/desk_api/resource/topic.rb +0 -9
- data/lib/desk_api/resource/topic_translation.rb +0 -9
- data/lib/desk_api/resource/user_preference.rb +0 -7
- data/lib/desk_api/resources.json +0 -76
- data/spec/cassettes/DeskApi_Client/_delete/deletes_a_resource.yml +0 -48
- data/spec/cassettes/DeskApi_Client/_get/fetches_resources.yml +0 -55
- data/spec/cassettes/DeskApi_Client/_patch/updates_a_resource.yml +0 -62
- data/spec/cassettes/DeskApi_Client/_post/creates_a_resource.yml +0 -58
- data/spec/cassettes/DeskApi_Resource_Case/once_closed/can_not_be_updated.yml +0 -124
- data/spec/cassettes/DeskApi_Resource_Page/_find/has_an_alias_by_id.yml +0 -54
- data/spec/cassettes/DeskApi_Resource_Page/_find/loads_the_requested_resource.yml +0 -54
- data/spec/cassettes/DeskApi_Resource_Page/_page/keeps_the_resource_as_loaded.yml +0 -113
- data/spec/cassettes/DeskApi_Resource_Page/_page/returns_the_current_page_and_loads_if_page_not_defined.yml +0 -313
- data/spec/cassettes/DeskApi_Resource_Page/_page/sets_the_resource_to_not_loaded.yml +0 -113
- data/spec/desk_api/resource/case_spec.rb +0 -28
- data/spec/desk_api/resource/page_spec.rb +0 -68
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1b2abb909d8097d68812564cb94b981e61affb4e
|
4
|
+
data.tar.gz: 6b00d878ab5b3086a68beafcde591faa619cc9f4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 32423d4711f1b5850e3066b0fc67ec2e340933ce57f1e4b5420adefd4e2d08827c79562acf512dcffef3c0de399c8d920d03fa29c73643d0b54836ba6f140882
|
7
|
+
data.tar.gz: 4238fa03c918f4d3d8d2d5b550c7fcf28b246fcccd3600d4a1444fd93680010aaa426a142b50ac995a453fffc551120c2b08c2013545261dfb4d609721a0206e
|
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -29,7 +29,7 @@ response = client.patch '/api/v2/topics/1', name: 'Changed the Topic Name'
|
|
29
29
|
response = client.delete '/api/v2/topics/1'
|
30
30
|
```
|
31
31
|
|
32
|
-
For ease of use and if you only create one connection to the desk.com API you can use `
|
32
|
+
For ease of use and if you only create one connection to the desk.com API you can use `DeskApi` directly:
|
33
33
|
|
34
34
|
```ruby
|
35
35
|
DeskApi.configure do |config|
|
@@ -46,39 +46,45 @@ DeskApi.delete '/api/v2/topics/1'
|
|
46
46
|
|
47
47
|
## Working with Resources and Collections
|
48
48
|
|
49
|
-
The API supports RESTful resources and so does this wrapper.
|
49
|
+
The API supports RESTful resources and so does this wrapper. These resources are automatically discovered, meaning you can navigate around without having to worry about anything. We also support two finder methods `by_url` and `find`.
|
50
50
|
|
51
51
|
### Finders
|
52
|
-
```ruby
|
53
|
-
# get the first user and find a case by url.
|
54
|
-
found_case = DeskApi.users.first.by_url '/api/v2/cases/1'
|
55
52
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
53
|
+
The method `by_url` can be called on all `DeskApi::Resource` instances and will return a lazy loaded instance of the resource. Since the update to v0.5 of the API wrapper the `find` method can now be called on all `DeskApi::Resource` instances too. _Gotcha:_ It will rebuild the base path based on the resource/collection it is called on. So if you call it on the cases collection `DeskApi.cases.find 1` the path will look like this: `/api/v2/cases/:id`.
|
54
|
+
|
55
|
+
| Method | Path |
|
56
|
+
| ----------------------------------------------------------- | --------------------------- |
|
57
|
+
| `DeskApi.cases.find(1)` | `/api/v2/cases/1` |
|
58
|
+
| `DeskApi.cases.entries.find(1)` | `/api/v2/cases/1` |
|
59
|
+
| `DeskApi.cases.search(subject: 'Test').find(1)` | `/api/v2/cases/1` |
|
60
|
+
| `DeskApi.cases.search(subject: 'Test').entries.find(1)` | `/api/v2/cases/1` |
|
61
|
+
| `DeskApi.cases.entries.first.replies.find(1)` | `/api/v2/cases/1/replies/1` |
|
62
|
+
| `DeskApi.cases.entries.first.replies.entries.first.find(1)` | `/api/v2/cases/1/replies/1` |
|
61
63
|
|
62
64
|
### Pagination
|
63
65
|
|
64
|
-
As mentioned above you can also navigate between resources and
|
66
|
+
As mentioned above you can also navigate between resources and pages of collections. _Please Notice:_ `DeskApi.cases` doesn't behave like an `Array` any longer, it's just a `DeskApi::Resource` so links like `next`, `previous`, `first` and `last` can be called directly. However you'll have to request the `entries` before you can loop through all the records on the page.
|
65
67
|
|
66
68
|
```ruby
|
67
69
|
cases = DeskApi.cases
|
68
|
-
cases.each do |my_case|
|
70
|
+
cases.entries.each do |my_case|
|
69
71
|
# do something with the case
|
70
72
|
end
|
73
|
+
|
71
74
|
# now move on to the next page
|
72
|
-
next_page = cases.
|
73
|
-
next_page.each do |my_case|
|
75
|
+
next_page = cases.next
|
76
|
+
next_page.entries.each do |my_case|
|
74
77
|
# do something with the case
|
75
78
|
end
|
79
|
+
|
76
80
|
# go back to the previous page
|
77
|
-
previous_page = next_page.
|
81
|
+
previous_page = next_page.previous
|
82
|
+
|
78
83
|
# or go to the last page
|
79
|
-
last_page = previous_page.
|
84
|
+
last_page = previous_page.last
|
85
|
+
|
80
86
|
# or go to the first page
|
81
|
-
first_page = last_page.
|
87
|
+
first_page = last_page.first
|
82
88
|
```
|
83
89
|
|
84
90
|
### Links
|
@@ -87,22 +93,23 @@ Pagination is pretty obvious but the cool part about pagination or rather resour
|
|
87
93
|
|
88
94
|
```ruby
|
89
95
|
# get the customer of the first case of the first page
|
90
|
-
customer = DeskApi.cases.first.customer
|
96
|
+
customer = DeskApi.cases.entries.first.customer
|
97
|
+
|
91
98
|
# who sent the first outbound reply of the first email
|
92
|
-
user_name = DeskApi.cases.select{ |my_case|
|
99
|
+
user_name = DeskApi.cases.entries.select{ |my_case|
|
93
100
|
my_case.type == 'email'
|
94
|
-
}.first.replies.select{ |reply|
|
101
|
+
}.first.replies.entries.select{ |reply|
|
95
102
|
reply.direction == 'out'
|
96
103
|
}.first.sent_by.name
|
97
104
|
```
|
98
105
|
|
99
106
|
### Lazy loading
|
100
107
|
|
101
|
-
Collections and resources in general are lazily loaded, meaning if you request the cases `DeskApi.cases` no actual request will be set off until you actually request data.
|
108
|
+
Collections and resources in general are lazily loaded, meaning if you request the cases `DeskApi.cases` no actual request will be set off until you actually request data. Meaning only necessary requests are sent which will keep the request count low - [desk.com rate limit](http://dev.desk.com/API/using-the-api/#rate-limits).
|
102
109
|
|
103
110
|
```ruby
|
104
|
-
DeskApi.cases.page(10).per_page(50).each do |my_case|
|
105
|
-
# in this method chain `.
|
111
|
+
DeskApi.cases.page(10).per_page(50).entries.each do |my_case|
|
112
|
+
# in this method chain `.entries' is the first method that acutally sends a request
|
106
113
|
end
|
107
114
|
|
108
115
|
# however if you request the current page numer and the resource is not loaded
|
@@ -112,7 +119,7 @@ DeskApi.cases.page == 1
|
|
112
119
|
|
113
120
|
### Side loading
|
114
121
|
|
115
|
-
APIv2 has a lot of great new features but the one I'm most excited about is side loading or embedding resources. You basically request one resource and tell the API to embed sub resources, eg. you need cases but also want to have the `assigned_user` - instead of requesting all cases and the `assigned_user` for each of those cases (30 cases = 31 API requests) you can now embed `assigned_user` into your cases list view (1 API
|
122
|
+
APIv2 has a lot of great new features but the one I'm most excited about is side loading or embedding resources. You basically request one resource and tell the API to embed sub resources, eg. you need cases but also want to have the `assigned_user` - instead of requesting all cases and the `assigned_user` for each of those cases (30 cases = 31 API requests) you can now embed `assigned_user` into your cases list view (1 API request).
|
116
123
|
|
117
124
|
Of course we had to bring this awesomeness into the API wrapper as soon as possible, so here you go:
|
118
125
|
|
@@ -124,7 +131,7 @@ customer = cases.first.customer
|
|
124
131
|
# you can use this feature in finders too
|
125
132
|
my_case = DeskApi.cases.find(1, embed: :customer)
|
126
133
|
# OR
|
127
|
-
my_case = DeskApi.cases.find(1, embed: [:customer, :assigned_user, :assigned_group])
|
134
|
+
my_case = DeskApi.cases.find(1, embed: [:customer, :assigned_user, :assigned_group, :message])
|
128
135
|
|
129
136
|
customer = my_case.customer
|
130
137
|
assigned_user = my_case.assigned_user
|
@@ -133,7 +140,7 @@ assigned_group = my_case.assigned_group
|
|
133
140
|
|
134
141
|
### Create, Update and Delete
|
135
142
|
|
136
|
-
Of course we support creating, updating and deleting resources but not all resources can be deleted or updated or created, if that's the case for the resource you're trying to update, it'll throw a `DeskApi::Error::
|
143
|
+
Of course we support creating, updating and deleting resources but not all resources can be deleted or updated or created, if that's the case for the resource you're trying to update, it'll throw a `DeskApi::Error::MethodNotAllowed` error. For ease of use and because we wanted to build as less business logic into the wrapper as possible, all of the methods are defined on each `DeskApi::Resource` and will be sent to desk.com. However the API might respond with an error if you do things that aren't supported.
|
137
144
|
|
138
145
|
```ruby
|
139
146
|
# let's create an article
|
@@ -159,17 +166,17 @@ end
|
|
159
166
|
# ATTENTION: Cases can not be deleted!
|
160
167
|
begin
|
161
168
|
DeskApi.cases.first.delete
|
162
|
-
rescue DeskApi::Error::
|
169
|
+
rescue DeskApi::Error::MethodNotAllowed => e
|
163
170
|
# too bad
|
164
171
|
end
|
165
172
|
```
|
166
173
|
|
167
174
|
### Getters & Setters
|
168
175
|
|
169
|
-
As you have seen in prior examples for each field on the resource we create a getter and setter.
|
176
|
+
As you have seen in prior examples for each field on the resource we create a getter and setter.
|
170
177
|
|
171
178
|
```ruby
|
172
|
-
customer = DeskApi.customers.
|
179
|
+
customer = DeskApi.customers.find(1)
|
173
180
|
|
174
181
|
puts customer.first_name
|
175
182
|
puts customer.last_name
|
@@ -185,8 +192,8 @@ updated_customer = customer.update title: 'Master of the Universe'
|
|
185
192
|
# users are not updatable
|
186
193
|
begin
|
187
194
|
user = DeskApi.users.first
|
188
|
-
user.name
|
189
|
-
rescue DeskApi::Error::
|
195
|
+
user.update name: 'Not updateable'
|
196
|
+
rescue DeskApi::Error::MethodNotAllowed
|
190
197
|
# too bad
|
191
198
|
end
|
192
199
|
```
|
data/desk_api.gemspec
CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |gem|
|
|
10
10
|
gem.version = DeskApi::VERSION
|
11
11
|
|
12
12
|
gem.authors = ['Thomas Stachl']
|
13
|
-
gem.email = '
|
13
|
+
gem.email = 'thomas@desk.com'
|
14
14
|
|
15
15
|
gem.require_paths = ['lib']
|
16
16
|
gem.files = `git ls-files`.split("\n")
|
@@ -20,13 +20,10 @@ Gem::Specification.new do |gem|
|
|
20
20
|
gem.extra_rdoc_files = ['README.md']
|
21
21
|
gem.rdoc_options = ['--line-numbers', '--inline--source', '--title', 'desk.rb']
|
22
22
|
|
23
|
-
gem.add_runtime_dependency('multi_json')
|
24
23
|
gem.add_runtime_dependency('faraday')
|
25
24
|
gem.add_runtime_dependency('faraday_middleware')
|
26
25
|
gem.add_runtime_dependency('simple_oauth')
|
27
|
-
gem.add_runtime_dependency('typhoeus')
|
28
26
|
gem.add_runtime_dependency('addressable')
|
29
|
-
gem.add_runtime_dependency('activesupport')
|
30
27
|
gem.add_runtime_dependency('hashie')
|
31
28
|
|
32
29
|
gem.add_development_dependency('rspec')
|
data/lib/desk_api.rb
CHANGED
@@ -3,14 +3,11 @@ require 'json'
|
|
3
3
|
require 'forwardable'
|
4
4
|
require 'addressable/uri'
|
5
5
|
|
6
|
-
require 'hashie/mash'
|
7
|
-
require 'active_support/inflector'
|
8
|
-
|
9
|
-
require 'desk_api/configuration'
|
10
|
-
require 'desk_api/client'
|
11
|
-
require 'desk_api/version'
|
12
|
-
|
13
6
|
module DeskApi
|
7
|
+
require 'desk_api/version'
|
8
|
+
require 'desk_api/configuration'
|
9
|
+
require 'desk_api/client'
|
10
|
+
|
14
11
|
class << self
|
15
12
|
include DeskApi::Configuration
|
16
13
|
|
@@ -31,5 +28,6 @@ module DeskApi
|
|
31
28
|
client.respond_to?(method_name, include_private)
|
32
29
|
end
|
33
30
|
end
|
31
|
+
|
34
32
|
setup
|
35
33
|
end
|
data/lib/desk_api/client.rb
CHANGED
@@ -1,67 +1,54 @@
|
|
1
|
-
require 'faraday'
|
2
|
-
|
3
|
-
require 'desk_api/configuration'
|
4
|
-
require 'desk_api/action/link'
|
5
|
-
require 'desk_api/action/resource'
|
6
|
-
require 'desk_api/error/client_error'
|
7
|
-
require 'desk_api/error/parse_error'
|
8
|
-
|
9
1
|
require 'desk_api/resource'
|
2
|
+
require 'desk_api/error/parser_error'
|
3
|
+
|
4
|
+
class DeskApi::Client
|
5
|
+
include DeskApi::Configuration
|
6
|
+
|
7
|
+
# Initializes a new Client object
|
8
|
+
#
|
9
|
+
# @param options [Hash]
|
10
|
+
# @return [DeskApi::Client]
|
11
|
+
def initialize(options = {})
|
12
|
+
DeskApi::Configuration.keys.each do |key|
|
13
|
+
instance_variable_set(:"@#{key}", options[key] || DeskApi.instance_variable_get(:"@#{key}"))
|
14
|
+
end
|
15
|
+
end
|
10
16
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
include DeskApi::Action::Resource
|
16
|
-
|
17
|
-
# Initializes a new Client object
|
18
|
-
#
|
19
|
-
# @param options [Hash]
|
20
|
-
# @return [DeskApi::Client]
|
21
|
-
def initialize(options = {})
|
22
|
-
DeskApi::Configuration.keys.each do |key|
|
23
|
-
instance_variable_set(:"@#{key}", options[key] || DeskApi.instance_variable_get(:"@#{key}"))
|
24
|
-
end
|
17
|
+
# Perform an HTTP DELETE request
|
18
|
+
def delete(path, params = {})
|
19
|
+
request(:delete, path, params)
|
20
|
+
end
|
25
21
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
setup_links(definition._links)
|
31
|
-
end
|
22
|
+
# Perform an HTTP GET request
|
23
|
+
def get(path, params = {})
|
24
|
+
request(:get, path, params)
|
25
|
+
end
|
32
26
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
27
|
+
# Perform an HTTP POST request
|
28
|
+
def post(path, params = {})
|
29
|
+
request(:post, path, params)
|
30
|
+
end
|
37
31
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
32
|
+
# Perform an HTTP PATCH request
|
33
|
+
def patch(path, params = {})
|
34
|
+
request(:patch, path, params)
|
35
|
+
end
|
42
36
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
37
|
+
private
|
38
|
+
# If the method is missing create a resource
|
39
|
+
def method_missing(method, *args, &block)
|
40
|
+
DeskApi::Resource.new(self, DeskApi::Resource.build_self_link("/api/v2/#{method}"))
|
41
|
+
end
|
47
42
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
def request(method, path, params = {})
|
56
|
-
connection.send(method, path, params)
|
57
|
-
rescue Faraday::Error::ClientError
|
58
|
-
raise DeskApi::Error::ClientError
|
59
|
-
rescue JSON::ParserError
|
60
|
-
raise DeskApi::Error::ParserError
|
61
|
-
end
|
43
|
+
def request(method, path, params = {})
|
44
|
+
connection.send(method, path, params)
|
45
|
+
rescue Faraday::Error::ClientError
|
46
|
+
raise DeskApi::Error::ClientError
|
47
|
+
rescue JSON::ParserError
|
48
|
+
raise DeskApi::Error::ParserError
|
49
|
+
end
|
62
50
|
|
63
|
-
|
64
|
-
|
65
|
-
end
|
51
|
+
def connection
|
52
|
+
@connection ||= Faraday.new endpoint, connection_options, &middleware
|
66
53
|
end
|
67
54
|
end
|
@@ -1,9 +1,6 @@
|
|
1
1
|
require 'faraday'
|
2
2
|
require 'faraday_middleware'
|
3
3
|
|
4
|
-
require 'typhoeus'
|
5
|
-
require 'typhoeus/adapters/faraday'
|
6
|
-
|
7
4
|
require 'desk_api/default'
|
8
5
|
require 'desk_api/request/retry'
|
9
6
|
require 'desk_api/response/raise_error'
|
@@ -11,122 +8,119 @@ require 'desk_api/error/configuration_error'
|
|
11
8
|
require 'desk_api/error/client_error'
|
12
9
|
require 'desk_api/error/server_error'
|
13
10
|
|
14
|
-
module DeskApi
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
]
|
34
|
-
end
|
11
|
+
module DeskApi::Configuration
|
12
|
+
extend Forwardable
|
13
|
+
attr_writer :consumer_secret, :token, :token_secret, :password
|
14
|
+
attr_accessor :consumer_key, :username, :endpoint, :subdomain, :connection_options, :middleware
|
15
|
+
def_delegator :options, :hash
|
16
|
+
|
17
|
+
class << self
|
18
|
+
def keys
|
19
|
+
@keys ||= [
|
20
|
+
:consumer_key,
|
21
|
+
:consumer_secret,
|
22
|
+
:token,
|
23
|
+
:token_secret,
|
24
|
+
:username,
|
25
|
+
:password,
|
26
|
+
:subdomain,
|
27
|
+
:endpoint,
|
28
|
+
:connection_options
|
29
|
+
]
|
35
30
|
end
|
31
|
+
end
|
36
32
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
33
|
+
# if subdomain is set make sure endpoint is correct
|
34
|
+
def endpoint
|
35
|
+
@endpoint ||= "https://#{@subdomain}.desk.com"
|
36
|
+
end
|
41
37
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
38
|
+
def middleware
|
39
|
+
@middleware ||= Proc.new do |builder|
|
40
|
+
builder.request :json
|
41
|
+
builder.request :basic_auth, @username, @password if basic_auth.values.all?
|
42
|
+
builder.request :oauth, oauth if oauth.values.all?
|
43
|
+
builder.request :retry
|
48
44
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
builder.response :json, content_type: /application\/json/
|
45
|
+
builder.response :dates
|
46
|
+
builder.response :raise_error, DeskApi::Error::ClientError
|
47
|
+
builder.response :raise_error, DeskApi::Error::ServerError
|
48
|
+
builder.response :json, content_type: /application\/json/
|
54
49
|
|
55
|
-
|
56
|
-
end
|
50
|
+
builder.adapter Faraday.default_adapter
|
57
51
|
end
|
52
|
+
end
|
58
53
|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
54
|
+
def configure
|
55
|
+
yield self
|
56
|
+
validate_credentials!
|
57
|
+
validate_endpoint!
|
58
|
+
self
|
59
|
+
end
|
65
60
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
end
|
70
|
-
self
|
61
|
+
def reset!
|
62
|
+
DeskApi::Configuration.keys.each do |key|
|
63
|
+
send("#{key}=", DeskApi::Default.options[key])
|
71
64
|
end
|
72
|
-
|
65
|
+
self
|
66
|
+
end
|
67
|
+
alias setup reset!
|
73
68
|
|
74
|
-
|
75
|
-
|
76
|
-
|
69
|
+
def credentials?
|
70
|
+
oauth.values.all? || basic_auth.values.all?
|
71
|
+
end
|
77
72
|
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
73
|
+
private
|
74
|
+
# @return [Hash]
|
75
|
+
def options
|
76
|
+
Hash[DeskApi::Configuration.keys.map{|key| [key, instance_variable_get(:"@#{key}")]}]
|
77
|
+
end
|
83
78
|
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
79
|
+
def oauth
|
80
|
+
{
|
81
|
+
consumer_key: @consumer_key,
|
82
|
+
consumer_secret: @consumer_secret,
|
83
|
+
token: @token,
|
84
|
+
token_secret: @token_secret
|
85
|
+
}
|
86
|
+
end
|
92
87
|
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
88
|
+
def basic_auth
|
89
|
+
{
|
90
|
+
username: @username,
|
91
|
+
password: @password
|
92
|
+
}
|
93
|
+
end
|
99
94
|
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
95
|
+
def validate_credentials!
|
96
|
+
unless credentials?
|
97
|
+
raise(DeskApi::Error::ConfigurationError, "Invalid credentials: Either username/password or OAuth credentials must be specified.")
|
98
|
+
end
|
104
99
|
|
105
|
-
|
106
|
-
|
107
|
-
|
100
|
+
if oauth.values.all?
|
101
|
+
oauth.each do |credential, value|
|
102
|
+
next if value.nil?
|
108
103
|
|
109
|
-
|
110
|
-
|
111
|
-
end
|
104
|
+
unless value.is_a?(String) || value.is_a?(Symbol)
|
105
|
+
raise(DeskApi::Error::ConfigurationError, "Invalid #{credential} specified: #{value} must be a string or symbol.")
|
112
106
|
end
|
113
107
|
end
|
108
|
+
end
|
114
109
|
|
115
|
-
|
116
|
-
|
117
|
-
|
110
|
+
if basic_auth.values.all?
|
111
|
+
basic_auth.each do |credential, value|
|
112
|
+
next if value.nil?
|
118
113
|
|
119
|
-
|
120
|
-
|
121
|
-
end
|
114
|
+
unless value.is_a?(String) || value.is_a?(Symbol)
|
115
|
+
raise(DeskApi::Error::ConfigurationError, "Invalid #{credential} specified: #{value} must be a string or symbol.")
|
122
116
|
end
|
123
117
|
end
|
124
118
|
end
|
119
|
+
end
|
125
120
|
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
end
|
121
|
+
def validate_endpoint!
|
122
|
+
unless endpoint =~ /^#{URI::regexp}$/
|
123
|
+
raise(DeskApi::Error::ConfigurationError, "Invalid endpoint specified: `#{endpoint}` must be a valid url.")
|
130
124
|
end
|
131
125
|
end
|
132
126
|
end
|