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

2264-3 #293

Open
wants to merge 6 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
11 changes: 11 additions & 0 deletions 2264/3/Gemfile
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'
9 changes: 9 additions & 0 deletions 2264/3/config.ru
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 }
5 changes: 5 additions & 0 deletions 2264/3/controllers/application_controller.rb
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
30 changes: 30 additions & 0 deletions 2264/3/controllers/posts_controller.rb
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
50 changes: 50 additions & 0 deletions 2264/3/helpers/comment_rating_counter.rb
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
Copy link

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)


def initialize(texts)
@uri = URI(URL + PATH)
Copy link

Choose a reason for hiding this comment

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

@uri - should be constant

build_documents(texts)
Copy link

@kislak kislak Aug 8, 2018

Choose a reason for hiding this comment

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

I should not probably do anything in initializer except for saving input

def initialize(texts)
  @texts = texts
end

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
35 changes: 35 additions & 0 deletions 2264/3/helpers/comments_parser.rb
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
Copy link

Choose a reason for hiding this comment

The 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:
U can just put the body 'parse_comments_from_page' right here

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
43 changes: 43 additions & 0 deletions 2264/3/helpers/post_analyzer.rb
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
Copy link

@kislak kislak Aug 8, 2018

Choose a reason for hiding this comment

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

def text_body
  @text_body ||= launch_comments_parser
end

seems that text_body doesn't belong to initializer

Copy link

@kislak kislak Aug 8, 2018

Choose a reason for hiding this comment

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

the same about @rating

@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
5 changes: 5 additions & 0 deletions 2264/3/models/comment.rb
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
7 changes: 7 additions & 0 deletions 2264/3/models/post.rb
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
10 changes: 10 additions & 0 deletions 2264/3/views/layout.erb
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>
25 changes: 25 additions & 0 deletions 2264/3/views/post/index.erb
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>
8 changes: 8 additions & 0 deletions 2264/3/views/post/new.erb
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>
39 changes: 39 additions & 0 deletions 2264/3/views/post/show.erb
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>