Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ivan Shishkov #21

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Binary file not shown.
Binary file not shown.
62 changes: 62 additions & 0 deletions Shishkov_Ivan/0/Main.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
require "roo"

require "roo-xls"

require "./WorkWithData.rb"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use snake case for file naming


funcs = FilesParse.new
months = {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make Constant

'январь' => 1,
'февраль' => 2,
'март' => 3,
'апрель' => 4,
'май' => 5,
'июнь' => 6,
'июль' => 7,
'авуст' => 8,
'сентябрь' => 9,
'октябрь' => 10,
'ноябрь' => 11,
'декабрь' => 12,
}

regions = ['Brest',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make Constant

'Vitebsk',
'Gomel',
'Grodno',
'Minsk',

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/AlignArray: Align the elements of an array literal if they span more than one line.
Layout/TrailingWhitespace: Trailing whitespace detected.

'Grodno Region',

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/AlignArray: Align the elements of an array literal if they span more than one line.

'Mogilyov']

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/AlignArray: Align the elements of an array literal if they span more than one line.

products = Hash.new
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{}

file_Paths = Dir['./data/*']
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use_snake_case


file_Paths.each{ |file_Path|
file = funcs.get_file(file_Path)

if file

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/IfUnlessModifier: Favor modifier if usage when having a single-line body. Another good alternative is the usage of control flow &&/||.

funcs.fetch_products_data(file, products, regions)
end
}
loop {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/BlockDelimiters: Avoid using {...} for multi-line blocks.

puts 'What price are you looking for?'
word = gets.chomp.downcase
keys = funcs.find_keys(word, products)
if keys.length == 0

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/NumericPredicate: Use keys.length.zero? instead of keys.length == 0.
Style/ZeroLengthPredicate: Use empty? instead of length == 0.

puts word.capitalize + ' can not be found in database'
else
keys.each{|key|

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/SpaceBeforeBlockBraces: Space missing to the left of {.
Layout/SpaceInsideBlockBraces: Space between { and | missing.
Style/BlockDelimiters: Avoid using {...} for multi-line blocks.

puts ''
recent_Price_Data = funcs.get_recent_price_data(key, products, months)
puts key.capitalize + ' is ' + recent_Price_Data['price'].to_s + ' BYN in Grodno these days.'
min_Price = funcs.get_min_price(products[key])

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Naming/VariableName: Use snake_case for variable names.

puts 'Lowest was on ' + min_Price['year'] +
'/' + months[min_Price['month']].to_s +

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/MultilineOperationIndentation: Align the operands of an expression spanning multiple lines.

' at price ' + min_Price['price'].to_s + ' BYN'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/MultilineOperationIndentation: Align the operands of an expression spanning multiple lines.

max_Price = funcs.get_max_price(products[key])
puts 'Maximum was on ' + max_Price['year'] +
'/' + months[max_Price['month']].to_s +
' at price ' + max_Price['price'].to_s + ' BYN'
}
end
puts
}
178 changes: 178 additions & 0 deletions Shishkov_Ivan/0/WorkWithData.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
class FilesParse

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/EndOfLine: Carriage return character detected.
Metrics/ClassLength: Class has too many lines. [157/100]


attr_accessor :current_month,
:current_year
def initialize
puts 'work'
@current_month = Time.now.strftime('%m')
@current_year = Time.now.strftime('%Y').to_s
end

def get_mount(month, monthNumber)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Naming/UncommunicativeMethodParamName: Only use lowercase characters for method parameter.
Naming/VariableName: Use snake_case for variable names.

current_months = @current_month
if monthNumber == @current_month

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/IfUnlessModifier: Favor modifier if usage when having a single-line body. Another good alternative is the usage of control flow &&/||.

return current_months = month

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Naming/VariableName: Use snake_case for variable names.

end
current_months
end

def get_year_key(products, key, product_year_data)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Naming/MethodName: Use snake_case for method names.
Lint/UnusedMethodArgument: Unused method argument - products. If it's necessary, use _ or _products as an argument name to indicate that it won't be used.
Lint/UnusedMethodArgument: Unused method argument - key. If it's necessary, use _ or _key as an argument name to indicate that it won't be used.
Naming/UncommunicativeMethodParamName: Only use lowercase characters for method parameter.
Naming/VariableName: Use snake_case for variable names.

if product_year_data[@current_year]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/GuardClause: Use a guard clause instead of wrapping the code inside a conditional expression.

return @current_year

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/RedundantReturn: Redundant return detected.

else

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/RedundantReturn: Redundant return detected.

return product_year_data.keys.max{|a,b| a.to_i <=> b.to_i}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/RedundantReturn: Redundant return detected.
Performance/CompareWithBlock: Use max_by(&:to_i) instead of max { |a, b| a.to_i <=> b.to_i }.
Layout/SpaceBeforeBlockBraces: Space missing to the left of {.
Layout/SpaceInsideBlockBraces: Space between { and | missing.
Layout/SpaceAfterComma: Space missing after comma.
Layout/SpaceInsideBlockBraces: Space missing inside }.

end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/RedundantReturn: Redundant return detected.
Performance/CompareWithBlock: Use max_by(&:to_i) instead of max { |a, b| a.to_i <=> b.to_i }.
Layout/SpaceBeforeBlockBraces: Space missing to the left of {.
Layout/SpaceInsideBlockBraces: Space between { and | missing.
Layout/SpaceAfterComma: Space missing after comma.
Layout/SpaceInsideBlockBraces: Space missing inside }.

end

def get_month_key(product_month_data, current_months, month_Map)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Naming/UncommunicativeMethodParamName: Only use lowercase characters for method parameter.
Naming/VariableName: Use snake_case for variable names.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Naming/MethodName: Use snake_case for method names.
Naming/UncommunicativeMethodParamName: Only use lowercase characters for method parameter.
Naming/VariableName: Use snake_case for variable names.

if product_month_data[current_months]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/GuardClause: Use a guard clause instead of wrapping the code inside a conditional expression.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Naming/UncommunicativeMethodParamName: Only use lowercase characters for method parameter.
Naming/VariableName: Use snake_case for variable names.

return current_months

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/RedundantReturn: Redundant return detected.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/GuardClause: Use a guard clause instead of wrapping the code inside a conditional expression.

else

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/RedundantReturn: Redundant return detected.

return product_month_data.keys.max{|a,b| month_Map[a] <=> month_Map[b]}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/RedundantReturn: Redundant return detected.
Layout/SpaceBeforeBlockBraces: Space missing to the left of {.
Layout/SpaceInsideBlockBraces: Space between { and | missing.
Layout/SpaceAfterComma: Space missing after comma.
Layout/SpaceInsideBlockBraces: Space missing inside }.

end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/RedundantReturn: Redundant return detected.
Layout/SpaceBeforeBlockBraces: Space missing to the left of {.
Layout/SpaceInsideBlockBraces: Space between { and | missing.
Layout/SpaceAfterComma: Space missing after comma.
Layout/SpaceInsideBlockBraces: Space missing inside }.

end

def get_recent_price_data(key, products, month_Map)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Naming/UncommunicativeMethodParamName: Only use lowercase characters for method parameter.
Naming/VariableName: Use snake_case for variable names.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Naming/MethodName: Use snake_case for method names.
Naming/UncommunicativeMethodParamName: Only use lowercase characters for method parameter.
Naming/VariableName: Use snake_case for variable names.

current_months = @current_month

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Naming/VariableName: Use snake_case for variable names.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Naming/UncommunicativeMethodParamName: Only use lowercase characters for method parameter.
Naming/VariableName: Use snake_case for variable names.

month_Map.each do |month, monthNumber|

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Naming/UncommunicativeBlockParamName: Only use lowercase characters for block parameter.
Naming/VariableName: Use snake_case for variable names.

current_months = get_mount(month, monthNumber)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Naming/VariableName: Use snake_case for variable names.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Naming/UncommunicativeBlockParamName: Only use lowercase characters for block parameter.
Naming/VariableName: Use snake_case for variable names.

end
product_year_data = products[key]
RGBD marked this conversation as resolved.
Show resolved Hide resolved
year_Key = get_year_key(products, key, product_year_data)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Naming/VariableName: Use snake_case for variable names.

product_month_data = product_year_data[year_Key]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Naming/VariableName: Use snake_case for variable names.

month_key = get_month_key(product_month_data, current_months, month_Map)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Naming/VariableName: Use snake_case for variable names.

{'price' => product_month_data[month_key]['Minsk'], 'year' => year_Key, 'month' => month_key }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/SpaceInsideHashLiteralBraces: Space inside { missing.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/SpaceInsideHashLiteralBraces: Space inside { missing.
Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.

end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/SpaceInsideHashLiteralBraces: Space inside { missing.


def get_file(file_path)
extention = file_path.split('.')[2]
if extention == 'xls' || extention == 'xlsx'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/MultipleComparison: Avoid comparing a variable with multiple items in a conditional, use Array#include? instead.

file = Roo::Excelx.new(file_path)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/MultipleComparison: Avoid comparing a variable with multiple items in a conditional, use Array#include? instead.

else
puts 'unsupported file type: ' + file_path
end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.

file
end

def find_keys(word, products)
keys = []
products.keys.each do |key|
keys.push(key) if (key.include?(word))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/ParenthesesAroundCondition: Don't use parentheses around the condition of an if.
Style/RedundantParentheses: Don't use parentheses around a method call.

end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/ParenthesesAroundCondition: Don't use parentheses around the condition of an if.
Style/RedundantParentheses: Don't use parentheses around a method call.

keys

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/SelfAssignment: Use self-assignment shorthand /=.
Style/NumericLiterals: Use underscores(_) as decimal mark and separate every 3 digits with them.

end

def fetch_products_data(file, products, regions)
for index in 9..file.last_row

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/For: Prefer each over for.

next if file.cell('E', index).nil?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/For: Prefer each over for.

year = file.cell('A', 3).split(' ')[2]
month = file.cell('A', 3).split(' ')[1]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metrics/AbcSize: Assignment Branch Condition size for get_Recent_Price_Data is too high. [25.48/15]
Metrics/MethodLength: Method has too many lines. [24/10]
Naming/MethodName: Use snake_case for method names.

key = file.cell('A', index).strip.downcase

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Naming/VariableName: Use snake_case for variable names.
Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.

add_products(products, year, month, key, regions, file, index)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/For: Prefer each over for.

end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Naming/VariableName: Use snake_case for variable names.
Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.

end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/SpaceBeforeBlockBraces: Space missing to the left of {.
Layout/SpaceInsideBlockBraces: Space between { and | missing.
Style/BlockDelimiters: Avoid using {...} for multi-line blocks.
Naming/UncommunicativeBlockParamName: Only use lowercase characters for block parameter.
Naming/VariableName: Use snake_case for variable names.

def add_products(products, year, month, key, regions, file, index)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metrics/AbcSize: Assignment Branch Condition size for add_products is too high. [27.24/15]
Metrics/MethodLength: Method has too many lines. [11/10]
Metrics/ParameterLists: Avoid parameter lists longer than 5 parameters. [7/5]

products[key] = {} unless products[key]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metrics/AbcSize: Assignment Branch Condition size for add_products is too high. [27.24/15]
Metrics/MethodLength: Method has too many lines. [11/10]
Metrics/ParameterLists: Avoid parameter lists longer than 5 parameters. [7/5]

products[key][year] = {} unless products[key][year]
products[key][year][month] = {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/TrailingWhitespace: Trailing whitespace detected.

regions[0] => format_value(file.cell('G', index), year),
regions[1] => format_value(file.cell('I', index), year),
regions[2] => format_value(file.cell('K', index), year),
regions[3] => format_value(file.cell('M', index), year),
regions[4] => format_value(file.cell('O', index), year),
regions[5] => format_value(file.cell('Q', index), year),
regions[6] => format_value(file.cell('S', index), year)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/TrailingWhitespace: Trailing whitespace detected.

}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/IndentHash: Indent the right brace the same as the start of the line where the left brace is.

end

def format_value(val, year)
if val

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/GuardClause: Use a guard clause instead of wrapping the code inside a conditional expression.

result = val.to_f

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/GuardClause: Use a guard clause instead of wrapping the code inside a conditional expression.


if year.to_i < 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/IfUnlessModifier: Favor modifier if usage when having a single-line body. Another good alternative is the usage of control flow &&/||.

result = result / 10000

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/SelfAssignment: Use self-assignment shorthand /=.
Style/NumericLiterals: Use underscores(_) as decimal mark and separate every 3 digits with them.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/GuardClause: Use a guard clause instead of wrapping the code inside a conditional expression.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/IfUnlessModifier: Favor modifier if usage when having a single-line body. Another good alternative is the usage of control flow &&/||.

end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/SelfAssignment: Use self-assignment shorthand /=.
Style/NumericLiterals: Use underscores(_) as decimal mark and separate every 3 digits with them.


result.round(2)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/IfUnlessModifier: Favor modifier if usage when having a single-line body. Another good alternative is the usage of control flow &&/||.

end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/SelfAssignment: Use self-assignment shorthand /=.
Style/NumericLiterals: Use underscores(_) as decimal mark and separate every 3 digits with them.

end

def get_min_price(hash)
min_year_price = 111_111_111
result = {}
hash.each do |year, year_hash|
min_month_data = find_min_month(year_hash)
puts(min_month_data)
min_year_data = find_min_year(year, min_month_data, min_year_price)
min_year_price = min_year_data['price'] if min_year_data['price']
result = actual_data(result, min_year_data)
end
result
end

def find_min_month(year_hash, min_month_price = 111_111_111, min_month = nil)
year_hash.each do |month, month_hash|
price = month_hash['Minsk']
next unless price
if price < min_month_price
min_month_price = price
min_month = month
end
end
{ 'price' => min_month_price, 'month' => min_month }
end

def find_min_year(year, min_month_data, min_year_price)
result = {}
min_month_price = min_month_data['price']
result['month'] = min_month_data['month']
if min_month_price < min_year_price
result['price'] = min_month_price
result['year'] = year
end
result
end

def get_max_price(hash)
max_year_price = 0
result = {}
hash.each do |year, year_hash|
max_month_data = find_max_month(year_hash)
max_year_data = find_max_year(year, max_month_data, max_year_price)
max_year_price = max_year_data['price'] if max_year_data['price']
result = actual_data(result, max_year_data)
end
result
end

def find_max_month(year_hash, max_month_price = 0, max_month = nil)
year_hash.each do |month, month_hash|
price = month_hash['Minsk']
next unless price
if price > max_month_price
max_month_price = price
max_month = month
end
end
{ 'price' => max_month_price, 'month' => max_month }
end

def find_max_year(year, max_month_data, max_year_price)
result = {}
max_month_price = max_month_data['price']
result['month'] = max_month_data['month']
if max_month_price > max_year_price
result['price'] = max_month_price
result['year'] = year
end
result
end

def actual_data(result, year_data)
result['month'] = year_data['month']
result['price'] = year_data['price'] if year_data['price']
result['year'] = year_data['year'] if year_data['year']
result
end
end