This repository has been archived by the owner on Jun 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
2227-3 #283
Open
VladislavIlyuschits
wants to merge
2
commits into
rubizza-camp:master
Choose a base branch
from
VladislavIlyuschits:2227-3
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
2227-3 #283
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
secrets.yml | ||
dumb.rdb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
source 'https://rubygems.org' | ||
ruby '2.5.1' | ||
|
||
gem 'date' | ||
gem 'json', '~> 1.8', '>= 1.8.3' | ||
gem 'mechanize' | ||
gem 'ohm', '~> 3.1', '>= 3.1.1' | ||
gem 'pry' | ||
gem 'shotgun' | ||
gem 'sinatra', '~> 1.4.0' | ||
gem 'thin' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
GEM | ||
remote: https://rubygems.org/ | ||
specs: | ||
coderay (1.1.2) | ||
connection_pool (2.2.2) | ||
daemons (1.2.6) | ||
date (1.0.0) | ||
domain_name (0.5.20180417) | ||
unf (>= 0.0.5, < 1.0.0) | ||
eventmachine (1.2.7) | ||
hiredis (0.6.1) | ||
http-cookie (1.0.3) | ||
domain_name (~> 0.5) | ||
json (1.8.6) | ||
mechanize (2.7.6) | ||
domain_name (~> 0.5, >= 0.5.1) | ||
http-cookie (~> 1.0) | ||
mime-types (>= 1.17.2) | ||
net-http-digest_auth (~> 1.1, >= 1.1.1) | ||
net-http-persistent (>= 2.5.2) | ||
nokogiri (~> 1.6) | ||
ntlm-http (~> 0.1, >= 0.1.1) | ||
webrobots (>= 0.0.9, < 0.2) | ||
method_source (0.9.0) | ||
mime-types (3.1) | ||
mime-types-data (~> 3.2015) | ||
mime-types-data (3.2016.0521) | ||
mini_portile2 (2.3.0) | ||
nest (3.1.1) | ||
redic | ||
net-http-digest_auth (1.4.1) | ||
net-http-persistent (3.0.0) | ||
connection_pool (~> 2.2) | ||
nokogiri (1.8.4) | ||
mini_portile2 (~> 2.3.0) | ||
ntlm-http (0.1.1) | ||
ohm (3.1.1) | ||
nest (~> 3) | ||
redic (~> 1.5.0) | ||
stal | ||
pry (0.11.3) | ||
coderay (~> 1.1.0) | ||
method_source (~> 0.9.0) | ||
rack (1.6.10) | ||
rack-protection (1.5.5) | ||
rack | ||
rake (11.3.0) | ||
redic (1.5.0) | ||
hiredis | ||
shotgun (0.9.2) | ||
rack (>= 1.0) | ||
sinatra (1.4.8) | ||
rack (~> 1.5) | ||
rack-protection (~> 1.4) | ||
tilt (>= 1.3, < 3) | ||
stal (0.3.0) | ||
redic (~> 1.5) | ||
thin (1.7.2) | ||
daemons (~> 1.0, >= 1.0.9) | ||
eventmachine (~> 1.0, >= 1.0.4) | ||
rack (>= 1, < 3) | ||
tilt (2.0.8) | ||
unf (0.1.4) | ||
unf_ext | ||
unf_ext (0.0.7.5) | ||
webrobots (0.1.2) | ||
|
||
PLATFORMS | ||
ruby | ||
|
||
DEPENDENCIES | ||
date | ||
json (~> 1.8, >= 1.8.3) | ||
mechanize | ||
ohm (~> 3.1, >= 3.1.1) | ||
pry | ||
rake (~> 11.2, >= 11.2.2) | ||
shotgun | ||
sinatra (~> 1.4.0) | ||
thin | ||
|
||
RUBY VERSION | ||
ruby 2.5.1p57 | ||
|
||
BUNDLED WITH | ||
1.16.2 |
29 changes: 29 additions & 0 deletions
29
2227/3/sinatra_project/app/controllers/articles_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Controller Sinatra | ||
# rubocop:disable Metrics/AbcSize, Metrics/MethodLength | ||
# This method smells of :reek:DuplicateMethodCall | ||
# This method smells of :reek:TooManyStatements | ||
module ArticlesController | ||
def self.registered(app) | ||
app.get '/article/create' do | ||
erb :create_article | ||
end | ||
|
||
app.post '/article/create' do | ||
article = Article.create(link: params['link']) | ||
ArticleDateBaseCreater.new(article).update_db | ||
redirect '/' | ||
end | ||
|
||
app.get '/article/:id' do | ||
@articles = Article.all | ||
@article = @articles[params[:id]] | ||
erb :show_comments | ||
end | ||
|
||
app.get '/' do | ||
@articles = Article.all | ||
erb :show_article | ||
end | ||
end | ||
end | ||
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Create and update article rating | ||
class ArticleDateBaseCreater | ||
attr_reader :article, :texts, :ratings | ||
|
||
def initialize(article) | ||
@article = article | ||
@texts = CommentsParser.new(article.link).parse_comments_from_page | ||
@ratings = CommentsRater.new(texts).create_answer | ||
end | ||
|
||
def update_db | ||
update_article_title | ||
create_article_stat | ||
update_article_rating | ||
end | ||
|
||
def update_article_title | ||
page = Mechanize.new.get(article.link) | ||
@article.update(title: page.title) | ||
end | ||
|
||
def create_article_stat | ||
texts.each_with_index { |text, index| article.comments.add(Comment.create(text: text, rating: ratings[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. ok, in ur ideal world azure returns scores in the same order. In the real world tomorrow they will shuffle these scores. I want to be sure that comments will be saved with correct rating. |
||
end | ||
|
||
def update_article_rating | ||
article_rating = (ratings.sum / texts.size).to_i | ||
@article.update(rating: article_rating) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Parser comment | ||
class CommentsParser | ||
COMMENTS_LIMIT = 50 | ||
attr_reader :link | ||
def initialize(link) | ||
@link = link | ||
end | ||
|
||
def parse_comments_from_page | ||
comments = JSON.parse(parse_comment)['comments'] | ||
comments.each_with_object([]) do |element, texts| | ||
texts << element['text'].tr("\n", ' ') | ||
end | ||
end | ||
|
||
def search_post_id | ||
page = Mechanize.new.get(link) | ||
page.search('app[entity-id]').to_s.match(/\d+/).to_s | ||
end | ||
|
||
def import_json_from_api | ||
"https://comments.api.onliner.by/news/tech.post/#{search_post_id}/comments?limit=#{COMMENTS_LIMIT}" | ||
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. I wanna see 50 top rated comments. Not just first 50 |
||
end | ||
|
||
def parse_comment | ||
Mechanize.new.get(import_json_from_api).body.to_s | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Commets rater | ||
class CommentsRater | ||
ACESS_KEY = YAML.load_file(File.join(Dir.pwd, 'secrets.yml'))['azure']['ACCES_KEY'] | ||
URI = 'https://westcentralus.api.cognitive.microsoft.com'.freeze | ||
PATH = '/text/analytics/v2.0/sentiment'.freeze | ||
|
||
attr_reader :uri, :documents, :request | ||
|
||
def initialize(texts) | ||
@uri = URI(URI + PATH) | ||
create_documents(texts) | ||
end | ||
|
||
def create_answer | ||
JSON.parse(create_response.body)['documents'].each_with_object([]) do |document, ratings| | ||
ratings << ((document['score'] * 200).to_i - 100) | ||
end | ||
end | ||
|
||
def create_response | ||
create_request | ||
Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http| | ||
http.request(request) | ||
end | ||
end | ||
|
||
def create_documents(texts) | ||
@documents = { documents: [] } | ||
texts.each_with_index do |text, index| | ||
documents[:documents] << { 'id' => index.to_s, 'language' => 'ru', 'text' => text } | ||
end | ||
end | ||
|
||
def create_request | ||
@request = Net::HTTP::Post.new(uri) | ||
request['Content-Type'] = 'application/json' | ||
request['Ocp-Apim-Subscription-Key'] = ACESS_KEY | ||
request.body = documents.to_json | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Model for Article | ||
class Article < Ohm::Model | ||
attribute :link | ||
attribute :title | ||
attribute :rating | ||
set :comments, :Comment | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Model for comments | ||
class Comment < Ohm::Model | ||
attribute :text | ||
attribute :rating | ||
reference :article, :Article | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<head> | ||
<meta charset='UTF-8' /> | ||
<title>Create articles</title> | ||
<style type="text/css"> | ||
|
||
a, button { | ||
font: bold 11px Arial; | ||
text-decoration: none; | ||
background-color: #EEEEEE; | ||
color: #333333; | ||
padding: 2px 6px 2px 6px; | ||
border-top: 1px solid #CCCCCC; | ||
border-right: 1px solid #333333; | ||
border-bottom: 1px solid #333333; | ||
border-left: 1px solid #CCCCCC; | ||
display:inline-block; | ||
margin-right:5px; | ||
} | ||
|
||
</style> | ||
</head> | ||
<body action='show' class='container'> | ||
<h3>Введите ссылку</h3> | ||
<form method='post'> | ||
<p><input type='text' name='link' id='link' /><p> | ||
<button type='submit'>Add link</button> | ||
<a href='/'>Back</a> | ||
</form> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<head> | ||
<meta charset='UTF-8' /> | ||
<title>Articles</title> | ||
<style type="text/css"> | ||
|
||
h1{ | ||
text-align: center; | ||
} | ||
|
||
.title{ | ||
text-decoration:none;color:#234b7b; | ||
} | ||
|
||
.title:hover{ | ||
color:Red; | ||
} | ||
|
||
.link { | ||
font: bold 11px Arial; | ||
text-decoration: none; | ||
background-color: #EEEEEE; | ||
color: #333333; | ||
padding: 2px 6px 2px 6px; | ||
border-top: 1px solid #CCCCCC; | ||
border-right: 1px solid #333333; | ||
border-bottom: 1px solid #333333; | ||
border-left: 1px solid #CCCCCC; | ||
display:inline-block; | ||
margin-right:5px; | ||
} | ||
|
||
body{ | ||
width:auto; | ||
padding:0 10px; | ||
font-size:11px; | ||
font-family:"Arial"; | ||
} | ||
|
||
table { | ||
border-width: medium; | ||
} | ||
|
||
td { | ||
border-bottom: 1px solid #ccc; | ||
padding: 10px; | ||
} | ||
|
||
</style> | ||
</head> | ||
<body> | ||
<h1>Articles</h1> | ||
<table class="table"> | ||
<thead> | ||
<tr> | ||
<th>Link</th> | ||
<th>Rating</th> | ||
</tr> | ||
</thead> | ||
<% @articles.each do |article| %> | ||
<tr><td><p><a class="title" href='/article/<%= article.id %>'><%= article.title %></a></p></td><td><%= article.rating %></td></tr> | ||
<% end %> | ||
</table> | ||
<p><a class="link" href='/article/create'>Add link</a></p> | ||
</body> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<head> | ||
<meta charset='UTF-8' /> | ||
<title>Article comments</title> | ||
<style type="text/css"> | ||
h1{ | ||
text-align: center; | ||
} | ||
|
||
a { | ||
font: bold 11px Arial; | ||
text-decoration: none; | ||
background-color: #EEEEEE; | ||
color: #333333; | ||
padding: 2px 6px 2px 6px; | ||
border-top: 1px solid #CCCCCC; | ||
border-right: 1px solid #333333; | ||
border-bottom: 1px solid #333333; | ||
border-left: 1px solid #CCCCCC; | ||
display:inline-block; | ||
margin-right:5px; | ||
} | ||
|
||
body{ | ||
width:auto; | ||
padding:0 10px; | ||
font-size:11px; | ||
font-family:"Arial"; | ||
} | ||
|
||
table { | ||
border-width: medium; | ||
} | ||
|
||
td { | ||
border-bottom: 1px solid #ccc; | ||
padding: 10px; | ||
} | ||
|
||
</style> | ||
</head> | ||
<body> | ||
<h1><%= @article.title %></p></h1> | ||
<a href='<%[email protected] %>'><%[email protected] %></a> | ||
<a href='/'>Back</a> | ||
<table class="table"> | ||
<tr> | ||
<th>Комментарии</th> | ||
<th>Рейтинг</th> | ||
</tr> | ||
<% @article.comments.each do |comment| %> | ||
<tr><td><%= comment.text %></a></td><td><%= comment.rating %></td></tr> | ||
<% end %> | ||
</table> | ||
</body> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
rly?