openstax_api 8.3.2 → 9.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Rakefile +1 -1
- data/app/controllers/openstax/api/v1/api_controller.rb +17 -12
- data/lib/openstax/api/engine.rb +2 -0
- data/lib/openstax/api/roar.rb +2 -2
- data/lib/openstax/api/rspec_helpers.rb +37 -39
- data/lib/openstax/api/version.rb +1 -1
- metadata +3 -24
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2f8d716f67fca1c587540e13f7db606522238ec2b77638f8278049d1812926c2
|
4
|
+
data.tar.gz: 062e0e434cb74bb13021f4211c5c919108a14d771321534ac0bf6a92b59ce556
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9f97f203f831a97d8bc4ffce4a348dff922c5b4f1b7795715daf6f2ef615231f18b0921dd226b36be3e08b494843dc91fba391b991418caa229b557dfa631298
|
7
|
+
data.tar.gz: 23d07578255d6cd7c84d08265ed866e02d1d20dfaa4efdcbaea260bc2064368382dab602b62fd1580c26b2a3b0f06ba7272e1d4665b21d371e8c855804dfde2f
|
data/Rakefile
CHANGED
@@ -12,27 +12,27 @@ module OpenStax
|
|
12
12
|
|
13
13
|
respond_to :json
|
14
14
|
|
15
|
-
#
|
16
|
-
# want even if something else goes on during the request. These
|
17
|
-
# are also paired with
|
15
|
+
# after_actions are in place to make sure certain things are set how we
|
16
|
+
# want even if something else goes on during the request. These actions
|
17
|
+
# are also paired with before_actions in case an exception prevents
|
18
18
|
# normal action completion.
|
19
19
|
|
20
20
|
# Always force JSON requests and send the Date header in the response
|
21
|
-
|
22
|
-
|
23
|
-
|
21
|
+
before_action :force_json_content_type
|
22
|
+
before_action :set_date_header
|
23
|
+
after_action :set_date_header
|
24
24
|
|
25
25
|
# Doorkeeper is used only if a token is present
|
26
26
|
# Access policies should be used to limit access to anonymous users
|
27
|
-
|
27
|
+
before_action :doorkeeper_authorize!, if: :token_user?
|
28
28
|
|
29
29
|
# Except for users logged in via a cookie, we can disable CSRF protection and enable CORS
|
30
|
-
|
31
|
-
|
32
|
-
|
30
|
+
skip_before_action :verify_authenticity_token, unless: :local_session_user?
|
31
|
+
skip_before_action :verify_authenticity_token, only: :options
|
32
|
+
skip_before_action :authenticate_user!, only: :options, raise: false
|
33
33
|
|
34
|
-
|
35
|
-
|
34
|
+
before_action :maybe_set_cors_headers
|
35
|
+
after_action :maybe_set_cors_headers
|
36
36
|
|
37
37
|
# Keep old current_user method so we can use it
|
38
38
|
alias_method :current_session_user, OpenStax::Api.configuration.current_user_method
|
@@ -40,6 +40,11 @@ module OpenStax
|
|
40
40
|
# Ensure we will never again confuse human users and api users
|
41
41
|
undef_method OpenStax::Api.configuration.current_user_method
|
42
42
|
|
43
|
+
# intended to be overridden by parent
|
44
|
+
def authenticate_user!
|
45
|
+
throw(:abort)
|
46
|
+
end
|
47
|
+
|
43
48
|
# Always return an ApiUser
|
44
49
|
def current_api_user
|
45
50
|
@current_api_user ||= ApiUser.new(doorkeeper_token, lambda { current_session_user })
|
data/lib/openstax/api/engine.rb
CHANGED
@@ -13,6 +13,8 @@ module OpenStax
|
|
13
13
|
class Engine < ::Rails::Engine
|
14
14
|
isolate_namespace OpenStax::Api
|
15
15
|
|
16
|
+
config.autoload_paths += Dir[config.root.join('app', 'representers', '{**}')]
|
17
|
+
|
16
18
|
config.generators do |g|
|
17
19
|
g.test_framework :rspec, fixture: false
|
18
20
|
g.fixture_replacement :factory_bot, dir: 'spec/factories'
|
data/lib/openstax/api/roar.rb
CHANGED
@@ -110,7 +110,7 @@ module OpenStax
|
|
110
110
|
|
111
111
|
model.with_lock do
|
112
112
|
if model.destroy
|
113
|
-
model.clear_association_cache
|
113
|
+
model.send :clear_association_cache
|
114
114
|
respond_with model, responder_options.merge(represent_with_options)
|
115
115
|
else
|
116
116
|
render_api_errors(model.errors)
|
@@ -133,7 +133,7 @@ module OpenStax
|
|
133
133
|
|
134
134
|
model.with_lock do
|
135
135
|
if model.restore(recursive: recursive)
|
136
|
-
model.clear_association_cache
|
136
|
+
model.send :clear_association_cache
|
137
137
|
respond_with model, responder_options.merge(represent_with_options)
|
138
138
|
else
|
139
139
|
render_api_errors(model.errors)
|
@@ -1,11 +1,15 @@
|
|
1
1
|
# Provides API-specific HTTP request methods
|
2
2
|
#
|
3
|
-
#
|
4
|
-
#
|
5
|
-
#
|
6
|
-
#
|
7
|
-
#
|
8
|
-
# :
|
3
|
+
# action is a symbol (controller specs) OR a relative url (request specs)
|
4
|
+
#
|
5
|
+
# doorkeeper_token is a Doorkeeper::AccessToken or nil
|
6
|
+
#
|
7
|
+
# args are a hash that can contain the following keys:
|
8
|
+
# :params -- a hash of parameters (controller specs) OR a json string (request specs)
|
9
|
+
# :body -- a JSON string (controller specs only)
|
10
|
+
# :format -- always set to :json for API calls
|
11
|
+
# :session, :flash -- hashes (controller specs)
|
12
|
+
# :headers, :env -- hashes (request specs)
|
9
13
|
#
|
10
14
|
# Helpful documentation:
|
11
15
|
# https://github.com/rails/rails/blob/3-2-stable/actionpack/lib/action_controller/test_case.rb
|
@@ -13,56 +17,55 @@
|
|
13
17
|
module OpenStax
|
14
18
|
module Api
|
15
19
|
module RSpecHelpers
|
16
|
-
|
17
|
-
def api_get(action, doorkeeper_token, args={})
|
20
|
+
def api_get(action, doorkeeper_token = nil, args={})
|
18
21
|
api_request(:get, action, doorkeeper_token, args)
|
19
22
|
end
|
20
23
|
|
21
|
-
def api_put(action, doorkeeper_token, args={})
|
24
|
+
def api_put(action, doorkeeper_token = nil, args={})
|
22
25
|
api_request(:put, action, doorkeeper_token, args)
|
23
26
|
end
|
24
27
|
|
25
|
-
def api_post(action, doorkeeper_token, args={})
|
28
|
+
def api_post(action, doorkeeper_token = nil, args={})
|
26
29
|
api_request(:post, action, doorkeeper_token, args)
|
27
30
|
end
|
28
31
|
|
29
|
-
def api_delete(action, doorkeeper_token, args={})
|
32
|
+
def api_delete(action, doorkeeper_token = nil, args={})
|
30
33
|
api_request(:delete, action, doorkeeper_token, args)
|
31
34
|
end
|
32
35
|
|
33
|
-
def api_patch(action, doorkeeper_token, args={})
|
36
|
+
def api_patch(action, doorkeeper_token = nil, args={})
|
34
37
|
api_request(:patch, action, doorkeeper_token, args)
|
35
38
|
end
|
36
39
|
|
37
|
-
def api_head(action, doorkeeper_token, args={})
|
40
|
+
def api_head(action, doorkeeper_token = nil, args={})
|
38
41
|
api_request(:head, action, doorkeeper_token, args)
|
39
42
|
end
|
40
43
|
|
41
|
-
def api_request(type, action, doorkeeper_token, args={})
|
42
|
-
raise IllegalArgument
|
43
|
-
|
44
|
-
header = is_a_controller_spec? ? request.env : {}
|
44
|
+
def api_request(type, action, doorkeeper_token = nil, args={})
|
45
|
+
raise IllegalArgument unless [:head, :get, :post, :patch, :put, :delete].include?(type)
|
45
46
|
|
46
|
-
|
47
|
-
header['HTTP_AUTHORIZATION'] = "Bearer #{doorkeeper_token.token}" \
|
48
|
-
if doorkeeper_token
|
47
|
+
headers = is_a_controller_spec? ? request.headers : {}
|
49
48
|
|
50
49
|
# Select the version of the API based on the spec metadata and populate the accept header
|
51
50
|
version_string = self.class.metadata[:version].try(:to_s)
|
52
|
-
raise ArgumentError, "Top-level 'describe' metadata must include a value for ':version'"
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
args[:raw_post_data]
|
60
|
-
end
|
51
|
+
raise ArgumentError, "Top-level 'describe' metadata must include a value for ':version'" \
|
52
|
+
if version_string.nil?
|
53
|
+
headers['HTTP_ACCEPT'] = "application/vnd.openstax.#{version_string}"
|
54
|
+
|
55
|
+
# Add the doorkeeper token header
|
56
|
+
headers['HTTP_AUTHORIZATION'] = "Bearer #{doorkeeper_token.token}" \
|
57
|
+
if doorkeeper_token
|
61
58
|
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
59
|
+
headers['CONTENT_TYPE'] = 'application/json'
|
60
|
+
|
61
|
+
if is_a_controller_spec?
|
62
|
+
request.headers.merge! headers
|
63
|
+
args[:format] = :json
|
64
|
+
# Convert the request body to JSON if needed
|
65
|
+
args[:body] = args[:body].to_json unless args[:body].nil? || args[:body].is_a?(String)
|
66
|
+
else
|
67
|
+
args[:headers] = headers
|
68
|
+
end
|
66
69
|
|
67
70
|
# If these helpers are used from a request spec, action can
|
68
71
|
# be a URL fragment string -- in such a case, prepend "/api"
|
@@ -73,11 +76,7 @@ module OpenStax
|
|
73
76
|
action = "/api#{action}" if !action.starts_with?("/api/")
|
74
77
|
end
|
75
78
|
|
76
|
-
|
77
|
-
send(type, action, args[:parameters], args[:session], args[:flash])
|
78
|
-
else
|
79
|
-
send(type, action, args[:parameters].to_json, header)
|
80
|
-
end
|
79
|
+
send type, action, args
|
81
80
|
end
|
82
81
|
|
83
82
|
private
|
@@ -85,7 +84,6 @@ module OpenStax
|
|
85
84
|
def is_a_controller_spec?
|
86
85
|
self.class.metadata[:type] == :controller
|
87
86
|
end
|
88
|
-
|
89
87
|
end
|
90
88
|
end
|
91
89
|
end
|
data/lib/openstax/api/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: openstax_api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 9.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dante Soares
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2019-05-23 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
@@ -18,9 +18,6 @@ dependencies:
|
|
18
18
|
- - ">="
|
19
19
|
- !ruby/object:Gem::Version
|
20
20
|
version: '3.1'
|
21
|
-
- - "<"
|
22
|
-
- !ruby/object:Gem::Version
|
23
|
-
version: '5.0'
|
24
21
|
type: :runtime
|
25
22
|
prerelease: false
|
26
23
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -28,9 +25,6 @@ dependencies:
|
|
28
25
|
- - ">="
|
29
26
|
- !ruby/object:Gem::Version
|
30
27
|
version: '3.1'
|
31
|
-
- - "<"
|
32
|
-
- !ruby/object:Gem::Version
|
33
|
-
version: '5.0'
|
34
28
|
- !ruby/object:Gem::Dependency
|
35
29
|
name: representable
|
36
30
|
requirement: !ruby/object:Gem::Requirement
|
@@ -219,20 +213,6 @@ dependencies:
|
|
219
213
|
- - ">="
|
220
214
|
- !ruby/object:Gem::Version
|
221
215
|
version: '0'
|
222
|
-
- !ruby/object:Gem::Dependency
|
223
|
-
name: squeel
|
224
|
-
requirement: !ruby/object:Gem::Requirement
|
225
|
-
requirements:
|
226
|
-
- - ">="
|
227
|
-
- !ruby/object:Gem::Version
|
228
|
-
version: '0'
|
229
|
-
type: :development
|
230
|
-
prerelease: false
|
231
|
-
version_requirements: !ruby/object:Gem::Requirement
|
232
|
-
requirements:
|
233
|
-
- - ">="
|
234
|
-
- !ruby/object:Gem::Version
|
235
|
-
version: '0'
|
236
216
|
- !ruby/object:Gem::Dependency
|
237
217
|
name: multi_json
|
238
218
|
requirement: !ruby/object:Gem::Requirement
|
@@ -294,8 +274,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
294
274
|
- !ruby/object:Gem::Version
|
295
275
|
version: '0'
|
296
276
|
requirements: []
|
297
|
-
|
298
|
-
rubygems_version: 2.7.3
|
277
|
+
rubygems_version: 3.0.1
|
299
278
|
signing_key:
|
300
279
|
specification_version: 4
|
301
280
|
summary: API utilities for OpenStax products and tools.
|