ikuseiGmbH/smart-village-app-cms

View on GitHub
app/services/api_request_service.rb

Summary

Maintainability
A
2 hrs
Test Coverage

Method has too many lines. [24/10]
Open

  def get_request(content_type_xml = false, pem = nil)
    uri = Addressable::URI.parse(@uri.strip).normalize

    uri.query = [uri.query, URI.encode_www_form(@params)].join("&") if @params&.any?

Severity: Minor
Found in app/services/api_request_service.rb by rubocop

This cop checks if the length of a method exceeds some maximum value. Comment lines can optionally be ignored. The maximum allowed length is configurable.

Assignment Branch Condition size for get_request is too high. [29.63/15]
Open

  def get_request(content_type_xml = false, pem = nil)
    uri = Addressable::URI.parse(@uri.strip).normalize

    uri.query = [uri.query, URI.encode_www_form(@params)].join("&") if @params&.any?

Severity: Minor
Found in app/services/api_request_service.rb by rubocop

This cop checks that the ABC size of methods is not higher than the configured maximum. The ABC size is based on assignments, branches (method calls), and conditions. See http://c2.com/cgi/wiki?AbcMetric and https://en.wikipedia.org/wiki/ABC_Software_Metric.

Method has too many lines. [17/10]
Open

  def post_request
    url = Addressable::URI.parse(@uri.strip).normalize
    http = Net::HTTP.new(url.host, url.port || 80)

    if @uri.include?("https://")
Severity: Minor
Found in app/services/api_request_service.rb by rubocop

This cop checks if the length of a method exceeds some maximum value. Comment lines can optionally be ignored. The maximum allowed length is configurable.

Complex method ApiRequestService#get_request (44.4)
Open

  def get_request(content_type_xml = false, pem = nil)
    uri = Addressable::URI.parse(@uri.strip).normalize

    uri.query = [uri.query, URI.encode_www_form(@params)].join("&") if @params&.any?

Severity: Minor
Found in app/services/api_request_service.rb by flog

Flog calculates the ABC score for methods. The ABC score is based on assignments, branches (method calls), and conditions.

You can read more about ABC metrics or the flog tool

Assignment Branch Condition size for post_request is too high. [20.83/15]
Open

  def post_request
    url = Addressable::URI.parse(@uri.strip).normalize
    http = Net::HTTP.new(url.host, url.port || 80)

    if @uri.include?("https://")
Severity: Minor
Found in app/services/api_request_service.rb by rubocop

This cop checks that the ABC size of methods is not higher than the configured maximum. The ABC size is based on assignments, branches (method calls), and conditions. See http://c2.com/cgi/wiki?AbcMetric and https://en.wikipedia.org/wiki/ABC_Software_Metric.

Cyclomatic complexity for get_request is too high. [8/6]
Open

  def get_request(content_type_xml = false, pem = nil)
    uri = Addressable::URI.parse(@uri.strip).normalize

    uri.query = [uri.query, URI.encode_www_form(@params)].join("&") if @params&.any?

Severity: Minor
Found in app/services/api_request_service.rb by rubocop

This cop checks that the cyclomatic complexity of methods is not higher than the configured maximum. The cyclomatic complexity is the number of linearly independent paths through a method. The algorithm counts decision points and adds one.

An if statement (or unless or ?:) increases the complexity by one. An else branch does not, since it doesn't add a decision point. The && operator (or keyword and) can be converted to a nested if statement, and ||/or is shorthand for a sequence of ifs, so they also add one. Loops can be said to have an exit condition, so they add one.

Perceived complexity for get_request is too high. [8/7]
Open

  def get_request(content_type_xml = false, pem = nil)
    uri = Addressable::URI.parse(@uri.strip).normalize

    uri.query = [uri.query, URI.encode_www_form(@params)].join("&") if @params&.any?

Severity: Minor
Found in app/services/api_request_service.rb by rubocop

This cop tries to produce a complexity score that's a measure of the complexity the reader experiences when looking at a method. For that reason it considers when nodes as something that doesn't add as much complexity as an if or a &&. Except if it's one of those special case/when constructs where there's no expression after case. Then the cop treats it as an if/elsif/elsif... and lets all the when nodes count. In contrast to the CyclomaticComplexity cop, this cop considers else nodes as adding complexity.

Example:

def my_method                   # 1
  if cond                       # 1
    case var                    # 2 (0.8 + 4 * 0.2, rounded)
    when 1 then func_one
    when 2 then func_two
    when 3 then func_three
    when 4..10 then func_other
    end
  else                          # 1
    do_something until a && b   # 2
  end                           # ===
end                             # 7 complexity points

Method get_request has a Cognitive Complexity of 13 (exceeds 5 allowed). Consider refactoring.
Open

  def get_request(content_type_xml = false, pem = nil)
    uri = Addressable::URI.parse(@uri.strip).normalize

    uri.query = [uri.query, URI.encode_www_form(@params)].join("&") if @params&.any?

Severity: Minor
Found in app/services/api_request_service.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

Complex method ApiRequestService#post_request (27.2)
Open

  def post_request
    url = Addressable::URI.parse(@uri.strip).normalize
    http = Net::HTTP.new(url.host, url.port || 80)

    if @uri.include?("https://")
Severity: Minor
Found in app/services/api_request_service.rb by flog

Flog calculates the ABC score for methods. The ABC score is based on assignments, branches (method calls), and conditions.

You can read more about ABC metrics or the flog tool

ApiRequestService#post_request has approx 11 statements
Open

  def post_request
Severity: Minor
Found in app/services/api_request_service.rb by reek

A method with Too Many Statements is any method that has a large number of lines.

Too Many Statements warns about any method that has more than 5 statements. Reek's smell detector for Too Many Statements counts +1 for every simple statement in a method and +1 for every statement within a control structure (if, else, case, when, for, while, until, begin, rescue) but it doesn't count the control structure itself.

So the following method would score +6 in Reek's statement-counting algorithm:

def parse(arg, argv, &error)
  if !(val = arg) and (argv.empty? or /\A-/ =~ (val = argv[0]))
    return nil, block, nil                                         # +1
  end
  opt = (val = parse_arg(val, &error))[1]                          # +2
  val = conv_arg(*val)                                             # +3
  if opt and !arg
    argv.shift                                                     # +4
  else
    val[0] = nil                                                   # +5
  end
  val                                                              # +6
end

(You might argue that the two assigments within the first @if@ should count as statements, and that perhaps the nested assignment should count as +2.)

ApiRequestService#get_request is controlled by argument 'content_type_xml'
Open

    if content_type_xml
Severity: Minor
Found in app/services/api_request_service.rb by reek

Control Parameter is a special case of Control Couple

Example

A simple example would be the "quoted" parameter in the following method:

def write(quoted)
  if quoted
    write_quoted @value
  else
    write_unquoted @value
  end
end

Fixing those problems is out of the scope of this document but an easy solution could be to remove the "write" method alltogether and to move the calls to "writequoted" / "writeunquoted" in the initial caller of "write".

ApiRequestService has at least 6 instance variables
Open

class ApiRequestService
Severity: Minor
Found in app/services/api_request_service.rb by reek

Too Many Instance Variables is a special case of LargeClass.

Example

Given this configuration

TooManyInstanceVariables:
  max_instance_variables: 3

and this code:

class TooManyInstanceVariables
  def initialize
    @arg_1 = :dummy
    @arg_2 = :dummy
    @arg_3 = :dummy
    @arg_4 = :dummy
  end
end

Reek would emit the following warning:

test.rb -- 5 warnings:
  [1]:TooManyInstanceVariables has at least 4 instance variables (TooManyInstanceVariables)

ApiRequestService#get_request has boolean parameter 'content_type_xml'
Open

  def get_request(content_type_xml = false, pem = nil)
Severity: Minor
Found in app/services/api_request_service.rb by reek

Boolean Parameter is a special case of Control Couple, where a method parameter is defaulted to true or false. A Boolean Parameter effectively permits a method's caller to decide which execution path to take. This is a case of bad cohesion. You're creating a dependency between methods that is not really necessary, thus increasing coupling.

Example

Given

class Dummy
  def hit_the_switch(switch = true)
    if switch
      puts 'Hitting the switch'
      # do other things...
    else
      puts 'Not hitting the switch'
      # do other things...
    end
  end
end

Reek would emit the following warning:

test.rb -- 3 warnings:
  [1]:Dummy#hit_the_switch has boolean parameter 'switch' (BooleanParameter)
  [2]:Dummy#hit_the_switch is controlled by argument switch (ControlParameter)

Note that both smells are reported, Boolean Parameter and Control Parameter.

Getting rid of the smell

This is highly dependent on your exact architecture, but looking at the example above what you could do is:

  • Move everything in the if branch into a separate method
  • Move everything in the else branch into a separate method
  • Get rid of the hit_the_switch method alltogether
  • Make the decision what method to call in the initial caller of hit_the_switch

ApiRequestService#get_request has approx 16 statements
Open

  def get_request(content_type_xml = false, pem = nil)
Severity: Minor
Found in app/services/api_request_service.rb by reek

A method with Too Many Statements is any method that has a large number of lines.

Too Many Statements warns about any method that has more than 5 statements. Reek's smell detector for Too Many Statements counts +1 for every simple statement in a method and +1 for every statement within a control structure (if, else, case, when, for, while, until, begin, rescue) but it doesn't count the control structure itself.

So the following method would score +6 in Reek's statement-counting algorithm:

def parse(arg, argv, &error)
  if !(val = arg) and (argv.empty? or /\A-/ =~ (val = argv[0]))
    return nil, block, nil                                         # +1
  end
  opt = (val = parse_arg(val, &error))[1]                          # +2
  val = conv_arg(*val)                                             # +3
  if opt and !arg
    argv.shift                                                     # +4
  else
    val[0] = nil                                                   # +5
  end
  val                                                              # +6
end

(You might argue that the two assigments within the first @if@ should count as statements, and that perhaps the nested assignment should count as +2.)

Method initialize has 5 arguments (exceeds 4 allowed). Consider refactoring.
Open

  def initialize(uri, login = nil, password = nil, params = {}, headers = {})
Severity: Minor
Found in app/services/api_request_service.rb - About 35 mins to fix

    ApiRequestService#post_request calls 'url.port' 2 times
    Open

        http = Net::HTTP.new(url.host, url.port || 80)
    
        if @uri.include?("https://")
          http = Net::HTTP.new(url.hostname, url.port || 443)
    Severity: Minor
    Found in app/services/api_request_service.rb by reek

    Duplication occurs when two fragments of code look nearly identical, or when two fragments of code have nearly identical effects at some conceptual level.

    Reek implements a check for Duplicate Method Call.

    Example

    Here's a very much simplified and contrived example. The following method will report a warning:

    def double_thing()
      @other.thing + @other.thing
    end

    One quick approach to silence Reek would be to refactor the code thus:

    def double_thing()
      thing = @other.thing
      thing + thing
    end

    A slightly different approach would be to replace all calls of double_thing by calls to @other.double_thing:

    class Other
      def double_thing()
        thing + thing
      end
    end

    The approach you take will depend on balancing other factors in your code.

    ApiRequestService has no descriptive comment
    Open

    class ApiRequestService
    Severity: Minor
    Found in app/services/api_request_service.rb by reek

    Classes and modules are the units of reuse and release. It is therefore considered good practice to annotate every class and module with a brief comment outlining its responsibilities.

    Example

    Given

    class Dummy
      # Do things...
    end

    Reek would emit the following warning:

    test.rb -- 1 warning:
      [1]:Dummy has no descriptive comment (IrresponsibleModule)

    Fixing this is simple - just an explaining comment:

    # The Dummy class is responsible for ...
    class Dummy
      # Do things...
    end

    ApiRequestService#get_request calls 'uri.hostname' 2 times
    Open

        http = Net::HTTP.new(uri.hostname, uri.port)
    
        if @uri.include?("https://")
          http = Net::HTTP.new(uri.hostname, 443)
    Severity: Minor
    Found in app/services/api_request_service.rb by reek

    Duplication occurs when two fragments of code look nearly identical, or when two fragments of code have nearly identical effects at some conceptual level.

    Reek implements a check for Duplicate Method Call.

    Example

    Here's a very much simplified and contrived example. The following method will report a warning:

    def double_thing()
      @other.thing + @other.thing
    end

    One quick approach to silence Reek would be to refactor the code thus:

    def double_thing()
      thing = @other.thing
      thing + thing
    end

    A slightly different approach would be to replace all calls of double_thing by calls to @other.double_thing:

    class Other
      def double_thing()
        thing + thing
      end
    end

    The approach you take will depend on balancing other factors in your code.

    Missing top-level class documentation comment.
    Open

    class ApiRequestService
    Severity: Minor
    Found in app/services/api_request_service.rb by rubocop

    This cop checks for missing top-level documentation of classes and modules. Classes with no body are exempt from the check and so are namespace modules - modules that have nothing in their bodies except classes, other modules, or constant definitions.

    The documentation requirement is annulled if the class or module has a "#:nodoc:" comment next to it. Likewise, "#:nodoc: all" does the same for all its children.

    Example:

    # bad
    class Person
      # ...
    end
    
    # good
    # Description/Explanation of Person class
    class Person
      # ...
    end

    There are no issues that match your filters.

    Category
    Status