From d2733290bb669fa309649927f1cf21ed2225eacc Mon Sep 17 00:00:00 2001 From: ZhenyaZhak Date: Mon, 30 Jul 2018 23:58:44 +0300 Subject: [PATCH] Split CommentsParser on 2 classes --- 2232/3/helpers/article_parser.rb | 4 +++- 2232/3/helpers/comments_analyzer.rb | 26 ++++++++++++++++++++++++++ 2232/3/helpers/comments_parser.rb | 18 ++---------------- 3 files changed, 31 insertions(+), 17 deletions(-) create mode 100644 2232/3/helpers/comments_analyzer.rb diff --git a/2232/3/helpers/article_parser.rb b/2232/3/helpers/article_parser.rb index ab2a6a622..fb143aedf 100644 --- a/2232/3/helpers/article_parser.rb +++ b/2232/3/helpers/article_parser.rb @@ -1,4 +1,5 @@ require_relative 'comments_parser' +require_relative 'comments_analyzer' class ArticleParser attr_reader :link @@ -16,7 +17,8 @@ def rating end def comments - @comments ||= CommentsParser.new(page, link).call + untreated_comments = CommentsParser.new(page, link).comments + @comments ||= CommentsAnalyzer.new(untreated_comments).analyze end private diff --git a/2232/3/helpers/comments_analyzer.rb b/2232/3/helpers/comments_analyzer.rb new file mode 100644 index 000000000..f8b62819d --- /dev/null +++ b/2232/3/helpers/comments_analyzer.rb @@ -0,0 +1,26 @@ +require_relative 'sentiment_parser' +require_relative 'comment_storage' + +class CommentsAnalyzer + def initialize(comments) + @comments = comments + end + + def analyze + sentiments.map do |sentiment| + author_text = @comments[sentiment['id'].to_i] + CommentStorage.new(author_text[0], author_text[1], rating_recount(sentiment['score'])) + end + end + + private + + # transfer rating from natural range to integer + def rating_recount(score) + (score * 200 - 100).to_i + end + + def sentiments + SentimentParser.new(@comments).call + end +end diff --git a/2232/3/helpers/comments_parser.rb b/2232/3/helpers/comments_parser.rb index b660cceff..951c82f82 100644 --- a/2232/3/helpers/comments_parser.rb +++ b/2232/3/helpers/comments_parser.rb @@ -1,6 +1,3 @@ -require_relative 'sentiment_parser' -require_relative 'comment_storage' - class CommentsParser LIMIT_COMMENTS = 50 @@ -9,29 +6,18 @@ def initialize(page, link) @link = link end - def call - sentiments.map do |sentiment| - author_text = comments[sentiment['id'].to_i] - CommentStorage.new(author_text[0], author_text[1], (sentiment['score'] * 200 - 100).to_i) - end + def comments + @comments ||= response.map { |comment| [comment['author']['name'], comment['text'].tr("\n", ' ')] } end private - def sentiments - SentimentParser.new(comments).call - end - def api_link number = @page.css('.news_view_count').attr('news_id').value category = @link.split('https://').last.split('.').first "https://comments.api.onliner.by/news/#{category}.post/#{number}/comments?limit=9999" end - def comments - @comments ||= response.map { |comment| [comment['author']['name'], comment['text'].tr("\n", ' ')] } - end - def rate(comment) comment['marks']['likes'] + comment['marks']['dislikes'] end