-
Notifications
You must be signed in to change notification settings - Fork 43
2264-3 #293
base: master
Are you sure you want to change the base?
2264-3 #293
Changes from all commits
f0406cb
f1ccfb6
eb66924
77ced0f
5c6626d
5b95d5a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
source 'https://rubygems.org' | ||
ruby '2.4.1' | ||
|
||
gem 'json' | ||
gem 'mechanize' | ||
gem 'ohm' | ||
gem 'pry' | ||
gem 'pry-nav' | ||
gem 'shotgun' | ||
gem 'sinatra' | ||
gem 'thin' |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
Bundler.require | ||
Dir.glob('./{controllers,helpers,models}/*.rb').sort.each { |file| require file } | ||
|
||
Post.redis = Redic.new('redis://127.0.0.1:6379/0') | ||
Comment.redis = Redic.new('redis://127.0.0.1:6379/1') | ||
|
||
use Rack::MethodOverride | ||
|
||
map('/') { run PostsController } |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
require 'sinatra' | ||
# main controller | ||
class ApplicationController < Sinatra::Base | ||
set :views, File.expand_path(File.join(__FILE__, '../../views')) | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
require 'sinatra' | ||
# controller for posts | ||
class PostsController < ApplicationController | ||
get '/posts/new' do | ||
erb :'post/new' | ||
end | ||
|
||
get '/posts' do | ||
@posts = Post.all | ||
erb :'post/index' | ||
end | ||
|
||
get '/post/:id' do | ||
@posts = Post.all | ||
@post = @posts[params[:id]] | ||
erb :'post/show' | ||
end | ||
|
||
post '/posts' do | ||
post = Post.create link: params[:link] | ||
PostAnalyser.new(post).launch | ||
redirect '/posts' | ||
end | ||
|
||
delete '/posts/:id/delete' do | ||
@post = Post.all[params[:id]] | ||
@post.delete | ||
redirect '/posts' | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
require 'net/https' | ||
require 'uri' | ||
require 'json' | ||
|
||
# Rating Counter | ||
class CommentRatingCounter | ||
URL = 'https://westcentralus.api.cognitive.microsoft.com'.freeze | ||
PATH = '/text/analytics/v2.0/sentiment'.freeze | ||
KEY = 'de8a560ccae541e08ec1a30dcdd191a4'.freeze | ||
|
||
attr_reader :uri, :documents, :request | ||
|
||
def initialize(texts) | ||
@uri = URI(URL + PATH) | ||
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. @uri - should be constant |
||
build_documents(texts) | ||
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 should not probably do anything in initializer except for saving input
|
||
end | ||
|
||
def launch_counter | ||
build_answer | ||
end | ||
|
||
private | ||
|
||
def build_documents(text_body) | ||
@documents = { documents: [] } | ||
text_body.each_with_index do |text, index| | ||
documents[:documents].push('id' => index.to_s, 'language' => 'ru', 'text' => text) | ||
end | ||
end | ||
|
||
def build_answer | ||
JSON.parse(build_response.body)['documents'].map do |document| | ||
(document['score'] * 200).to_i - 100 | ||
end | ||
end | ||
|
||
def build_response | ||
build_request | ||
Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http| | ||
http.request(request) | ||
end | ||
end | ||
|
||
def build_request | ||
@request = Net::HTTP::Post.new(uri) | ||
request['Content-Type'] = 'application/json' | ||
request['Ocp-Apim-Subscription-Key'] = KEY | ||
request.body = documents.to_json | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
require 'json' | ||
require 'mechanize' | ||
|
||
# Comments parser | ||
class CommentsParser | ||
attr_reader :agent, :path_to_page | ||
|
||
def initialize(link) | ||
@agent = Mechanize.new | ||
@path_to_page = link | ||
end | ||
|
||
def launch_parser | ||
parse_comments_from_page | ||
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. method should call at least 2 other methods - otherwise U should remove it: |
||
end | ||
|
||
private | ||
|
||
def parse_comments_from_page | ||
response = parse_comments | ||
comments = JSON.parse(response.body)['comments'] | ||
comments.map do |elem| | ||
elem['text'].sub("\n", ' ') | ||
end | ||
end | ||
|
||
def parse_page_code | ||
agent.get(path_to_page).parser.css('span.news_view_count').last.values[1] | ||
end | ||
|
||
def parse_comments | ||
on_url = "https://comments.api.onliner.by/news/tech.post/#{parse_page_code}/comments?limit=100&_=0.9046614793472092" | ||
agent.get(on_url) | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# class is used for analyzing onliner's posts | ||
class PostAnalyser | ||
attr_reader :post, :text_body, :ratings | ||
|
||
def initialize(post) | ||
@post = post | ||
@text_body = launch_comments_parser | ||
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.
seems that text_body doesn't belong to initializer 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. the same about |
||
@ratings = launch_rating_counter | ||
end | ||
|
||
def launch | ||
refresh_post_title | ||
build_post_stat | ||
refresh_post_rating | ||
end | ||
|
||
private | ||
|
||
def launch_comments_parser | ||
CommentsParser.new(post.link).launch_parser | ||
end | ||
|
||
def launch_rating_counter | ||
CommentRatingCounter.new(text_body).launch_counter | ||
end | ||
|
||
def refresh_post_title | ||
page = Mechanize.new.get(post.link) | ||
@post.update(title: page.title) | ||
end | ||
|
||
def build_post_stat | ||
text_body.each_with_index do |text, index| | ||
comment = Comment.create(text: text, rating: ratings[index]) | ||
post.comments.add(comment) | ||
end | ||
end | ||
|
||
def refresh_post_rating | ||
post_rating = (ratings.sum / text_body.size).to_i | ||
@post.update(rating: post_rating) | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# class for onliner's comments | ||
class Comment < Ohm::Model | ||
attribute :text | ||
attribute :rating | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# class for onliner posts | ||
class Post < Ohm::Model | ||
attribute :link | ||
attribute :title | ||
set :comments, :Comment | ||
attribute :rating | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<mete charset="UTF-8" /> | ||
<title>Your links</title> | ||
</head> | ||
<body> | ||
<%= yield %> | ||
</body> | ||
</html> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<div> | ||
<h1>Your Links</h1> | ||
<div> | ||
<table> | ||
<thead> | ||
<tr> | ||
<th>Links</th> | ||
<th>Title</th> | ||
</tr> | ||
</thead> | ||
|
||
<tbody> | ||
<% @posts.each do |post| %> | ||
<tr> | ||
<td><a href='/post/<%= post.id %>'><%= post.title %></a></td> | ||
<td><%= post.rating %></td> | ||
</tr> | ||
<% end %> | ||
</tbody> | ||
</table> | ||
</div> | ||
<div> | ||
<a href='/post/new'>Add new post</a> | ||
</div> | ||
</div> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
|
||
<h1>Add Post</h1> | ||
|
||
<form action="/posts" method="post"> | ||
<label for="post">Post's link</label> | ||
<input type="text" name="link" id="link" > | ||
<button type="submit">Add</button> | ||
</form> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<body> | ||
<div> | ||
<a href='<%[email protected] %>'> | ||
<%[email protected] %> | ||
</a> | ||
</div> | ||
<div> | ||
<a href='/'> | ||
Watch all posts | ||
</a> | ||
</div> | ||
<div> | ||
<form action="/posts/<%= @post.id %>/delete" method="post"> | ||
<input id="hidden" type="hidden" name="_method" value="delete"> | ||
<input type="submit" onclick="return confirm('Do you realy need to delete this post?')" value="Delete post"> | ||
</form> | ||
</div> | ||
<h1> | ||
<%= @post.title %> | ||
</h1> | ||
<table> | ||
<tr> | ||
<th>Comments</th> | ||
<th>Ratings</th> | ||
</tr> | ||
<% @post.comments.each do |comment| %> | ||
<tr> | ||
<td> | ||
<div> | ||
<%= comment.text %> | ||
</div> | ||
</td> | ||
<td> | ||
<%= comment.rating %> | ||
</td> | ||
</tr> | ||
<% end %> | ||
</table> | ||
</body> |
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.
should the class count raiting?
attr_reader should define visible for outside incoming and outgoing params:
I assume it should be texts and ratings
the rest is implementation details - it should be in private section (methods in private sections)