clever-ruby 0.3.1 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.drone.yml +14 -0
- data/.rubocop.yml +4 -0
- data/LICENSE +190 -0
- data/README.md +37 -7
- data/Rakefile +12 -2
- data/clever-ruby.gemspec +17 -13
- data/lib/clever-ruby.rb +87 -61
- data/lib/clever-ruby/api_operations/list.rb +9 -4
- data/lib/clever-ruby/api_operations/page.rb +7 -4
- data/lib/clever-ruby/api_operations/pagelist.rb +6 -3
- data/lib/clever-ruby/api_resource.rb +31 -10
- data/lib/clever-ruby/clever_object.rb +34 -35
- data/lib/clever-ruby/configuration.rb +2 -1
- data/lib/clever-ruby/district.rb +4 -12
- data/lib/clever-ruby/errors/api_connection_error.rb +1 -1
- data/lib/clever-ruby/errors/api_error.rb +1 -1
- data/lib/clever-ruby/errors/authentication_error.rb +1 -1
- data/lib/clever-ruby/errors/clever_error.rb +4 -3
- data/lib/clever-ruby/errors/invalid_request_error.rb +4 -3
- data/lib/clever-ruby/event.rb +6 -5
- data/lib/clever-ruby/json.rb +2 -1
- data/lib/clever-ruby/school.rb +2 -0
- data/lib/clever-ruby/section.rb +2 -0
- data/lib/clever-ruby/student.rb +6 -3
- data/lib/clever-ruby/teacher.rb +2 -0
- data/lib/clever-ruby/util.rb +31 -32
- data/lib/clever-ruby/version.rb +2 -1
- data/test/data/vcr_cassettes/districts.yml +10 -16
- data/test/data/vcr_cassettes/districts_event_pages.yml +627 -62
- data/test/data/vcr_cassettes/districts_events.yml +243 -41
- data/test/data/vcr_cassettes/districts_school_pages.yml +93 -102
- data/test/data/vcr_cassettes/districts_schools.yml +85 -92
- data/test/data/vcr_cassettes/districts_section_pages.yml +3364 -314
- data/test/data/vcr_cassettes/districts_sections.yml +591 -266
- data/test/data/vcr_cassettes/districts_student_pages.yml +1701 -14694
- data/test/data/vcr_cassettes/districts_students.yml +209 -2960
- data/test/data/vcr_cassettes/districts_students_filtered.yml +39 -64
- data/test/data/vcr_cassettes/districts_teacher_pages.yml +455 -202
- data/test/data/vcr_cassettes/districts_teachers.yml +244 -163
- data/test/data/vcr_cassettes/error_handling.yml +36 -52
- data/test/data/vcr_cassettes/schools.yml +20 -29
- data/test/data/vcr_cassettes/schools_optional_attributes.yml +21 -30
- data/test/data/vcr_cassettes/sections.yml +1069 -114
- data/test/data/vcr_cassettes/students.yml +1095 -1296
- data/test/data/vcr_cassettes/teachers.yml +2341 -872
- data/test/integration/api_operations/list_test.rb +16 -15
- data/test/integration/district_test.rb +18 -17
- data/test/integration/error_handling_test.rb +8 -7
- data/test/test_helper.rb +2 -2
- data/test/unit/clever_test.rb +13 -13
- data/test/unit/configuration_test.rb +8 -11
- data/test/unit/event_test.rb +18 -21
- data/test/unit/optional_attributes_test.rb +21 -14
- metadata +99 -55
- data/.travis.yml +0 -9
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 99033cb0c5a65221b812e16e8fe2fdfe9e72511e
|
4
|
+
data.tar.gz: 8ef75558e1f4cefdc30bc5e0d4f9549084535936
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 45abc14b94d80f7b42703aeeeea9c1c682e9bcd966c9c70602d265fe3d5f96f2d5598421b532cac046004684b080086278953d1dcca67f83bbffcca8de2c9523
|
7
|
+
data.tar.gz: 9e5f17fce13546e08d25e6aef27591cf056828eb5535045d8e8948b5e15089dd0042d57371ad9c3e4dca18fb77d65f944f13e844091dd7fae072ff9d1baab8d1
|
data/.drone.yml
ADDED
data/.rubocop.yml
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,190 @@
|
|
1
|
+
Apache License
|
2
|
+
Version 2.0, January 2004
|
3
|
+
http://www.apache.org/licenses/
|
4
|
+
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
6
|
+
|
7
|
+
1. Definitions.
|
8
|
+
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
11
|
+
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
13
|
+
the copyright owner that is granting the License.
|
14
|
+
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
16
|
+
other entities that control, are controlled by, or are under common
|
17
|
+
control with that entity. For the purposes of this definition,
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
19
|
+
direction or management of such entity, whether by contract or
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
22
|
+
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
24
|
+
exercising permissions granted by this License.
|
25
|
+
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
27
|
+
including but not limited to software source code, documentation
|
28
|
+
source, and configuration files.
|
29
|
+
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
31
|
+
transformation or translation of a Source form, including but
|
32
|
+
not limited to compiled object code, generated documentation,
|
33
|
+
and conversions to other media types.
|
34
|
+
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
36
|
+
Object form, made available under the License, as indicated by a
|
37
|
+
copyright notice that is included in or attached to the work
|
38
|
+
(an example is provided in the Appendix below).
|
39
|
+
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
46
|
+
the Work and Derivative Works thereof.
|
47
|
+
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
49
|
+
the original version of the Work and any modifications or additions
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
61
|
+
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
64
|
+
subsequently incorporated within the Work.
|
65
|
+
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
72
|
+
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
78
|
+
where such license applies only to those patent claims licensable
|
79
|
+
by such Contributor that are necessarily infringed by their
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
82
|
+
institute patent litigation against any entity (including a
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
85
|
+
or contributory patent infringement, then any patent licenses
|
86
|
+
granted to You under this License for that Work shall terminate
|
87
|
+
as of the date such litigation is filed.
|
88
|
+
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
91
|
+
modifications, and in Source or Object form, provided that You
|
92
|
+
meet the following conditions:
|
93
|
+
|
94
|
+
(a) You must give any other recipients of the Work or
|
95
|
+
Derivative Works a copy of this License; and
|
96
|
+
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
98
|
+
stating that You changed the files; and
|
99
|
+
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
102
|
+
attribution notices from the Source form of the Work,
|
103
|
+
excluding those notices that do not pertain to any part of
|
104
|
+
the Derivative Works; and
|
105
|
+
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
108
|
+
include a readable copy of the attribution notices contained
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
111
|
+
of the following places: within a NOTICE text file distributed
|
112
|
+
as part of the Derivative Works; within the Source form or
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
114
|
+
within a display generated by the Derivative Works, if and
|
115
|
+
wherever such third-party notices normally appear. The contents
|
116
|
+
of the NOTICE file are for informational purposes only and
|
117
|
+
do not modify the License. You may add Your own attribution
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
120
|
+
that such additional attribution notices cannot be construed
|
121
|
+
as modifying the License.
|
122
|
+
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
124
|
+
may provide additional or different license terms and conditions
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
128
|
+
the conditions stated in this License.
|
129
|
+
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
133
|
+
this License, without any additional terms or conditions.
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
135
|
+
the terms of any separate license agreement you may have executed
|
136
|
+
with Licensor regarding such Contributions.
|
137
|
+
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
140
|
+
except as required for reasonable and customary use in describing the
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
142
|
+
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
152
|
+
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
158
|
+
incidental, or consequential damages of any character arising as a
|
159
|
+
result of this License or out of the use or inability to use the
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
162
|
+
other commercial damages or losses), even if such Contributor
|
163
|
+
has been advised of the possibility of such damages.
|
164
|
+
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
168
|
+
or other liability obligations and/or rights consistent with this
|
169
|
+
License. However, in accepting such obligations, You may act only
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
174
|
+
of your accepting any such warranty or additional liability.
|
175
|
+
|
176
|
+
END OF TERMS AND CONDITIONS
|
177
|
+
|
178
|
+
Copyright 2014 Clever, Inc.
|
179
|
+
|
180
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
181
|
+
you may not use this file except in compliance with the License.
|
182
|
+
You may obtain a copy of the License at
|
183
|
+
|
184
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
185
|
+
|
186
|
+
Unless required by applicable law or agreed to in writing, software
|
187
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
188
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
189
|
+
See the License for the specific language governing permissions and
|
190
|
+
limitations under the License.
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Clever::Ruby
|
2
2
|
|
3
|
-
[![Build Status](https://
|
3
|
+
[![Build Status](https://drone.ops.clever.com/github.com/Clever/clever-ruby/status.svg?branch=master)](https://drone.ops.clever.com/github.com/Clever/clever-ruby)
|
4
4
|
|
5
5
|
Ruby bindings to the Clever API.
|
6
6
|
|
@@ -8,23 +8,53 @@ Ruby bindings to the Clever API.
|
|
8
8
|
|
9
9
|
Add this line to your application's Gemfile:
|
10
10
|
|
11
|
-
|
11
|
+
```bash
|
12
|
+
gem 'clever-ruby'
|
13
|
+
```
|
12
14
|
|
13
15
|
And then execute:
|
14
16
|
|
15
|
-
|
17
|
+
```bash
|
18
|
+
$ bundle
|
19
|
+
```
|
16
20
|
|
17
21
|
Or install it yourself as:
|
18
22
|
|
19
|
-
|
23
|
+
```bash
|
24
|
+
$ gem install clever-ruby
|
25
|
+
```
|
26
|
+
|
27
|
+
## Run tests
|
28
|
+
|
29
|
+
To run all tests:
|
30
|
+
|
31
|
+
```bash
|
32
|
+
$ rake test
|
33
|
+
```
|
34
|
+
|
35
|
+
To run all tests and the linter ([rubocop](https://github.com/bbatsov/rubocop)):
|
36
|
+
|
37
|
+
```bash
|
38
|
+
$ rake
|
39
|
+
```
|
40
|
+
|
41
|
+
The linter follows the [Ruby Style Guide](https://github.com/bbatsov/ruby-style-guide) with a few exceptions. To just run the linter:
|
42
|
+
|
43
|
+
```bash
|
44
|
+
$ rake lint
|
45
|
+
```
|
46
|
+
|
47
|
+
Running specific tests is not currently supported.
|
20
48
|
|
21
49
|
## Usage
|
22
50
|
|
23
51
|
Configure clever-ruby to use your Clever API key:
|
24
52
|
|
25
|
-
|
26
|
-
|
27
|
-
|
53
|
+
```ruby
|
54
|
+
Clever.configure do |config|
|
55
|
+
config.api_key = 'YOUR-API-KEY'
|
56
|
+
end
|
57
|
+
```
|
28
58
|
|
29
59
|
## Contributing
|
30
60
|
|
data/Rakefile
CHANGED
@@ -9,8 +9,6 @@ Rake::TestTask.new(:test) do |test|
|
|
9
9
|
test.verbose = true
|
10
10
|
end
|
11
11
|
|
12
|
-
task :default => :test
|
13
|
-
|
14
12
|
require 'rdoc/task'
|
15
13
|
require 'clever-ruby/version'
|
16
14
|
Rake::RDocTask.new do |rdoc|
|
@@ -19,3 +17,15 @@ Rake::RDocTask.new do |rdoc|
|
|
19
17
|
rdoc.rdoc_files.include('README*')
|
20
18
|
rdoc.rdoc_files.include('lib/**/*.rb')
|
21
19
|
end
|
20
|
+
|
21
|
+
require 'rubocop/rake_task'
|
22
|
+
|
23
|
+
desc 'Run RuboCop on the lib directory'
|
24
|
+
RuboCop::RakeTask.new(:lint) do |task|
|
25
|
+
task.patterns = ['lib/**/*.rb', 'test/**/*.rb']
|
26
|
+
end
|
27
|
+
|
28
|
+
task default: [] do
|
29
|
+
Rake::Task[:test].execute
|
30
|
+
Rake::Task[:lint].execute
|
31
|
+
end
|
data/clever-ruby.gemspec
CHANGED
@@ -2,29 +2,33 @@
|
|
2
2
|
lib = File.expand_path('../lib', __FILE__)
|
3
3
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
4
|
require 'clever-ruby/version'
|
5
|
+
require 'English'
|
5
6
|
|
6
7
|
Gem::Specification.new do |gem|
|
7
|
-
gem.name =
|
8
|
+
gem.name = 'clever-ruby'
|
8
9
|
gem.version = Clever::VERSION
|
9
|
-
gem.authors = [
|
10
|
-
gem.email = [
|
11
|
-
gem.description =
|
12
|
-
gem.summary =
|
13
|
-
gem.homepage =
|
10
|
+
gem.authors = ['Clever Dev']
|
11
|
+
gem.email = ['tech-support@clever.com']
|
12
|
+
gem.description = 'Ruby bindings to the Clever API.'
|
13
|
+
gem.summary = 'Ruby bindings to the Clever API.'
|
14
|
+
gem.homepage = 'http://github.com/Clever/clever-ruby'
|
14
15
|
|
15
|
-
gem.
|
16
|
-
gem.
|
17
|
-
gem.
|
18
|
-
gem.
|
16
|
+
gem.required_ruby_version = '>= 1.9.3'
|
17
|
+
gem.files = `git ls-files`.split($RS)
|
18
|
+
gem.executables = gem.files.grep(/^bin\//).map { |f| File.basename f }
|
19
|
+
gem.test_files = gem.files.grep(/^(test|spec|features)\//)
|
20
|
+
gem.require_paths = ['lib']
|
21
|
+
|
22
|
+
gem.add_runtime_dependency 'multi_json', '~> 1.10.0'
|
23
|
+
gem.add_runtime_dependency 'rest-client', '~> 1.6.7'
|
19
24
|
|
20
|
-
gem.add_runtime_dependency 'multi_json', '~> 1.7.0'
|
21
|
-
gem.add_runtime_dependency 'rest-client', '~> 1.6.7'
|
22
25
|
gem.add_development_dependency 'rake'
|
23
26
|
gem.add_development_dependency 'minitest', '~> 4.4.0'
|
24
27
|
gem.add_development_dependency 'shoulda', '~> 3.3.2'
|
25
28
|
gem.add_development_dependency 'rdoc', '~> 3.12'
|
26
29
|
gem.add_development_dependency 'vcr', '~> 2.4.0'
|
27
30
|
gem.add_development_dependency 'webmock', '~> 1.9.0'
|
28
|
-
gem.add_development_dependency 'pry', '~> 0.
|
31
|
+
gem.add_development_dependency 'pry', '~> 0.10.1'
|
29
32
|
gem.add_development_dependency 'activesupport', '~> 3.2.11'
|
33
|
+
gem.add_development_dependency 'rubocop', '~> 0.26.0'
|
30
34
|
end
|
data/lib/clever-ruby.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
# rubocop:disable FileName
|
1
2
|
require 'rest_client'
|
2
3
|
require 'multi_json'
|
3
4
|
require 'open-uri'
|
@@ -33,6 +34,7 @@ require 'clever-ruby/errors/api_error'
|
|
33
34
|
require 'clever-ruby/errors/authentication_error'
|
34
35
|
require 'clever-ruby/errors/invalid_request_error'
|
35
36
|
|
37
|
+
# Clever Ruby library
|
36
38
|
module Clever
|
37
39
|
class << self
|
38
40
|
def configure
|
@@ -58,132 +60,156 @@ module Clever
|
|
58
60
|
|
59
61
|
def self.convert_to_query_string(params = nil)
|
60
62
|
if params && params.count
|
61
|
-
|
62
|
-
"#{URI
|
63
|
-
|
63
|
+
params_arr = Util.flatten_params(params).map do |p|
|
64
|
+
"#{URI.encode(p[0].to_s)}=#{URI.encode(p[1].to_s)}"
|
65
|
+
end
|
66
|
+
'?' + params_arr.join('&')
|
64
67
|
else
|
65
68
|
''
|
66
69
|
end
|
67
70
|
end
|
68
71
|
|
69
|
-
def self.
|
70
|
-
raise AuthenticationError.new('No API key provided. (HINT: set your API key using "Clever.configure { |config| config.api_key = <API-KEY> }" or your token using "Clever.configure { |config| config.token = <TOKEN> }")') unless Clever.api_key or Clever.token
|
71
|
-
|
72
|
-
params = Util.objects_to_ids(params)
|
73
|
-
url = self.api_url(url)
|
74
|
-
|
72
|
+
def self.create_payload(method, url, params)
|
75
73
|
case method.to_s.downcase.to_sym
|
76
74
|
when :get, :head, :delete
|
77
|
-
url += convert_to_query_string
|
75
|
+
url += convert_to_query_string params
|
78
76
|
payload = nil
|
79
77
|
else
|
80
78
|
payload = params
|
81
79
|
end
|
80
|
+
[url, payload]
|
81
|
+
end
|
82
82
|
|
83
|
-
|
84
|
-
|
85
|
-
|
83
|
+
def self.create_request_opts(method, url, params, headers)
|
84
|
+
params = Util.objects_to_ids params
|
85
|
+
url = api_url url
|
86
|
+
url, payload = create_payload method, url, params
|
87
|
+
|
88
|
+
headers[:Authorization] = 'Bearer ' + Clever.token if Clever.token
|
86
89
|
|
87
90
|
opts = {
|
88
|
-
:
|
89
|
-
:
|
90
|
-
:
|
91
|
-
:
|
92
|
-
:
|
93
|
-
:
|
91
|
+
method: method,
|
92
|
+
url: url,
|
93
|
+
headers: headers,
|
94
|
+
open_timeout: 30,
|
95
|
+
payload: payload,
|
96
|
+
timeout: 120
|
94
97
|
}
|
95
98
|
if Clever.api_key
|
96
99
|
opts[:user] = Clever.api_key
|
97
|
-
opts[:password] =
|
100
|
+
opts[:password] = ''
|
98
101
|
end
|
102
|
+
opts
|
103
|
+
end
|
99
104
|
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
# Work around RestClient bug
|
106
|
-
if e.message =~ /\WRequestFailed\W/
|
107
|
-
e = APIConnectionError.new('Unexpected HTTP response code')
|
108
|
-
self.handle_restclient_error(e)
|
109
|
-
else
|
110
|
-
raise
|
111
|
-
end
|
112
|
-
rescue RestClient::ExceptionWithResponse => e
|
113
|
-
if rcode = e.http_code and rbody = e.http_body
|
114
|
-
self.handle_api_error(rcode, rbody)
|
115
|
-
else
|
116
|
-
self.handle_restclient_error(e)
|
117
|
-
end
|
118
|
-
rescue RestClient::Exception, Errno::ECONNREFUSED => e
|
119
|
-
self.handle_restclient_error(e)
|
105
|
+
def self.check_authorization
|
106
|
+
unless Clever.api_key || Clever.token
|
107
|
+
fail AuthenticationError, 'No API key provided. (HINT: set your API key using '\
|
108
|
+
'"Clever.configure { |config| config.api_key = <API-KEY> }" or your token using '\
|
109
|
+
'"Clever.configure { |config| config.token = <TOKEN> }")'
|
120
110
|
end
|
111
|
+
end
|
112
|
+
|
113
|
+
def self.request(method, url, params = nil, headers = {})
|
114
|
+
check_authorization
|
115
|
+
opts = create_request_opts method, url, params, headers
|
116
|
+
response = execute_request opts
|
121
117
|
|
122
118
|
rbody = response.body
|
123
119
|
rcode = response.code
|
124
120
|
begin
|
125
|
-
# Would use :
|
121
|
+
# Would use symbolize_names: true, but apparently there is
|
126
122
|
# some library out there that makes symbolize_names not work.
|
127
|
-
resp = Clever::JSON.load
|
123
|
+
resp = Clever::JSON.load rbody
|
128
124
|
rescue MultiJson::DecodeError
|
129
|
-
raise APIError.new(
|
125
|
+
raise APIError.new(
|
126
|
+
"Invalid response object from API: #{rbody.inspect} (HTTP response code was #{rcode})",
|
127
|
+
rcode, rbody)
|
130
128
|
end
|
131
129
|
|
132
|
-
resp = Util.symbolize_names
|
130
|
+
resp = Util.symbolize_names resp
|
133
131
|
resp
|
134
132
|
end
|
135
133
|
|
136
134
|
private
|
137
135
|
|
138
136
|
def self.execute_request(opts)
|
139
|
-
|
137
|
+
begin
|
138
|
+
request = RestClient::Request.execute opts
|
139
|
+
rescue SocketError => e
|
140
|
+
handle_restclient_error e
|
141
|
+
rescue NoMethodError => e
|
142
|
+
# TODO: investigate
|
143
|
+
# Work around RestClient bug
|
144
|
+
if e.message =~ /\WRequestFailed\W/
|
145
|
+
handle_restclient_error APIConnectionError.new 'Unexpected HTTP response code'
|
146
|
+
else raise
|
147
|
+
end
|
148
|
+
rescue RestClient::ExceptionWithResponse => e
|
149
|
+
rcode = e.http_code
|
150
|
+
rbody = e.http_body
|
151
|
+
if rcode && rbody
|
152
|
+
handle_api_error rcode, rbody
|
153
|
+
else handle_restclient_error e
|
154
|
+
end
|
155
|
+
rescue RestClient::Exception, Errno::ECONNREFUSED => e
|
156
|
+
handle_restclient_error e
|
157
|
+
end
|
158
|
+
|
159
|
+
request
|
140
160
|
end
|
141
161
|
|
142
162
|
def self.handle_restclient_error(e)
|
143
163
|
case e
|
144
164
|
when RestClient::ServerBrokeConnection, RestClient::RequestTimeout
|
145
|
-
message = "Could not connect to Clever (#{configuration.api_base}).
|
165
|
+
message = "Could not connect to Clever (#{configuration.api_base}). " \
|
166
|
+
'Please check your internet connection and try again.'
|
146
167
|
when SocketError
|
147
|
-
message =
|
168
|
+
message = 'Unexpected error communicating when trying to connect to Clever. HINT: ' \
|
169
|
+
'You may be seeing this message because your DNS is not working. To check, try running ' \
|
170
|
+
"'host api.clever.com' from the command line."
|
148
171
|
else
|
149
|
-
message =
|
172
|
+
message = 'Unexpected error communicating with Clever.'
|
150
173
|
end
|
151
174
|
message += "\n\n(Network error: #{e.message})"
|
152
|
-
|
175
|
+
fail APIConnectionError, message
|
153
176
|
end
|
154
177
|
|
155
178
|
def self.handle_api_error(rcode, rbody)
|
156
179
|
begin
|
157
|
-
error_obj = Clever::JSON.load
|
158
|
-
error_obj = Util.symbolize_names
|
159
|
-
error = error_obj[:error]
|
160
|
-
|
161
|
-
|
180
|
+
error_obj = Clever::JSON.load rbody
|
181
|
+
error_obj = Util.symbolize_names error_obj
|
182
|
+
error = error_obj[:error]
|
183
|
+
fail CleverError unless error # escape from parsing
|
184
|
+
rescue MultiJson::DecodeError, CleverError
|
185
|
+
raise APIError.new(
|
186
|
+
"Invalid response object from API: #{rbody.inspect} (HTTP response code was #{rcode})",
|
187
|
+
rcode, rbody)
|
162
188
|
end
|
163
189
|
|
164
190
|
case rcode
|
165
191
|
when 400, 404 then
|
166
|
-
|
192
|
+
fail invalid_request_error error, rcode, rbody, error_obj
|
167
193
|
when 401
|
168
|
-
|
194
|
+
fail authentication_error error, rcode, rbody, error_obj
|
169
195
|
else
|
170
|
-
|
196
|
+
fail api_error error, rcode, rbody, error_obj
|
171
197
|
end
|
172
198
|
end
|
173
199
|
|
174
200
|
def self.invalid_request_error(error, rcode, rbody, error_obj)
|
175
201
|
if error.is_a? Hash
|
176
|
-
InvalidRequestError.new
|
202
|
+
InvalidRequestError.new error[:message], error[:param], rcode, rbody, error_obj
|
177
203
|
else
|
178
|
-
InvalidRequestError.new
|
204
|
+
InvalidRequestError.new error, '', rcode, rbody, error_obj
|
179
205
|
end
|
180
206
|
end
|
181
207
|
|
182
208
|
def self.authentication_error(error, rcode, rbody, error_obj)
|
183
|
-
AuthenticationError.new
|
209
|
+
AuthenticationError.new error[:message], rcode, rbody, error_obj
|
184
210
|
end
|
185
211
|
|
186
212
|
def self.api_error(error, rcode, rbody, error_obj)
|
187
|
-
APIError.new
|
213
|
+
APIError.new error[:message], rcode, rbody, error_obj
|
188
214
|
end
|
189
215
|
end
|