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

Kirill Hryvicki - 0 #20

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
18 changes: 18 additions & 0 deletions Hryvicki Kirill/0/loadFiles.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require 'mechanize'
require 'open-uri'
require 'uri'

agent = Mechanize.new
page = agent.get('http://www.belstat.gov.by/ofitsialnaya-statistika/makroekonomika-i-okruzhayushchaya-sreda/tseny/operativnaya-informatsiya_4/srednie-tseny-na-potrebitelskie-tovary-i-uslugi-po-respublike-belarus')
page.links_with(:href => /.xls/).each do |link|

Choose a reason for hiding this comment

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

Style/HashSyntax: Use the new Ruby 1.9 hash syntax.

str_link = link.href.to_s
str_link = URI.unescape(str_link)
str_link = URI.escape(str_link)

Choose a reason for hiding this comment

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

Lint/UriEscapeUnescape: URI.escape method is obsolete and should not be used. Instead, use CGI.escape, URI.encode_www_form or URI.encode_www_form_component depending on your specific use case.

if /http:\/\/www.belstat.gov.by/ === str_link

Choose a reason for hiding this comment

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

Style/ConditionalAssignment: Use the return of the conditional for variable assignment and comparison.
Performance/RegexpMatch: Use match? instead of === when MatchData is not used.
Style/RegexpLiteral: Use %r around regular expression.
Style/CaseEquality: Avoid the use of the case equality operator ===.

f_link = str_link

Choose a reason for hiding this comment

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

Layout/IndentationWidth: Use 2 (not 4) spaces for indentation.

else
f_link = 'http://www.belstat.gov.by' + str_link

Choose a reason for hiding this comment

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

Layout/IndentationWidth: Use 2 (not 4) spaces for indentation.

end
download = open(f_link)

Choose a reason for hiding this comment

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

Security/Open: The use of Kernel#open is a serious security risk.

IO.copy_stream(download, "./data/#{download.base_uri.to_s.split('/')[-1]}")
end

Choose a reason for hiding this comment

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

Layout/TrailingBlankLines: Final newline missing.

213 changes: 213 additions & 0 deletions Hryvicki Kirill/0/run.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
require 'roo'
RGBD marked this conversation as resolved.
Show resolved Hide resolved
require 'roo-xls'

def getFileInstance(file_path)

Choose a reason for hiding this comment

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

Metrics/MethodLength: Method has too many lines. [14/10]
Naming/MethodName: Use snake_case for method names.

ext = file_path.split('.')[2]
file_instance = nil

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.

if ext == 'xls' || ext == '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_instance = Roo::Spreadsheet.open(file_path)

Choose a reason for hiding this comment

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

Lint/UselessAssignment: Useless assignment to variable - file_instance.

case ext
when 'xls'
file_instance = Roo::Excel.new(file_path)
when 'xlsx'
file_instance = Roo::Excelx.new(file_path)
end
else
puts 'unsupported file type: ' + file_path
end

return file_instance

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.

end

def findKeys(word, products)

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.

result = []
products.keys.each { |key|

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.

Copy link
Collaborator

Choose a reason for hiding this comment

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

можно использовать select, и обойтись без объявления result

Copy link
Author

Choose a reason for hiding this comment

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

Сделано

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

Layout/SpaceAroundKeyword: Space after keyword if is missing.

}
return result

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.

end

def fetchProductsData(file_instance, products, regions)

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 fetchProductsData is too high. [41.79/15]
Metrics/MethodLength: Method has too many lines. [17/10]
Naming/MethodName: Use snake_case for method names.

for n in 9..file_instance.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.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Enumerable.drop(9), iterate over rows.

Copy link
Author

Choose a reason for hiding this comment

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

Заменил на (9..file_instance.last_row).each do |n|

next if file_instance.cell('E', n) == nil

Choose a reason for hiding this comment

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

Style/NilComparison: Prefer the use of the nil? predicate.


year = file_instance.cell('A', 3).split(' ')[2]
month = file_instance.cell('A', 3).split(' ')[1]
key = file_instance.cell('A', n).strip.downcase

products[key] = {} if !products[key]

Choose a reason for hiding this comment

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

Style/NegatedIf: Favor unless over if for negative conditions.

products[key][year] = {} if !products[key][year]

Choose a reason for hiding this comment

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

Style/NegatedIf: Favor unless over if for negative conditions.


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/IndentationConsistency: Inconsistent indentation detected.

regions[0] => formatValue(file_instance.cell('G', n), year),
regions[1] => formatValue(file_instance.cell('I', n), year),
regions[2] => formatValue(file_instance.cell('K', n), year),
regions[3] => formatValue(file_instance.cell('M', n), year),
regions[4] => formatValue(file_instance.cell('O', n), year),
regions[5] => formatValue(file_instance.cell('Q', n), year),
regions[6] => formatValue(file_instance.cell('S', n), year)
}
end
end

def formatValue(val, year)

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.

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
result = result / 10000 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/SelfAssignment: Use self-assignment shorthand /=.
Style/NumericLiterals: Use underscores(_) as decimal mark and separate every 3 digits with them.

return result.round(2)

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.

end
end

def getRecentPriceData(key, products, month_map)

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 getRecentPriceData is too high. [26.74/15]
Metrics/MethodLength: Method has too many lines. [23/10]
Naming/MethodName: Use snake_case for method names.

current_year = Time.now.strftime('%Y').to_s
current_month = Time.now.strftime('%m')

Choose a reason for hiding this comment

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

Lint/UselessAssignment: Useless assignment to variable - current_month.


month_map.each { |month, month_number|

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.

current_month = month if month_number == current_month
}

product_year_data = products[key];

Choose a reason for hiding this comment

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

Style/Semicolon: Do not use semicolons to terminate expressions.


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/ConditionalAssignment: Use the return of the conditional for variable assignment and comparison.

year_key = current_year
else
year_key = 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.

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/SpaceAfterComma: Space missing after comma.
Layout/SpaceInsideBlockBraces: Space missing inside }.

end

product_month_data = product_year_data[year_key]

if product_month_data[current_month]

Choose a reason for hiding this comment

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

Style/ConditionalAssignment: Use the return of the conditional for variable assignment and comparison.

month_key = current_month
else
month_key = product_month_data.keys.max{ |a,b| month_map[a].to_i <=> month_map[b].to_i}

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/SpaceAfterComma: Space missing after comma.
Layout/SpaceInsideBlockBraces: Space missing inside }.

end

return {

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.

'price' => product_month_data[month_key]['Minsk'],
'year' => year_key,
'month' => month_key,
'product' => key
}

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.

end

def getMinPrice(hash)

Choose a reason for hiding this comment

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

Metrics/MethodLength: Method has too many lines. [19/10]
Naming/MethodName: Use snake_case for method names.

result = {}
min_year_price = 9999999999999

Choose a reason for hiding this comment

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

Style/NumericLiterals: Use underscores(_) as decimal mark and separate every 3 digits with them.


hash.each do |year, year_hash|
min_month_price = 9999999999999

Choose a reason for hiding this comment

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

Style/NumericLiterals: Use underscores(_) as decimal mark and separate every 3 digits with them.

year_hash.each do |month, month_hash|
price = month_hash['Minsk']
next if !price

Choose a reason for hiding this comment

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

Style/NegatedIf: Favor unless over if for negative conditions.
Layout/TrailingWhitespace: Trailing whitespace detected.

if price < min_month_price
min_month_price = price
result['month'] = month
end
end
if min_month_price < min_year_price

Choose a reason for hiding this comment

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

Style/Next: Use next to skip iteration.

min_year_price = min_month_price
result['year'] = year
result['price'] = min_year_price
end
end
return result

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.

end

def getMaxPrice(hash)

Choose a reason for hiding this comment

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

Metrics/MethodLength: Method has too many lines. [19/10]
Naming/MethodName: Use snake_case for method names.

result = {}
max_year_price = 0

hash.each do |year, year_hash|
max_month_price = 0
year_hash.each do |month, month_hash|
price = month_hash['Minsk']
next if !price

Choose a reason for hiding this comment

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

Style/NegatedIf: Favor unless over if for negative conditions.


if price > max_month_price
max_month_price = price
result['month'] = month
end
end
if max_month_price > max_year_price

Choose a reason for hiding this comment

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

Style/Next: Use next to skip iteration.

max_year_price = max_month_price
result['year'] = year
result['price'] = max_year_price
end
end
return result

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.

end

def getSimilarPriceProducts(data, products)

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 getSimilarPriceProducts is too high. [17.23/15]
Metrics/MethodLength: Method has too many lines. [11/10]
Naming/MethodName: Use snake_case for method names.

result = []
price = data['price']
year = data['year']
month = data['month']
origin_product = data['product']

products.each { |product, product_data|

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.

next if !product_data[year]

Choose a reason for hiding this comment

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

Style/NegatedIf: Favor unless over if for negative conditions.

next if !product_data[year][month]

Choose a reason for hiding this comment

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

Style/NegatedIf: Favor unless over if for negative conditions.

result.push(product) if product_data[year][month]['Minsk'] == price && product != origin_product
}
return result

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.

end

def main

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 main is too high. [61.07/15]
Metrics/MethodLength: Method has too many lines. [50/10]

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 main is too high. [60.09/15]
Metrics/MethodLength: Method has too many lines. [48/10]

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

Choose a reason for hiding this comment

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

Style/TrailingCommaInHashLiteral: Avoid comma after the last item of a hash.

}

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 = ['Brest', 'Vitebsk', 'Gomel', 'Grodno', 'Minsk', 'Minsk Region', 'Mogilyov']
products = {}
file_paths = Dir['./data/*']

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.

file_paths.each do |file_path|
file_instance = getFileInstance(file_path)
fetchProductsData(file_instance, products, regions) if file_instance

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.

end

Choose a reason for hiding this comment

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

Layout/EmptyLines: Extra blank line detected.

loop do
puts 'What price are you looking for?'
word = gets.chomp.downcase # .encode('UTF-8')
keys = findKeys(word, products)
if keys.empty?
puts word.capitalize + ' can not be found in database'
else
keys.each do |key|
recent_price_data = getRecentPriceData(key, products, month_map)
puts ''
puts key.capitalize + ' is ' + recent_price_data['price'].to_s + ' BYN in Minsk these days.'

min_price = getMinPrice(products[key])
puts 'Lowest was on ' + min_price['year'] + '/' + month_map[min_price['month']].to_s +
' 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 = getMaxPrice(products[key])
puts 'Maximum was on ' + max_price['year'] + '/' + month_map[max_price['month']].to_s +
' at price ' + max_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.


similar_products = getSimilarPriceProducts(recent_price_data, products)
if similar_products.empty?
puts 'No products for similar price'
else
puts 'For similar price you also can afford'
puts similar_products
end
end
end
end
end

main