-
Notifications
You must be signed in to change notification settings - Fork 21
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
base: master
Are you sure you want to change the base?
Ivan Shishkov #21
Changes from 10 commits
f839f67
2e38fc3
272c815
c8d36df
ca632ed
e807eb3
e97252f
08f2804
f5c6c03
a0fbdbb
f2d625d
08426dd
613e169
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
require "roo" | ||
|
||
require "roo-xls" | ||
|
||
require "./WorkWithData.rb" | ||
|
||
funcs = FilesParse.new | ||
months = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make Constant |
||
'Vitebsk', | ||
'Gomel', | ||
'Grodno', | ||
'Minsk', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
'Grodno Region', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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'] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. {} |
||
file_Paths = Dir['./data/*'] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/NumericPredicate: Use keys.length.zero? instead of keys.length == 0. |
||
puts word.capitalize + ' can not be found in database' | ||
else | ||
keys.each{|key| | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Layout/SpaceBeforeBlockBraces: Space missing to the left of {. |
||
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]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 + | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
class FilesParse | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Layout/EndOfLine: Carriage return character detected. |
||
|
||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Naming/UncommunicativeMethodParamName: Only use lowercase characters for method parameter. |
||
current_months = @current_month | ||
if monthNumber == @current_month | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Naming/MethodName: Use snake_case for method names. |
||
if product_year_data[@current_year] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/RedundantReturn: Redundant return detected. |
||
else | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/RedundantReturn: Redundant return detected. |
||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/RedundantReturn: Redundant return detected. |
||
end | ||
|
||
def get_month_key(product_month_data, current_months, month_Map) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Naming/UncommunicativeMethodParamName: Only use lowercase characters for method parameter. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Naming/MethodName: Use snake_case for method names. |
||
if product_month_data[current_months] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Naming/UncommunicativeMethodParamName: Only use lowercase characters for method parameter. |
||
return current_months | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/RedundantReturn: Redundant return detected. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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]} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/RedundantReturn: Redundant return detected. |
||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/RedundantReturn: Redundant return detected. |
||
end | ||
|
||
def get_recent_price_data(key, products, month_Map) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Naming/UncommunicativeMethodParamName: Only use lowercase characters for method parameter. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Naming/MethodName: Use snake_case for method names. |
||
current_months = @current_month | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Naming/VariableName: Use snake_case for variable names. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Naming/UncommunicativeMethodParamName: Only use lowercase characters for method parameter. |
||
month_Map.each do |month, monthNumber| | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Naming/UncommunicativeBlockParamName: Only use lowercase characters for block parameter. |
||
current_months = get_mount(month, monthNumber) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Naming/VariableName: Use snake_case for variable names. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Naming/UncommunicativeBlockParamName: Only use lowercase characters for block parameter. |
||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Layout/SpaceInsideHashLiteralBraces: Space inside { missing. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Layout/SpaceInsideHashLiteralBraces: Space inside { missing. |
||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/ParenthesesAroundCondition: Don't use parentheses around the condition of an if. |
||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/ParenthesesAroundCondition: Don't use parentheses around the condition of an if. |
||
keys | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/SelfAssignment: Use self-assignment shorthand /=. |
||
end | ||
|
||
def fetch_products_data(file, products, regions) | ||
for index in 9..file.last_row | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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] |
||
key = file.cell('A', index).strip.downcase | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Naming/VariableName: Use snake_case for variable names. |
||
add_products(products, year, month, key, regions, file, index) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/For: Prefer each over for. |
||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Naming/VariableName: Use snake_case for variable names. |
||
end | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Layout/SpaceBeforeBlockBraces: Space missing to the left of {. |
||
def add_products(products, year, month, key, regions, file, index) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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] |
||
products[key] = {} unless products[key] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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] |
||
products[key][year] = {} unless products[key][year] | ||
products[key][year][month] = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Layout/TrailingWhitespace: Trailing whitespace detected. |
||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/SelfAssignment: Use self-assignment shorthand /=. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/SelfAssignment: Use self-assignment shorthand /=. |
||
|
||
result.round(2) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/SelfAssignment: Use self-assignment shorthand /=. |
||
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 |
There was a problem hiding this comment.
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