jsonapi-utils 0.4.8 → 0.4.9
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.
- checksums.yaml +4 -4
 - data/lib/jsonapi/utils/request.rb +76 -37
 - data/lib/jsonapi/utils/version.rb +1 -1
 - metadata +4 -4
 
    
        checksums.yaml
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            ---
         
     | 
| 
       2 
2 
     | 
    
         
             
            SHA1:
         
     | 
| 
       3 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       4 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 3 
     | 
    
         
            +
              metadata.gz: 9e79dcc432ea412e938f711f040d12c70cb5196f
         
     | 
| 
      
 4 
     | 
    
         
            +
              data.tar.gz: e34a24d48a769016f737c156311523935b4714a7
         
     | 
| 
       5 
5 
     | 
    
         
             
            SHA512:
         
     | 
| 
       6 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       7 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 6 
     | 
    
         
            +
              metadata.gz: 9b4222af57feada6a8c45b7385e8c889535e8000e229093e4053ce2812ef041aaf799216267e5105195282b027d516cf5b01db12204697f33d92c169a3b2ecd1
         
     | 
| 
      
 7 
     | 
    
         
            +
              data.tar.gz: 4accdb7433731907f5cd9121c8e8227d916dae0480e1decf56b727208b7ef1537ef765236c4058130c21f74e77e09aceefcc4f1ed18c3d9d152c200b0f773030
         
     | 
| 
         @@ -1,48 +1,87 @@ 
     | 
|
| 
       1 
     | 
    
         
            -
            module JSONAPI
         
     | 
| 
       2 
     | 
    
         
            -
              module  
     | 
| 
       3 
     | 
    
         
            -
                 
     | 
| 
       4 
     | 
    
         
            -
             
     | 
| 
       5 
     | 
    
         
            -
             
     | 
| 
       6 
     | 
    
         
            -
             
     | 
| 
       7 
     | 
    
         
            -
                   
     | 
| 
      
 1 
     | 
    
         
            +
            module JSONAPI::Utils
         
     | 
| 
      
 2 
     | 
    
         
            +
              module Request
         
     | 
| 
      
 3 
     | 
    
         
            +
                # Setup and check request before action gets actually evaluated.
         
     | 
| 
      
 4 
     | 
    
         
            +
                #
         
     | 
| 
      
 5 
     | 
    
         
            +
                # @api public
         
     | 
| 
      
 6 
     | 
    
         
            +
                def jsonapi_request_handling
         
     | 
| 
      
 7 
     | 
    
         
            +
                  setup_request
         
     | 
| 
      
 8 
     | 
    
         
            +
                  check_request
         
     | 
| 
      
 9 
     | 
    
         
            +
                end
         
     | 
| 
       8 
10 
     | 
    
         | 
| 
       9 
     | 
    
         
            -
             
     | 
| 
       10 
     | 
    
         
            -
             
     | 
| 
       11 
     | 
    
         
            -
             
     | 
| 
       12 
     | 
    
         
            -
             
     | 
| 
       13 
     | 
    
         
            -
             
     | 
| 
       14 
     | 
    
         
            -
             
     | 
| 
       15 
     | 
    
         
            -
             
     | 
| 
       16 
     | 
    
         
            -
             
     | 
| 
       17 
     | 
    
         
            -
             
     | 
| 
      
 11 
     | 
    
         
            +
                # Instantiate the request object.
         
     | 
| 
      
 12 
     | 
    
         
            +
                #
         
     | 
| 
      
 13 
     | 
    
         
            +
                # @return [JSONAPI::RequestParser]
         
     | 
| 
      
 14 
     | 
    
         
            +
                #
         
     | 
| 
      
 15 
     | 
    
         
            +
                # @api public
         
     | 
| 
      
 16 
     | 
    
         
            +
                def setup_request
         
     | 
| 
      
 17 
     | 
    
         
            +
                  @request ||= JSONAPI::RequestParser.new(
         
     | 
| 
      
 18 
     | 
    
         
            +
                    params,
         
     | 
| 
      
 19 
     | 
    
         
            +
                    context: context,
         
     | 
| 
      
 20 
     | 
    
         
            +
                    key_formatter: key_formatter,
         
     | 
| 
      
 21 
     | 
    
         
            +
                    server_error_callbacks: (self.class.server_error_callbacks || [])
         
     | 
| 
      
 22 
     | 
    
         
            +
                  )
         
     | 
| 
      
 23 
     | 
    
         
            +
                end
         
     | 
| 
       18 
24 
     | 
    
         | 
| 
       19 
     | 
    
         
            -
             
     | 
| 
       20 
     | 
    
         
            -
             
     | 
| 
       21 
     | 
    
         
            -
             
     | 
| 
      
 25 
     | 
    
         
            +
                # Render an error response if the parsed request got any error.
         
     | 
| 
      
 26 
     | 
    
         
            +
                #
         
     | 
| 
      
 27 
     | 
    
         
            +
                # @api public
         
     | 
| 
      
 28 
     | 
    
         
            +
                def check_request
         
     | 
| 
      
 29 
     | 
    
         
            +
                  @request.errors.blank? || jsonapi_render_errors(json: @request)
         
     | 
| 
      
 30 
     | 
    
         
            +
                end
         
     | 
| 
       22 
31 
     | 
    
         | 
| 
       23 
     | 
    
         
            -
             
     | 
| 
       24 
     | 
    
         
            -
             
     | 
| 
       25 
     | 
    
         
            -
             
     | 
| 
      
 32 
     | 
    
         
            +
                # Override the JSONAPI::ActsAsResourceController#process_request method.
         
     | 
| 
      
 33 
     | 
    
         
            +
                #
         
     | 
| 
      
 34 
     | 
    
         
            +
                # It might be removed when the following line on JR is fixed:
         
     | 
| 
      
 35 
     | 
    
         
            +
                # https://github.com/cerebris/jsonapi-resources/blob/release-0-8/lib/jsonapi/acts_as_resource_controller.rb#L62
         
     | 
| 
      
 36 
     | 
    
         
            +
                #
         
     | 
| 
      
 37 
     | 
    
         
            +
                # @return [String]
         
     | 
| 
      
 38 
     | 
    
         
            +
                #
         
     | 
| 
      
 39 
     | 
    
         
            +
                # @api public
         
     | 
| 
      
 40 
     | 
    
         
            +
                def process_request
         
     | 
| 
      
 41 
     | 
    
         
            +
                  process_operations
         
     | 
| 
      
 42 
     | 
    
         
            +
                  render_results(@operation_results)
         
     | 
| 
      
 43 
     | 
    
         
            +
                rescue => e
         
     | 
| 
      
 44 
     | 
    
         
            +
                  handle_exceptions(e)
         
     | 
| 
      
 45 
     | 
    
         
            +
                end
         
     | 
| 
       26 
46 
     | 
    
         | 
| 
       27 
     | 
    
         
            -
             
     | 
| 
       28 
     | 
    
         
            -
             
     | 
| 
       29 
     | 
    
         
            -
             
     | 
| 
      
 47 
     | 
    
         
            +
                # Helper to get params for the main resource.
         
     | 
| 
      
 48 
     | 
    
         
            +
                #
         
     | 
| 
      
 49 
     | 
    
         
            +
                # @return [Hash]
         
     | 
| 
      
 50 
     | 
    
         
            +
                #
         
     | 
| 
      
 51 
     | 
    
         
            +
                # @api public
         
     | 
| 
      
 52 
     | 
    
         
            +
                def resource_params
         
     | 
| 
      
 53 
     | 
    
         
            +
                  build_params_for(:resource)
         
     | 
| 
      
 54 
     | 
    
         
            +
                end
         
     | 
| 
      
 55 
     | 
    
         
            +
             
     | 
| 
      
 56 
     | 
    
         
            +
                # Helper to get params for relationship params.
         
     | 
| 
      
 57 
     | 
    
         
            +
                #
         
     | 
| 
      
 58 
     | 
    
         
            +
                # @return [Hash]
         
     | 
| 
      
 59 
     | 
    
         
            +
                #
         
     | 
| 
      
 60 
     | 
    
         
            +
                # @api public
         
     | 
| 
      
 61 
     | 
    
         
            +
                def relationship_params
         
     | 
| 
      
 62 
     | 
    
         
            +
                  build_params_for(:relationship)
         
     | 
| 
      
 63 
     | 
    
         
            +
                end
         
     | 
| 
       30 
64 
     | 
    
         | 
| 
       31 
     | 
    
         
            -
             
     | 
| 
      
 65 
     | 
    
         
            +
                private
         
     | 
| 
       32 
66 
     | 
    
         | 
| 
       33 
     | 
    
         
            -
             
     | 
| 
       34 
     | 
    
         
            -
             
     | 
| 
      
 67 
     | 
    
         
            +
                # Extract params from request and build a Hash with params
         
     | 
| 
      
 68 
     | 
    
         
            +
                # for either the main resource or relationships.
         
     | 
| 
      
 69 
     | 
    
         
            +
                #
         
     | 
| 
      
 70 
     | 
    
         
            +
                # @return [Hash]
         
     | 
| 
      
 71 
     | 
    
         
            +
                #
         
     | 
| 
      
 72 
     | 
    
         
            +
                # @api private
         
     | 
| 
      
 73 
     | 
    
         
            +
                def build_params_for(param_type)
         
     | 
| 
      
 74 
     | 
    
         
            +
                  return {} if @request.operations.empty?
         
     | 
| 
       35 
75 
     | 
    
         | 
| 
       36 
     | 
    
         
            -
             
     | 
| 
       37 
     | 
    
         
            -
             
     | 
| 
      
 76 
     | 
    
         
            +
                  keys = %i(attributes to_one to_many)
         
     | 
| 
      
 77 
     | 
    
         
            +
                  operation = @request.operations.find { |e| e.options[:data].keys & keys == keys }
         
     | 
| 
       38 
78 
     | 
    
         | 
| 
       39 
     | 
    
         
            -
             
     | 
| 
       40 
     | 
    
         
            -
             
     | 
| 
       41 
     | 
    
         
            -
             
     | 
| 
       42 
     | 
    
         
            -
             
     | 
| 
       43 
     | 
    
         
            -
             
     | 
| 
       44 
     | 
    
         
            -
             
     | 
| 
       45 
     | 
    
         
            -
                    end
         
     | 
| 
      
 79 
     | 
    
         
            +
                  if operation.nil?
         
     | 
| 
      
 80 
     | 
    
         
            +
                    {}
         
     | 
| 
      
 81 
     | 
    
         
            +
                  elsif param_type == :relationship
         
     | 
| 
      
 82 
     | 
    
         
            +
                    operation.options[:data].values_at(:to_one, :to_many).compact.reduce(&:merge)
         
     | 
| 
      
 83 
     | 
    
         
            +
                  else
         
     | 
| 
      
 84 
     | 
    
         
            +
                    operation.options[:data][:attributes]
         
     | 
| 
       46 
85 
     | 
    
         
             
                  end
         
     | 
| 
       47 
86 
     | 
    
         
             
                end
         
     | 
| 
       48 
87 
     | 
    
         
             
              end
         
     | 
    
        metadata
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            --- !ruby/object:Gem::Specification
         
     | 
| 
       2 
2 
     | 
    
         
             
            name: jsonapi-utils
         
     | 
| 
       3 
3 
     | 
    
         
             
            version: !ruby/object:Gem::Version
         
     | 
| 
       4 
     | 
    
         
            -
              version: 0.4. 
     | 
| 
      
 4 
     | 
    
         
            +
              version: 0.4.9
         
     | 
| 
       5 
5 
     | 
    
         
             
            platform: ruby
         
     | 
| 
       6 
6 
     | 
    
         
             
            authors:
         
     | 
| 
       7 
7 
     | 
    
         
             
            - Tiago Guedes
         
     | 
| 
         @@ -9,7 +9,7 @@ authors: 
     | 
|
| 
       9 
9 
     | 
    
         
             
            autorequire: 
         
     | 
| 
       10 
10 
     | 
    
         
             
            bindir: exe
         
     | 
| 
       11 
11 
     | 
    
         
             
            cert_chain: []
         
     | 
| 
       12 
     | 
    
         
            -
            date: 2017- 
     | 
| 
      
 12 
     | 
    
         
            +
            date: 2017-03-12 00:00:00.000000000 Z
         
     | 
| 
       13 
13 
     | 
    
         
             
            dependencies:
         
     | 
| 
       14 
14 
     | 
    
         
             
            - !ruby/object:Gem::Dependency
         
     | 
| 
       15 
15 
     | 
    
         
             
              name: jsonapi-resources
         
     | 
| 
         @@ -17,14 +17,14 @@ dependencies: 
     | 
|
| 
       17 
17 
     | 
    
         
             
                requirements:
         
     | 
| 
       18 
18 
     | 
    
         
             
                - - "~>"
         
     | 
| 
       19 
19 
     | 
    
         
             
                  - !ruby/object:Gem::Version
         
     | 
| 
       20 
     | 
    
         
            -
                    version: 0. 
     | 
| 
      
 20 
     | 
    
         
            +
                    version: 0.8.0
         
     | 
| 
       21 
21 
     | 
    
         
             
              type: :runtime
         
     | 
| 
       22 
22 
     | 
    
         
             
              prerelease: false
         
     | 
| 
       23 
23 
     | 
    
         
             
              version_requirements: !ruby/object:Gem::Requirement
         
     | 
| 
       24 
24 
     | 
    
         
             
                requirements:
         
     | 
| 
       25 
25 
     | 
    
         
             
                - - "~>"
         
     | 
| 
       26 
26 
     | 
    
         
             
                  - !ruby/object:Gem::Version
         
     | 
| 
       27 
     | 
    
         
            -
                    version: 0. 
     | 
| 
      
 27 
     | 
    
         
            +
                    version: 0.8.0
         
     | 
| 
       28 
28 
     | 
    
         
             
            - !ruby/object:Gem::Dependency
         
     | 
| 
       29 
29 
     | 
    
         
             
              name: bundler
         
     | 
| 
       30 
30 
     | 
    
         
             
              requirement: !ruby/object:Gem::Requirement
         
     |