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

2407 - 3 #287

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
16 changes: 16 additions & 0 deletions 2407/3/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

source 'https://rubygems.org'

git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }

gem 'faraday'
gem 'mechanize'
gem 'ohm'
gem 'redis'
gem 'sinatra'
gem 'slim'

group :development do
gem 'shotgun'
end
104 changes: 104 additions & 0 deletions 2407/3/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
GEM
remote: https://rubygems.org/
specs:
autoprefixer-rails (9.0.0)
execjs
bootstrap (4.1.3)
autoprefixer-rails (>= 6.0.3)
popper_js (>= 1.12.9, < 2)
sass (>= 3.5.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)
execjs (2.7.0)
faraday (0.15.2)
multipart-post (>= 1.2, < 3)
ffi (1.9.25)
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)
mime-types (3.1)
mime-types-data (~> 3.2015)
mime-types-data (3.2016.0521)
mini_portile2 (2.3.0)
multipart-post (2.0.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
popper_js (1.14.3)
rack (2.0.5)
rack-protection (2.0.3)
rack
rb-fsevent (0.10.3)
rb-inotify (0.9.10)
ffi (>= 0.5.0, < 2)
redic (1.5.0)
hiredis
redis (4.0.1)
sass (3.5.7)
sass-listen (~> 4.0.0)
sass-listen (4.0.0)
rb-fsevent (~> 0.9, >= 0.9.4)
rb-inotify (~> 0.9, >= 0.9.7)
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)
slim (3.0.9)
temple (>= 0.7.6, < 0.9)
tilt (>= 1.3.3, < 2.1)
stal (0.3.0)
redic (~> 1.5)
temple (0.8.0)
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
bootstrap
faraday
json
mechanize
ohm
redis
shotgun
sinatra
slim
thin

BUNDLED WITH
1.16.2
9 changes: 9 additions & 0 deletions 2407/3/config.ru
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require 'sinatra/base'
require 'bundler'
Bundler.require

use Rack::MethodOverride
Dir.glob('./{helpers,controllers,models}/*.rb').each { |file| require file }
Article.redis = Redic.new('redis://127.0.0.1:6379')
Comment.redis = Redic.new('redis://127.0.0.1:6379')
map('/articles') { run ArticlesController }
6 changes: 6 additions & 0 deletions 2407/3/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class ApplicationController < Sinatra::Base
set :views, File.expand_path('../views', __dir__)
not_found do
slim :not_found
end
end
34 changes: 34 additions & 0 deletions 2407/3/controllers/articles_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require_relative 'application_controller'

class ArticlesController < ApplicationController
get '/' do
@articles = Article.all
slim :index
end

get '/add' do
slim :add
end

post '/add' do
article = ArticleCreator.new(params[:link])
article_db = Article.create link: params[:link], rating: article.rating
article.comments.map { |coment| article_db.comments.add(Comment.create(text: coment.text, rating: coment.rating)) }
redirect '/articles'
end

get '/:id' do
@article = Article.all[params[:id]]
slim :show_article
end

delete '/:id' do
Article.all[params[:id]].delete
redirect '/articles'
end

delete '/' do
Article.all.each(&:delete)
redirect '/articles'
end
end
13 changes: 13 additions & 0 deletions 2407/3/helpers/article_creator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class ArticleCreator
def initialize(link)
@link = link
end

def rating
comments.map(&:rating).reduce(:+) / comments.size
end

def comments
@comments ||= CommentsCreator.new(@link).create
end
end
19 changes: 19 additions & 0 deletions 2407/3/helpers/comments_creator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class CommentsCreator
def initialize(link)
@link = link
end

def create
comments_as_strings.map.with_index { |text, index| SpecificComment.new(text, ratings_of_comments[index]) }
end

private

def ratings_of_comments
@ratings_of_comments ||= TextChecker.new(comments_as_strings).check
end

def comments_as_strings
@comments_as_strings ||= CommentsParser.new(@link).parse
end
end
34 changes: 34 additions & 0 deletions 2407/3/helpers/comments_parser.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require 'json'
require 'mechanize'

class CommentsParser
def initialize(link)
@link = link
end

def parse
response_as_json['comments'].map { |comment| comment['text'] }
end

private

def response_as_json
JSON.parse(response_of_server.body)
end

def response_of_server
Faraday.get "https://comments.api.onliner.by/news/#{arcticle_category}.post/#{article_id}/comments?limit=50"
end

def arcticle_category
@link.split('.').first.split('/').last
end

def article_id
page.css('.news_view_count').attr('news_id').value
end

def page
Mechanize.new.get(@link)
end
end
7 changes: 7 additions & 0 deletions 2407/3/helpers/specific_comment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class SpecificComment
attr_reader :text, :rating
def initialize(text, rating)
@text = text
@rating = rating
end
end
37 changes: 37 additions & 0 deletions 2407/3/helpers/text_checker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require 'uri'
require 'json'
require 'net/https'

class TextChecker
ACCESS_KEY = 'c2b502fd9bf046c0ac3820967368c382'.freeze
LINK = 'https://westcentralus.api.cognitive.microsoft.com/text/analytics/v2.0/sentiment'.freeze
URL = URI(LINK)
def initialize(texts)
@texts = texts
end

def check
JSON.parse(response.body)['documents'].map { |text| (text['score'].round(2) * 200 - 100).to_i }
end

private

def response
Net::HTTP.start(URL.host, URL.port, use_ssl: URL.scheme == 'https') do |http|
http.request(request_preparation)
end
end

def request_preparation
request = Net::HTTP::Post.new(URL, 'Content-Type' => 'application/json', 'Ocp-Apim-Subscription-Key' => ACCESS_KEY)
request.body = document.to_json
request
end
# rubocop:disable Lint/UnneededCopDisableDirective
# rubocop:disable Lint/Syntax
def document
{ 'documents': @texts.map.with_index { |text, index| { 'id' => index.to_s, 'language' => 'ru', 'text' => text } } }

Choose a reason for hiding this comment

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

Lint/Syntax: unexpected token tCOLON
Lint/Syntax: unexpected token tRCURLY

Choose a reason for hiding this comment

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

Lint/Syntax: unexpected token tCOLON
Lint/Syntax: unexpected token tRCURLY

end
# rubocop:enable Lint/Syntax
# rubocop:enable Lint/UnneededCopDisableDirective
end
6 changes: 6 additions & 0 deletions 2407/3/models/article.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require 'ohm'
class Article < Ohm::Model
attribute :link
attribute :rating
set :comments, :Comment
end
5 changes: 5 additions & 0 deletions 2407/3/models/comment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'ohm'
class Comment < Ohm::Model
attribute :text
attribute :rating
end
7 changes: 7 additions & 0 deletions 2407/3/views/add.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
div class="jumbotron"
p class="lead" Just paste in link to the article you want to add
form method='post'
input class="form-control" name='link'
hr class="my-4"
p class="lead"
button class="btn btn-primary btn-lg" type='submit' Add link
19 changes: 19 additions & 0 deletions 2407/3/views/index.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
nav class="navbar"
a сlass="navbar-brand"
h1 OnlinerArticlesAssestments
a class="btn btn-primary btn-lg" href="/articles/add" role="button" Add an article
.container
table class='table table table'
thead class='thead-dark'
tr
th Links
th Rating
tbody
- @articles.each do |article|
tr
td
a href="articles/#{article.id}" = article.link
td
= article.rating
form action='/articles' method='post'
button type='submit' id='hidden' name='_method' value='delete' class='btn btn-warning btn-lg' onclick="return confirm('Are you really sure that you want to delete all articles?')" Delete all articles
10 changes: 10 additions & 0 deletions 2407/3/views/layout.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
doctype html
html lang="ru"
head
meta content=("text/html; charset=UTF-8") http-equiv="Content-Type" /
meta charset="utf-8" /
meta content="width=device-width, initial-scale=1, shrink-to-fit=no" name="viewport" /
link crossorigin="anonymous" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" integrity="sha384-PsH8R72JQ3SOdhVi3uxftmaW6Vc51MKb0q5P2rRUpPvrszuE4W1povHYgTpBfshb" rel="stylesheet" /
body
div class='jumbotron'
== yield
24 changes: 24 additions & 0 deletions 2407/3/views/show_article.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
div
table class="table"
thead align='middle'
tr
th
a class='btn btn-outline-dark' href="/articles" role="button" Go back
th
h1 = @article.link
th
form action="#{@article.id}" method='post'
button type='submit' id='hidden' name='_method' value='delete' class='btn btn-outline-dark' Delete an article
div
table class="table table-striped"
thead class="thead-dark"
tr
th Comments
th Rate
tbody
- @article.comments.each do |comment|
tr
td
= comment.text
td
= comment.rating