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

2227-3 #283

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions 2227/3/sinatra_project/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
secrets.yml
dumb.rdb
11 changes: 11 additions & 0 deletions 2227/3/sinatra_project/Gemfile
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'
86 changes: 86 additions & 0 deletions 2227/3/sinatra_project/Gemfile.lock
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 2227/3/sinatra_project/app/controllers/articles_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Controller Sinatra
# rubocop:disable Metrics/AbcSize, Metrics/MethodLength
Copy link
Collaborator

Choose a reason for hiding this comment

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

rly?

# 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
30 changes: 30 additions & 0 deletions 2227/3/sinatra_project/app/lib/ArticleDateBaseCreater.rb
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])) }
Copy link
Collaborator

Choose a reason for hiding this comment

The 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
28 changes: 28 additions & 0 deletions 2227/3/sinatra_project/app/lib/CommentsParser.rb
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}"
Copy link
Collaborator

Choose a reason for hiding this comment

The 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
40 changes: 40 additions & 0 deletions 2227/3/sinatra_project/app/lib/CommentsRater.rb
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
7 changes: 7 additions & 0 deletions 2227/3/sinatra_project/app/models/article.rb
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
6 changes: 6 additions & 0 deletions 2227/3/sinatra_project/app/models/comment.rb
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
30 changes: 30 additions & 0 deletions 2227/3/sinatra_project/app/views/create_article.erb
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>
65 changes: 65 additions & 0 deletions 2227/3/sinatra_project/app/views/show_article.erb
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>

54 changes: 54 additions & 0 deletions 2227/3/sinatra_project/app/views/show_comments.erb
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>
Loading