Skip to content

Commit

Permalink
Mapping checker added
Browse files Browse the repository at this point in the history
  • Loading branch information
k-rudy committed Apr 17, 2014
1 parent da100c6 commit c802389
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 4 deletions.
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ gem 'turbolinks'
# Used for bible scraping
gem 'nokogiri'

# Colorizing console output
gem 'colored'

group :development, :test do
gem 'pry'
end
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ GEM
coffee-script-source
execjs
coffee-script-source (1.7.0)
colored (1.2)
connection_pool (2.0.0)
coveralls (0.7.0)
multi_json (~> 1.3)
Expand Down Expand Up @@ -171,6 +172,7 @@ PLATFORMS
ruby

DEPENDENCIES
colored
coveralls
factory_girl_rails
jquery-rails
Expand Down
34 changes: 33 additions & 1 deletion lib/bible/scrapers/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ def scrape
Bible::Book.all.map { |book| scrape_book(book) }
end

# Returns the list of books titles that require mapping
#
# @return [ Array ] array of book titles needed to map
def missing_mappings
books_with_missing_mapping.map(&:title)
end

private

# Srapes single book translation
Expand Down Expand Up @@ -54,7 +61,7 @@ def process_verse(book, chapter, verse_number)
#
# @return [ String ] mapping value
def book_mapping(book)
title = book.title.downcase
title = book.title.downcase.gsub(/\s+/, '')
CONFIG[:sources][translation][:mappings][title] || title
end

Expand Down Expand Up @@ -97,6 +104,31 @@ def url
def translation
@translation ||= name.underscore.split('/').last
end

# Returns the list of books titles that require mapping
#
# @return [ Array ] array of books needed to map
def books_with_missing_mapping
Bible::Book.all.select do |book|
mapping = book_mapping(book)
Rails.logger.info("Mapping Check: #{book.title}")
begin
out scrape_verse(mapping, 1, 1), true
rescue OpenURI::HTTPError
out false, true
end
end
end

# Outputs iteration result in a Rspec like format
#
# @param [ true, false ] revert - indicates whether it's needed to revert the result
#
# @retun [ true, false] result itself
def out(result, revert = false)
(result ? print('.'.green) : print('F'.red)) unless Rails.env.test?
revert ? !result : result
end
end
end
end
Expand Down
13 changes: 13 additions & 0 deletions lib/tasks/scraper.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace :scraper do

desc 'Get the list of books that require mappings'
task :missing_mappings, [:translation] => [:environment] do |t, args|
if args[:translation].present?
puts 'Checking mappings. This can take a while...'
result = Bible::Scrapers.const_get(args[:translation].camelize).missing_mappings.to_sentence
puts "\nRequire mapping: #{result}"
else
puts 'Please set the translation like: rake scraper:missing_mappings[ru]'
end
end
end
14 changes: 13 additions & 1 deletion spec/libs/bible/scrapers/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,18 @@

describe '#missing_mappings' do

it 'returns the list of books that need mapping'
let(:genesis) { build(:book, title: 'Gn') }
let(:leviticus) { build(:book, title: 'Le') }

before do
subject.stub(translation: 'ru')
subject.stub(:scrape_verse).with('ge', 1, 1).and_return('Genesis')
subject.stub(:scrape_verse).with('le', 1, 1).and_return(nil)
Bible::Book.stub(all: [ genesis, leviticus ])
end

it 'returns the list of books that need mapping' do
expect(subject.missing_mappings).to eq([ 'Le' ])
end
end
end
4 changes: 2 additions & 2 deletions spec/libs/bible/scrapers/ru_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@
context 'when the verse number exists' do

it 'returns verse text' do
pending 'API checks are suspended on CI' if ENV['TRAVIS']
pending 'integrated API checks are suspended due to performance reasons'
expect(subject.send(:scrape_verse, 'ge', 1, 1)).to eq("В начале сотворил Бог небо и землю.")
end
end

context 'when the verse number doesnt exist' do

it 'returns nil' do
pending 'API checks are suspended on CI' if ENV['TRAVIS']
pending 'integrated API checks are suspended due to performance reasons'
expect(subject.send(:scrape_verse, 'ge', 1, 100)).to be_nil
end
end
Expand Down

0 comments on commit c802389

Please sign in to comment.