From b7e0e254525dd5e557e2e4e4894bf2e11347c889 Mon Sep 17 00:00:00 2001 From: Marie Date: Fri, 27 Jul 2018 17:20:38 +0300 Subject: [PATCH 01/16] Created gitignore and working application --- 2377/.gitignore | 1 + 2377/3/sinatra/Gemfile | 13 +++++ 2377/3/sinatra/Gemfile.lock | 72 ++++++++++++++++++++++++++++ 2377/3/sinatra/app.rb | 42 ++++++++++++++++ 2377/3/sinatra/azure_api.rb | 32 +++++++++++++ 2377/3/sinatra/config.yml | 1 + 2377/3/sinatra/models/comment.rb | 6 +++ 2377/3/sinatra/models/link.rb | 7 +++ 2377/3/sinatra/parser.rb | 19 ++++++++ 2377/3/sinatra/views/active_page.erb | 38 +++++++++++++++ 2377/3/sinatra/views/analysis.erb | 19 ++++++++ 2377/3/sinatra/views/main.erb | 15 ++++++ 12 files changed, 265 insertions(+) create mode 100644 2377/.gitignore create mode 100644 2377/3/sinatra/Gemfile create mode 100644 2377/3/sinatra/Gemfile.lock create mode 100644 2377/3/sinatra/app.rb create mode 100644 2377/3/sinatra/azure_api.rb create mode 100644 2377/3/sinatra/config.yml create mode 100644 2377/3/sinatra/models/comment.rb create mode 100644 2377/3/sinatra/models/link.rb create mode 100644 2377/3/sinatra/parser.rb create mode 100644 2377/3/sinatra/views/active_page.erb create mode 100644 2377/3/sinatra/views/analysis.erb create mode 100644 2377/3/sinatra/views/main.erb diff --git a/2377/.gitignore b/2377/.gitignore new file mode 100644 index 000000000..1d3ed4c17 --- /dev/null +++ b/2377/.gitignore @@ -0,0 +1 @@ +config.yml diff --git a/2377/3/sinatra/Gemfile b/2377/3/sinatra/Gemfile new file mode 100644 index 000000000..cdbbbc7e7 --- /dev/null +++ b/2377/3/sinatra/Gemfile @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +source "https://rubygems.org" + +gem "sinatra" +gem "ohm" +gem "mechanize" + +group :development do + gem "pry" + gem "shotgun" +end + diff --git a/2377/3/sinatra/Gemfile.lock b/2377/3/sinatra/Gemfile.lock new file mode 100644 index 000000000..08d778cd0 --- /dev/null +++ b/2377/3/sinatra/Gemfile.lock @@ -0,0 +1,72 @@ +GEM + remote: https://rubygems.org/ + specs: + coderay (1.1.2) + connection_pool (2.2.2) + domain_name (0.5.20180417) + unf (>= 0.0.5, < 1.0.0) + 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) + 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 + 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) + tilt (2.0.8) + unf (0.1.4) + unf_ext + unf_ext (0.0.7.5) + webrobots (0.1.2) + +PLATFORMS + ruby + +DEPENDENCIES + mechanize + ohm + pry + shotgun + sinatra + +BUNDLED WITH + 1.16.2 diff --git a/2377/3/sinatra/app.rb b/2377/3/sinatra/app.rb new file mode 100644 index 000000000..fba2acbd8 --- /dev/null +++ b/2377/3/sinatra/app.rb @@ -0,0 +1,42 @@ +require "sinatra/base" +require_relative 'models/link' +require_relative 'models/comment' +require_relative 'parser' +require_relative 'azure_api' +require 'ohm' +require 'yaml' +# Main class for application +class MyApp < Sinatra::Base + post '/get-link' do + link = Link.create :link => params[:link] + please = Parser.new + please.get_comments(link) + please.parse_json(link) + pray = TextAnalytics.new + link.comments.each do |comment| + pray.analyse(comment) + end + total = link.comments.inject(0) { |total, comment| total + comment.score.to_i } / link.comments.size + link.update :score => total + erb :active_page + end + + get '/' do + erb :main + end + + post '/clear' do + Ohm.redis.call("FLUSHALL") + erb :active_page + end + + get '/analysis/:id' do + @comments = Link[params[:id]].comments + + erb :analysis + end + + # $0 is the executed file + # __FILE__ is the current file + run! if __FILE__ == $0 +end diff --git a/2377/3/sinatra/azure_api.rb b/2377/3/sinatra/azure_api.rb new file mode 100644 index 000000000..ba9f5ea5a --- /dev/null +++ b/2377/3/sinatra/azure_api.rb @@ -0,0 +1,32 @@ +# rubocop:disable Style/UnneededInterpolation +require 'net/https' +require 'uri' +require 'json' +# Analysis via Azure API +class TextAnalytics + def analyse(comment) + uri = 'https://westeurope.api.cognitive.microsoft.com' + path = '/text/analytics/v2.0/sentiment' + uri = URI(uri + path) + + documents = { 'documents': [{ 'id' => '1', 'language' => 'ru', 'text' => "#{comment.comment}" }] } + + request = Net::HTTP::Post.new(uri) + request['Content-Type'] = 'application/json' + access_key = YAML.load_file(File.join(Dir.pwd, 'config.yml'))['access_key'] + request['Ocp-Apim-Subscription-Key'] = access_key + + request.body = documents.to_json + + response = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http| + http.request request + end + comment.update score: convert((JSON response.body)['documents'][0]['score']) + end + + def convert(value) + x = 1 / value + 200 / x - 100 + end +end +# rubocop:enable Style/UnneededInterpolation diff --git a/2377/3/sinatra/config.yml b/2377/3/sinatra/config.yml new file mode 100644 index 000000000..d3ba06370 --- /dev/null +++ b/2377/3/sinatra/config.yml @@ -0,0 +1 @@ +access_key : '31b7fe3bb2be4d04abbe29711030675a' diff --git a/2377/3/sinatra/models/comment.rb b/2377/3/sinatra/models/comment.rb new file mode 100644 index 000000000..22d3c1b03 --- /dev/null +++ b/2377/3/sinatra/models/comment.rb @@ -0,0 +1,6 @@ +require 'ohm' +# Describes comment +class Comment < Ohm::Model + attribute :comment + attribute :score +end diff --git a/2377/3/sinatra/models/link.rb b/2377/3/sinatra/models/link.rb new file mode 100644 index 000000000..9417db939 --- /dev/null +++ b/2377/3/sinatra/models/link.rb @@ -0,0 +1,7 @@ +require 'ohm' +# Describes link +class Link < Ohm::Model + attribute :link + list :comments, :Comment + attribute :score +end diff --git a/2377/3/sinatra/parser.rb b/2377/3/sinatra/parser.rb new file mode 100644 index 000000000..666d3ad6d --- /dev/null +++ b/2377/3/sinatra/parser.rb @@ -0,0 +1,19 @@ +require 'open-uri' +require 'nokogiri' +require 'net/http' +require 'json' +# Parsing of comments +class Parser + def get_comments(link) + response = Net::HTTP.get_response(URI.parse(link.link)).body + news_id = Nokogiri::HTML(response).xpath('//span[@news_id]').to_s.match(/\d+/).to_s + @json_comments = JSON.parse(Net::HTTP.get_response(URI.parse('https://comments.api.onliner.by/news/tech.post/' + news_id + '/comments?limit=50')).body) + end + + def parse_json(link) + comments = @json_comments['comments'].each.map { |hash| hash['text'] } + comments.each do |comment| + link.comments.push(Comment.create(comment: comment)) + end + end +end diff --git a/2377/3/sinatra/views/active_page.erb b/2377/3/sinatra/views/active_page.erb new file mode 100644 index 000000000..2015d1a1f --- /dev/null +++ b/2377/3/sinatra/views/active_page.erb @@ -0,0 +1,38 @@ + + + + + analysis + + + +
+

+ +

+
+
+

+
+ + + <%if Link.all%> + <% Link.all.each do |link| %> + + + + + <% end%> + <% end%> +
ваша ссылочка рейтинг
<%=link.link%> <%= link.score%> + + target="_blank"> +
+ + + + diff --git a/2377/3/sinatra/views/analysis.erb b/2377/3/sinatra/views/analysis.erb new file mode 100644 index 000000000..00ccd8ea6 --- /dev/null +++ b/2377/3/sinatra/views/analysis.erb @@ -0,0 +1,19 @@ + + + + + analysis + + + + <%if @comments%> + <% @comments.each do |comment| %> + + + + <% end%> + <% end%> +
<%=comment.comment%> <%=comment.score%> +
+ + diff --git a/2377/3/sinatra/views/main.erb b/2377/3/sinatra/views/main.erb new file mode 100644 index 000000000..eab120f37 --- /dev/null +++ b/2377/3/sinatra/views/main.erb @@ -0,0 +1,15 @@ + + + + + analysis + + + +
+

+
+ + From 4e8b20017217b18858cecc46d9aace56fcea126d Mon Sep 17 00:00:00 2001 From: Marie Date: Fri, 27 Jul 2018 17:23:32 +0300 Subject: [PATCH 02/16] Fixed --- 2377/3/sinatra/config.yml | 1 - 1 file changed, 1 deletion(-) delete mode 100644 2377/3/sinatra/config.yml diff --git a/2377/3/sinatra/config.yml b/2377/3/sinatra/config.yml deleted file mode 100644 index d3ba06370..000000000 --- a/2377/3/sinatra/config.yml +++ /dev/null @@ -1 +0,0 @@ -access_key : '31b7fe3bb2be4d04abbe29711030675a' From c7e1a8a150316f7efe566dbb65fe60a6fa354168 Mon Sep 17 00:00:00 2001 From: Marie Date: Mon, 30 Jul 2018 09:46:33 +0300 Subject: [PATCH 03/16] Minor dog fixes --- 2377/3/sinatra/app.rb | 2 +- 2377/3/sinatra/azure_api.rb | 18 +++++++++--------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/2377/3/sinatra/app.rb b/2377/3/sinatra/app.rb index fba2acbd8..8f4cc6c29 100644 --- a/2377/3/sinatra/app.rb +++ b/2377/3/sinatra/app.rb @@ -38,5 +38,5 @@ class MyApp < Sinatra::Base # $0 is the executed file # __FILE__ is the current file - run! if __FILE__ == $0 + run! if $PROGRAM_NAME == __FILE__ end diff --git a/2377/3/sinatra/azure_api.rb b/2377/3/sinatra/azure_api.rb index ba9f5ea5a..4bfd7347d 100644 --- a/2377/3/sinatra/azure_api.rb +++ b/2377/3/sinatra/azure_api.rb @@ -1,4 +1,3 @@ -# rubocop:disable Style/UnneededInterpolation require 'net/https' require 'uri' require 'json' @@ -13,20 +12,21 @@ def analyse(comment) request = Net::HTTP::Post.new(uri) request['Content-Type'] = 'application/json' - access_key = YAML.load_file(File.join(Dir.pwd, 'config.yml'))['access_key'] request['Ocp-Apim-Subscription-Key'] = access_key - request.body = documents.to_json - response = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http| http.request request end - comment.update score: convert((JSON response.body)['documents'][0]['score']) + @value = (JSON response.body)['documents'][0]['score'] + comment.update score: convert + end + + def convert + proportion = 1 / @value + 200 / proportion - 100 end - def convert(value) - x = 1 / value - 200 / x - 100 + def access_key + @access_key = YAML.load_file(File.join(Dir.pwd, 'config.yml'))['access_key'] end end -# rubocop:enable Style/UnneededInterpolation From 39ee0e8a303743c186855193f8060c544f0fce9b Mon Sep 17 00:00:00 2001 From: Marie Date: Tue, 31 Jul 2018 15:28:02 +0300 Subject: [PATCH 04/16] Fixing in process --- 2377/3/sinatra/Gemfile | 13 ++++++------- 2377/3/sinatra/app.rb | 10 +++++----- 2377/3/sinatra/azure_api.rb | 7 +++---- 3 files changed, 14 insertions(+), 16 deletions(-) diff --git a/2377/3/sinatra/Gemfile b/2377/3/sinatra/Gemfile index cdbbbc7e7..2221d0dfb 100644 --- a/2377/3/sinatra/Gemfile +++ b/2377/3/sinatra/Gemfile @@ -1,13 +1,12 @@ # frozen_string_literal: true -source "https://rubygems.org" +source 'https://rubygems.org' -gem "sinatra" -gem "ohm" -gem "mechanize" +gem 'mechanize' +gem 'ohm' +gem 'sinatra' group :development do - gem "pry" - gem "shotgun" + gem 'pry' + gem 'shotgun' end - diff --git a/2377/3/sinatra/app.rb b/2377/3/sinatra/app.rb index 8f4cc6c29..b609cb74e 100644 --- a/2377/3/sinatra/app.rb +++ b/2377/3/sinatra/app.rb @@ -1,4 +1,4 @@ -require "sinatra/base" +require 'sinatra/base' require_relative 'models/link' require_relative 'models/comment' require_relative 'parser' @@ -8,7 +8,7 @@ # Main class for application class MyApp < Sinatra::Base post '/get-link' do - link = Link.create :link => params[:link] + link = Link.create link: params[:link] please = Parser.new please.get_comments(link) please.parse_json(link) @@ -16,11 +16,11 @@ class MyApp < Sinatra::Base link.comments.each do |comment| pray.analyse(comment) end - total = link.comments.inject(0) { |total, comment| total + comment.score.to_i } / link.comments.size - link.update :score => total + total = link.comments.inject(0) { |sum, comment| sum + comment.score.to_i } / link.comments.size + link.update score: total erb :active_page end - + get '/' do erb :main end diff --git a/2377/3/sinatra/azure_api.rb b/2377/3/sinatra/azure_api.rb index 4bfd7347d..23873dcfd 100644 --- a/2377/3/sinatra/azure_api.rb +++ b/2377/3/sinatra/azure_api.rb @@ -17,12 +17,11 @@ def analyse(comment) response = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http| http.request request end - @value = (JSON response.body)['documents'][0]['score'] - comment.update score: convert + comment.update score: convert((JSON response.body)['documents'][0]['score']) end - def convert - proportion = 1 / @value + def convert(value) + proportion = 1 / value 200 / proportion - 100 end From 355305945529a68b4b386e84ab550e2bb7f41d56 Mon Sep 17 00:00:00 2001 From: Marie Date: Tue, 31 Jul 2018 15:47:35 +0300 Subject: [PATCH 05/16] Fixed parser --- 2377/3/sinatra/app.rb | 1 - 2377/3/sinatra/parser.rb | 24 ++++++++++++++++-------- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/2377/3/sinatra/app.rb b/2377/3/sinatra/app.rb index b609cb74e..b24b5e42d 100644 --- a/2377/3/sinatra/app.rb +++ b/2377/3/sinatra/app.rb @@ -11,7 +11,6 @@ class MyApp < Sinatra::Base link = Link.create link: params[:link] please = Parser.new please.get_comments(link) - please.parse_json(link) pray = TextAnalytics.new link.comments.each do |comment| pray.analyse(comment) diff --git a/2377/3/sinatra/parser.rb b/2377/3/sinatra/parser.rb index 666d3ad6d..650acd4a4 100644 --- a/2377/3/sinatra/parser.rb +++ b/2377/3/sinatra/parser.rb @@ -4,16 +4,24 @@ require 'json' # Parsing of comments class Parser + def get_comments(link) - response = Net::HTTP.get_response(URI.parse(link.link)).body - news_id = Nokogiri::HTML(response).xpath('//span[@news_id]').to_s.match(/\d+/).to_s - @json_comments = JSON.parse(Net::HTTP.get_response(URI.parse('https://comments.api.onliner.by/news/tech.post/' + news_id + '/comments?limit=50')).body) - end - - def parse_json(link) - comments = @json_comments['comments'].each.map { |hash| hash['text'] } + json_comments = JSON.parse(Net::HTTP.get_response(URI.parse('https://comments.api.onliner.by/news/tech.post/' + get_id(link) + '/comments?limit=50')).body) + comments = json_comments['comments'].each.map { |hash| hash['text'] } comments.each do |comment| - link.comments.push(Comment.create(comment: comment)) + link.comments.push(Comment.create(comment: comment)) end end + + private + + def response(link) + Net::HTTP.get_response(URI.parse(link.link)).body + end + + def get_id(link) + Nokogiri::HTML(response(link)).xpath('//span[@news_id]').to_s.match(/\d+/).to_s + end + + end From d7a640f71197f7c268f066f7df0f9588f4cefbe1 Mon Sep 17 00:00:00 2001 From: Marie Date: Tue, 31 Jul 2018 16:02:32 +0300 Subject: [PATCH 06/16] Parser fully fixed --- 2377/3/sinatra/parser.rb | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/2377/3/sinatra/parser.rb b/2377/3/sinatra/parser.rb index 650acd4a4..9fcf1784c 100644 --- a/2377/3/sinatra/parser.rb +++ b/2377/3/sinatra/parser.rb @@ -4,17 +4,21 @@ require 'json' # Parsing of comments class Parser - + ONLINER = 'https://comments.api.onliner.by/news/tech.post/'.freeze def get_comments(link) - json_comments = JSON.parse(Net::HTTP.get_response(URI.parse('https://comments.api.onliner.by/news/tech.post/' + get_id(link) + '/comments?limit=50')).body) + json_comments = JSON.parse(Net::HTTP.get_response(URI.parse(ONLINER + get_id(link) + '/comments?limit=50')).body) comments = json_comments['comments'].each.map { |hash| hash['text'] } - comments.each do |comment| - link.comments.push(Comment.create(comment: comment)) - end + push(comments, link) end private + def push(comments, link) + comments.each do |comment| + link.comments.push(Comment.create(comment: comment)) + end + end + def response(link) Net::HTTP.get_response(URI.parse(link.link)).body end @@ -22,6 +26,4 @@ def response(link) def get_id(link) Nokogiri::HTML(response(link)).xpath('//span[@news_id]').to_s.match(/\d+/).to_s end - - end From 0cd59c9e7703e9176bd09fd713d2e0634e38d294 Mon Sep 17 00:00:00 2001 From: Marie Date: Tue, 31 Jul 2018 17:24:50 +0300 Subject: [PATCH 07/16] azure_api fixed --- 2377/3/sinatra/app.rb | 2 +- 2377/3/sinatra/azure_api.rb | 32 ++++++++++++++++++++------------ 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/2377/3/sinatra/app.rb b/2377/3/sinatra/app.rb index b24b5e42d..70c880a00 100644 --- a/2377/3/sinatra/app.rb +++ b/2377/3/sinatra/app.rb @@ -25,7 +25,7 @@ class MyApp < Sinatra::Base end post '/clear' do - Ohm.redis.call("FLUSHALL") + Ohm.redis.call('FLUSHALL') erb :active_page end diff --git a/2377/3/sinatra/azure_api.rb b/2377/3/sinatra/azure_api.rb index 23873dcfd..d05f31e7e 100644 --- a/2377/3/sinatra/azure_api.rb +++ b/2377/3/sinatra/azure_api.rb @@ -3,19 +3,13 @@ require 'json' # Analysis via Azure API class TextAnalytics - def analyse(comment) - uri = 'https://westeurope.api.cognitive.microsoft.com' - path = '/text/analytics/v2.0/sentiment' - uri = URI(uri + path) - - documents = { 'documents': [{ 'id' => '1', 'language' => 'ru', 'text' => "#{comment.comment}" }] } + def initialize + @uri = URI('https://westeurope.api.cognitive.microsoft.com/text/analytics/v2.0/sentiment') + end - request = Net::HTTP::Post.new(uri) - request['Content-Type'] = 'application/json' - request['Ocp-Apim-Subscription-Key'] = access_key - request.body = documents.to_json - response = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http| - http.request request + def analyse(comment) + response = Net::HTTP.start(@uri.host, @uri.port, use_ssl: @uri.scheme == 'https') do |http| + http.request request(comment) end comment.update score: convert((JSON response.body)['documents'][0]['score']) end @@ -25,7 +19,21 @@ def convert(value) 200 / proportion - 100 end + private + def access_key @access_key = YAML.load_file(File.join(Dir.pwd, 'config.yml'))['access_key'] end + + def documents(comment) + { 'documents': [{ 'id' => '1', 'language' => 'ru', 'text' => comment.comment.to_s }] } + end + + def request(comment) + request = Net::HTTP::Post.new(@uri) + request['Content-Type'] = 'application/json' + request['Ocp-Apim-Subscription-Key'] = access_key + request.body = documents(comment).to_json + request + end end From 1aa5a4001f92bdb7cd3838ea78ae527464453335 Mon Sep 17 00:00:00 2001 From: Marie Date: Tue, 31 Jul 2018 17:43:41 +0300 Subject: [PATCH 08/16] tCOLON fixed --- 2377/3/sinatra/azure_api.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/2377/3/sinatra/azure_api.rb b/2377/3/sinatra/azure_api.rb index d05f31e7e..259b3b6fa 100644 --- a/2377/3/sinatra/azure_api.rb +++ b/2377/3/sinatra/azure_api.rb @@ -26,7 +26,7 @@ def access_key end def documents(comment) - { 'documents': [{ 'id' => '1', 'language' => 'ru', 'text' => comment.comment.to_s }] } + { 'documents': [{ 'id': '1', 'language': 'ru', 'text': comment.comment.to_s }] } end def request(comment) From 260a9c3d59143a91a1f6c943c20e004f0153a8ae Mon Sep 17 00:00:00 2001 From: Marie Date: Wed, 1 Aug 2018 08:34:41 +0300 Subject: [PATCH 09/16] REST --- 2377/3/sinatra/app.rb | 4 ++-- 2377/3/sinatra/azure_api.rb | 4 ++++ 2377/3/sinatra/views/{analysis.erb => links.erb} | 0 3 files changed, 6 insertions(+), 2 deletions(-) rename 2377/3/sinatra/views/{analysis.erb => links.erb} (100%) diff --git a/2377/3/sinatra/app.rb b/2377/3/sinatra/app.rb index 70c880a00..8b536abe1 100644 --- a/2377/3/sinatra/app.rb +++ b/2377/3/sinatra/app.rb @@ -29,10 +29,10 @@ class MyApp < Sinatra::Base erb :active_page end - get '/analysis/:id' do + get '/links/:id' do @comments = Link[params[:id]].comments - erb :analysis + erb :links end # $0 is the executed file diff --git a/2377/3/sinatra/azure_api.rb b/2377/3/sinatra/azure_api.rb index 259b3b6fa..3250d3c3a 100644 --- a/2377/3/sinatra/azure_api.rb +++ b/2377/3/sinatra/azure_api.rb @@ -1,3 +1,5 @@ +# disable:InstanceVariableAssumption +# disable:UtilityFunction require 'net/https' require 'uri' require 'json' @@ -37,3 +39,5 @@ def request(comment) request end end +# enable:InstanceVariableAssumption +# enable:UtilityFunction diff --git a/2377/3/sinatra/views/analysis.erb b/2377/3/sinatra/views/links.erb similarity index 100% rename from 2377/3/sinatra/views/analysis.erb rename to 2377/3/sinatra/views/links.erb From 8828f01f8b9f21b08e1d1ee86bb0503af01c75e9 Mon Sep 17 00:00:00 2001 From: Marie Date: Wed, 1 Aug 2018 08:44:28 +0300 Subject: [PATCH 10/16] hound doesn't work --- 2377/3/sinatra/azure_api.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/2377/3/sinatra/azure_api.rb b/2377/3/sinatra/azure_api.rb index 3250d3c3a..468267b13 100644 --- a/2377/3/sinatra/azure_api.rb +++ b/2377/3/sinatra/azure_api.rb @@ -28,7 +28,7 @@ def access_key end def documents(comment) - { 'documents': [{ 'id': '1', 'language': 'ru', 'text': comment.comment.to_s }] } + { 'documents': [{ 'id' => '1', 'language' => 'ru', 'text' => comment.comment.to_s }] } end def request(comment) From 0e00f88450997e5f3683dfe7c3aa0f786756af75 Mon Sep 17 00:00:00 2001 From: Marie Date: Wed, 1 Aug 2018 09:46:38 +0300 Subject: [PATCH 11/16] Fix --- 2377/3/sinatra/views/active_page.erb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/2377/3/sinatra/views/active_page.erb b/2377/3/sinatra/views/active_page.erb index 2015d1a1f..1656cf5ab 100644 --- a/2377/3/sinatra/views/active_page.erb +++ b/2377/3/sinatra/views/active_page.erb @@ -26,7 +26,7 @@ <%=link.link%> <%= link.score%> - target="_blank"> + target="_blank"> <% end%> From 58e18dcb1ad60d670e8c2feffeee4bbdcc256c05 Mon Sep 17 00:00:00 2001 From: Marie Date: Wed, 1 Aug 2018 10:09:22 +0300 Subject: [PATCH 12/16] converter created --- 2377/3/sinatra/azure_api.rb | 12 ++---------- 2377/3/sinatra/converter.rb | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 10 deletions(-) create mode 100644 2377/3/sinatra/converter.rb diff --git a/2377/3/sinatra/azure_api.rb b/2377/3/sinatra/azure_api.rb index 468267b13..58e1fc91d 100644 --- a/2377/3/sinatra/azure_api.rb +++ b/2377/3/sinatra/azure_api.rb @@ -1,8 +1,7 @@ -# disable:InstanceVariableAssumption -# disable:UtilityFunction require 'net/https' require 'uri' require 'json' +require_relative 'converter' # Analysis via Azure API class TextAnalytics def initialize @@ -13,12 +12,7 @@ def analyse(comment) response = Net::HTTP.start(@uri.host, @uri.port, use_ssl: @uri.scheme == 'https') do |http| http.request request(comment) end - comment.update score: convert((JSON response.body)['documents'][0]['score']) - end - - def convert(value) - proportion = 1 / value - 200 / proportion - 100 + comment.update score: Converter.new(((JSON response.body)['documents'][0]['score'])).final_value end private @@ -39,5 +33,3 @@ def request(comment) request end end -# enable:InstanceVariableAssumption -# enable:UtilityFunction diff --git a/2377/3/sinatra/converter.rb b/2377/3/sinatra/converter.rb new file mode 100644 index 000000000..4477e7c69 --- /dev/null +++ b/2377/3/sinatra/converter.rb @@ -0,0 +1,14 @@ +# Converts things +class Converter + attr_reader :value + attr_reader :final_value + def initialize(value) + @value = value + @final_value = convert + end + + def convert + proportion = 1 / value + 200 / proportion - 100 + end +end From a2721243600fdcec171aa0b6abcfae7ca7918211 Mon Sep 17 00:00:00 2001 From: Marie Date: Wed, 1 Aug 2018 10:18:48 +0300 Subject: [PATCH 13/16] tCOLON & RCURLY --- 2377/3/sinatra/azure_api.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/2377/3/sinatra/azure_api.rb b/2377/3/sinatra/azure_api.rb index 58e1fc91d..3b0b05767 100644 --- a/2377/3/sinatra/azure_api.rb +++ b/2377/3/sinatra/azure_api.rb @@ -22,7 +22,9 @@ def access_key end def documents(comment) - { 'documents': [{ 'id' => '1', 'language' => 'ru', 'text' => comment.comment.to_s }] } + documents = {} + documents['documents'] = [{ 'id' => '1', 'language' => 'ru', 'text' => comment.comment.to_s }] + documents end def request(comment) From 935b2428f62b901e7a8246ec1187b1af825f09d7 Mon Sep 17 00:00:00 2001 From: Marie Date: Wed, 1 Aug 2018 10:32:03 +0300 Subject: [PATCH 14/16] hound fix --- 2377/3/sinatra/azure_api.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/2377/3/sinatra/azure_api.rb b/2377/3/sinatra/azure_api.rb index 3b0b05767..f90881c1b 100644 --- a/2377/3/sinatra/azure_api.rb +++ b/2377/3/sinatra/azure_api.rb @@ -12,7 +12,7 @@ def analyse(comment) response = Net::HTTP.start(@uri.host, @uri.port, use_ssl: @uri.scheme == 'https') do |http| http.request request(comment) end - comment.update score: Converter.new(((JSON response.body)['documents'][0]['score'])).final_value + update_comment(comment, response) end private @@ -27,6 +27,10 @@ def documents(comment) documents end + def update_comment(comment, response) + comment.update score: Converter.new(((JSON response.body)['documents'][0]['score'])).final_value + end + def request(comment) request = Net::HTTP::Post.new(@uri) request['Content-Type'] = 'application/json' From 65134d84c452470bc1db16ae12b1309bc2865ccf Mon Sep 17 00:00:00 2001 From: Marie Date: Thu, 2 Aug 2018 09:21:55 +0300 Subject: [PATCH 15/16] REST done --- 2377/3/sinatra/app.rb | 11 ++++++++--- 2377/3/sinatra/views/active_page.erb | 4 ++-- 2377/3/sinatra/views/main.erb | 2 +- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/2377/3/sinatra/app.rb b/2377/3/sinatra/app.rb index 8b536abe1..625009f9f 100644 --- a/2377/3/sinatra/app.rb +++ b/2377/3/sinatra/app.rb @@ -7,7 +7,7 @@ require 'yaml' # Main class for application class MyApp < Sinatra::Base - post '/get-link' do + post '/links/new' do link = Link.create link: params[:link] please = Parser.new please.get_comments(link) @@ -17,6 +17,10 @@ class MyApp < Sinatra::Base end total = link.comments.inject(0) { |sum, comment| sum + comment.score.to_i } / link.comments.size link.update score: total + redirect '/links' + end + + get '/links' do erb :active_page end @@ -24,9 +28,10 @@ class MyApp < Sinatra::Base erb :main end - post '/clear' do + get '/clear' do Ohm.redis.call('FLUSHALL') - erb :active_page + #erb :active_page + redirect '/' end get '/links/:id' do diff --git a/2377/3/sinatra/views/active_page.erb b/2377/3/sinatra/views/active_page.erb index 1656cf5ab..5e4bc3f89 100644 --- a/2377/3/sinatra/views/active_page.erb +++ b/2377/3/sinatra/views/active_page.erb @@ -10,12 +10,12 @@ -
+

-
+

diff --git a/2377/3/sinatra/views/main.erb b/2377/3/sinatra/views/main.erb index eab120f37..04cd08ccc 100644 --- a/2377/3/sinatra/views/main.erb +++ b/2377/3/sinatra/views/main.erb @@ -8,7 +8,7 @@ - +

From 5f48a1b97a7fc4a5bf40ecadef7c4cdc7f80d98e Mon Sep 17 00:00:00 2001 From: Marie Date: Thu, 2 Aug 2018 09:23:21 +0300 Subject: [PATCH 16/16] typo fixed --- 2377/3/sinatra/app.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/2377/3/sinatra/app.rb b/2377/3/sinatra/app.rb index 625009f9f..eb6a02800 100644 --- a/2377/3/sinatra/app.rb +++ b/2377/3/sinatra/app.rb @@ -30,7 +30,6 @@ class MyApp < Sinatra::Base get '/clear' do Ohm.redis.call('FLUSHALL') - #erb :active_page redirect '/' end