openjaf/cenit

View on GitHub
app/controllers/api/v1/api_controller.rb

Summary

Maintainability
F
5 days
Test Coverage

File api_controller.rb has 446 lines of code (exceeds 250 allowed). Consider refactoring.
Open

module Api::V1
  class ApiController < ApplicationController
    before_action :authorize_account, :save_request_data, except: [:new_user, :cors_check, :auth]
    before_action :find_item, only: [:show, :destroy, :pull, :run]
    before_action :authorize_action, except: [:auth, :new_user, :cors_check, :push]
Severity: Minor
Found in app/controllers/api/v1/api_controller.rb - About 6 hrs to fix

    Method index has a Cognitive Complexity of 38 (exceeds 5 allowed). Consider refactoring.
    Open

        def index
          if klass
            @items =
              if @criteria.present?
                if (sort_key = @criteria.delete(:sort_by))
    Severity: Minor
    Found in app/controllers/api/v1/api_controller.rb - About 5 hrs to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Method new_user has a Cognitive Complexity of 31 (exceeds 5 allowed). Consider refactoring.
    Open

        def new_user
          data = (JSON.parse(@webhook_body) rescue {}).keep_if { |key, _| %w(email password password_confirmation token code).include?(key) }
          data = data.with_indifferent_access
          data.reverse_merge!(email: params[:email], password: pwd = params[:password], password_confirmation: params[:password_confirmation] || pwd)
          status = :not_acceptable
    Severity: Minor
    Found in app/controllers/api/v1/api_controller.rb - About 4 hrs to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Method new has a Cognitive Complexity of 24 (exceeds 5 allowed). Consider refactoring.
    Open

        def new
          response =
            {
              success: success_report = {},
              errors: broken_report = {}
    Severity: Minor
    Found in app/controllers/api/v1/api_controller.rb - About 3 hrs to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Method push has a Cognitive Complexity of 20 (exceeds 5 allowed). Consider refactoring.
    Open

        def push
          response =
            {
              success: success_report = Hash.new { |h, k| h[k] = [] },
              errors: broken_report = Hash.new { |h, k| h[k] = [] }
    Severity: Minor
    Found in app/controllers/api/v1/api_controller.rb - About 2 hrs to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Method authorize_account has a Cognitive Complexity of 20 (exceeds 5 allowed). Consider refactoring.
    Open

        def authorize_account
          user = nil
    
          # New key and token params.
          key = request.headers['X-Tenant-Access-Key'] || params.delete('X-Tenant-Access-Key')
    Severity: Minor
    Found in app/controllers/api/v1/api_controller.rb - About 2 hrs to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Class ApiController has 24 methods (exceeds 20 allowed). Consider refactoring.
    Open

      class ApiController < ApplicationController
        before_action :authorize_account, :save_request_data, except: [:new_user, :cors_check, :auth]
        before_action :find_item, only: [:show, :destroy, :pull, :run]
        before_action :authorize_action, except: [:auth, :new_user, :cors_check, :push]
        rescue_from Exception, with: :exception_handler
    Severity: Minor
    Found in app/controllers/api/v1/api_controller.rb - About 2 hrs to fix

      Method new_user has 40 lines of code (exceeds 25 allowed). Consider refactoring.
      Open

          def new_user
            data = (JSON.parse(@webhook_body) rescue {}).keep_if { |key, _| %w(email password password_confirmation token code).include?(key) }
            data = data.with_indifferent_access
            data.reverse_merge!(email: params[:email], password: pwd = params[:password], password_confirmation: params[:password_confirmation] || pwd)
            status = :not_acceptable
      Severity: Minor
      Found in app/controllers/api/v1/api_controller.rb - About 1 hr to fix

        Method index has 38 lines of code (exceeds 25 allowed). Consider refactoring.
        Open

            def index
              if klass
                @items =
                  if @criteria.present?
                    if (sort_key = @criteria.delete(:sort_by))
        Severity: Minor
        Found in app/controllers/api/v1/api_controller.rb - About 1 hr to fix

          Method authorize_action has a Cognitive Complexity of 12 (exceeds 5 allowed). Consider refactoring.
          Open

              def authorize_action
                if klass
                  action_symbol =
                    case @_action_name
                    when 'push'
          Severity: Minor
          Found in app/controllers/api/v1/api_controller.rb - About 1 hr to fix

          Cognitive Complexity

          Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

          A method's cognitive complexity is based on a few simple rules:

          • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
          • Code is considered more complex for each "break in the linear flow of the code"
          • Code is considered more complex when "flow breaking structures are nested"

          Further reading

          Method new has 33 lines of code (exceeds 25 allowed). Consider refactoring.
          Open

              def new
                response =
                  {
                    success: success_report = {},
                    errors: broken_report = {}
          Severity: Minor
          Found in app/controllers/api/v1/api_controller.rb - About 1 hr to fix

            Method authorize_account has 30 lines of code (exceeds 25 allowed). Consider refactoring.
            Open

                def authorize_account
                  user = nil
            
                  # New key and token params.
                  key = request.headers['X-Tenant-Access-Key'] || params.delete('X-Tenant-Access-Key')
            Severity: Minor
            Found in app/controllers/api/v1/api_controller.rb - About 1 hr to fix

              Method push has 30 lines of code (exceeds 25 allowed). Consider refactoring.
              Open

                  def push
                    response =
                      {
                        success: success_report = Hash.new { |h, k| h[k] = [] },
                        errors: broken_report = Hash.new { |h, k| h[k] = [] }
              Severity: Minor
              Found in app/controllers/api/v1/api_controller.rb - About 1 hr to fix

                Method save_request_data has 26 lines of code (exceeds 25 allowed). Consider refactoring.
                Open

                    def save_request_data
                      @data_types ||= {}
                      @request_id = request.uuid
                      @webhook_body = request.body.read
                      @ns_slug = params[:ns]
                Severity: Minor
                Found in app/controllers/api/v1/api_controller.rb - About 1 hr to fix

                  Method show has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring.
                  Open

                      def show
                        if @item.orm_model.data_type.is_a?(Setup::FileDataType)
                          send_data @item.data, filename: @item[:filename], type: @item[:contentType]
                        else
                          option = {}
                  Severity: Minor
                  Found in app/controllers/api/v1/api_controller.rb - About 55 mins to fix

                  Cognitive Complexity

                  Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                  A method's cognitive complexity is based on a few simple rules:

                  • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                  • Code is considered more complex for each "break in the linear flow of the code"
                  • Code is considered more complex when "flow breaking structures are nested"

                  Further reading

                  Method pull has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
                  Open

                      def pull
                        if @item.is_a?(Setup::CrossSharedCollection)
                          begin
                            pull_request = @webhook_body.present? ? JSON.parse(@webhook_body) : {}
                            render json: @item.pull(pull_request).to_json
                  Severity: Minor
                  Found in app/controllers/api/v1/api_controller.rb - About 35 mins to fix

                  Cognitive Complexity

                  Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                  A method's cognitive complexity is based on a few simple rules:

                  • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                  • Code is considered more complex for each "break in the linear flow of the code"
                  • Code is considered more complex when "flow breaking structures are nested"

                  Further reading

                  Method each_root has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
                  Open

                        def each_root(&block)
                          if (roots = Nokogiri::XML::DocumentFragment.parse(config[:message]).element_children)
                            roots.each do |root|
                              if (elements = root.element_children)
                                elements.each { |e| block.call(root.name, e) }
                  Severity: Minor
                  Found in app/controllers/api/v1/api_controller.rb - About 35 mins to fix

                  Cognitive Complexity

                  Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                  A method's cognitive complexity is based on a few simple rules:

                  • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                  • Code is considered more complex for each "break in the linear flow of the code"
                  • Code is considered more complex when "flow breaking structures are nested"

                  Further reading

                  Method save_request_data has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
                  Open

                      def save_request_data
                        @data_types ||= {}
                        @request_id = request.uuid
                        @webhook_body = request.body.read
                        @ns_slug = params[:ns]
                  Severity: Minor
                  Found in app/controllers/api/v1/api_controller.rb - About 25 mins to fix

                  Cognitive Complexity

                  Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                  A method's cognitive complexity is based on a few simple rules:

                  • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                  • Code is considered more complex for each "break in the linear flow of the code"
                  • Code is considered more complex when "flow breaking structures are nested"

                  Further reading

                  Identical blocks of code found in 2 locations. Consider refactoring.
                  Open

                          elsif data[:email]
                            data[:password] = Devise.friendly_token unless data[:password]
                            data[:password_confirmation] = data[:password] unless data[:password_confirmation]
                            if (user = User.new(data)).valid?(context: :create)
                              if (captcha_token = CaptchaToken.create(email: data[:email], data: data)).errors.blank?
                  Severity: Major
                  Found in app/controllers/api/v1/api_controller.rb and 1 other location - About 1 hr to fix
                  app/controllers/api/v2/api_controller.rb on lines 310..325

                  Duplicated Code

                  Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                  Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                  When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                  Tuning

                  This issue has a mass of 69.

                  We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                  The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                  If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                  See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                  Refactorings

                  Further Reading

                  Identical blocks of code found in 2 locations. Consider refactoring.
                  Open

                      def pull
                        if @item.is_a?(Setup::CrossSharedCollection)
                          begin
                            pull_request = @webhook_body.present? ? JSON.parse(@webhook_body) : {}
                            render json: @item.pull(pull_request).to_json
                  Severity: Minor
                  Found in app/controllers/api/v1/api_controller.rb and 1 other location - About 55 mins to fix
                  app/controllers/api/v2/api_controller.rb on lines 222..232

                  Duplicated Code

                  Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                  Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                  When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                  Tuning

                  This issue has a mass of 44.

                  We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                  The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                  If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                  See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                  Refactorings

                  Further Reading

                  Identical blocks of code found in 2 locations. Consider refactoring.
                  Open

                              if @ns_name.nil?
                                ns = Setup::Namespace.where(slug: @ns_slug).first
                                @ns_name = (ns && ns.name) || ''
                              end
                              if @ns_name
                  Severity: Minor
                  Found in app/controllers/api/v1/api_controller.rb and 1 other location - About 50 mins to fix
                  app/controllers/api/v2/api_controller.rb on lines 545..553

                  Duplicated Code

                  Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                  Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                  When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                  Tuning

                  This issue has a mass of 43.

                  We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                  The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                  If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                  See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                  Refactorings

                  Further Reading

                  Identical blocks of code found in 2 locations. Consider refactoring.
                  Open

                      def auth
                        authorize_account
                        if Account.current
                          self.cors_header
                          render json: { status: "Sucess Auth" }, status: 200
                  Severity: Minor
                  Found in app/controllers/api/v1/api_controller.rb and 1 other location - About 20 mins to fix
                  app/controllers/api/v2/api_controller.rb on lines 267..275

                  Duplicated Code

                  Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                  Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                  When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                  Tuning

                  This issue has a mass of 28.

                  We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                  The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                  If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                  See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                  Refactorings

                  Further Reading

                  There are no issues that match your filters.

                  Category
                  Status