From e9d19b0629f4c8f266e9647517893fe283bf9800 Mon Sep 17 00:00:00 2001 From: Evenstein Date: Fri, 27 Jul 2018 17:55:47 +0300 Subject: [PATCH 1/2] Task #3 - Sinatra and CRUD (ready index, new) --- 2269/3/Gemfile | 8 ++ 2269/3/Gemfile.lock | 81 ++++++++++++++++++++ 2269/3/config.ru | 9 +++ 2269/3/controllers/application_controller.rb | 8 ++ 2269/3/controllers/articles_controller.rb | 36 +++++++++ 2269/3/helpers/article_helper.rb | 28 +++++++ 2269/3/helpers/comment_helper.rb | 49 ++++++++++++ 2269/3/models/article.rb | 7 ++ 2269/3/models/comment.rb | 5 ++ 2269/3/views/articles/create.erb | 8 ++ 2269/3/views/articles/index.erb | 18 +++++ 2269/3/views/layout.erb | 18 +++++ 12 files changed, 275 insertions(+) create mode 100755 2269/3/Gemfile create mode 100755 2269/3/Gemfile.lock create mode 100755 2269/3/config.ru create mode 100755 2269/3/controllers/application_controller.rb create mode 100755 2269/3/controllers/articles_controller.rb create mode 100755 2269/3/helpers/article_helper.rb create mode 100755 2269/3/helpers/comment_helper.rb create mode 100755 2269/3/models/article.rb create mode 100755 2269/3/models/comment.rb create mode 100755 2269/3/views/articles/create.erb create mode 100755 2269/3/views/articles/index.erb create mode 100755 2269/3/views/layout.erb diff --git a/2269/3/Gemfile b/2269/3/Gemfile new file mode 100755 index 000000000..80facf1c8 --- /dev/null +++ b/2269/3/Gemfile @@ -0,0 +1,8 @@ +source 'https://rubygems.org' + +gem 'mechanize' +gem 'ohm' +gem 'shotgun' +gem 'sidekiq' +gem 'sinatra' +gem 'thin' diff --git a/2269/3/Gemfile.lock b/2269/3/Gemfile.lock new file mode 100755 index 000000000..e2e862c77 --- /dev/null +++ b/2269/3/Gemfile.lock @@ -0,0 +1,81 @@ +GEM + remote: https://rubygems.org/ + specs: + concurrent-ruby (1.0.5) + 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) + 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 + redic (1.5.0) + hiredis + redis (4.0.1) + shotgun (0.9.2) + rack (>= 1.0) + sidekiq (5.1.3) + concurrent-ruby (~> 1.0) + connection_pool (~> 2.2, >= 2.2.0) + rack-protection (>= 1.5.0) + redis (>= 3.3.5, < 5) + 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 + mechanize + ohm + shotgun + sidekiq + sinatra + thin + +BUNDLED WITH + 1.16.2 diff --git a/2269/3/config.ru b/2269/3/config.ru new file mode 100755 index 000000000..67901369c --- /dev/null +++ b/2269/3/config.ru @@ -0,0 +1,9 @@ +Bundler.require(:default) + +Dir.glob('./{models,controllers,helpers}/*.rb').each { |file| require_relative file } + +Article.redis = Redic.new('redis://127.0.0.1:6379/0') +Comment.redis = Redic.new('redis://127.0.0.1:6379/0') + +map('/articles') { run ArticlesController } +map('/') { run ApplicationController } diff --git a/2269/3/controllers/application_controller.rb b/2269/3/controllers/application_controller.rb new file mode 100755 index 000000000..02488cbd2 --- /dev/null +++ b/2269/3/controllers/application_controller.rb @@ -0,0 +1,8 @@ +# Class +class ApplicationController < Sinatra::Base + set :views, File.expand_path(File.join(__FILE__, '../../views')) + + get '/' do + redirect '/articles' + end +end \ No newline at end of file diff --git a/2269/3/controllers/articles_controller.rb b/2269/3/controllers/articles_controller.rb new file mode 100755 index 000000000..ddcf58753 --- /dev/null +++ b/2269/3/controllers/articles_controller.rb @@ -0,0 +1,36 @@ +require_relative './application_controller.rb' + +# Controller +class ArticlesController < ApplicationController + # index + get '/' do + @articles = Article.all + erb :'/articles/index' + end + + # new + get '/new' do + erb :'/articles/create' + end + + # show + get '/:id' do + params[:id] + end + + # create + post '/' do + ArticleHelper.new(params[:url]) + redirect '/' + end + + # put + put '/:id' do + params[:id] + end + + # delete + delete '/:id' do + params[:id] + end +end diff --git a/2269/3/helpers/article_helper.rb b/2269/3/helpers/article_helper.rb new file mode 100755 index 000000000..724f2b300 --- /dev/null +++ b/2269/3/helpers/article_helper.rb @@ -0,0 +1,28 @@ +# Class +class ArticleHelper + def initialize(url) + @agent = Mechanize.new { |agnt| agnt.user_agent_alias = 'Mac Safari' } + @agent.history_added = proc { sleep 1 } + @article = Article.create(url: url, title: retrieve_title(url)) + update_article(url) + end + + attr_reader :agent, :article + + def retrieve_title(url) + page = @agent.get(url) + page.title + end + + def update_article(url) + helper = CommentHelper.new + contents = helper.retrieve_comments(url) + ratings = helper.retrieve_rating(contents) + contents.each_with_index do |content, index| + comment = Comment.create(content: content, rate: ratings[index]) + @article.comments.add(comment) + end + article_rate = (ratings.sum / comments.size).to_i + @article.update(rating: article_rate) + end +end diff --git a/2269/3/helpers/comment_helper.rb b/2269/3/helpers/comment_helper.rb new file mode 100755 index 000000000..c11ecea58 --- /dev/null +++ b/2269/3/helpers/comment_helper.rb @@ -0,0 +1,49 @@ +# Class +class CommentHelper + def initialize + @agent = Mechanize.new { |agnt| agnt.user_agent_alias = 'Mac Safari' } + @agent.history_added = proc { sleep 1 } + end + + attr_reader :agent, :data + + def retrieve_comments(path) + article_id = @agent.get(path).css('.news_view_count').last.values[1] + url = "https://comments.api.onliner.by/news/tech.post/#{article_id}/comments?limit=50&_=0.9841189675826583" + response = agent.get(url) + comments = [] + JSON.parse(response.body)['comments'].each { |elem| comments << elem['text'].tr("\n", ' ') } + end + + def retrieve_rating(comments) + @data = { documents: [] } + comments.each_with_index do |comment, index| + @data[:documents] << { 'id' => index.to_s, 'language' => 'ru', 'text' => comment[:text] } + end + calculate_rating + end + + def calculate_rating + url = URI('https://westeurope.api.cognitive.microsoft.com/text/analytics/v2.0/sentiment') + response = send_request(url) + rating = [] + JSON.parse(response.body)['documents'].map do |value| + rating << (value['score'].to_i * 200 - 100) + end + rating + end + + def send_request(url) + Net::HTTP.start(url.host, url.port, use_ssl: url.scheme == 'https') do |http| + http.request(build_request(url)) + end + end + + def build_request(url) + key = YAML.safe_load(File.open('config.yml').read)['api_key'] + request = Net::HTTP::Post.new(url, 'Content-Type' => 'application/json', 'Ocp-Apim-Subscription-Key' => key) + request.body = @data.to_json + request + end +end + diff --git a/2269/3/models/article.rb b/2269/3/models/article.rb new file mode 100755 index 000000000..37700b3a8 --- /dev/null +++ b/2269/3/models/article.rb @@ -0,0 +1,7 @@ +# Class that represents model of Article +class Article < Ohm::Model + attribute :url + attribute :title + set :comments, :Comment + attribute :rating +end diff --git a/2269/3/models/comment.rb b/2269/3/models/comment.rb new file mode 100755 index 000000000..48d63e2e4 --- /dev/null +++ b/2269/3/models/comment.rb @@ -0,0 +1,5 @@ +# Class that represents model of Comment to a Article +class Comment < Ohm::Model + attribute :content + attribute :rate +end diff --git a/2269/3/views/articles/create.erb b/2269/3/views/articles/create.erb new file mode 100755 index 000000000..aa688319d --- /dev/null +++ b/2269/3/views/articles/create.erb @@ -0,0 +1,8 @@ +

New article

+
+
+ + +
+ +
\ No newline at end of file diff --git a/2269/3/views/articles/index.erb b/2269/3/views/articles/index.erb new file mode 100755 index 000000000..c3df0aca0 --- /dev/null +++ b/2269/3/views/articles/index.erb @@ -0,0 +1,18 @@ +

Article's List

+
+ + + + + + + <% @articles.each do |article| %> + + + + + + <% end %> +
IDLinkRating
<%= article.id %>

<%= article.title %>

+
+
Add Article
\ No newline at end of file diff --git a/2269/3/views/layout.erb b/2269/3/views/layout.erb new file mode 100755 index 000000000..9181f52b0 --- /dev/null +++ b/2269/3/views/layout.erb @@ -0,0 +1,18 @@ + + + + Task #3 + + + + + +
+ + <%= yield %> + +
+ + + + From 5fe24d06160a2bc13953553a17d1c6448b7c90c4 Mon Sep 17 00:00:00 2001 From: Evenstein Date: Sat, 28 Jul 2018 22:03:04 +0300 Subject: [PATCH 2/2] Task #3 - finished (realized all CRUD-methods and added nested views) --- 2269/3/Gemfile.lock | 6 ++++++ 2269/3/config.ru | 3 ++- 2269/3/controllers/application_controller.rb | 3 ++- 2269/3/controllers/articles_controller.rb | 12 +++++------- 2269/3/helpers/article_helper.rb | 2 +- 2269/3/helpers/comment_helper.rb | 8 ++++---- 2269/3/views/articles/create.erb | 4 ++-- 2269/3/views/articles/index.erb | 18 +++++++++++++++--- 2269/3/views/articles/show.erb | 18 ++++++++++++++++++ 9 files changed, 55 insertions(+), 19 deletions(-) create mode 100644 2269/3/views/articles/show.erb diff --git a/2269/3/Gemfile.lock b/2269/3/Gemfile.lock index e2e862c77..5205c3d33 100755 --- a/2269/3/Gemfile.lock +++ b/2269/3/Gemfile.lock @@ -1,6 +1,7 @@ GEM remote: https://rubygems.org/ specs: + coderay (1.1.2) concurrent-ruby (1.0.5) connection_pool (2.2.2) daemons (1.2.6) @@ -19,6 +20,7 @@ GEM 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) @@ -36,6 +38,9 @@ GEM 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 @@ -72,6 +77,7 @@ PLATFORMS DEPENDENCIES mechanize ohm + pry shotgun sidekiq sinatra diff --git a/2269/3/config.ru b/2269/3/config.ru index 67901369c..078cc30ab 100755 --- a/2269/3/config.ru +++ b/2269/3/config.ru @@ -1,4 +1,5 @@ -Bundler.require(:default) +require 'bundler' +Bundler.require Dir.glob('./{models,controllers,helpers}/*.rb').each { |file| require_relative file } diff --git a/2269/3/controllers/application_controller.rb b/2269/3/controllers/application_controller.rb index 02488cbd2..49dcacd04 100755 --- a/2269/3/controllers/application_controller.rb +++ b/2269/3/controllers/application_controller.rb @@ -1,8 +1,9 @@ # Class class ApplicationController < Sinatra::Base set :views, File.expand_path(File.join(__FILE__, '../../views')) + set :method_override, true get '/' do redirect '/articles' end -end \ No newline at end of file +end diff --git a/2269/3/controllers/articles_controller.rb b/2269/3/controllers/articles_controller.rb index ddcf58753..0d3215d02 100755 --- a/2269/3/controllers/articles_controller.rb +++ b/2269/3/controllers/articles_controller.rb @@ -15,7 +15,8 @@ class ArticlesController < ApplicationController # show get '/:id' do - params[:id] + @article = Article[params[:id]] + erb :'/articles/show' end # create @@ -24,13 +25,10 @@ class ArticlesController < ApplicationController redirect '/' end - # put - put '/:id' do - params[:id] - end - # delete delete '/:id' do - params[:id] + Article[params[:id]].comments.each(&:delete) + Article[params[:id]].delete + redirect '/' end end diff --git a/2269/3/helpers/article_helper.rb b/2269/3/helpers/article_helper.rb index 724f2b300..366beb2ff 100755 --- a/2269/3/helpers/article_helper.rb +++ b/2269/3/helpers/article_helper.rb @@ -22,7 +22,7 @@ def update_article(url) comment = Comment.create(content: content, rate: ratings[index]) @article.comments.add(comment) end - article_rate = (ratings.sum / comments.size).to_i + article_rate = (ratings.sum / contents.size).to_i @article.update(rating: article_rate) end end diff --git a/2269/3/helpers/comment_helper.rb b/2269/3/helpers/comment_helper.rb index c11ecea58..60b3c09df 100755 --- a/2269/3/helpers/comment_helper.rb +++ b/2269/3/helpers/comment_helper.rb @@ -13,12 +13,13 @@ def retrieve_comments(path) response = agent.get(url) comments = [] JSON.parse(response.body)['comments'].each { |elem| comments << elem['text'].tr("\n", ' ') } + comments end def retrieve_rating(comments) @data = { documents: [] } comments.each_with_index do |comment, index| - @data[:documents] << { 'id' => index.to_s, 'language' => 'ru', 'text' => comment[:text] } + @data[:documents] << { 'id' => index.to_s, 'language' => 'ru', 'text' => comment } end calculate_rating end @@ -28,7 +29,7 @@ def calculate_rating response = send_request(url) rating = [] JSON.parse(response.body)['documents'].map do |value| - rating << (value['score'].to_i * 200 - 100) + rating << ((value['score'] * 200).round(0) - 100) end rating end @@ -40,10 +41,9 @@ def send_request(url) end def build_request(url) - key = YAML.safe_load(File.open('config.yml').read)['api_key'] + key = File.read('api_key') request = Net::HTTP::Post.new(url, 'Content-Type' => 'application/json', 'Ocp-Apim-Subscription-Key' => key) request.body = @data.to_json request end end - diff --git a/2269/3/views/articles/create.erb b/2269/3/views/articles/create.erb index aa688319d..a9010e1ad 100755 --- a/2269/3/views/articles/create.erb +++ b/2269/3/views/articles/create.erb @@ -1,8 +1,8 @@

New article

- +
- +
\ No newline at end of file diff --git a/2269/3/views/articles/index.erb b/2269/3/views/articles/index.erb index c3df0aca0..10b1589ce 100755 --- a/2269/3/views/articles/index.erb +++ b/2269/3/views/articles/index.erb @@ -3,14 +3,26 @@ - + + <% @articles.each do |article| %> - - + + + <% end %>
IDLink +
Article's title
+
Rating
Actions
<%= article.id %>

<%= article.title %>

<%= article.title %>
<%= article.rating %> +
+
+ + Show + +
+
+
diff --git a/2269/3/views/articles/show.erb b/2269/3/views/articles/show.erb new file mode 100644 index 000000000..9e57382b1 --- /dev/null +++ b/2269/3/views/articles/show.erb @@ -0,0 +1,18 @@ +

<%= @article.title %>

+
+ + + + + + <% @article.comments.each do |comment| %> + + + + + <% end %> +
Comment text
Rating
<%= comment.content %>
<%= comment.rate %>
+
+
Back
+
+
\ No newline at end of file