From 17001a76761b6db2cef24edf9f33f2af2ec1b652 Mon Sep 17 00:00:00 2001 From: Rails for Vic Date: Thu, 30 Nov 2017 14:05:01 +0000 Subject: [PATCH 1/2] controller restructuration --- Rakefile | 5 +- application/app.rb | 96 --- application/controller/app.rb | 31 + application/controller/init.rb | 1 + application/controller/paragraphs.rb | 24 + application/controller/summaries.rb | 24 + application/controller/topic.rb | 41 + application/controller/topics.rb | 21 + application/init.rb | 2 +- coverage/.last_run.json | 2 +- coverage/.resultset.json | 73 +- coverage/index.html | 758 ++++++++++++------- domain/database_repositories/article.rb | 48 +- domain/database_repositories/paragraph.rb | 46 +- domain/wikipedia_mappers/catalog_mapper.rb | 19 +- domain/wikipedia_mappers/paragraph_mapper.rb | 59 +- test.rb | 2 +- 17 files changed, 780 insertions(+), 472 deletions(-) delete mode 100644 application/app.rb create mode 100644 application/controller/app.rb create mode 100644 application/controller/init.rb create mode 100644 application/controller/paragraphs.rb create mode 100644 application/controller/summaries.rb create mode 100644 application/controller/topic.rb create mode 100644 application/controller/topics.rb diff --git a/Rakefile b/Rakefile index d4fe8ca..f921cbc 100644 --- a/Rakefile +++ b/Rakefile @@ -11,12 +11,13 @@ task :console do end namespace :quality do + CODE = '**/*.rb' task :flog do - sh 'flog lib/' + sh "flog #{CODE}" end task :reek do - sh 'reek lib/' + sh "reek #{CODE}" end end diff --git a/application/app.rb b/application/app.rb deleted file mode 100644 index 6bb34f8..0000000 --- a/application/app.rb +++ /dev/null @@ -1,96 +0,0 @@ -require 'roda' - -module WiKey -#Web Api - class Api < Roda - plugin :environments - plugin :json - plugin :halt - - route do |routing| - app = Api - - routing.root do - { 'message' => "WiKey API v0.1 up in #{app.environment}." } - end - # /api branch - routing.on 'api' do - # /api/v0.1 branch - routing.on 'v0.1' do - # /api/v0.1/topic/name branch - routing.on 'topics' do - routing.get do - topics = FindDatabaseTopic.all - http_response = HttpResponseRepresenter.new(topics.value) - response.status = http_response.http_code - if topics.success? - TopicsRepresenter.new(Topics.new(topics.value.message)).to_json - else - http_response.to_json - end - end - end - routing.on 'topic', String do |topic_name| - # GET /api/v0.1/topic/topic_name request - routing.get do - find_result = FindDatabaseArticle.call(topic: topic_name.capitalize, catalog: 'default') - result = find_result.value.message - http_response = HttpResponseRepresenter.new(find_result.value) - response.status = http_response.http_code - if find_result.success? - ArticleRepresenter.new(Article.new(result.topic, result.catalogs, result.paragraphs)).to_json - else - http_response.to_json - end - end - # POST /api/v0.1/topic/topic_name - routing.post do - service_result = LoadFromWiki.new.call( - gateway: Wiki::Api, - topic: topic_name - ) - - http_response = HttpResponseRepresenter.new(service_result.value) - result = service_result.value.message - response.status = http_response.http_code - if service_result.success? - response['Loaction'] = "/api/v0.1/source/#{topic_name}" - ArticleRepresenter.new(Article.new(result.topic, result.catalogs, result.paragraphs)).to_json - else - http_response.to_json - end - end - end - routing.on 'paragraphs', String, String do |topic_name, catalog_name| - - routing.get do - find_result = FindDatabaseArticle.call(topic: topic_name.capitalize, catalog: catalog_name) - result = find_result.value.message - http_response = HttpResponseRepresenter.new(find_result.value) - response.status = http_response.http_code - if find_result.success? - ArticleRepresenter.new(Article.new(result.topic, result.catalogs, result.paragraphs)).to_json - else - http_response.to_json - end - end - end - routing.on 'summaries', String, String do |topic_name, catalog_name| - - routing.get do - find_result = FindDatabaseArticle.summarize(topic: topic_name.capitalize, catalog: catalog_name) - result = find_result.value.message - http_response = HttpResponseRepresenter.new(find_result.value) - response.status = http_response.http_code - if find_result.success? - ArticleRepresenter.new(Article.new(result.topic, result.catalogs, result.paragraphs)).to_json - else - http_response.to_json - end - end - end - end - end - end - end -end \ No newline at end of file diff --git a/application/controller/app.rb b/application/controller/app.rb new file mode 100644 index 0000000..545833e --- /dev/null +++ b/application/controller/app.rb @@ -0,0 +1,31 @@ +require 'roda' + +module WiKey +#Web Api + class Api < Roda + plugin :environments + plugin :json + plugin :halt + plugin :multi_route + + require_relative 'topic' + require_relative 'paragraphs' + require_relative 'topics' + require_relative 'summaries' + + route do |routing| + app = Api + routing.root do + { 'message' => "WiKey API v0.1 up in #{app.environment}." } + end + # /api branch + routing.on 'api' do + # /api/v0.1 branch + routing.on 'v0.1' do + # /api/v0.1/topic/name branch + routing.multi_route + end + end + end + end +end \ No newline at end of file diff --git a/application/controller/init.rb b/application/controller/init.rb new file mode 100644 index 0000000..e60159f --- /dev/null +++ b/application/controller/init.rb @@ -0,0 +1 @@ +require_relative 'app.rb' \ No newline at end of file diff --git a/application/controller/paragraphs.rb b/application/controller/paragraphs.rb new file mode 100644 index 0000000..be95a62 --- /dev/null +++ b/application/controller/paragraphs.rb @@ -0,0 +1,24 @@ +require "roda" + +module WiKey + + class Api < Roda + # GET /api/v0.1/paragraphs/topic_name/catalog_name + route('paragraphs') do |routing| + + routing.on String, String do |topic_name, catalog_name| + routing.get do + find_result = FindDatabaseArticle.call(topic: topic_name.capitalize, catalog: catalog_name) + result = find_result.value.message + http_response = HttpResponseRepresenter.new(find_result.value) + response.status = http_response.http_code + if find_result.success? + ArticleRepresenter.new(Article.new(result.topic, result.catalogs, result.paragraphs)).to_json + else + http_response.to_json + end + end + end + end + end +end \ No newline at end of file diff --git a/application/controller/summaries.rb b/application/controller/summaries.rb new file mode 100644 index 0000000..87f87ca --- /dev/null +++ b/application/controller/summaries.rb @@ -0,0 +1,24 @@ +require "roda" + +module WiKey + + class Api < Roda + #GET api/v0.1/summaries/topic_name/catalog_name + route('summaries') do |routing| + + routing.on String, String do |topic_name, catalog_name| + routing.get do + find_result = FindDatabaseArticle.summarize(topic: topic_name.capitalize, catalog: catalog_name) + result = find_result.value.message + http_response = HttpResponseRepresenter.new(find_result.value) + response.status = http_response.http_code + if find_result.success? + ArticleRepresenter.new(Article.new(result.topic, result.catalogs, result.paragraphs)).to_json + else + http_response.to_json + end + end + end + end + end +end \ No newline at end of file diff --git a/application/controller/topic.rb b/application/controller/topic.rb new file mode 100644 index 0000000..8b4be80 --- /dev/null +++ b/application/controller/topic.rb @@ -0,0 +1,41 @@ +require "roda" + +module WiKey + + class Api < Roda + route('topic') do |routing| + + routing.on String do |topic_name| + # GET /api/v0.1/topic/topic_name request + routing.get do + find_result = FindDatabaseArticle.call(topic: topic_name.capitalize, catalog: 'default') + result = find_result.value.message + http_response = HttpResponseRepresenter.new(find_result.value) + response.status = http_response.http_code + if find_result.success? + ArticleRepresenter.new(Article.new(result.topic, result.catalogs, result.paragraphs)).to_json + else + http_response.to_json + end + end + # POST /api/v0.1/topic/topic_name + routing.post do + service_result = LoadFromWiki.new.call( + gateway: Wiki::Api, + topic: topic_name + ) + + http_response = HttpResponseRepresenter.new(service_result.value) + result = service_result.value.message + response.status = http_response.http_code + if service_result.success? + response['Loaction'] = "/api/v0.1/source/#{topic_name}" + ArticleRepresenter.new(Article.new(result.topic, result.catalogs, result.paragraphs)).to_json + else + http_response.to_json + end + end + end + end + end +end \ No newline at end of file diff --git a/application/controller/topics.rb b/application/controller/topics.rb new file mode 100644 index 0000000..92a530e --- /dev/null +++ b/application/controller/topics.rb @@ -0,0 +1,21 @@ +require "roda" + +module WiKey + + class Api < Roda + route('topics') do |routing| + routing.on do + routing.get do + topics = FindDatabaseTopic.all + http_response = HttpResponseRepresenter.new(topics.value) + response.status = http_response.http_code + if topics.success? + TopicsRepresenter.new(Topics.new(topics.value.message)).to_json + else + http_response.to_json + end + end + end + end + end +end \ No newline at end of file diff --git a/application/init.rb b/application/init.rb index 88ee37e..3589e47 100644 --- a/application/init.rb +++ b/application/init.rb @@ -1,4 +1,4 @@ require_relative 'services/init.rb' require_relative 'representers/init.rb' -require_relative "app.rb" +require_relative "controller/init.rb" require_relative '../config/environment.rb' \ No newline at end of file diff --git a/coverage/.last_run.json b/coverage/.last_run.json index 302a99c..9e3a898 100644 --- a/coverage/.last_run.json +++ b/coverage/.last_run.json @@ -1,5 +1,5 @@ { "result": { - "covered_percent": 88.59 + "covered_percent": 89.67 } } diff --git a/coverage/.resultset.json b/coverage/.resultset.json index de2ff67..1d4f2a2 100644 --- a/coverage/.resultset.json +++ b/coverage/.resultset.json @@ -394,6 +394,7 @@ null, null, null, + null, 2, null, 2, @@ -408,6 +409,7 @@ null, null, null, + null, 0, null, 0, @@ -815,8 +817,6 @@ 0, 0, 0, - 0, - 0, null, 0, null, @@ -831,14 +831,12 @@ null, null, 1, - 0, - 0, - 0, + 10, + 50, null, null, 1, 8, - 8, null, 8, 1384, @@ -869,6 +867,15 @@ null, null, null, + 1, + 1, + 18, + 18, + 18, + 18, + 18, + null, + null, null, null, null, @@ -908,24 +915,36 @@ 11, 11, 11, - 154, null, + 10, + null, + 10, + null, + null, + null, + null, + null, + null, + 1, + 1, + 11, 11, 154, null, 11, - 1740, - 10, - 10, - 40, null, null, - 0, + 1, + 11, + 154, null, null, - 10, null, + 1, + 10, + 0, null, + 10, null, null, null, @@ -1089,12 +1108,12 @@ null, 1, 11, + 11, null, null, 1, 11, 11, - 11, 154, 1903, null, @@ -1112,23 +1131,30 @@ 11, 11, 11, + 11, + null, + null, + 1, + 22, null, null, 1, 11, 11, - 11, + null, + null, + 1, 11, 154, 143, null, - 11, null, null, 1, - 11, - 11, - 11, + 4158, + null, + null, + 1, 11, 11, 4169, @@ -1141,6 +1167,13 @@ 11, null, null, + 1, + 11, + 11, + 11, + null, + null, + null, null, null, null, @@ -1210,6 +1243,6 @@ null ] }, - "timestamp": 1511527916 + "timestamp": 1511604881 } } diff --git a/coverage/index.html b/coverage/index.html index 13f1a6e..6698240 100644 --- a/coverage/index.html +++ b/coverage/index.html @@ -14,27 +14,27 @@ loading