desk_api 0.1.3 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (94) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +1 -2
  3. data/Gemfile +0 -6
  4. data/README.md +38 -31
  5. data/desk_api.gemspec +1 -4
  6. data/lib/desk_api.rb +5 -7
  7. data/lib/desk_api/client.rb +44 -57
  8. data/lib/desk_api/configuration.rb +89 -95
  9. data/lib/desk_api/default.rb +51 -55
  10. data/lib/desk_api/error.rb +42 -44
  11. data/lib/desk_api/rate_limit.rb +20 -22
  12. data/lib/desk_api/resource.rb +142 -65
  13. data/lib/desk_api/version.rb +1 -1
  14. data/spec/cassettes/DeskApi_Client/using_Basic_Authentication/_delete/deletes_a_resource.yml +22 -22
  15. data/spec/cassettes/DeskApi_Client/using_Basic_Authentication/_get/fetches_resources.yml +26 -26
  16. data/spec/cassettes/DeskApi_Client/using_Basic_Authentication/_patch/updates_a_resource.yml +30 -30
  17. data/spec/cassettes/DeskApi_Client/using_Basic_Authentication/_post/creates_a_resource.yml +27 -27
  18. data/spec/cassettes/DeskApi_Client/using_OAuth/_delete/deletes_a_resource.yml +22 -22
  19. data/spec/cassettes/DeskApi_Client/using_OAuth/_get/fetches_resources.yml +31 -286
  20. data/spec/cassettes/DeskApi_Client/using_OAuth/_patch/updates_a_resource.yml +35 -35
  21. data/spec/cassettes/DeskApi_Client/using_OAuth/_post/creates_a_resource.yml +28 -28
  22. data/spec/cassettes/DeskApi_Error/_from_response/can_be_created_from_a_faraday_response.yml +22 -22
  23. data/spec/cassettes/DeskApi_Error/_from_response/uses_the_body_message_if_present.yml +22 -22
  24. data/spec/cassettes/DeskApi_Error/on_validation_error/allows_access_to_error_hash.yml +22 -22
  25. data/spec/cassettes/DeskApi_Resource/_by_url/finds_resources_by_url.yml +37 -286
  26. data/spec/cassettes/DeskApi_Resource/_create/creates_a_new_topic.yml +27 -27
  27. data/spec/cassettes/DeskApi_Resource/_create/throws_an_error_creating_a_user.yml +54 -0
  28. data/spec/cassettes/DeskApi_Resource/_delete/deletes_a_resource.yml +83 -79
  29. data/spec/cassettes/DeskApi_Resource/_delete/throws_an_error_deleting_a_non_deletalbe_resource.yml +78 -28
  30. data/spec/cassettes/DeskApi_Resource/_exec_/can_be_forced_to_reload.yml +34 -285
  31. data/spec/cassettes/DeskApi_Resource/_exec_/loads_the_current_resource.yml +34 -285
  32. data/spec/cassettes/DeskApi_Resource/_find/has_an_alias_by_id.yml +54 -0
  33. data/spec/cassettes/DeskApi_Resource/_find/loads_the_requested_resource.yml +54 -0
  34. data/spec/cassettes/DeskApi_Resource/_get_linked_resource/returns_linked_resources.yml +113 -0
  35. data/spec/cassettes/DeskApi_Resource/_get_linked_resource/returns_nil_if_link_is_nil.yml +62 -0
  36. data/spec/cassettes/DeskApi_Resource/_get_linked_resource/saves_the_linked_resource_instead_of_the_url.yml +113 -0
  37. data/spec/cassettes/DeskApi_Resource/_method_missing/loads_the_resource_to_find_a_suitable_method.yml +34 -285
  38. data/spec/cassettes/DeskApi_Resource/_method_missing/raises_an_error_if_method_does_not_exist.yml +34 -285
  39. data/spec/cassettes/DeskApi_Resource/_page/keeps_the_resource_as_loaded.yml +113 -0
  40. data/spec/cassettes/DeskApi_Resource/_page/returns_the_current_page_and_loads_if_page_not_defined.yml +62 -0
  41. data/spec/cassettes/DeskApi_Resource/_page/sets_the_resource_to_not_loaded.yml +113 -0
  42. data/spec/cassettes/DeskApi_Resource/_search/allows_searching_on_search_enabled_resources.yml +23 -23
  43. data/spec/cassettes/DeskApi_Resource/_search/throws_an_error_if_search_is_not_enabled.yml +52 -0
  44. data/spec/cassettes/DeskApi_Resource/_update/can_update_without_a_hash.yml +178 -58
  45. data/spec/cassettes/DeskApi_Resource/_update/throws_an_error_updating_a_user.yml +86 -28
  46. data/spec/cassettes/DeskApi_Resource/_update/updates_a_topic.yml +61 -57
  47. data/spec/desk_api/client_spec.rb +9 -16
  48. data/spec/desk_api/resource_spec.rb +118 -43
  49. data/spec/spec_helper.rb +1 -1
  50. metadata +24 -102
  51. data/lib/desk_api/action/create.rb +0 -15
  52. data/lib/desk_api/action/delete.rb +0 -9
  53. data/lib/desk_api/action/embeddable.rb +0 -47
  54. data/lib/desk_api/action/field.rb +0 -33
  55. data/lib/desk_api/action/link.rb +0 -29
  56. data/lib/desk_api/action/resource.rb +0 -14
  57. data/lib/desk_api/action/search.rb +0 -15
  58. data/lib/desk_api/action/update.rb +0 -17
  59. data/lib/desk_api/error/method_not_supported.rb +0 -9
  60. data/lib/desk_api/error/not_embeddable.rb +0 -8
  61. data/lib/desk_api/error/not_updateable.rb +0 -10
  62. data/lib/desk_api/error/parse_error.rb +0 -9
  63. data/lib/desk_api/resource/article.rb +0 -10
  64. data/lib/desk_api/resource/article_translation.rb +0 -8
  65. data/lib/desk_api/resource/attachment.rb +0 -8
  66. data/lib/desk_api/resource/case.rb +0 -11
  67. data/lib/desk_api/resource/company.rb +0 -8
  68. data/lib/desk_api/resource/customer.rb +0 -9
  69. data/lib/desk_api/resource/filter.rb +0 -7
  70. data/lib/desk_api/resource/inbound_mailbox.rb +0 -7
  71. data/lib/desk_api/resource/integration_url.rb +0 -9
  72. data/lib/desk_api/resource/job.rb +0 -9
  73. data/lib/desk_api/resource/label.rb +0 -9
  74. data/lib/desk_api/resource/macro.rb +0 -9
  75. data/lib/desk_api/resource/macro_action.rb +0 -7
  76. data/lib/desk_api/resource/note.rb +0 -7
  77. data/lib/desk_api/resource/page.rb +0 -64
  78. data/lib/desk_api/resource/reply.rb +0 -10
  79. data/lib/desk_api/resource/topic.rb +0 -9
  80. data/lib/desk_api/resource/topic_translation.rb +0 -9
  81. data/lib/desk_api/resource/user_preference.rb +0 -7
  82. data/lib/desk_api/resources.json +0 -76
  83. data/spec/cassettes/DeskApi_Client/_delete/deletes_a_resource.yml +0 -48
  84. data/spec/cassettes/DeskApi_Client/_get/fetches_resources.yml +0 -55
  85. data/spec/cassettes/DeskApi_Client/_patch/updates_a_resource.yml +0 -62
  86. data/spec/cassettes/DeskApi_Client/_post/creates_a_resource.yml +0 -58
  87. data/spec/cassettes/DeskApi_Resource_Case/once_closed/can_not_be_updated.yml +0 -124
  88. data/spec/cassettes/DeskApi_Resource_Page/_find/has_an_alias_by_id.yml +0 -54
  89. data/spec/cassettes/DeskApi_Resource_Page/_find/loads_the_requested_resource.yml +0 -54
  90. data/spec/cassettes/DeskApi_Resource_Page/_page/keeps_the_resource_as_loaded.yml +0 -113
  91. data/spec/cassettes/DeskApi_Resource_Page/_page/returns_the_current_page_and_loads_if_page_not_defined.yml +0 -313
  92. data/spec/cassettes/DeskApi_Resource_Page/_page/sets_the_resource_to_not_loaded.yml +0 -113
  93. data/spec/desk_api/resource/case_spec.rb +0 -28
  94. 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: e26e977b6cb1ed65a5dee9ccd93a3f4617fb71e0
4
- data.tar.gz: 46a9845c1399f0a4644cde8f614c9ea38bcfc1e1
3
+ metadata.gz: 1b2abb909d8097d68812564cb94b981e61affb4e
4
+ data.tar.gz: 6b00d878ab5b3086a68beafcde591faa619cc9f4
5
5
  SHA512:
6
- metadata.gz: 3c2e40fa8dbff7ac13c88e43233c155123efe5d4cd4c87c173e6b4428f725d76e4da01d49481c9d7ddfc6039f2aa25ad328da34d8c3ba19a664bed74c35309d5
7
- data.tar.gz: 146cccebb60d61ce7c779a9fe35fe0f26284922270ffe97efbbe4f6c927db15d1bd54fc00844187a8d7ebfad4a0647f6c44ca068c93899c736899868a9a6fa50
6
+ metadata.gz: 32423d4711f1b5850e3066b0fc67ec2e340933ce57f1e4b5420adefd4e2d08827c79562acf512dcffef3c0de399c8d920d03fa29c73643d0b54836ba6f140882
7
+ data.tar.gz: 4238fa03c918f4d3d8d2d5b550c7fcf28b246fcccd3600d4a1444fd93680010aaa426a142b50ac995a453fffc551120c2b08c2013545261dfb4d609721a0206e
@@ -1,5 +1,4 @@
1
1
  rvm:
2
2
  - 1.9.3
3
3
  - 2.0.0
4
- - jruby-19mode # JRuby in 1.9 mode
5
- - rbx-19mode
4
+ - jruby-19mode # JRuby in 1.9 mode
data/Gemfile CHANGED
@@ -4,12 +4,6 @@ group :development, :test do |variable|
4
4
  # Testing infrastructure
5
5
  gem 'guard'
6
6
  gem 'guard-rspec'
7
-
8
- if RUBY_PLATFORM =~ /darwin/
9
- # OS X integration
10
- gem 'ruby_gntp'
11
- gem 'rb-fsevent'
12
- end
13
7
  end
14
8
 
15
9
  gemspec
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 `Desk` directly:
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. Those resources are automatically discovered, meaning you can navigate around without having to worry about anything. We also support two finder methods `by_url` and `by_id`, the former works with any resource, the latter needs a collection.
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
- # find a case by case number
57
- found_case = DeskApi.cases.find 1
58
- # the old #by_id still works
59
- found_case = DeskApi.cases.by_id 1
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 mainly pages of collections.
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.next_page
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.previous_page
81
+ previous_page = next_page.previous
82
+
78
83
  # or go to the last page
79
- last_page = previous_page.last_page
84
+ last_page = previous_page.last
85
+
80
86
  # or go to the first page
81
- first_page = last_page.first_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. This makes sure only necessary requests are fired and which keeps the overall requests low and spares the [desk.com rate limit](http://dev.desk.com/API/using-the-api/#rate-limits).
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 `.each' is the first method that acutally sends a request
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 REQUEST!!!!).
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::MethodNotSupported` error. The specific method won't be defined on the resource either `DeskApi.cases.first.respond_to?(:delete) == false`.
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::MethodNotSupported => e
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. Be careful if a resource is not updatable it won't have a setter specified.
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.by_id(1)
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 = 'Not updateable'
189
- rescue DeskApi::Error::MethodNotSupported
195
+ user.update name: 'Not updateable'
196
+ rescue DeskApi::Error::MethodNotAllowed
190
197
  # too bad
191
198
  end
192
199
  ```
@@ -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 = 'tom@desk.com'
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')
@@ -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
@@ -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
- module DeskApi
12
- class Client
13
- include DeskApi::Configuration
14
- include DeskApi::Action::Link
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
- # load the initial resources (should actually be a call to /api/v2 but not yet)
27
- resources = File.open(File.expand_path('resources.json', File.dirname(__FILE__))).read
28
- definition = Hashie::Mash.new(JSON.parse(resources))
29
- # on the client we only have links
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
- # Perform an HTTP DELETE request
34
- def delete(path, params = {})
35
- request(:delete, path, params)
36
- end
27
+ # Perform an HTTP POST request
28
+ def post(path, params = {})
29
+ request(:post, path, params)
30
+ end
37
31
 
38
- # Perform an HTTP GET request
39
- def get(path, params = {})
40
- request(:get, path, params)
41
- end
32
+ # Perform an HTTP PATCH request
33
+ def patch(path, params = {})
34
+ request(:patch, path, params)
35
+ end
42
36
 
43
- # Perform an HTTP POST request
44
- def post(path, params = {})
45
- request(:post, path, params)
46
- end
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
- # Perform an HTTP PATCH request
49
- def patch(path, params = {})
50
- request(:patch, path, params)
51
- end
52
-
53
- private
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
- def connection
64
- @connection ||= Faraday.new endpoint, connection_options, &middleware
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
- module Configuration
16
- extend Forwardable
17
- attr_writer :consumer_secret, :token, :token_secret, :password
18
- attr_accessor :consumer_key, :username, :endpoint, :subdomain, :connection_options, :middleware
19
- def_delegator :options, :hash
20
-
21
- class << self
22
- def keys
23
- @keys ||= [
24
- :consumer_key,
25
- :consumer_secret,
26
- :token,
27
- :token_secret,
28
- :username,
29
- :password,
30
- :subdomain,
31
- :endpoint,
32
- :connection_options
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
- # if subdomain is set make sure endpoint is correct
38
- def endpoint
39
- @endpoint ||= "https://#{@subdomain}.desk.com"
40
- end
33
+ # if subdomain is set make sure endpoint is correct
34
+ def endpoint
35
+ @endpoint ||= "https://#{@subdomain}.desk.com"
36
+ end
41
37
 
42
- def middleware
43
- @middleware ||= Proc.new do |builder|
44
- builder.request :json
45
- builder.request :basic_auth, @username, @password if basic_auth.values.all?
46
- builder.request :oauth, oauth if oauth.values.all?
47
- builder.request :retry
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
- builder.response :mashify
50
- builder.response :dates
51
- builder.response :raise_error, DeskApi::Error::ClientError
52
- builder.response :raise_error, DeskApi::Error::ServerError
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
- builder.adapter :typhoeus
56
- end
50
+ builder.adapter Faraday.default_adapter
57
51
  end
52
+ end
58
53
 
59
- def configure
60
- yield self
61
- validate_credentials!
62
- validate_endpoint!
63
- self
64
- end
54
+ def configure
55
+ yield self
56
+ validate_credentials!
57
+ validate_endpoint!
58
+ self
59
+ end
65
60
 
66
- def reset!
67
- DeskApi::Configuration.keys.each do |key|
68
- send("#{key}=", DeskApi::Default.options[key])
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
- alias setup reset!
65
+ self
66
+ end
67
+ alias setup reset!
73
68
 
74
- def credentials?
75
- oauth.values.all? || basic_auth.values.all?
76
- end
69
+ def credentials?
70
+ oauth.values.all? || basic_auth.values.all?
71
+ end
77
72
 
78
- private
79
- # @return [Hash]
80
- def options
81
- Hash[DeskApi::Configuration.keys.map{|key| [key, instance_variable_get(:"@#{key}")]}]
82
- end
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
- def oauth
85
- {
86
- consumer_key: @consumer_key,
87
- consumer_secret: @consumer_secret,
88
- token: @token,
89
- token_secret: @token_secret
90
- }
91
- end
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
- def basic_auth
94
- {
95
- username: @username,
96
- password: @password
97
- }
98
- end
88
+ def basic_auth
89
+ {
90
+ username: @username,
91
+ password: @password
92
+ }
93
+ end
99
94
 
100
- def validate_credentials!
101
- unless credentials?
102
- raise(Error::ConfigurationError, "Invalid credentials: Either username/password or OAuth credentials must be specified.")
103
- end
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
- if oauth.values.all?
106
- oauth.each do |credential, value|
107
- next if value.nil?
100
+ if oauth.values.all?
101
+ oauth.each do |credential, value|
102
+ next if value.nil?
108
103
 
109
- unless value.is_a?(String) || value.is_a?(Symbol)
110
- raise(Error::ConfigurationError, "Invalid #{credential} specified: #{value} must be a string or symbol.")
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
- if basic_auth.values.all?
116
- basic_auth.each do |credential, value|
117
- next if value.nil?
110
+ if basic_auth.values.all?
111
+ basic_auth.each do |credential, value|
112
+ next if value.nil?
118
113
 
119
- unless value.is_a?(String) || value.is_a?(Symbol)
120
- raise(Error::ConfigurationError, "Invalid #{credential} specified: #{value} must be a string or symbol.")
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
- def validate_endpoint!
127
- unless endpoint =~ /^#{URI::regexp}$/
128
- raise(Error::ConfigurationError, "Invalid endpoint specified: `#{endpoint}` must be a valid url.")
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