Skip to content
This repository has been archived by the owner on Jun 27, 2019. It is now read-only.

Commit

Permalink
Split CommentsParser on 2 classes
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhenyaZhak committed Jul 30, 2018
1 parent 4e2f376 commit d273329
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 17 deletions.
4 changes: 3 additions & 1 deletion 2232/3/helpers/article_parser.rb
@@ -1,4 +1,5 @@
require_relative 'comments_parser'
require_relative 'comments_analyzer'

class ArticleParser
attr_reader :link
Expand All @@ -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
Expand Down
26 changes: 26 additions & 0 deletions 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
18 changes: 2 additions & 16 deletions 2232/3/helpers/comments_parser.rb
@@ -1,6 +1,3 @@
require_relative 'sentiment_parser'
require_relative 'comment_storage'

class CommentsParser
LIMIT_COMMENTS = 50

Expand All @@ -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
Expand Down

0 comments on commit d273329

Please sign in to comment.