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

2312-3 #289

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
13 changes: 13 additions & 0 deletions 2312/3/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
source 'https://rubygems.org'

gem 'json'
gem 'mechanize'
gem 'ohm'
gem 'rake'
gem 'sinatra'
gem 'thin'

group :development do
gem 'pry'
gem 'shotgun'
end
83 changes: 83 additions & 0 deletions 2312/3/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
GEM
remote: https://rubygems.org/
specs:
coderay (1.1.2)
connection_pool (2.2.2)
daemons (1.2.6)
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 (2.1.0)
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)
mustermann (1.0.2)
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 (2.0.5)
rack-protection (2.0.3)
rack
rake (12.3.1)
redic (1.5.0)
hiredis
shotgun (0.9.2)
rack (>= 1.0)
sinatra (2.0.3)
mustermann (~> 1.0)
rack (~> 2.0)
rack-protection (= 2.0.3)
tilt (~> 2.0)
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
json
mechanize
ohm
pry
rake
shotgun
sinatra
thin

BUNDLED WITH
1.16.0
7 changes: 7 additions & 0 deletions 2312/3/config.ru
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'bundler'
Bundler.require

Dir.glob('./{controllers,lib,models}/*.rb').each { |file| require file }

map('/posts') { run PostsController }
map('/') { run ApplicationController }
13 changes: 13 additions & 0 deletions 2312/3/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# application controller
class ApplicationController < Sinatra::Base
set :views, File.expand_path(File.join(__FILE__, '../../views'))
set :method_override, true

not_found do
erb :not_found
end

get '/' do
redirect to '/posts'
end
end
40 changes: 40 additions & 0 deletions 2312/3/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
require_relative './application_controller.rb'

# posts controller
class PostsController < ApplicationController
get '/' do
@posts = Post.all
erb :'/posts/index'
end

get '/new' do
erb :'/posts/create'
end

post '/' do
@post = PostBuilder.new(params).create
redirect to '/'
end

get '/:id' do
@post = Post[params[:id]]
@comments = @post.comments
erb :'/posts/show'
end

get '/:id/edit' do
@post = Post[params[:id]]
erb :'posts/edit'
end

delete '/:id' do
Post[params[:id]].delete
redirect to '/'
end

post '/:id' do
@post = Post[params[:id]]
@post.update(link: params[:post])
redirect to "/#{params[:id]}"
end
end
14 changes: 14 additions & 0 deletions 2312/3/lib/comment_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# this module helps to ease navigation through comment parameters
module CommentHelper
def self.votes(options)
options['marks'].values.reduce(:+)
end

def self.author(options)
options['author']['name']
end

def self.text(options)
options['text']
end
end
15 changes: 15 additions & 0 deletions 2312/3/lib/comments_builder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# this class creates comment
class CommentsBuilder
attr_reader :comments, :post
def initialize(comments, given_post)
@comments = comments
@post = given_post
end

# :reek:FeatureEnvy
def create
comments.each do |comment|
Comment.create(author: comment[:author], text: comment[:text], rating: comment[:rating], post: post)
end
end
end
23 changes: 23 additions & 0 deletions 2312/3/lib/comments_referee.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# this class returns comments with ratings
class CommentsReferee
attr_reader :comments, :ratings
def initialize(comments_arr)
@comments = comments_arr
sort_comments_by_votes
@ratings = RatingCounter.new(comments).comments_ratings
end

# :reek:FeatureEnvy
def put_ratings
comments.zip(ratings).map do |record|
record.first[:rating] = record.last
record[0]
end
end

private

def sort_comments_by_votes
comments.sort_by { |comment| comment[:votes] }.reverse
end
end
29 changes: 29 additions & 0 deletions 2312/3/lib/post_builder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# this class creates a post
class PostBuilder
attr_reader :post_link, :comments, :post

def initialize(options)
@post_link = options[:post]
@comments = PostParser.new(post_link).comments
end

def create
@post = Post.create(link: post_link, title: title, rating: post_rating)
CommentsBuilder.new(comments_with_rating, post).create
post
end

private

def post_rating
comments_with_rating.map { |comment| comment[:rating] }.sum / comments.size
end

def comments_with_rating
CommentsReferee.new(comments).put_ratings
end

def title
PostParser.new(post_link).title
end
end
30 changes: 30 additions & 0 deletions 2312/3/lib/post_parser.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# this class processes given link, gets all comments, and returns hash with processed data
class PostParser
attr_reader :agent, :post_link
def initialize(link = 'https://people.onliner.by/2018/07/23/vodol')
@agent = Mechanize.new
@post_link = link
end

def title
agent.get(post_link).search('.news-header__title').text.strip
end

def comments
data['comments'].reverse.map do |comment|
{ author: CommentHelper.author(comment),
text: CommentHelper.text(comment),
votes: CommentHelper.votes(comment) }
end
end

private

def data
JSON.parse(@agent.get("https://comments.api.onliner.by/news/tech.post/#{post_id}/comments?limit=50").body)
end

def post_id
agent.get(post_link).search('app[entity-id]').to_s.match(/\d+/).to_s
end
end
29 changes: 29 additions & 0 deletions 2312/3/lib/rating_counter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require 'net/https'
require 'uri'
require 'json'

# this class returns all ratings of provided comments
class RatingCounter
attr_reader :request, :uri, :data, :comments

def initialize(sorted_comments)
@comments = sorted_comments
@data = data_for_request
@request = RequestGenerator.new(data)
end

def comments_ratings
response = request.response
JSON.parse(response.body)['documents'].map { |data| (data['score'] * 200).to_i - 100 }
end

private

def data_for_request
data = { documents: [] }
comments.each_with_index do |comment, index|
data[:documents] << { 'id' => index.to_s, 'language' => 'ru', 'text' => comment[:text] }
end
data
end
end
17 changes: 17 additions & 0 deletions 2312/3/lib/request_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# this class creates and sends request to Azure
class RequestGenerator
attr_reader :request, :uri
def initialize(data)
@uri = URI('https://westcentralus.api.cognitive.microsoft.com/text/analytics/v2.0/sentiment')
@request = Net::HTTP::Post.new(uri)
request['Content-Type'] = 'application/json'
request['Ocp-Apim-Subscription-Key'] = 'c3d16669aba747e5be12a669a1b7736a'
request.body = data.to_json
end

def response
Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
http.request(request)
end
end
end
7 changes: 7 additions & 0 deletions 2312/3/models/comment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Comment model
class Comment < Ohm::Model
attribute :author
attribute :text
attribute :rating
reference :post, :Post
end
8 changes: 8 additions & 0 deletions 2312/3/models/post.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Post model
class Post < Ohm::Model
attribute :link
attribute :title
attribute :rating
unique :link
collection :comments, :Comment
end
26 changes: 26 additions & 0 deletions 2312/3/views/layout.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<head>
<link href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
</head>
<body>
<link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" rel="stylesheet" id="bootstrap-css">
<div class="navbar">
<div class="navbar-inner">
<div class="container">
<a class="brand" href="/">Onliner.by Comments Parser</a>
<div class="nav-collapse">
<ul class="nav pull-right">
<li class="divider-vertical"></li>
<li>
<a href="/posts/new"><span class="glyphicon glyphicon-plus"></span><b> New post</b></a>
</li>
</ul>
</div><!-- /.nav-collapse -->
</div>
</div><!-- /navbar-inner -->
</div>
<div class="container">
<%= yield %>
</div>
</body>
12 changes: 12 additions & 0 deletions 2312/3/views/not_found.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<div class="row">
<div class="col-md-12">
<div class="error-template text-center">
<h1>404 Not Found</h1>
<div class="error-details">
Sorry, Requested page not found!

Don't you try to sneak about? -_-
</div>
</div>
</div>
</div>
7 changes: 7 additions & 0 deletions 2312/3/views/posts/create.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<h2>Input link, please:</h2>
<form action="/posts" method="post">
<div class="form-group">
<input type="text" class="form-control" id="post" aria-describedby="emailHelp" title="Example: https://tech.onliner.by/2018/07/26/eksperiment-biosfera-2" placeholder="Link to process" name="post" value="" pattern="^(http|https):\/\/\w+\.onliner.+" autofocus>
</div>
<button type="submit" class="btn btn-success">Process</button>
</form>
Loading