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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7296239fd11318616e99546c687c2f99a387e17425f0f6efca8f6b4f77059fc6
4
- data.tar.gz: 0dcbc6e75158fda3afe0ded916182726a285d6bf565ac1bb131b4115290bcebc
3
+ metadata.gz: 2f8d716f67fca1c587540e13f7db606522238ec2b77638f8278049d1812926c2
4
+ data.tar.gz: 062e0e434cb74bb13021f4211c5c919108a14d771321534ac0bf6a92b59ce556
5
5
  SHA512:
6
- metadata.gz: 3296d8f4e8354e4fb26ebd43d75ce509160a2c2d4b1a74c245b111514431ce50f9bc330f1eb8ba49975f7db00d592f81a7374bd0fe98303b7fb6341144b9d6b3
7
- data.tar.gz: 6483a27055e0b8f01850dd77cf22ecfe39ffb3d74d9a41ef2bbd52ece5fda56754d0ea50fd2a8c0c259b57a8f4129d4fb7739cedc453dfe955f8e0bbf95aab84
6
+ metadata.gz: 9f97f203f831a97d8bc4ffce4a348dff922c5b4f1b7795715daf6f2ef615231f18b0921dd226b36be3e08b494843dc91fba391b991418caa229b557dfa631298
7
+ data.tar.gz: 23d07578255d6cd7c84d08265ed866e02d1d20dfaa4efdcbaea260bc2064368382dab602b62fd1580c26b2a3b0f06ba7272e1d4665b21d371e8c855804dfde2f
data/Rakefile CHANGED
@@ -17,4 +17,4 @@ require 'rspec/core/rake_task'
17
17
  desc 'Run all specs in spec directory (excluding plugin specs)'
18
18
  RSpec::Core::RakeTask.new(:spec => 'app:db:test:prepare')
19
19
 
20
- task :default => :spec
20
+ task default: :spec
@@ -12,27 +12,27 @@ module OpenStax
12
12
 
13
13
  respond_to :json
14
14
 
15
- # after_filters are in place to make sure certain things are set how we
16
- # want even if something else goes on during the request. These filters
17
- # are also paired with before_filters in case an exception prevents
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
- before_filter :force_json_content_type
22
- before_filter :set_date_header
23
- after_filter :set_date_header
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
- before_filter :doorkeeper_authorize!, if: :token_user?
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
- skip_before_filter :verify_authenticity_token, unless: :local_session_user?
31
- skip_before_filter :authenticate_user!, only: :options
32
- skip_before_filter :verify_authenticity_token, only: :options
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
- before_filter :maybe_set_cors_headers
35
- after_filter :maybe_set_cors_headers
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 })
@@ -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'
@@ -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
- # The args at the end of each request is interpreted a hash that can contain
4
- # keys for:
5
- # :raw_post_data -- a JSON string (if a Hash provided, to_json will be called on it)
6
- # :parameters -- a hash of parameters
7
- # :session -- whatever built-in request methods expect
8
- # :flash -- whatever built-in request methods expect
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 if ![:get, :post, :put, :delete, :patch, :head].include?(type)
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
- # Add the doorkeeper token info
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'" if version_string.nil?
53
- header['HTTP_ACCEPT'] = "application/vnd.openstax.#{version_string}"
54
-
55
- # Set the raw post data in the request, converting to JSON if needed
56
- if args[:raw_post_data]
57
- header['RAW_POST_DATA'] = args[:raw_post_data].is_a?(Hash) ?
58
- args[:raw_post_data].to_json :
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
- # Set the data format
63
- args[:parameters] ||= {}
64
- args[:parameters][:format] = 'json'
65
- header['CONTENT_TYPE'] = 'application/json'
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
- if is_a_controller_spec?
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
@@ -1,5 +1,5 @@
1
1
  module OpenStax
2
2
  module Api
3
- VERSION = "8.3.2"
3
+ VERSION = '9.0.0'
4
4
  end
5
5
  end
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: 8.3.2
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: 2018-11-28 00:00:00.000000000 Z
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
- rubyforge_project:
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.