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

2199 - 3 #277

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open

2199 - 3 #277

wants to merge 8 commits into from

Conversation

LikaLex
Copy link
Contributor

@LikaLex LikaLex commented Jul 27, 2018

Номер

2199

Номер задания

3

Ссылка на видео с демо

https://youtu.be/lQrc5G4Vhd8

Комментарии

The processing is so long, because the internet is not very good and Onliner is loaded for a long time

@browser ||= Capybara.current_session
end

def visit_page

Choose a reason for hiding this comment

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

TooManyStatements: OnlinerPageParser#visit_page has approx 7 statements. More info.

@texts = texts
end

def analyze

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 analyze is too high. [16.03/15]


def analyze
JSON.parse(run_request.body)['documents'].each_with_object([]) do |result, store|
document = documents.find { |doc| doc[:id].to_s == result['id'] }

Choose a reason for hiding this comment

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

NestedIterators: CommentAnalyzer#analyze contains iterators nested 2 deep. More info.


def request
request = Net::HTTP::Post.new(endpoint)
request['Content-Type'] = 'application/json'

Choose a reason for hiding this comment

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

FeatureEnvy: CommentAnalyzer#request refers to 'request' more than self (maybe move it to another class?). More info.

@alexshagov
Copy link
Collaborator

Stick to RESTful routes.

Example:
image

# analyze comment
class CommentAnalyzer
ACCESS_KEY = ENV['ACCESS_KEY']
AZURE_ENDPOINT = ENV['AZURE_ENDPOINT']
Copy link
Collaborator

Choose a reason for hiding this comment

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

Does Azure endpoint contain responsive information?
If no, why should we store it in .ENV variable?

document = documents.find { |doc| doc[:id].to_s == result['id'] }
store << {
text: document[:text],
rating: result['score'] * 200 - 100
Copy link
Collaborator

Choose a reason for hiding this comment

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

The formula is too magical for me (where do the numbers (*200 - 100 ) come from?)
Let's move it to separate method and describe a little bit.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We get a decimal number and this formula needed to lead it to necessary range


# :reek:FeatureEnvy
def request
request = Net::HTTP::Post.new(endpoint)
Copy link
Collaborator

Choose a reason for hiding this comment

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

You can use #tap.

Net::HTTP::Post.new(endpoint).tap do |request|
   request['Content-Type'] = 'application/json'
   request['Ocp-Apim-Subscription-Key'] = ACCESS_KEY
   request.body = serialized_texts
end

end
end

def serialized_texts
Copy link
Collaborator

Choose a reason for hiding this comment

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

If it's serialized_texts JSON, call it serialized_texts_json :)

end
# rubocop:enable Metrics/AbcSize

def endpoint
Copy link
Collaborator

Choose a reason for hiding this comment

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

Again, what endpoint? I see from the code below that it's the #azure_endpoint. Why not say it explicitly?

browser.visit(@link)
rescue StandardError => exception
attempts += 1
sleep(2 * attempts)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why did you choose 2*attempts formula?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I use exponential backoff. I did something wrong??

link = params[:link]
@page = Page.find(link: link).first
unless @page
comment_texts = OnlinerPageParser.new(link).top_comment_texts
Copy link
Collaborator

Choose a reason for hiding this comment

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

I guess the Parser is redunant word here.
It's just OnlinerPage.new(link). And then you ask your OnlinerPage about its '#top_comments' and so on.

Or if you want to stick to comments, call it OnlinerPageCommentsParser

@page = Page.find(link: link).first
unless @page
comment_texts = OnlinerPageParser.new(link).top_comment_texts
comments_with_score = CommentAnalyzer.new(comment_texts).analyze
Copy link
Collaborator

Choose a reason for hiding this comment

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

Eventually, it's a Comment or Comments analyzer? From your code, I see that it analyzes all comments, not the particular one.

erb :show
end

post '/pages' do
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's meditate on this endpoint.

I'd like to have less code in here.
Something like this:

#app.rb
post '/pages' do
   if Page.exists?(link: link)
     @page = Page.find(link: link)
   else
     @page = Page.create_from(link: link)
   end
   erb :show
end

It's just an idea, but it will make your post action more verbose and will hide all Page and Comment creation logic.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants