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

gem 'haml'
gem 'mechanize'
gem 'ohm'
gem 'redis'
gem 'scss'
gem 'sinatra'
gem 'sinatra-asset-pipeline'
gem 'sinatra-static-assets'

group :development do
gem 'rake'
end
105 changes: 105 additions & 0 deletions 2312/3/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
GEM
remote: https://rubygems.org/
specs:
coffee-script (2.4.1)
coffee-script-source
execjs
coffee-script-source (1.12.2)
concurrent-ruby (1.0.5)
connection_pool (2.2.2)
domain_name (0.5.20180417)
unf (>= 0.0.5, < 1.0.0)
execjs (2.7.0)
ffi (1.9.25)
haml (5.0.4)
temple (>= 0.8.0)
tilt
hiredis (0.6.1)
http-cookie (1.0.3)
domain_name (~> 0.5)
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)
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
rack (2.0.5)
rack-protection (2.0.3)
rack
rake (12.3.1)
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)
scss (1.0.2)
sinatra (2.0.3)
mustermann (~> 1.0)
rack (~> 2.0)
rack-protection (= 2.0.3)
tilt (~> 2.0)
sinatra-asset-pipeline (2.1.0)
coffee-script (~> 2.4)
rake (~> 12.3)
sass (~> 3.5)
sinatra (~> 2.0)
sprockets (~> 3.7)
sprockets-helpers (~> 1.2)
sinatra-static-assets (1.0.4)
sinatra (>= 1.1.0)
sprockets (3.7.2)
concurrent-ruby (~> 1.0)
rack (> 1, < 3)
sprockets-helpers (1.2.1)
sprockets (>= 2.2)
stal (0.3.0)
redic (~> 1.5)
temple (0.8.0)
tilt (2.0.8)
unf (0.1.4)
unf_ext
unf_ext (0.0.7.5)
webrobots (0.1.2)

PLATFORMS
ruby

DEPENDENCIES
haml
mechanize
ohm
rake
redis
scss
sinatra
sinatra-asset-pipeline
sinatra-static-assets

BUNDLED WITH
1.16.2
4 changes: 4 additions & 0 deletions 2312/3/Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require 'sinatra/asset_pipeline/task'
require './commen_analyzer'

Sinatra::AssetPipeline::Task.define! CommentAnalyzer
2 changes: 2 additions & 0 deletions 2312/3/app.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require_relative 'comment_analyzer'
CommentAnalyzer.run!
25 changes: 25 additions & 0 deletions 2312/3/comment_analyzer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require 'bundler'
Bundler.require
require 'json'
require 'haml'
require 'mechanize'
require 'ohm'
require 'redis'
require 'sass'
require 'sinatra'
require 'sinatra/asset_pipeline'
require 'sinatra/static_assets'
# Prepare assets to application
class CommentAnalyzer < Sinatra::Base
set :root, File.dirname(__FILE__)
set :assets_precompile, %w(*.css *.png *.jpg *.svg)
set :assets_paths, ['assets/stylesheets', 'assets/images']
set :assets_css_compressor, :sass
register Sinatra::AssetPipeline
def image_tag(path)
"<img src=\"#{image_path(path)}\"/>"
end
get '/' do
haml :index
end
end
14 changes: 14 additions & 0 deletions 2312/3/comments_json.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require './onliner_parser'
require './comments_reader'
require './api/text_analytics'
require './comments_rating'
# Class analyze comments and create json file
class CommentsJSON
attr_reader :json
def initialize(link)
comments_array = CommentsReader.new(link).comments_list
response_from_azure = TextAnalytic.new(comments_array).json
@json = CommentsRating.new(response_from_azure, comments_array).rating
end
end
puts CommentsJSON.new('https://tech.onliner.by/2018/07/24/dji-4').json
30 changes: 30 additions & 0 deletions 2312/3/comments_rating.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require './api/text_analytics.rb'
require './comments_reader.rb'
# This class add rating to each comment
class CommentsRating
attr_reader :response_from_azure, :api_rating, :comments_rating, :rating, :comments_array
def initialize(response_from_azure, comments_array)
@api_rating = response_from_azure['documents']
@comments_array = comments_array
@comments_rating = {}
calculate_comments_ratings
@rating = { post_rating: post_rating,
comments_ratings: comments_rating,
comments_id: comments_ids }
end
private
def post_rating
rating = 0
comments_rating.each { |_comment, coments_rating| rating += coments_rating }
(rating / comments_rating.size).to_i
end
def calculate_comments_ratings
api_rating.each do |comment|
id = comment['id'].to_i
comments_rating[id.to_i] = (comment['score'] * 200 - 100).to_i
end
end
def comments_ids
(0...api_rating.size).each_with_object({}) { |id, hash| hash[id] = comments_array[id] }
end
end
13 changes: 13 additions & 0 deletions 2312/3/comments_reader.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require './onliner_parser'
# This class extracts comments from json
class CommentsReader
attr_reader :link, :comments_list
def initialize(link)
@link = link
@comments_list = search_comments
end
private
def search_comments
OnlinerParser.new(link).json['comments'].map { |comment| comment['text'] }
end
end
2 changes: 2 additions & 0 deletions 2312/3/config.ru
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require_relative 'comment_analyzer'
run CommentAnalyzer
5 changes: 5 additions & 0 deletions 2312/3/models/comment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Comment < Ohm::Model
attribute :text
attribute :rating
collection :post, :Post
end
5 changes: 5 additions & 0 deletions 2312/3/models/post.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Post < Ohm::Model
attribute :link
attribute :post_rating
collection :comment, :Comment
end
21 changes: 21 additions & 0 deletions 2312/3/onliner_parser.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require 'mechanize'
require 'json'
# This class search post id from link to after send this id and get json with comments
class OnlinerParser
attr_reader :link, :json
COMMENTS_LIMIT = 50
def initialize(link)
@link = link
@json = parse_json
end
private

Choose a reason for hiding this comment

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

Layout/EmptyLinesAroundAccessModifier: Keep a blank line before and after private.

def search_post_id
Mechanize.new.get(link).search('app[entity-id]').to_s.match(/\d+/).to_s
end
def import_json_from_api

Choose a reason for hiding this comment

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

Layout/EmptyLineBetweenDefs: Use empty lines between method definitions.

"https://comments.api.onliner.by/news/tech.post/#{search_post_id}/comments?limit=#{COMMENTS_LIMIT}"
end
def parse_json

Choose a reason for hiding this comment

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

Layout/EmptyLineBetweenDefs: Use empty lines between method definitions.

JSON.parse Mechanize.new.get(import_json_from_api).body
end
end
24 changes: 24 additions & 0 deletions 2312/3/request_api.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'net/https'
require 'uri'
require 'json'
# Class send request to Azure API
class RequestApi
attr_reader :uri, :request
ACCESS_KEY = 'paste_your_key'.freeze
def initialize(data)
@uri = URI('https://westeurope.api.cognitive.microsoft.com/text/analytics/v2.0/sentiment')
@request = create_request(data)
end
def create_request(data)

Choose a reason for hiding this comment

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

Layout/EmptyLineBetweenDefs: Use empty lines between method definitions.

request = Net::HTTP::Post.new(uri)
request['Content-Type'] = 'application/json'
request['Ocp-Apim-Subscription-Key'] = ACCESS_KEY
request.body = data.to_json
request
end
def request_respond

Choose a reason for hiding this comment

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

Layout/EmptyLineBetweenDefs: Use empty lines between method definitions.

Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
http.request(request)
end
end
end
3 changes: 3 additions & 0 deletions 2312/3/stylesheets/_colors.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
$background_color: #e7e5e4;
$black_color: #131313;
$white_color: #ffffff;
3 changes: 3 additions & 0 deletions 2312/3/stylesheets/_mixins.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@mixin flex-container($direction, $wrap, $justify, $align) {
display: flex; flex-flow: $direction $wrap; justify-content: $justify; align-items: $align;
}
16 changes: 16 additions & 0 deletions 2312/3/stylesheets/app.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
@import 'colors';
@import 'mixins';
@import 'pages/index';

html, body {
width: 100%; height: 100%; margin: 0; padding: 0; background: $background-color;
font-family: 'Lato', sans-serif; font-weight: 400; font-size: 16px;
}
body { min-height: 100%; }
main, main .page { width: 100%; height: 100%; }

h1, h2, h3 { margin: 0 0 1rem 0; }

h1 { font-size: 3em; }
h2 { font-size: 2em; }
h3 { font-size: 1em; }
26 changes: 26 additions & 0 deletions 2312/3/stylesheets/pages/_index.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
main .page.indexPage {
@include flex-container(column, nowrap, center, center);
text-align: center;
.title {
margin-bottom: 5em;
h1 {
color: $black_color; font-size: 5.75em; letter-spacing: .15em;

Choose a reason for hiding this comment

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

Name of variable black_color should be written in all lowercase letters with hyphens instead of underscores
Property 'font-size' should be placed on own line
Properties should be ordered color, font-size, font-weight, letter-spacing, text-shadow
Property 'letter-spacing' should be placed on own line
.15 should be written with a leading zero as 0.15

font-weight: 400; text-shadow:

Choose a reason for hiding this comment

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

Property 'text-shadow' should be placed on own line
Colon after property should be followed by one space

1px -1px 0 #767676, -1px 2px 1px #737272,

Choose a reason for hiding this comment

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

Color literals like #e4e3e2 should only be used in variable declarations; they should be referred to via variable everywhere else.

-2px 4px 1px #767474, -3px 6px 1px #787777,
-4px 8px 1px #7b7a7a, -5px 10px 1px #7f7d7d,
-6px 12px 1px #828181, -7px 14px 1px #868585,
-8px 16px 1px #8b8a89, -9px 18px 1px #8f8e8d,
-10px 20px 1px #949392, -11px 22px 1px #999897,
-12px 24px 1px #9e9c9c, -13px 26px 1px #a3a1a1,
-14px 28px 1px #a8a6a6, -15px 30px 1px #adabab,
-16px 32px 1px #b2b1b0, -17px 34px 1px #b7b6b5,
-18px 36px 1px #bcbbba, -19px 38px 1px #c1bfbf,
-20px 40px 1px #c6c4c4, -21px 42px 1px #cbc9c8,
-22px 44px 1px #cfcdcd, -23px 46px 1px #d4d2d1,
-24px 48px 1px #d8d6d5, -25px 50px 1px #dbdad9,
-26px 52px 1px #dfdddc, -27px 54px 1px #e2e0df,
-28px 56px 1px #e4e3e2; }

Choose a reason for hiding this comment

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

Rule declaration should be followed by an empty line

text-rendering: optimizeLegibility;

Choose a reason for hiding this comment

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

Expected item on line 24 to appear before line 6. Rule sets should be ordered as follows: @extends, @includes without @content, properties, @includes with @content, nested rule sets
Line should be indented 4 spaces, but was indented 6 spaces

}
}
27 changes: 27 additions & 0 deletions 2312/3/text_analytics.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require './api/request_api.rb'
require 'json'
# This class works with Microsoft Azure Text Analytic API
class TextAnalytic
attr_reader :comments, :request_api, :request, :request_respond
def initialize(comments_list)
@comments = comments_list
@request_api = RequestApi.new(request_data)
@request = request_api.request
@request_respond = request_api.request_respond
end
def json

Choose a reason for hiding this comment

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

Layout/EmptyLineBetweenDefs: Use empty lines between method definitions.

JSON.parse request_respond.body
end
private

Choose a reason for hiding this comment

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

Layout/EmptyLinesAroundAccessModifier: Keep a blank line before and after private.

def request_data

Choose a reason for hiding this comment

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

TooManyStatements: TextAnalytic#request_data has approx 8 statements. More info.

data = {}
comment_id = -1
hash_array = []
comments.each do |comment|
comment_id += 1
hash_array << { 'id' => comment_id, 'language' => 'ru', 'text' => comment }
end
data['documents'] = hash_array
data
end
end
6 changes: 6 additions & 0 deletions 2312/3/views/index.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.page.indexPage
.title
%h1 Comment Analyzer
%form
%input
%button Анализировать
10 changes: 10 additions & 0 deletions 2312/3/views/layout.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
!!!
%html
%head
%title Comment Analyzer
= stylesheet_tag 'app'
%link(href='https://fonts.googleapis.com/css?family=Lato:400,400italic,700,700italic' rel='stylesheet' type='text/css')

Choose a reason for hiding this comment

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

Line is too long. [123/80]
Prefer the hash attributes syntax (%tag{ lang: 'en' }) over HTML attributes syntax (%tag(lang=en))

%link{ :type => "image/png", :href => image_path("favicon.ico"), :rel => "shortcut icon" }

Choose a reason for hiding this comment

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

Line is too long. [94/80]

%meta{:name => 'viewport', :content => 'width=device-width; initial-scale=1; user-scalable=0;'}

Choose a reason for hiding this comment

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

Line is too long. [99/80]
Hash attribute should start with one space after the opening brace
Hash attribute should end with one space before the closing brace

%body
%main= yield