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..3124f28
--- /dev/null
+++ b/application/controller/app.rb
@@ -0,0 +1,37 @@
+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
+
+ private
+
+ def normalized(string)
+ string.gsub('_',' ')
+ 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..8061a68
--- /dev/null
+++ b/application/controller/paragraphs.rb
@@ -0,0 +1,26 @@
+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|
+ topic_name = normalized(topic_name)
+ catalog_name = normalized(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..8a1bfaa
--- /dev/null
+++ b/application/controller/summaries.rb
@@ -0,0 +1,26 @@
+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|
+ topic_name = normalized(topic_name)
+ catalog_name = normalized(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..3749a87
--- /dev/null
+++ b/application/controller/topic.rb
@@ -0,0 +1,42 @@
+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
+ topic_name = normalized(topic_name)
+ 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 @@
-
Generated
2017-11-24T12:51:57+00:00
+
Generated
2017-11-25T10:14:41+00:00
All Files
- (88.59%
+ (89.67%
covered at
- 59.23
+ 62.24
hits/line)
52 files in total.
- 587 relevant lines.
- 520 lines covered and
- 67 lines missed
+ 600 relevant lines.
+ 538 lines covered and
+ 62 lines missed
@@ -53,7 +53,7 @@
| application/app.rb |
82.14 % |
- 96 |
+ 98 |
56 |
46 |
10 |
@@ -232,12 +232,12 @@
| domain/database_repositories/article.rb |
- 84.0 % |
- 47 |
- 25 |
- 21 |
+ 86.67 % |
+ 59 |
+ 30 |
+ 26 |
4 |
- 88.6 |
+ 15.7 |
@@ -272,12 +272,12 @@
| domain/database_repositories/paragraph.rb |
- 59.46 % |
- 72 |
- 37 |
- 22 |
- 15 |
- 341.0 |
+ 75.0 % |
+ 77 |
+ 40 |
+ 30 |
+ 10 |
+ 319.1 |
@@ -443,11 +443,11 @@
| domain/wikipedia_mappers/paragraph_mapper.rb |
100.0 % |
- 81 |
- 48 |
- 48 |
+ 95 |
+ 53 |
+ 53 |
0 |
- 356.9 |
+ 402.0 |
@@ -978,199 +978,211 @@ 82.14 % covered
end
-
+
+
+
+ # GET /api/v0.1/paragraphs/topic_name/catalog_name
+
+
+
2
routing.on 'paragraphs', String, String do |topic_name, catalog_name|
-
+
-
+
2
routing.get do
-
+
2
find_result = FindDatabaseArticle.call(topic: topic_name.capitalize, catalog: catalog_name)
-
+
2
result = find_result.value.message
-
+
2
http_response = HttpResponseRepresenter.new(find_result.value)
-
+
2
response.status = http_response.http_code
-
+
2
if find_result.success?
-
+
1
ArticleRepresenter.new(Article.new(result.topic, result.catalogs, result.paragraphs)).to_json
-
+
else
-
+
1
http_response.to_json
-
+
end
-
+
end
-
+
end
-
+
+
+
+ #GET api/v0.1/summaries/topic_name/catalog_name
+
+
+
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
@@ -3367,10 +3379,10 @@ 89.47 % covered
@@ -3435,7 +3447,7 @@
84.0 % covered
11
- rebuild_entity(db_topic, catalog_name, [])
+ rebuild_entity(db_topic, catalog_name, false)
@@ -3477,7 +3489,7 @@ 84.0 % covered
- rebuild_entity(db_topic, catalog_name, [topic_name, catalog_name])
+ rebuild_entity(db_topic, catalog_name, true)
@@ -3507,154 +3519,226 @@ 84.0 % covered
11
- catalog_error = true
+ catalogs = build_catalog(db_record)
11
- catalogs = db_record.catalogs.map do |db_catalog|
+ return nil if catalog_error(catalogs, catalog_name)
-
- 154
+
- Catalog.rebuild_entity(db_catalog)
+
+
-
+
+ 10
+
+ paragraphs = build_pargraphs(db_record, catalog_name, summary)
+
+
+
- end
+
-
+
+ 10
+
+ Entity::Article.new(
+
+
+
+
+
+ topic: Topic.rebuild_entity(db_record),
+
+
+
+
+
+ catalogs: catalogs,
+
+
+
+
+
+ paragraphs: paragraphs
+
+
+
+
+
+ )
+
+
+
+
+
+ end
+
+
+
+
+
+
+
+
+
+ 1
+
+ private
+
+
+
+ 1
+
+ def self.catalog_error(catalogs, catalog_name)
+
+
+
+ 11
+
+ error = true
+
+
+
11
catalogs.each do |catalog|
-
+
154
- catalog_error = false if catalog.name == catalog_name
+ error = false if catalog.name == catalog_name
-
+
end
-
+
11
- return nil if catalog_error
+ error
-
- 1740
+
+
- default_paragraphs = db_record.paragraphs.select { |p| p.catalog.name == catalog_name }
+ end
-
- 10
+
+
- if summary.empty?
+
-
- 10
+
+ 1
- paragraphs = default_paragraphs.map do |db_paragraph|
+ def self.build_catalog(db_record)
-
- 40
+
+ 11
- Paragraph.rebuild_entity(db_paragraph, nil)
+ db_record.catalogs.map do |db_catalog|
-
-
+
+ 154
- end
+ Catalog.rebuild_entity(db_catalog)
-
+
- else
+ end
-
+
- paragraphs = Paragraph.summarize(summary[0], summary[1])
+ end
-
+
- end
+
-
-
+
+ 1
-
+ def self.build_pargraphs(db_record, catalog_name, summary)
-
+
10
- Entity::Article.new(
+ if summary
-
+
- topic: Topic.rebuild_entity(db_record),
+ Paragraph.summarize(db_record.name, catalog_name)
-
+
- catalogs: catalogs,
+ else
-
-
+
+ 10
- paragraphs: paragraphs
+ Paragraph.find_by_topic_catalog(db_record.name, catalog_name)
-
+
- )
+ end
-
+
end
-
+
+
+
+
+
+
+
end
-
+
end
-
+
end
@@ -4049,11 +4133,11 @@ 100.0 % covered
@@ -4117,376 +4201,406 @@
59.46 % covered
- db_topic = Database::TopicOrm.first(name: topic_name.capitalize)
+ all = get_all(topic_name, catalog_name)
- db_catalog = Database::CatalogOrm.first(name: catalog_name)
+ summaries = []
- paragraphs = Database::ParagraphOrm.where(topic_id: db_topic.id, catalog_id: db_catalog.id).all
+ all[2].each do |paragraph|
- summaries = []
-
-
-
-
-
- paragraphs.each do |paragraph|
-
-
-
-
-
record = Entity::Summarize.new(paragraph.content)
-
+
summaries.push(record.summarize)
-
+
end
-
+
- summaries.map {|summary| rebuild_entity(paragraphs[0], summary)}
+ summaries.map {|summary| rebuild_entity(all[2][0], summary)}
-
+
end
-
+
-
+
1
def self.all
-
+
Database::ParagraphOrm.all.map {|db_paragraph| rebuild_entity(db_paragraph, nil)}
-
+
end
-
+
-
+
1
def self.find_by_topic(topic_name)
-
+
db_topic = Database::TopicOrm.first(name: topic_name.capitalize)
-
+
Database::ParagraphOrm.where(topic_id: db_topic.id).all.map {|db_paragraph| rebuild_entity(db_paragraph, nil)}
-
+
end
-
+
-
+
1
def self.find_by_topic_catalog(topic_name, catalog_name)
-
-
-
- db_topic = Database::TopicOrm.first(name: topic_name.capitalize)
-
-
-
-
+
+ 10
- db_catalog = Database::CatalogOrm.first(name: catalog_name)
+ all = get_all(topic_name, catalog_name)
-
+
+ 50
-
- Database::ParagraphOrm.where(topic_id: db_topic.id, catalog_id: db_catalog.id).all.map {|db_paragraph| rebuild_entity(db_paragraph, nil)}
+ Database::ParagraphOrm.where(topic_id: all[0].id, catalog_id: all[1].id).all.map {|db_paragraph| rebuild_entity(db_paragraph, nil)}
-
+
end
-
+
-
+
1
def self.create(entities)
-
- 8
-
- db_topic = Database::TopicOrm.first(name: entities[0].topic)
-
-
-
+
8
- db_catalog = Database::CatalogOrm.first(name: entities[0].catalog)
+ all = get_all(entities[0].topic, entities[0].catalog)
-
+
-
+
8
db_paragraphs = entities.map do |entity|
-
+
1384
- db_catalog = Database::CatalogOrm.first(name: entity.catalog) if db_catalog.name != entity.catalog
+ all[1] = Database::CatalogOrm.first(name: entity.catalog) if all[1].name != entity.catalog
-
+
1384
db_paragraph = Database::ParagraphOrm.new(content: entity.content)
-
+
1384
- db_paragraph.topic = db_topic
+ db_paragraph.topic = all[0]
-
+
1384
- db_paragraph.catalog = db_catalog
+ db_paragraph.catalog = all[1]
-
+
1384
db_paragraph.save
-
+
end
-
+
-
+
1392
db_paragraphs.map {|db_paragraph| rebuild_entity(db_paragraph, nil)}
-
+
-
+
end
-
+
-
+
1
def self.rebuild_entity(db_record, summary)
-
+
1424
return nil unless db_record
-
+
1424
if summary == nil
-
+
1424
Entity::Paragraph.new(
-
+
content: db_record.content,
-
+
catalog: db_record.catalog.name,
-
+
topic: db_record.topic.name
-
+
)
-
+
else
-
+
Entity::Paragraph.new(
-
+
content: summary,
-
+
catalog: db_record.catalog.name,
-
+
topic: db_record.topic.name
-
+
)
-
+
end
-
+
end
-
+
-
+
+ 1
+
+ private
+
+
+
+ 1
+
+ def self.get_all(topic_name, catalog_name)
+
+
+
+ 18
+
+ db_topic = Database::TopicOrm.first(name: topic_name.capitalize)
+
+
+
+ 18
+
+ db_catalog = Database::CatalogOrm.first(name: catalog_name)
+
+
+
+ 18
+
+ db_paragraphs = Database::ParagraphOrm.where(topic_id: db_topic.id, catalog_id: db_catalog.id).all
+
+
+
+ 18
+
+ all = [db_topic, db_catalog, db_paragraphs]
+
+
+
+ 18
+
+ all
+
+
+
+
+
+ end
+
+
+
+
+
+
+
+
+
end
-
+
-
+
end
-
+
end
@@ -6054,8 +6168,8 @@ 100.0 % covered
domain/wikipedia_mappers/paragraph_mapper.rb
100.0 % covered
- 48 relevant lines.
- 48 lines covered and
+ 53 relevant lines.
+ 53 lines covered and
0 lines missed.
@@ -6213,34 +6327,34 @@ 100.0 % covered
@article_data = article_data
-
+
+ 11
+
+ @paragraphs = []
+
+
+
end
-
+
-
+
1
def build_entity
-
- 11
-
- paragraph_hash = build_paragraphs_in_hash
-
-
11
- paragraphs = []
+ paragraph_hash = build_paragraphs_in_hash
@@ -6288,7 +6402,7 @@ 100.0 % covered
1903
- paragraphs.push(paragraph)
+ @paragraphs.push(paragraph)
@@ -6306,7 +6420,7 @@ 100.0 % covered
11
- paragraphs
+ @paragraphs
@@ -6330,220 +6444,304 @@ 100.0 % covered
1
- def build_catalogs
+ def pre_setting
11
- html_doc = Nokogiri::HTML(@article_data['extract'])
+ @catalog_hash = {}
11
- catalogs = html_doc.css('h2')
+ @catalog_hash['default'] = []
11
- catalogs
+ setting_catalogs
-
+
+ 11
+
+ setting_hash_with_catalog
+
+
+
end
-
+
-
+
1
- def build_hash
+ def nokogiri_parse
-
- 11
+
+ 22
- catalogs = build_catalogs
+ Nokogiri::HTML(@article_data['extract'])
+
+
+
+
+
+ end
+
+
+
+
+
+
+
+
+
+ 1
+
+ def setting_catalogs
-
+
11
- article_hash = {}
+ html_doc = nokogiri_parse
-
+
11
- article_hash['default'] = []
+ @catalogs = html_doc.css('h2')
-
+
+
+
+ end
+
+
+
+
+
+
+
+
+
+ 1
+
+ def setting_hash_with_catalog
+
+
+
11
- catalogs.each do |catalog|
+ @catalogs.each do |catalog|
-
+
154
break if catalog.text == 'See also'
-
+
143
- article_hash[catalog.text] = []
+ @catalog_hash[catalog.text] = []
-
+
end
-
- 11
-
- article_hash
-
-
-
+
end
-
+
-
+
1
- def build_paragraphs_in_hash
+ def a_paragraph(element)
-
- 11
+
+ 4158
- html_doc = Nokogiri::HTML(@article_data['extract'])
+ element.name != 'h2' && !element.text.include?("\n") && !element.text.empty?
-
- 11
+
- elements = html_doc.children[1].children[0].children
+
+ end
-
- 11
+
+
+
+
+
+
+
+ 1
- paragraph_hash = build_hash
+ def setting_paragraphs(hash, elements)
-
+
11
key = 'default'
-
+
11
elements.each do |element|
-
+
4169
break if element.text == 'See also'
-
+
4158
- if element.name != 'h2' && !element.text.include?("\n") && !element.text.empty?
+ if a_paragraph(element)
-
+
1903
- paragraph_hash[key].push(element.text)
+ hash[key].push(element.text)
-
+
2255
elsif element.name == 'h2'
-
+
143
key = element.text
-
+
end
-
+
end
-
+
11
- paragraph_hash
+ hash
-
+
end
-
+
-
+
+ 1
+
+ def build_paragraphs_in_hash
+
+
+
+ 11
+
+ pre_setting
+
+
+
+ 11
+
+ elements = nokogiri_parse.children[1].children[0].children
+
+
+
+ 11
+
+ setting_paragraphs(@catalog_hash, elements)
+
+
+
+
+
+ end
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
end
-
+
end
-
+
end
-
+
end
diff --git a/domain/database_repositories/article.rb b/domain/database_repositories/article.rb
index 9db31a7..9636ae3 100644
--- a/domain/database_repositories/article.rb
+++ b/domain/database_repositories/article.rb
@@ -7,34 +7,22 @@ def self.find(topic_name, catalog_name)
db_topic = Database::TopicOrm.first(name: topic_name.capitalize)
return nil unless db_topic
- rebuild_entity(db_topic, catalog_name, [])
+ rebuild_entity(db_topic, catalog_name, false)
end
def self.summarize(topic_name, catalog_name)
db_topic = Database::TopicOrm.first(name: topic_name.capitalize)
return nil unless db_topic
- rebuild_entity(db_topic, catalog_name, [topic_name, catalog_name])
+ rebuild_entity(db_topic, catalog_name, true)
end
def self.rebuild_entity(db_record, catalog_name, summary)
return nil unless db_record
- catalog_error = true
- catalogs = db_record.catalogs.map do |db_catalog|
- Catalog.rebuild_entity(db_catalog)
- end
- catalogs.each do |catalog|
- catalog_error = false if catalog.name == catalog_name
- end
- return nil if catalog_error
- default_paragraphs = db_record.paragraphs.select { |p| p.catalog.name == catalog_name }
- if summary.empty?
- paragraphs = default_paragraphs.map do |db_paragraph|
- Paragraph.rebuild_entity(db_paragraph, nil)
- end
- else
- paragraphs = Paragraph.summarize(summary[0], summary[1])
- end
+ catalogs = build_catalog(db_record)
+ return nil if catalog_error(catalogs, catalog_name)
+
+ paragraphs = build_pargraphs(db_record, catalog_name, summary)
Entity::Article.new(
topic: Topic.rebuild_entity(db_record),
@@ -42,6 +30,30 @@ def self.rebuild_entity(db_record, catalog_name, summary)
paragraphs: paragraphs
)
end
+
+ private
+ def self.catalog_error(catalogs, catalog_name)
+ error = true
+ catalogs.each do |catalog|
+ error = false if catalog.name == catalog_name
+ end
+ error
+ end
+
+ def self.build_catalog(db_record)
+ db_record.catalogs.map do |db_catalog|
+ Catalog.rebuild_entity(db_catalog)
+ end
+ end
+
+ def self.build_pargraphs(db_record, catalog_name, summary)
+ if summary
+ Paragraph.summarize(db_record.name, catalog_name)
+ else
+ Paragraph.find_by_topic_catalog(db_record.name, catalog_name)
+ end
+ end
+
end
end
end
\ No newline at end of file
diff --git a/domain/database_repositories/paragraph.rb b/domain/database_repositories/paragraph.rb
index eb38d02..c762a7f 100644
--- a/domain/database_repositories/paragraph.rb
+++ b/domain/database_repositories/paragraph.rb
@@ -7,15 +7,13 @@ module Repository
class Paragraph
def self.summarize(topic_name, catalog_name)
- db_topic = Database::TopicOrm.first(name: topic_name.capitalize)
- db_catalog = Database::CatalogOrm.first(name: catalog_name)
- paragraphs = Database::ParagraphOrm.where(topic_id: db_topic.id, catalog_id: db_catalog.id).all
summaries = []
- paragraphs.each do |paragraph|
+ db_paragraphs = get_db_paragraph(topic_name, catalog_name)
+ db_paragraphs.each do |paragraph|
record = Entity::Summarize.new(paragraph.content)
summaries.push(record.summarize)
end
- summaries.map {|summary| rebuild_entity(paragraphs[0], summary)}
+ summaries.map {|summary| rebuild_entity(db_paragraphs[0], summary)}
end
def self.all
@@ -23,19 +21,17 @@ def self.all
end
def self.find_by_topic(topic_name)
- db_topic = Database::TopicOrm.first(name: topic_name.capitalize)
+ db_topic = get_db_topic
Database::ParagraphOrm.where(topic_id: db_topic.id).all.map {|db_paragraph| rebuild_entity(db_paragraph, nil)}
end
def self.find_by_topic_catalog(topic_name, catalog_name)
- db_topic = Database::TopicOrm.first(name: topic_name.capitalize)
- db_catalog = Database::CatalogOrm.first(name: catalog_name)
- Database::ParagraphOrm.where(topic_id: db_topic.id, catalog_id: db_catalog.id).all.map {|db_paragraph| rebuild_entity(db_paragraph, nil)}
+ get_db_paragraph(topic_name, catalog_name).map {|db_paragraph| rebuild_entity(db_paragraph, nil)}
end
def self.create(entities)
- db_topic = Database::TopicOrm.first(name: entities[0].topic)
- db_catalog = Database::CatalogOrm.first(name: entities[0].catalog)
+ db_topic = get_db_topic(entities[0].topic)
+ db_catalog = get_db_catalog(entities[0].catalog)
db_paragraphs = entities.map do |entity|
db_catalog = Database::CatalogOrm.first(name: entity.catalog) if db_catalog.name != entity.catalog
@@ -52,20 +48,32 @@ def self.create(entities)
def self.rebuild_entity(db_record, summary)
return nil unless db_record
if summary == nil
- Entity::Paragraph.new(
- content: db_record.content,
- catalog: db_record.catalog.name,
- topic: db_record.topic.name
- )
+ content = db_record.content
else
- Entity::Paragraph.new(
- content: summary,
+ content = summary
+ end
+ Entity::Paragraph.new(
+ content: content,
catalog: db_record.catalog.name,
topic: db_record.topic.name
)
- end
end
+ private
+
+ def self.get_db_topic(topic_name)
+ Database::TopicOrm.first(name: topic_name.capitalize)
+ end
+
+ def self.get_db_catalog(catalog_name)
+ Database::CatalogOrm.first(name: catalog_name)
+ end
+
+ def self.get_db_paragraph(topic_name, catalog_name)
+ db_topic = get_db_topic(topic_name)
+ db_catalog = get_db_catalog(catalog_name)
+ Database::ParagraphOrm.where(topic_id: db_topic.id, catalog_id: db_catalog.id).all
+ end
end
end
diff --git a/domain/wikipedia_mappers/catalog_mapper.rb b/domain/wikipedia_mappers/catalog_mapper.rb
index e21fdfb..81dc230 100644
--- a/domain/wikipedia_mappers/catalog_mapper.rb
+++ b/domain/wikipedia_mappers/catalog_mapper.rb
@@ -28,24 +28,23 @@ def initialize(article_data)
def build_entity
catalogs = build_catalogs
catalogs_array = []
- catalogs_array.push(
- Entity::Catalog.new(
- name: 'default',
- topic: @article_data['title']
- )
- )
+ catalogs_array.push( new_entity('Default', @article_data['title']) )
catalogs.each do |catalog|
break if catalog.content == 'See also'
- catalog = Entity::Catalog.new(
- name: catalog.content,
- topic: @article_data['title'],
- )
+ catalog = new_entity(catalog.content, @article_data['title'])
catalogs_array.push(catalog)
end
catalogs_array
end
private
+ def new_entity(name, topic)
+ Entity::Catalog.new(
+ name: name,
+ topic: topic
+ )
+ end
+
def build_catalogs
html_doc = Nokogiri::HTML(@article_data['extract'])
catalogs = html_doc.css('h2')
diff --git a/domain/wikipedia_mappers/paragraph_mapper.rb b/domain/wikipedia_mappers/paragraph_mapper.rb
index b6e65e3..aa906fa 100644
--- a/domain/wikipedia_mappers/paragraph_mapper.rb
+++ b/domain/wikipedia_mappers/paragraph_mapper.rb
@@ -23,11 +23,11 @@ class DataMapper
def initialize(article_data)
@article_data = article_data
+ @paragraphs = []
end
def build_entity
paragraph_hash = build_paragraphs_in_hash
- paragraphs = []
paragraph_hash.keys.each do |key|
paragraph_hash[key].each do |value|
paragraph = Entity::Paragraph.new(
@@ -35,46 +35,57 @@ def build_entity
topic: @article_data['title'],
catalog: key
)
- paragraphs.push(paragraph)
+ @paragraphs.push(paragraph)
end
end
- paragraphs
+ @paragraphs
end
private
- def build_catalogs
- html_doc = Nokogiri::HTML(@article_data['extract'])
- catalogs = html_doc.css('h2')
- catalogs
+ def pre_setting
+ @catalog_hash = {}
+ @catalog_hash['default'] = []
+ setting_catalogs
+ setting_hash_with_catalog
end
- def build_hash
- catalogs = build_catalogs
- article_hash = {}
- article_hash['default'] = []
- catalogs.each do |catalog|
+ def nokogiri_parse
+ Nokogiri::HTML(@article_data['extract'])
+ end
+
+ def setting_catalogs
+ html_doc = nokogiri_parse
+ @catalogs = html_doc.css('h2')
+ end
+
+ def setting_hash_with_catalog
+ @catalogs.each do |catalog|
break if catalog.text == 'See also'
- article_hash[catalog.text] = []
+ @catalog_hash[catalog.text] = []
end
- article_hash
end
- def build_paragraphs_in_hash
- html_doc = Nokogiri::HTML(@article_data['extract'])
- elements = html_doc.children[1].children[0].children
- paragraph_hash = build_hash
+ def a_paragraph(element)
+ element.name != 'h2' && !element.text.include?("\n") && !element.text.empty?
+ end
+
+ def setting_paragraphs(hash, elements)
key = 'default'
elements.each do |element|
break if element.text == 'See also'
- if element.name != 'h2' && !element.text.include?("\n") && !element.text.empty?
- paragraph_hash[key].push(element.text)
- elsif element.name == 'h2'
- key = element.text
- end
+ hash[key].push(element.text) if a_paragraph(element)
+ key = element.text if element.name == 'h2'
end
- paragraph_hash
+ hash
end
+ def build_paragraphs_in_hash
+ pre_setting
+ elements = nokogiri_parse.children[1].children[0].children
+ setting_paragraphs(@catalog_hash, elements)
+ end
+
+
end
end
end
diff --git a/test.rb b/test.rb
index 7d44c61..21ff98d 100644
--- a/test.rb
+++ b/test.rb
@@ -13,7 +13,7 @@
require_relative './config/environment.rb'
require_relative './init.rb'
-require_relative 'application/app.rb'
+require_relative 'application/controller/app.rb'
Hirb.enable