otwcode/otwarchive

View on GitHub
app/models/story_parser.rb

Summary

Maintainability
F
6 days
Test Coverage

File story_parser.rb has 705 lines of code (exceeds 250 allowed). Consider refactoring.
Open

class StoryParser
  require 'timeout'
  require 'nokogiri'
  require 'mechanize'
  require 'open-uri'
Severity: Major
Found in app/models/story_parser.rb - About 1 day to fix

    Class StoryParser has 45 methods (exceeds 20 allowed). Consider refactoring.
    Open

    class StoryParser
      require 'timeout'
      require 'nokogiri'
      require 'mechanize'
      require 'open-uri'
    Severity: Minor
    Found in app/models/story_parser.rb - About 6 hrs to fix

      Method set_work_attributes has a Cognitive Complexity of 26 (exceeds 5 allowed). Consider refactoring.
      Open

        def set_work_attributes(work, location = "", options = {})
          raise Error, "Work could not be downloaded" if work.nil?
      
          @options = options
          work.imported_from_url = location
      Severity: Minor
      Found in app/models/story_parser.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 import_many has a Cognitive Complexity of 20 (exceeds 5 allowed). Consider refactoring.
      Open

        def import_many(urls, options = {})
          # Try to get the works
          works = []
          failed_urls = []
          errors = []
      Severity: Minor
      Found in app/models/story_parser.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 parse_common has a Cognitive Complexity of 19 (exceeds 5 allowed). Consider refactoring.
      Open

        def parse_common(story, location = nil, encoding = nil, detect_tags = true)
          work_params = { title: "Untitled Imported Work", chapter_attributes: { content: "" } }
      
          # Encode as HTML - the dummy "foo" tag will be stripped out by the sanitizer but forces Nokogiri to
          # preserve line breaks in plain text documents
      Severity: Minor
      Found in app/models/story_parser.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 set_work_attributes has 60 lines of code (exceeds 25 allowed). Consider refactoring.
      Open

        def set_work_attributes(work, location = "", options = {})
          raise Error, "Work could not be downloaded" if work.nil?
      
          @options = options
          work.imported_from_url = location
      Severity: Major
      Found in app/models/story_parser.rb - About 2 hrs to fix

        Method import_from_urls has a Cognitive Complexity of 15 (exceeds 5 allowed). Consider refactoring.
        Open

          def import_from_urls(urls, options = {})
            # Try to get the works
            works = []
            failed_urls = []
            errors = []
        Severity: Minor
        Found in app/models/story_parser.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 parse_story_from_deviantart has a Cognitive Complexity of 15 (exceeds 5 allowed). Consider refactoring.
        Open

          def parse_story_from_deviantart(_story, detect_tags = true)
            work_params = { chapter_attributes: {} }
            storytext = ""
            notes = ""
        
        
        Severity: Minor
        Found in app/models/story_parser.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 parse_story_from_deviantart has 42 lines of code (exceeds 25 allowed). Consider refactoring.
        Open

          def parse_story_from_deviantart(_story, detect_tags = true)
            work_params = { chapter_attributes: {} }
            storytext = ""
            notes = ""
        
        
        Severity: Minor
        Found in app/models/story_parser.rb - About 1 hr to fix

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

            def download_chaptered_from_efiction(location)
              chapter_contents = []
              if location.match(/^(?<site>.*)\/.*viewstory\.php.*sid=(?<storyid>\d+)($|&)/i)
                site = Regexp.last_match[:site]
                storyid = Regexp.last_match[:storyid]
          Severity: Minor
          Found in app/models/story_parser.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 import_many has 31 lines of code (exceeds 25 allowed). Consider refactoring.
          Open

            def import_many(urls, options = {})
              # Try to get the works
              works = []
              failed_urls = []
              errors = []
          Severity: Minor
          Found in app/models/story_parser.rb - About 1 hr to fix

            Method scan_text_for_meta has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring.
            Open

              def scan_text_for_meta(text, detect_tags = true)
                # break up the text with some extra newlines to make matching more likely
                # and strip out some tags
                text = text.gsub(/<br/, "\n<br")
                text.gsub!(/<p/, "\n<p")
            Severity: Minor
            Found in app/models/story_parser.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 meta_or_default has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring.
            Open

              def meta_or_default(detected_field, provided_field, default = nil)
                if @options[:override_tags] || detected_field.blank?
                  if provided_field.blank?
                    detected_field.blank? ? default : detected_field
                  else
            Severity: Minor
            Found in app/models/story_parser.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 scan_text_for_meta has 27 lines of code (exceeds 25 allowed). Consider refactoring.
            Open

              def scan_text_for_meta(text, detect_tags = true)
                # break up the text with some extra newlines to make matching more likely
                # and strip out some tags
                text = text.gsub(/<br/, "\n<br")
                text.gsub!(/<p/, "\n<p")
            Severity: Minor
            Found in app/models/story_parser.rb - About 1 hr to fix

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

                def import_from_urls(urls, options = {})
                  # Try to get the works
                  works = []
                  failed_urls = []
                  errors = []
              Severity: Minor
              Found in app/models/story_parser.rb - About 1 hr to fix

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

                  def download_with_timeout(location, limit = 10)
                    story = ""
                    Timeout.timeout(STORY_DOWNLOAD_TIMEOUT) do
                      begin
                        # we do a little cleanup here in case the user hasn't included the 'http://'
                Severity: Minor
                Found in app/models/story_parser.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 download_from_lj has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
                Open

                  def download_from_lj(location)
                    url = location
                    url.gsub!(/\#(.*)$/, "") # strip off any anchor information
                    url.gsub!(/\?(.*)$/, "") # strip off any existing params at the end
                    url.gsub!('_', '-') # convert underscores in usernames to hyphens
                Severity: Minor
                Found in app/models/story_parser.rb - About 45 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 process_warnings has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
                Open

                  def process_warnings(warning_string, freeform_string)
                    result = {
                        archive_warning_string: warning_string,
                        freeform_string: freeform_string
                    }
                Severity: Minor
                Found in app/models/story_parser.rb - About 45 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

                Consider simplifying this complex logical expression.
                Open

                          if body.nil? || body_to_check == last_body || chapnum > MAX_CHAPTER_COUNT || body.match(/<div class='chaptertitle'> by <\/div>/) || body.match(/Access denied./) || body.match(/Chapter : /)
                            break
                          end
                Severity: Major
                Found in app/models/story_parser.rb - About 40 mins to fix

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

                    def get_collection_names(collection_string)
                      collections = ""
                      collection_string.split(',').map(&:squish).each do |collection_name|
                        collection = Collection.find_by(name: collection_name) || Collection.find_by(title: collection_name)
                        if collection
                  Severity: Minor
                  Found in app/models/story_parser.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 parse_story_from_unknown has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
                  Open

                    def parse_story_from_unknown(story, detect_tags = true)
                      work_params = { chapter_attributes: {} }
                      story_head = ""
                      story_head = @doc.css("head").inner_html if @doc.css("head")
                  
                  
                  Severity: Minor
                  Found in app/models/story_parser.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 parse_author_from_lj has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
                  Open

                    def parse_author_from_lj(location)
                      return if location !~ %r{^(?:http:\/\/)?(?<lj_name>[^.]*).(?<site_name>livejournal\.com|dreamwidth\.org|insanejournal\.com|journalfen.net)}
                      email = ""
                      lj_name = Regexp.last_match[:lj_name]
                      site_name = Regexp.last_match[:site_name]
                  Severity: Minor
                  Found in app/models/story_parser.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

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

                    def parse_author(location, ext_author_name, ext_author_email)
                      if location.present? && ext_author_name.blank? && ext_author_email.blank?
                        source = get_source_if_known(KNOWN_AUTHOR_PARSERS, location)
                        if source.nil?
                          raise Error, "No external author name or email specified"
                  Severity: Minor
                  Found in app/models/story_parser.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

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

                    def parse_author_common(email, name)
                      errors = []
                  
                      errors << "No author name specified" if name.blank?
                  
                  
                  Severity: Minor
                  Found in app/models/story_parser.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

                          if work && work.save
                            work.chapters.each(&:save)
                            works << work
                          else
                            failed_urls << url
                  Severity: Minor
                  Found in app/models/story_parser.rb and 1 other location - About 25 mins to fix
                  app/models/story_parser.rb on lines 86..93

                  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 29.

                  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 work && work.save
                              work.chapters.each(&:save)
                              works << work
                            else
                              failed_urls << url
                  Severity: Minor
                  Found in app/models/story_parser.rb and 1 other location - About 25 mins to fix
                  app/models/story_parser.rb on lines 176..183

                  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 29.

                  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