easy-jsonapi 1.0.0 → 1.0.5

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.
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JSONAPI
4
+ module Exceptions
5
+
6
+ # Error to raise when error found while parsing json
7
+ class JSONParseError < StandardError
8
+ end
9
+ end
10
+ end
@@ -33,7 +33,7 @@ module JSONAPI
33
33
  end
34
34
 
35
35
  err_msg = JSONAPI::Exceptions::UserDefinedExceptions.check_user_query_param_requirements(rack_req_params, config_manager, opts)
36
- return err_msg unless err_msg.nil?
36
+ raise err_msg unless err_msg.nil?
37
37
 
38
38
  nil
39
39
  end
@@ -62,8 +62,10 @@ module JSONAPI
62
62
  # with underscores instead of dashes.
63
63
  # @param config (see #check_user_document_requirements)
64
64
  def check_user_header_requirements(headers, config_manager, opts)
65
- return if config_manager.nil? || config_manager.default?
65
+ return if config_manager.nil?
66
+
66
67
  config = get_config(config_manager, opts[:http_method], opts[:path])
68
+ return if config.default? && config_manager.size.positive?
67
69
 
68
70
  err =
69
71
  check_for_required_headers(headers, config.required_headers)
@@ -77,8 +79,10 @@ module JSONAPI
77
79
  # @param rack_req_params [Hash] The hash of the query parameters given by Rack::Request
78
80
  # @param config (see #check_user_document_requirements)
79
81
  def check_user_query_param_requirements(rack_req_params, config_manager, opts)
80
- return if config_manager.nil? || config_manager.default?
82
+ return if config_manager.nil?
83
+
81
84
  config = get_config(config_manager, opts[:http_method], opts[:path])
85
+ return if config.default? && config_manager.size.positive?
82
86
 
83
87
  err =
84
88
  check_for_required_params(rack_req_params, config.required_query_params)
@@ -49,7 +49,7 @@ module JSONAPI
49
49
  return super unless is_a? JSONAPI::Item
50
50
  return super unless @item.is_a? Hash
51
51
  if should_update_var?(method_name)
52
- @item[method_name[..-2].to_sym] = args[0]
52
+ @item[method_name[0..-2].to_sym] = args[0]
53
53
  elsif should_get_var?(method_name)
54
54
  @item[method_name]
55
55
  else
@@ -75,7 +75,7 @@ module JSONAPI
75
75
  # variable.
76
76
  # @param (see #method_missing)
77
77
  def should_update_var?(method_name)
78
- method_name.to_s[-1] == '=' && @item[method_name[..-2].to_sym].nil? == false
78
+ method_name.to_s[-1] == '=' && @item[method_name[0..-2].to_sym].nil? == false
79
79
  end
80
80
 
81
81
  # Checks to see if the method has the same name as an existing instance
@@ -2,7 +2,7 @@
2
2
 
3
3
  require 'easy/jsonapi/exceptions'
4
4
  require 'easy/jsonapi/config_manager'
5
- require 'oj'
5
+ require 'easy/jsonapi/parser/json_parser'
6
6
 
7
7
  module JSONAPI
8
8
 
@@ -88,9 +88,12 @@ module JSONAPI
88
88
  # @param config_manager [JSONAPI::ConfigManager::Config] The config object to use modify compliance checking
89
89
  # @return [NilClass | Array] Nil meaning no error or a 400 level http response
90
90
  def check_compliance(env, config_manager)
91
+ # Store separately so you can rewind for next middleware or app
92
+ body = env['rack.input'].read
93
+ env['rack.input'].rewind
91
94
  opts = { http_method: env['REQUEST_METHOD'], path: env['PATH_INFO'] }
92
95
 
93
- header_error = check_headers_compliance(env, config_manager, opts)
96
+ header_error = check_headers_compliance(env, body, config_manager, opts)
94
97
  return header_error unless header_error.nil?
95
98
 
96
99
  req = Rack::Request.new(env)
@@ -99,16 +102,16 @@ module JSONAPI
99
102
 
100
103
  return unless env['CONTENT_TYPE']
101
104
 
102
- body_error = check_req_body_compliance(env, config_manager, opts)
105
+ body_error = check_req_body_compliance(env, body, config_manager, opts)
103
106
  return body_error unless body_error.nil?
104
107
  end
105
108
 
106
109
  # Checks whether the http headers are jsonapi compliant
107
110
  # @param (see #call)
108
111
  # @return [NilClass | Array] Nil meaning no error or a 400 level http response
109
- def check_headers_compliance(env, config_manager, opts)
110
- JSONAPI::Exceptions::HeadersExceptions.check_request(env, config_manager, opts)
111
- rescue JSONAPI::Exceptions::HeadersExceptions::InvalidHeader || JSONAPI::Exceptions::UserDefinedExceptions::InvalidHeader => e
112
+ def check_headers_compliance(env, body, config_manager, opts)
113
+ JSONAPI::Exceptions::HeadersExceptions.check_request(env, body, config_manager, opts)
114
+ rescue JSONAPI::Exceptions::HeadersExceptions::InvalidHeader, JSONAPI::Exceptions::UserDefinedExceptions::InvalidHeader => e
112
115
  raise if environment_development?(env)
113
116
 
114
117
  [e.status_code, {}, []]
@@ -119,7 +122,7 @@ module JSONAPI
119
122
  # @return [NilClass | Array] Nil meaning no error or a 400 level http response
120
123
  def check_query_param_compliance(env, query_params, config_manager, opts)
121
124
  JSONAPI::Exceptions::QueryParamsExceptions.check_compliance(query_params, config_manager, opts)
122
- rescue JSONAPI::Exceptions::QueryParamsExceptions::InvalidQueryParameter || JSONAPI::Exceptions::UserDefinedExceptions::InvalidQueryParam => e
125
+ rescue JSONAPI::Exceptions::QueryParamsExceptions::InvalidQueryParameter, JSONAPI::Exceptions::UserDefinedExceptions::InvalidQueryParam => e
123
126
  raise if environment_development?(env)
124
127
 
125
128
  [e.status_code, {}, []]
@@ -128,16 +131,13 @@ module JSONAPI
128
131
  # @param env (see #call)
129
132
  # @param req (see #check_query_param_compliance)
130
133
  # @raise If the document body is not JSONAPI compliant
131
- def check_req_body_compliance(env, config_manager, opts)
132
- # Store separately so you can rewind for next middleware or app
133
- body = env['rack.input'].read
134
- env['rack.input'].rewind
134
+ def check_req_body_compliance(env, body, config_manager, opts)
135
135
  JSONAPI::Exceptions::DocumentExceptions.check_compliance(body, config_manager, opts)
136
- rescue JSONAPI::Exceptions::DocumentExceptions::InvalidDocument || JSONAPI::Exceptions::UserDefinedExceptions::InvalidDocument => e
136
+ rescue JSONAPI::Exceptions::DocumentExceptions::InvalidDocument, JSONAPI::Exceptions::UserDefinedExceptions::InvalidDocument => e
137
137
  raise if environment_development?(env)
138
138
 
139
139
  [e.status_code, {}, []]
140
- rescue Oj::ParseError
140
+ rescue JSONAPI::Exceptions::JSONParseError
141
141
  raise if environment_development?(env)
142
142
 
143
143
  [400, {}, []]
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'easy/jsonapi/document'
4
- require 'oj'
4
+ require 'easy/jsonapi/parser/json_parser'
5
5
 
6
6
  module JSONAPI
7
7
  module Parser
@@ -16,7 +16,7 @@ module JSONAPI
16
16
  # @raise [JSONAPI::Parser::InvalidDocument] if document is invalid.
17
17
  def self.parse(req_body)
18
18
  return if req_body.nil?
19
- document_hash = Oj.load(req_body, symbol_keys: true) # parse json string into hash
19
+ document_hash = JSONAPI::Parser::JSONParser.parse(req_body) # parse json string into hash
20
20
  parse_hash(document_hash)
21
21
  end
22
22
 
@@ -20,7 +20,7 @@ module JSONAPI
20
20
  h_collection = JSONAPI::HeaderCollection.new
21
21
  env.each_key do |k|
22
22
  if k.start_with?('HTTP_') && (k != 'HTTP_VERSION')
23
- h_collection << JSONAPI::HeaderCollection::Header.new(k[5..], env[k])
23
+ h_collection << JSONAPI::HeaderCollection::Header.new(k[5..-1], env[k])
24
24
  elsif k == 'CONTENT_TYPE'
25
25
  h_collection << JSONAPI::HeaderCollection::Header.new(k, env[k])
26
26
  end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'oj'
4
+ require 'easy/jsonapi/exceptions/json_parse_error'
5
+
6
+ module JSONAPI
7
+ module Parser
8
+ # A wrapper class for OJ parser
9
+ module JSONParser
10
+
11
+ # Parse JSON string into a ruby hash
12
+ # @param document [String] The JSON string to parse
13
+ # @raise [JSONAPI::Exceptions::JSONParseError]
14
+ def self.parse(document, symbol_keys: true)
15
+ Oj.load(document, symbol_keys: symbol_keys)
16
+
17
+ rescue Oj::ParseError => e
18
+ raise JSONAPI::Exceptions::JSONParseError, e.message
19
+ end
20
+
21
+ # Convert ruby hash into JSON
22
+ # @param ruby_hash [Hash] THe hash to convert into JSON
23
+ def self.dump(ruby_hash)
24
+ Oj.dump(ruby_hash)
25
+ end
26
+
27
+ end
28
+ end
29
+ end
@@ -8,10 +8,12 @@ module JSONAPI
8
8
  # A collection of QueryParam objects
9
9
  class QueryParamCollection < JSONAPI::NameValuePairCollection
10
10
 
11
+ # The special query params defined by the JSON:API specification
12
+ SPECIAL_QUERY_PARAMS = %i[sorts filters fields page includes].freeze
13
+
11
14
  # @param param_arr [Array<JSONAPI::Request::QueryParamCollection::QueryParam] The
12
15
  # query params to initialize the collection with
13
16
  def initialize(param_arr = [])
14
- @param_names = []
15
17
  super(param_arr, item_type: JSONAPI::Request::QueryParamCollection::QueryParam)
16
18
  end
17
19
 
@@ -43,8 +45,12 @@ module JSONAPI
43
45
  # @param args If any arguments were passed to the method called
44
46
  # @param block If a block was passed to the method called
45
47
  def method_missing(method_name, *args, &block)
46
- super unless include?(method_name)
47
- get(method_name)
48
+ included = include?(method_name)
49
+ super unless included || SPECIAL_QUERY_PARAMS.include?(method_name)
50
+ if included
51
+ return get(method_name)
52
+ end
53
+ nil
48
54
  end
49
55
 
50
56
  # Whether or not method missing should be called.
@@ -82,7 +82,7 @@ module JSONAPI
82
82
  add_member(loc_in_h, res_name, included: true)
83
83
  else
84
84
  add_member(loc_in_h, res_name, included: res_included?(i_arr))
85
- store_include(loc_in_h[res_name][:relationships], i_arr[2..])
85
+ store_include(loc_in_h[res_name][:relationships], i_arr[2..-1])
86
86
  end
87
87
  end
88
88
 
@@ -151,7 +151,7 @@ module JSONAPI
151
151
  def all_hash_path?(hash, args)
152
152
  return false if (args.size.positive? && !hash.is_a?(Hash)) || hash.nil?
153
153
  return true if args.size.zero? && !hash.nil?
154
- all_hash_path?(hash[args.first], args[1..])
154
+ all_hash_path?(hash[args.first], args[1..-1])
155
155
  end
156
156
  end
157
157
  end
metadata CHANGED
@@ -1,29 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: easy-jsonapi
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joshua DeMoss, Joe Viscomi
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-03-25 00:00:00.000000000 Z
11
+ date: 2021-03-30 00:00:00.000000000 Z
12
12
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: license_finder
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - "~>"
18
- - !ruby/object:Gem::Version
19
- version: '6.10'
20
- type: :development
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - "~>"
25
- - !ruby/object:Gem::Version
26
- version: '6.10'
27
13
  - !ruby/object:Gem::Dependency
28
14
  name: rack
29
15
  requirement: !ruby/object:Gem::Requirement
@@ -108,6 +94,20 @@ dependencies:
108
94
  - - "~>"
109
95
  - !ruby/object:Gem::Version
110
96
  version: '0.39'
97
+ - !ruby/object:Gem::Dependency
98
+ name: codecov
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0.4'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0.4'
111
111
  - !ruby/object:Gem::Dependency
112
112
  name: oj
113
113
  requirement: !ruby/object:Gem::Requirement
@@ -131,10 +131,16 @@ executables: []
131
131
  extensions: []
132
132
  extra_rdoc_files: []
133
133
  files:
134
- - ".github/workflows/publish-gem.yml"
135
- - ".github/workflows/rake.yml"
134
+ - ".dockerignore"
135
+ - ".github/workflows/codecov.yml"
136
+ - ".github/workflows/publish.yml"
137
+ - ".gitignore"
136
138
  - ".rspec"
139
+ - ".rspec_status"
140
+ - ".rubocop.yml"
141
+ - ".ruby-gemset"
137
142
  - ".ruby-version"
143
+ - ".travis.yml"
138
144
  - CHANGELOG.md
139
145
  - CODE_OF_CONDUCT.md
140
146
  - Gemfile
@@ -142,8 +148,6 @@ files:
142
148
  - LICENSE.txt
143
149
  - README.md
144
150
  - Rakefile
145
- - UsingTheRequestObject.md
146
- - UsingUserConfigurations.md
147
151
  - bin/bundle
148
152
  - bin/console
149
153
  - bin/htmldiff
@@ -170,6 +174,8 @@ files:
170
174
  - bin/yard
171
175
  - bin/yardoc
172
176
  - bin/yri
177
+ - docs/UsingTheRequestObject.md
178
+ - docs/UsingUserConfigurations.md
173
179
  - easy-jsonapi.gemspec
174
180
  - lib/easy/jsonapi.rb
175
181
  - lib/easy/jsonapi/collection.rb
@@ -193,6 +199,7 @@ files:
193
199
  - lib/easy/jsonapi/exceptions.rb
194
200
  - lib/easy/jsonapi/exceptions/document_exceptions.rb
195
201
  - lib/easy/jsonapi/exceptions/headers_exceptions.rb
202
+ - lib/easy/jsonapi/exceptions/json_parse_error.rb
196
203
  - lib/easy/jsonapi/exceptions/naming_exceptions.rb
197
204
  - lib/easy/jsonapi/exceptions/query_params_exceptions.rb
198
205
  - lib/easy/jsonapi/exceptions/user_defined_exceptions.rb
@@ -206,6 +213,7 @@ files:
206
213
  - lib/easy/jsonapi/parser.rb
207
214
  - lib/easy/jsonapi/parser/document_parser.rb
208
215
  - lib/easy/jsonapi/parser/headers_parser.rb
216
+ - lib/easy/jsonapi/parser/json_parser.rb
209
217
  - lib/easy/jsonapi/parser/rack_req_params_parser.rb
210
218
  - lib/easy/jsonapi/request.rb
211
219
  - lib/easy/jsonapi/request/query_param_collection.rb
@@ -234,7 +242,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
234
242
  requirements:
235
243
  - - ">="
236
244
  - !ruby/object:Gem::Version
237
- version: '2.7'
245
+ version: '2.5'
238
246
  required_rubygems_version: !ruby/object:Gem::Requirement
239
247
  requirements:
240
248
  - - ">="
@@ -1,35 +0,0 @@
1
- # This workflow uses actions that are not certified by GitHub.
2
- # They are provided by a third-party and are governed by
3
- # separate terms of service, privacy policy, and support
4
- # documentation.
5
- # This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake
6
- # For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby
7
-
8
- name: Ruby
9
-
10
- on:
11
- push:
12
- branches: [ master ]
13
- pull_request:
14
- branches: [ master ]
15
-
16
- jobs:
17
- test:
18
-
19
- runs-on: ubuntu-latest
20
- strategy:
21
- matrix:
22
- ruby-version: ['2.7', '3.0']
23
-
24
- steps:
25
- - uses: actions/checkout@v2
26
- - name: Set up Ruby
27
- # To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
28
- # change this to (see https://github.com/ruby/setup-ruby#versioning):
29
- # uses: ruby/setup-ruby@v1
30
- uses: ruby/setup-ruby@473e4d8fe5dd94ee328fdfca9f8c9c7afc9dae5e
31
- with:
32
- ruby-version: ${{ matrix.ruby-version }}
33
- bundler-cache: true # runs 'bundle install' and caches installed gems automatically
34
- - name: Run tests
35
- run: bundle exec rake