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'
Class StoryParser
has 45 methods (exceeds 20 allowed). Consider refactoring. Open
class StoryParser
require 'timeout'
require 'nokogiri'
require 'mechanize'
require 'open-uri'
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
- Read upRead up
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 = []
- Read upRead up
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
- Read upRead up
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
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 = []
- Read upRead up
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 = ""
- Read upRead up
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 = ""
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]
- Read upRead up
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 = []
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")
- Read upRead up
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
- Read upRead up
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")
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 = []
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://'
- Read upRead up
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
- Read upRead up
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
}
- Read upRead up
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
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
- Read upRead up
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")
- Read upRead up
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]
- Read upRead up
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"
- Read upRead up
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?
- Read upRead up
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
- Read upRead up
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
- Extract Method
- Extract Class
- Form Template Method
- Introduce Null Object
- Pull Up Method
- Pull Up Field
- Substitute Algorithm
Further Reading
- Don't Repeat Yourself on the C2 Wiki
- Duplicated Code on SourceMaking
- Refactoring: Improving the Design of Existing Code by Martin Fowler. Duplicated Code, p76
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
- Read upRead up
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
- Extract Method
- Extract Class
- Form Template Method
- Introduce Null Object
- Pull Up Method
- Pull Up Field
- Substitute Algorithm
Further Reading
- Don't Repeat Yourself on the C2 Wiki
- Duplicated Code on SourceMaking
- Refactoring: Improving the Design of Existing Code by Martin Fowler. Duplicated Code, p76