From f0406cbf146a31492348e4bc1c29d990be7cb377 Mon Sep 17 00:00:00 2001 From: mersXX Date: Wed, 25 Jul 2018 18:21:43 +0300 Subject: [PATCH 1/6] Start develeping of Sinatra app + parser --- 2264/3/parser.rb | 12 ++++++++++++ 2264/3/test.rb | 6 ++++++ 2264/3/views/index.erb | 10 ++++++++++ 3 files changed, 28 insertions(+) create mode 100644 2264/3/parser.rb create mode 100644 2264/3/test.rb create mode 100644 2264/3/views/index.erb diff --git a/2264/3/parser.rb b/2264/3/parser.rb new file mode 100644 index 000000000..ea311216f --- /dev/null +++ b/2264/3/parser.rb @@ -0,0 +1,12 @@ +require 'mechanize' +require 'json' + +agent = Mechanize.new +page = agent.get('https://tech.onliner.by/2018/07/24/intel-26') + +pp page.css('p').inner_html + + + + + diff --git a/2264/3/test.rb b/2264/3/test.rb new file mode 100644 index 000000000..87358117f --- /dev/null +++ b/2264/3/test.rb @@ -0,0 +1,6 @@ +require 'sinatra' + +get '/' do + erb :index +end + diff --git a/2264/3/views/index.erb b/2264/3/views/index.erb new file mode 100644 index 000000000..01243a0f7 --- /dev/null +++ b/2264/3/views/index.erb @@ -0,0 +1,10 @@ + + + + + Test page + + +

Add Link

+ + \ No newline at end of file From f1ccfb60de6283c9c87aa12bddace68f8796e90c Mon Sep 17 00:00:00 2001 From: mersXX Date: Thu, 26 Jul 2018 23:20:20 +0300 Subject: [PATCH 2/6] Create basic architecture and set up env --- 2264/3/Gemfile | 9 +++++++++ 2264/3/config.ru | 0 2264/3/controllers/application_controller.rb | 3 +++ .../{test.rb => controllers/links_controller.rb} | 4 ++++ 2264/3/models/comment.rb | 4 ++++ 2264/3/models/link.rb | 4 ++++ 2264/3/views/index.erb | 2 +- 2264/3/views/new.erb | 16 ++++++++++++++++ 8 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 2264/3/Gemfile create mode 100644 2264/3/config.ru create mode 100644 2264/3/controllers/application_controller.rb rename 2264/3/{test.rb => controllers/links_controller.rb} (61%) create mode 100644 2264/3/models/comment.rb create mode 100644 2264/3/models/link.rb create mode 100644 2264/3/views/new.erb diff --git a/2264/3/Gemfile b/2264/3/Gemfile new file mode 100644 index 000000000..03c68fa2d --- /dev/null +++ b/2264/3/Gemfile @@ -0,0 +1,9 @@ +source 'https://rubygems.org' +ruby '2.4.1' + +gem 'sinatra' +gem 'thin' +gem 'shotgun' +gem 'json' +gem 'mechanize' +gem 'ohm' diff --git a/2264/3/config.ru b/2264/3/config.ru new file mode 100644 index 000000000..e69de29bb diff --git a/2264/3/controllers/application_controller.rb b/2264/3/controllers/application_controller.rb new file mode 100644 index 000000000..a9b5bf69c --- /dev/null +++ b/2264/3/controllers/application_controller.rb @@ -0,0 +1,3 @@ +class ApplicationController < Sinatra::Base + +end \ No newline at end of file diff --git a/2264/3/test.rb b/2264/3/controllers/links_controller.rb similarity index 61% rename from 2264/3/test.rb rename to 2264/3/controllers/links_controller.rb index 87358117f..755a1becd 100644 --- a/2264/3/test.rb +++ b/2264/3/controllers/links_controller.rb @@ -4,3 +4,7 @@ erb :index end +get '/new' do + erb :new +end + diff --git a/2264/3/models/comment.rb b/2264/3/models/comment.rb new file mode 100644 index 000000000..d5a02daab --- /dev/null +++ b/2264/3/models/comment.rb @@ -0,0 +1,4 @@ +class Link < Ohm::Model + attribute :title + attribute :rating +end \ No newline at end of file diff --git a/2264/3/models/link.rb b/2264/3/models/link.rb new file mode 100644 index 000000000..9c7c758a1 --- /dev/null +++ b/2264/3/models/link.rb @@ -0,0 +1,4 @@ +class Link < Ohm::Model + attribute :text + attribute :rating +end \ No newline at end of file diff --git a/2264/3/views/index.erb b/2264/3/views/index.erb index 01243a0f7..6e5124a2d 100644 --- a/2264/3/views/index.erb +++ b/2264/3/views/index.erb @@ -5,6 +5,6 @@ Test page -

Add Link

+

Your links

\ No newline at end of file diff --git a/2264/3/views/new.erb b/2264/3/views/new.erb new file mode 100644 index 000000000..418be2b57 --- /dev/null +++ b/2264/3/views/new.erb @@ -0,0 +1,16 @@ + + + + + New link + + +

Add Link

+
+ + + +
+Back to Index + + \ No newline at end of file From eb669248ea8d0ff368b25a90e9d8385d49e0ae67 Mon Sep 17 00:00:00 2001 From: mersXX Date: Fri, 27 Jul 2018 12:27:32 +0300 Subject: [PATCH 3/6] Add views and do refactoring --- 2264/3/Gemfile | 14 ++++++++------ 2264/3/controllers/application_controller.rb | 4 +++- 2264/3/controllers/links_controller.rb | 12 +++++++----- 2264/3/models/comment.rb | 2 +- 2264/3/models/link.rb | 4 ++-- 2264/3/views/layout.erb | 10 ++++++++++ 2264/3/views/{ => link}/index.erb | 0 2264/3/views/link/new.erb | 19 +++++++++++++++++++ 2264/3/views/new.erb | 16 ---------------- 9 files changed, 50 insertions(+), 31 deletions(-) create mode 100644 2264/3/views/layout.erb rename 2264/3/views/{ => link}/index.erb (100%) create mode 100644 2264/3/views/link/new.erb delete mode 100644 2264/3/views/new.erb diff --git a/2264/3/Gemfile b/2264/3/Gemfile index 03c68fa2d..fc9b70df0 100644 --- a/2264/3/Gemfile +++ b/2264/3/Gemfile @@ -1,9 +1,11 @@ source 'https://rubygems.org' ruby '2.4.1' -gem 'sinatra' -gem 'thin' -gem 'shotgun' -gem 'json' -gem 'mechanize' -gem 'ohm' +group :development do + gem 'sinatra' + gem 'thin' + gem 'shotgun' + gem 'json' + gem 'mechanize' + gem 'ohm' +end \ No newline at end of file diff --git a/2264/3/controllers/application_controller.rb b/2264/3/controllers/application_controller.rb index a9b5bf69c..d96bede78 100644 --- a/2264/3/controllers/application_controller.rb +++ b/2264/3/controllers/application_controller.rb @@ -1,3 +1,5 @@ -class ApplicationController < Sinatra::Base +require 'sinatra' +class ApplicationController < Sinatra::Base + set :views, File.expand_path(File.join(__FILE__, '../../views')) end \ No newline at end of file diff --git a/2264/3/controllers/links_controller.rb b/2264/3/controllers/links_controller.rb index 755a1becd..c5d9caadb 100644 --- a/2264/3/controllers/links_controller.rb +++ b/2264/3/controllers/links_controller.rb @@ -1,10 +1,12 @@ require 'sinatra' -get '/' do - erb :index +class LinksController < ApplicationController +get '/links' do + @links = Link.all + end -get '/new' do - erb :new +get '/links/new' do + erb :'link/new' end - +end \ No newline at end of file diff --git a/2264/3/models/comment.rb b/2264/3/models/comment.rb index d5a02daab..9c7c758a1 100644 --- a/2264/3/models/comment.rb +++ b/2264/3/models/comment.rb @@ -1,4 +1,4 @@ class Link < Ohm::Model - attribute :title + attribute :text attribute :rating end \ No newline at end of file diff --git a/2264/3/models/link.rb b/2264/3/models/link.rb index 9c7c758a1..1a5f07d98 100644 --- a/2264/3/models/link.rb +++ b/2264/3/models/link.rb @@ -1,4 +1,4 @@ class Link < Ohm::Model - attribute :text + attribute :title attribute :rating -end \ No newline at end of file +end diff --git a/2264/3/views/layout.erb b/2264/3/views/layout.erb new file mode 100644 index 000000000..238552914 --- /dev/null +++ b/2264/3/views/layout.erb @@ -0,0 +1,10 @@ + + + + + Your links + + + <%= yield %> + + \ No newline at end of file diff --git a/2264/3/views/index.erb b/2264/3/views/link/index.erb similarity index 100% rename from 2264/3/views/index.erb rename to 2264/3/views/link/index.erb diff --git a/2264/3/views/link/new.erb b/2264/3/views/link/new.erb new file mode 100644 index 000000000..2754cf388 --- /dev/null +++ b/2264/3/views/link/new.erb @@ -0,0 +1,19 @@ + + + + + New link + + +

Add Link

+
+
+ + + +
+
+Back to Index + + + diff --git a/2264/3/views/new.erb b/2264/3/views/new.erb deleted file mode 100644 index 418be2b57..000000000 --- a/2264/3/views/new.erb +++ /dev/null @@ -1,16 +0,0 @@ - - - - - New link - - -

Add Link

-
- - - -
-Back to Index - - \ No newline at end of file From 77ced0f6ea9451b3915149f34de1711edf9bbdb5 Mon Sep 17 00:00:00 2001 From: mersXX Date: Fri, 27 Jul 2018 16:36:25 +0300 Subject: [PATCH 4/6] Create CRD PostsController --- 2264/3/Gemfile | 16 ++++++------- 2264/3/config.ru | 4 ++++ 2264/3/controllers/links_controller.rb | 12 ---------- 2264/3/controllers/posts_controller.rb | 32 ++++++++++++++++++++++++++ 2264/3/models/comment.rb | 2 +- 2264/3/models/link.rb | 4 ---- 2264/3/models/post.rb | 6 +++++ 2264/3/views/link/index.erb | 10 -------- 2264/3/views/link/new.erb | 19 --------------- 2264/3/views/post/index.erb | 21 +++++++++++++++++ 2264/3/views/post/new.erb | 9 ++++++++ 2264/3/views/post/show.erb | 6 +++++ 12 files changed, 87 insertions(+), 54 deletions(-) delete mode 100644 2264/3/controllers/links_controller.rb create mode 100644 2264/3/controllers/posts_controller.rb delete mode 100644 2264/3/models/link.rb create mode 100644 2264/3/models/post.rb delete mode 100644 2264/3/views/link/index.erb delete mode 100644 2264/3/views/link/new.erb create mode 100644 2264/3/views/post/index.erb create mode 100644 2264/3/views/post/new.erb create mode 100644 2264/3/views/post/show.erb diff --git a/2264/3/Gemfile b/2264/3/Gemfile index fc9b70df0..80a684afe 100644 --- a/2264/3/Gemfile +++ b/2264/3/Gemfile @@ -1,11 +1,11 @@ source 'https://rubygems.org' ruby '2.4.1' -group :development do - gem 'sinatra' - gem 'thin' - gem 'shotgun' - gem 'json' - gem 'mechanize' - gem 'ohm' -end \ No newline at end of file +gem 'sinatra' +gem 'thin' +gem 'shotgun' +gem 'json' +gem 'mechanize' +gem 'ohm' +gem 'pry' +gem 'pry-nav' \ No newline at end of file diff --git a/2264/3/config.ru b/2264/3/config.ru index e69de29bb..01724524a 100644 --- a/2264/3/config.ru +++ b/2264/3/config.ru @@ -0,0 +1,4 @@ +Bundler.require +Dir.glob('./{controllers,helpers,models}/*.rb').sort.each { |file| require file } + +map('/') { run PostsController } \ No newline at end of file diff --git a/2264/3/controllers/links_controller.rb b/2264/3/controllers/links_controller.rb deleted file mode 100644 index c5d9caadb..000000000 --- a/2264/3/controllers/links_controller.rb +++ /dev/null @@ -1,12 +0,0 @@ -require 'sinatra' - -class LinksController < ApplicationController -get '/links' do - @links = Link.all - -end - -get '/links/new' do - erb :'link/new' -end -end \ No newline at end of file diff --git a/2264/3/controllers/posts_controller.rb b/2264/3/controllers/posts_controller.rb new file mode 100644 index 000000000..df64a990f --- /dev/null +++ b/2264/3/controllers/posts_controller.rb @@ -0,0 +1,32 @@ +require 'sinatra' + +class PostsController < ApplicationController + + get '/posts/new' do + erb :'post/new' + end + + get '/posts' do + @posts = Post.all + + erb :'post/index' + + end + + get '/posts/:id' do + @post = @posts[params[:id]] + erb :'post/show' + end + + post '/posts' do + @post = Post.create link: params[:link] + redirect '/posts' + end + + delete '/posts/:id/delete' do + @post = Post.all[params[:id]] + @post.delete + redirect '/posts' + end +end + diff --git a/2264/3/models/comment.rb b/2264/3/models/comment.rb index 9c7c758a1..02cb049c2 100644 --- a/2264/3/models/comment.rb +++ b/2264/3/models/comment.rb @@ -1,4 +1,4 @@ -class Link < Ohm::Model +class Comment < Ohm::Model attribute :text attribute :rating end \ No newline at end of file diff --git a/2264/3/models/link.rb b/2264/3/models/link.rb deleted file mode 100644 index 1a5f07d98..000000000 --- a/2264/3/models/link.rb +++ /dev/null @@ -1,4 +0,0 @@ -class Link < Ohm::Model - attribute :title - attribute :rating -end diff --git a/2264/3/models/post.rb b/2264/3/models/post.rb new file mode 100644 index 000000000..749e7c3ec --- /dev/null +++ b/2264/3/models/post.rb @@ -0,0 +1,6 @@ +require 'ohm' + +class Post < Ohm::Model + attribute :link + attribute :title +end diff --git a/2264/3/views/link/index.erb b/2264/3/views/link/index.erb deleted file mode 100644 index 6e5124a2d..000000000 --- a/2264/3/views/link/index.erb +++ /dev/null @@ -1,10 +0,0 @@ - - - - - Test page - - -

Your links

- - \ No newline at end of file diff --git a/2264/3/views/link/new.erb b/2264/3/views/link/new.erb deleted file mode 100644 index 2754cf388..000000000 --- a/2264/3/views/link/new.erb +++ /dev/null @@ -1,19 +0,0 @@ - - - - - New link - - -

Add Link

-
-
- - - -
-
-Back to Index - - - diff --git a/2264/3/views/post/index.erb b/2264/3/views/post/index.erb new file mode 100644 index 000000000..6c1ab23f7 --- /dev/null +++ b/2264/3/views/post/index.erb @@ -0,0 +1,21 @@ +
+

Your Links

+
+ + + + + + + + + + <% @posts.each do |post| %> + + + + <% end %> + +
LinksTitle
<%= post.link %>
+
+
diff --git a/2264/3/views/post/new.erb b/2264/3/views/post/new.erb new file mode 100644 index 000000000..a4e5ce87f --- /dev/null +++ b/2264/3/views/post/new.erb @@ -0,0 +1,9 @@ + + +

New Post

+ +
+ + + +
\ No newline at end of file diff --git a/2264/3/views/post/show.erb b/2264/3/views/post/show.erb new file mode 100644 index 000000000..b272bbb24 --- /dev/null +++ b/2264/3/views/post/show.erb @@ -0,0 +1,6 @@ +
+

<%= @post.title %>

+ +
\ No newline at end of file From 5c6626d69fc70a95fbaba24cb502c5ad45f18b84 Mon Sep 17 00:00:00 2001 From: mersXX Date: Fri, 27 Jul 2018 20:42:57 +0300 Subject: [PATCH 5/6] Add CommentsParser and PostAnalyzator, finish task 3 --- 2264/3/Gemfile | 11 ++++-- 2264/3/config.ru | 5 +++ 2264/3/controllers/posts_controller.rb | 8 ++-- 2264/3/helpers/comment_rating_counter.rb | 50 ++++++++++++++++++++++++ 2264/3/helpers/comments_parser.rb | 35 +++++++++++++++++ 2264/3/helpers/post_analyzer.rb | 42 ++++++++++++++++++++ 2264/3/models/post.rb | 3 +- 2264/3/views/post/index.erb | 6 ++- 2264/3/views/post/new.erb | 5 +-- 2264/3/views/post/show.erb | 43 +++++++++++++++++--- 10 files changed, 190 insertions(+), 18 deletions(-) create mode 100644 2264/3/helpers/comment_rating_counter.rb create mode 100644 2264/3/helpers/comments_parser.rb create mode 100644 2264/3/helpers/post_analyzer.rb diff --git a/2264/3/Gemfile b/2264/3/Gemfile index 80a684afe..f5bc12547 100644 --- a/2264/3/Gemfile +++ b/2264/3/Gemfile @@ -1,11 +1,14 @@ source 'https://rubygems.org' ruby '2.4.1' -gem 'sinatra' -gem 'thin' -gem 'shotgun' gem 'json' gem 'mechanize' gem 'ohm' gem 'pry' -gem 'pry-nav' \ No newline at end of file +gem 'pry-nav' +gem 'sinatra' +gem 'shotgun' +gem 'thin' + + + diff --git a/2264/3/config.ru b/2264/3/config.ru index 01724524a..eebe9267a 100644 --- a/2264/3/config.ru +++ b/2264/3/config.ru @@ -1,4 +1,9 @@ Bundler.require Dir.glob('./{controllers,helpers,models}/*.rb').sort.each { |file| require file } +Post.redis = Redic.new('redis://127.0.0.1:6379/0') +Comment.redis = Redic.new('redis://127.0.0.1:6379/1') + +use Rack::MethodOverride + map('/') { run PostsController } \ No newline at end of file diff --git a/2264/3/controllers/posts_controller.rb b/2264/3/controllers/posts_controller.rb index df64a990f..4a12b3ed7 100644 --- a/2264/3/controllers/posts_controller.rb +++ b/2264/3/controllers/posts_controller.rb @@ -8,18 +8,18 @@ class PostsController < ApplicationController get '/posts' do @posts = Post.all - erb :'post/index' - end - get '/posts/:id' do + get '/post/:id' do + @posts = Post.all @post = @posts[params[:id]] erb :'post/show' end post '/posts' do - @post = Post.create link: params[:link] + post = Post.create link: params[:link] + PostAnalyser.new(post).launch redirect '/posts' end diff --git a/2264/3/helpers/comment_rating_counter.rb b/2264/3/helpers/comment_rating_counter.rb new file mode 100644 index 000000000..e0a5bdce2 --- /dev/null +++ b/2264/3/helpers/comment_rating_counter.rb @@ -0,0 +1,50 @@ +require 'net/https' +require 'uri' +require 'json' + +# Rating Counter +class CommentRatingCounter + URL = 'https://westcentralus.api.cognitive.microsoft.com'.freeze + PATH = '/text/analytics/v2.0/sentiment'.freeze + KEY = 'de8a560ccae541e08ec1a30dcdd191a4'.freeze + + attr_reader :uri, :documents, :request + + def initialize(texts) + @uri = URI(URL + PATH) + build_documents(texts) + end + + def launch_counter + build_answer + end + + private + + def build_documents(text_body) + @documents = { documents: [] } + text_body.each_with_index do |text, index| + documents[:documents].push('id' => index.to_s, 'language' => 'ru', 'text' => text) + end + end + + def build_answer + JSON.parse(build_response.body)['documents'].map do |document| + (document['score'] * 200).to_i - 100 + end + end + + def build_response + build_request + Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http| + http.request(request) + end + end + + def build_request + @request = Net::HTTP::Post.new(uri) + request['Content-Type'] = 'application/json' + request['Ocp-Apim-Subscription-Key'] = KEY + request.body = documents.to_json + end +end \ No newline at end of file diff --git a/2264/3/helpers/comments_parser.rb b/2264/3/helpers/comments_parser.rb new file mode 100644 index 000000000..a12ecf525 --- /dev/null +++ b/2264/3/helpers/comments_parser.rb @@ -0,0 +1,35 @@ +require 'json' +require 'mechanize' + +# Comments parser +class CommentsParser + attr_reader :agent, :path_to_page + + def initialize(link) + @agent = Mechanize.new + @path_to_page = link + end + + def launch_parser + parse_comments_from_page + end + + private + + def parse_comments_from_page + response = parse_comments + comments = JSON.parse(response.body)['comments'] + comments.map do |elem| + elem['text'].sub("\n", ' ') + end + end + + def parse_page_code + agent.get(path_to_page).parser.css('span.news_view_count').last.values[1] + end + + def parse_comments + on_url = "https://comments.api.onliner.by/news/tech.post/#{parse_page_code}/comments?limit=100&_=0.9046614793472092" + agent.get(on_url) + end +end \ No newline at end of file diff --git a/2264/3/helpers/post_analyzer.rb b/2264/3/helpers/post_analyzer.rb new file mode 100644 index 000000000..751308bfe --- /dev/null +++ b/2264/3/helpers/post_analyzer.rb @@ -0,0 +1,42 @@ +class PostAnalyser + attr_reader :post, :text_body, :ratings + + def initialize(post) + @post = post + @text_body = launch_comments_parser + @ratings = launch_rating_counter + end + + def launch + refresh_post_title + build_post_stat + refresh_post_rating + end + + private + + def launch_comments_parser + CommentsParser.new(post.link).launch_parser + end + + def launch_rating_counter + CommentRatingCounter.new(text_body).launch_counter + end + + def refresh_post_title + page = Mechanize.new.get(post.link) + @post.update(title: page.title) + end + + def build_post_stat + text_body.each_with_index do |text, index| + comment = Comment.create(text: text, rating: ratings[index]) + post.comments.add(comment) + end + end + + def refresh_post_rating + post_rating = (ratings.sum / text_body.size).to_i + @post.update(rating: post_rating) + end +end \ No newline at end of file diff --git a/2264/3/models/post.rb b/2264/3/models/post.rb index 749e7c3ec..4b1b13593 100644 --- a/2264/3/models/post.rb +++ b/2264/3/models/post.rb @@ -1,6 +1,7 @@ -require 'ohm' class Post < Ohm::Model attribute :link attribute :title + set :comments, :Comment + attribute :rating end diff --git a/2264/3/views/post/index.erb b/2264/3/views/post/index.erb index 6c1ab23f7..4465b3995 100644 --- a/2264/3/views/post/index.erb +++ b/2264/3/views/post/index.erb @@ -12,10 +12,14 @@ <% @posts.each do |post| %> - <%= post.link %> + <%= post.title %> + <%= post.rating %> <% end %> + diff --git a/2264/3/views/post/new.erb b/2264/3/views/post/new.erb index a4e5ce87f..e4d89aa89 100644 --- a/2264/3/views/post/new.erb +++ b/2264/3/views/post/new.erb @@ -1,9 +1,8 @@ - -

New Post

+

Add Post

- +
\ No newline at end of file diff --git a/2264/3/views/post/show.erb b/2264/3/views/post/show.erb index b272bbb24..108b86f86 100644 --- a/2264/3/views/post/show.erb +++ b/2264/3/views/post/show.erb @@ -1,6 +1,39 @@ +
-

<%= @post.title %>

- -
\ No newline at end of file + + <%=@post.link %> + + + +
+
+ + +
+
+

+ <%= @post.title %> +

+ + + + + + <% @post.comments.each do |comment| %> + + + + + <% end %> +
CommentsRatings
+
+ <%= comment.text %> +
+
+ <%= comment.rating %> +
+ \ No newline at end of file From 5b95d5af935016275e9b7fa29af59cde79244e2c Mon Sep 17 00:00:00 2001 From: mersXX Date: Fri, 27 Jul 2018 20:53:19 +0300 Subject: [PATCH 6/6] Fix small defects --- 2264/3/Gemfile | 5 +---- 2264/3/config.ru | 2 +- 2264/3/controllers/application_controller.rb | 4 ++-- 2264/3/controllers/posts_controller.rb | 4 +--- 2264/3/helpers/comment_rating_counter.rb | 2 +- 2264/3/helpers/comments_parser.rb | 2 +- 2264/3/helpers/post_analyzer.rb | 3 ++- 2264/3/models/comment.rb | 3 ++- 2264/3/models/post.rb | 2 +- 2264/3/parser.rb | 12 ------------ 10 files changed, 12 insertions(+), 27 deletions(-) delete mode 100644 2264/3/parser.rb diff --git a/2264/3/Gemfile b/2264/3/Gemfile index f5bc12547..f6f286ae1 100644 --- a/2264/3/Gemfile +++ b/2264/3/Gemfile @@ -6,9 +6,6 @@ gem 'mechanize' gem 'ohm' gem 'pry' gem 'pry-nav' -gem 'sinatra' gem 'shotgun' +gem 'sinatra' gem 'thin' - - - diff --git a/2264/3/config.ru b/2264/3/config.ru index eebe9267a..92ca91fbb 100644 --- a/2264/3/config.ru +++ b/2264/3/config.ru @@ -6,4 +6,4 @@ Comment.redis = Redic.new('redis://127.0.0.1:6379/1') use Rack::MethodOverride -map('/') { run PostsController } \ No newline at end of file +map('/') { run PostsController } diff --git a/2264/3/controllers/application_controller.rb b/2264/3/controllers/application_controller.rb index d96bede78..850294a75 100644 --- a/2264/3/controllers/application_controller.rb +++ b/2264/3/controllers/application_controller.rb @@ -1,5 +1,5 @@ require 'sinatra' - +# main controller class ApplicationController < Sinatra::Base set :views, File.expand_path(File.join(__FILE__, '../../views')) -end \ No newline at end of file +end diff --git a/2264/3/controllers/posts_controller.rb b/2264/3/controllers/posts_controller.rb index 4a12b3ed7..3a3c41cd2 100644 --- a/2264/3/controllers/posts_controller.rb +++ b/2264/3/controllers/posts_controller.rb @@ -1,7 +1,6 @@ require 'sinatra' - +# controller for posts class PostsController < ApplicationController - get '/posts/new' do erb :'post/new' end @@ -29,4 +28,3 @@ class PostsController < ApplicationController redirect '/posts' end end - diff --git a/2264/3/helpers/comment_rating_counter.rb b/2264/3/helpers/comment_rating_counter.rb index e0a5bdce2..da92b8493 100644 --- a/2264/3/helpers/comment_rating_counter.rb +++ b/2264/3/helpers/comment_rating_counter.rb @@ -47,4 +47,4 @@ def build_request request['Ocp-Apim-Subscription-Key'] = KEY request.body = documents.to_json end -end \ No newline at end of file +end diff --git a/2264/3/helpers/comments_parser.rb b/2264/3/helpers/comments_parser.rb index a12ecf525..0d758206c 100644 --- a/2264/3/helpers/comments_parser.rb +++ b/2264/3/helpers/comments_parser.rb @@ -32,4 +32,4 @@ def parse_comments on_url = "https://comments.api.onliner.by/news/tech.post/#{parse_page_code}/comments?limit=100&_=0.9046614793472092" agent.get(on_url) end -end \ No newline at end of file +end diff --git a/2264/3/helpers/post_analyzer.rb b/2264/3/helpers/post_analyzer.rb index 751308bfe..a31494517 100644 --- a/2264/3/helpers/post_analyzer.rb +++ b/2264/3/helpers/post_analyzer.rb @@ -1,3 +1,4 @@ +# class is used for analyzing onliner's posts class PostAnalyser attr_reader :post, :text_body, :ratings @@ -39,4 +40,4 @@ def refresh_post_rating post_rating = (ratings.sum / text_body.size).to_i @post.update(rating: post_rating) end -end \ No newline at end of file +end diff --git a/2264/3/models/comment.rb b/2264/3/models/comment.rb index 02cb049c2..d667b3041 100644 --- a/2264/3/models/comment.rb +++ b/2264/3/models/comment.rb @@ -1,4 +1,5 @@ +# class for onliner's comments class Comment < Ohm::Model attribute :text attribute :rating -end \ No newline at end of file +end diff --git a/2264/3/models/post.rb b/2264/3/models/post.rb index 4b1b13593..59ee3e379 100644 --- a/2264/3/models/post.rb +++ b/2264/3/models/post.rb @@ -1,4 +1,4 @@ - +# class for onliner posts class Post < Ohm::Model attribute :link attribute :title diff --git a/2264/3/parser.rb b/2264/3/parser.rb deleted file mode 100644 index ea311216f..000000000 --- a/2264/3/parser.rb +++ /dev/null @@ -1,12 +0,0 @@ -require 'mechanize' -require 'json' - -agent = Mechanize.new -page = agent.get('https://tech.onliner.by/2018/07/24/intel-26') - -pp page.css('p').inner_html - - - - -