Skip to content

Commit

Permalink
V2.2.0 (#84)
Browse files Browse the repository at this point in the history
* Create LICENSE.md

* media models

* Dump 14

* unneeded code

* do not add db

* Media content in API

* submodule update

* Time to move surah info into database (#79)

* time to move surah info into database

* retrun info wrt to language

* surah infos

* no more tafsir search

* Segments and Surah info apis

* Upgrade ruby to 2.3.1

* fixed specs and added check for audio availability (#81)
  • Loading branch information
mmahalwy authored Jan 19, 2017
1 parent 6e8bf9d commit 4230782
Show file tree
Hide file tree
Showing 27 changed files with 251 additions and 38 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
config/deploy.rb
vendor/cache
vendor/bundle/
db/structure.sql
db
dump.rdb
passenger.*
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ git:
submodules: false

rvm:
- 2.2.3
- 2.3.1

cache: bundler

Expand Down
8 changes: 4 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM phusion/passenger-customizable:0.9.17
FROM phusion/passenger-customizable:0.9.19

# set correct environment variables
ENV HOME /root
Expand All @@ -7,14 +7,14 @@ ENV HOME /root
CMD ["/sbin/my_init"]

# customizing passenger-customizable image
RUN /pd_build/ruby2.2.sh
RUN /pd_build/ruby-2.3.*.sh
RUN /pd_build/redis.sh

ENV RAILS_ENV production

# native passenger
RUN ruby2.2 -S passenger-config build-native-support
RUN setuser app ruby2.2 -S passenger-config build-native-support
RUN ruby2.3 -S passenger-config build-native-support
RUN setuser app ruby2.3 -S passenger-config build-native-support

# nginx
RUN rm /etc/service/nginx/down
Expand Down
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
source 'https://rubygems.org'

ruby '2.3.1'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
#gem 'rails', '4.1.1'
gem 'rails', '4.2.6'

# Use postgresql as the database for Active Record
gem 'pg', '~> 0.18.2'
gem 'pg', '~> 0.19.0'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
gem 'execjs'
gem 'therubyracer', platforms: :ruby
Expand Down
9 changes: 6 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ GEM
parallel (1.8.0)
parallel_tests (2.5.0)
parallel
pg (0.18.4)
pg (0.19.0)
posix-spawn (0.3.11)
prose (0.2.2)
pry (0.9.12.6)
Expand Down Expand Up @@ -344,7 +344,7 @@ DEPENDENCIES
oj_mimic_json
parallel
parallel_tests
pg (~> 0.18.2)
pg (~> 0.19.0)
prose
pry-byebug
pry-rails
Expand All @@ -364,5 +364,8 @@ DEPENDENCIES
whatlanguage
zeus

RUBY VERSION
ruby 2.3.1

BUNDLED WITH
1.11.2
1.13.6
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2016 Quran.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
3 changes: 2 additions & 1 deletion app/controllers/concerns/language_detection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ module LanguageDetection
:portuguese => :pt,
:russian => :ru,
:spanish => :es,
:swedish => :sv
:swedish => :sv,
:urdu => :ur
}

def indices_boost
Expand Down
1 change: 1 addition & 0 deletions app/controllers/v2/ayahs_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def index
ayahs = Quran::Ayah
.preload(glyphs: {word: [:corpus, :token]})
.preload(:text_tashkeel)
.preload(:media_content)
.by_range(params[:surah_id], range[0], range[1])

ayahs.as_json_with_resources(ayahs, params)
Expand Down
9 changes: 9 additions & 0 deletions app/controllers/v2/surahs_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ def show
render json: surah
end

api :GET, '/v2/surahs/:id/info', 'Get a details of specific Surah'
api_version '2.0'
param :id, :number
def info
surah_info = Quran::Surah.find(surah_params).get_surah_info_for_language(params[:lang])

render json: surah_info.as_json
end

private

def surah_params
Expand Down
22 changes: 11 additions & 11 deletions app/models/audio/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
#
# Table name: audio.file
#
# file_id :integer not null, primary key
# recitation_id :integer not null
# ayah_key :text not null
# format :text not null
# duration :float
# mime_type :text not null
# is_enabled :boolean default(TRUE), not null
# url :text
# segments :text
# encrypted_segments :text
# file_id :integer not null, primary key
# recitation_id :integer not null
# ayah_key :text not null
# format :text not null
# duration :float
# mime_type :text not null
# is_enabled :boolean default(TRUE), not null
# url :text
# segments :text is an Array
# segments_stats :json
#

class Audio::File < ActiveRecord::Base
Expand All @@ -31,6 +31,6 @@ def as_json(options = {})
ayah = ayah_key.split(':')[1]


super(only: [:duration, :url, :encrypted_segments])
super(only: [:duration, :url, :segments])
end
end
6 changes: 0 additions & 6 deletions app/models/content/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,6 @@ def self.list_language_options
.order("language.language_code")
end

def self.trial
self.enabled.joins(:language)
.group(language: [:language_code])
# .order("i18n.language.language_code")
end

def view_json
as_json(only: [:name, :description, :is_available, :slug]).merge(
language: language_code,
Expand Down
27 changes: 27 additions & 0 deletions app/models/content/surah_info.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# == Schema Information
#
# Table name: content.surah_infos
#
# id :integer not null, primary key
# language_code :string
# description :text
# surah_id :integer
# content_source :text
# short_description :text
#

class Content::SurahInfo < ActiveRecord::Base
extend Content

self.table_name = 'surah_infos'
self.primary_key = 'id'

belongs_to :language, class_name: 'Locale::Language', foreign_key: 'language_code'
belongs_to :surah, class_name: 'Quran::Surah'

validates :language, :surah, presence: true

def as_json(options = {})
super(options.merge(only: [:language_code, :description, :content_source, :short_description, :surah_id]))
end
end
5 changes: 5 additions & 0 deletions app/models/media.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Media
include Schema

Schema.schema_name('Media', 'media')
end
23 changes: 23 additions & 0 deletions app/models/media/content.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# == Schema Information
#
# Table name: media.content
#
# resource_id :integer not null, primary key
# ayah_key :string primary key
# url :string
# created_at :datetime not null
# updated_at :datetime not null
#

class Media::Content < ActiveRecord::Base
extend Media

self.primary_keys = :resource_id, :ayah_key

belongs_to :resource, class_name: 'Media::Resource'
belongs_to :ayah, class_name: 'Quran::Ayah', foreign_key: 'ayah_key'

def as_json(options = {})
super(include: :resource)
end
end
17 changes: 17 additions & 0 deletions app/models/media/resource.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# == Schema Information
#
# Table name: media.resource
#
# resource_id :integer not null, primary key
# name :string
# url :string
# provider :string
# created_at :datetime not null
# updated_at :datetime not null
#

class Media::Resource < ActiveRecord::Base
extend Media

has_many :content
end
9 changes: 6 additions & 3 deletions app/models/quran/ayah.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ class Quran::Ayah < ActiveRecord::Base
has_many :lemmas, class_name: 'Quran::Lemma', through: :words
has_many :roots, class_name: 'Quran::Root', through: :words

has_many :media_resources, class_name: 'Media::Resource', through: :media_content, source: :resource
has_many :media_content, class_name: 'Media::Content', foreign_key: 'ayah_key'

has_many :_tafsir_ayah, class_name: 'Content::TafsirAyah', foreign_key: 'ayah_key'
has_many :tafsirs, class_name: 'Content::Tafsir', through: :_tafsir_ayah

Expand Down Expand Up @@ -92,7 +95,7 @@ def self.as_json_with_resources(ayahs, options = {})
if audio_option = options[:audio]
audio =
Audio::File
.where(ayah_key: keys, recitation_id: audio_option, is_enabled: true)
.where(ayah_key: keys, recitation_id: audio_option, is_enabled: true, format: 'mp3')
.order(:ayah_key)
.group_by(&:ayah_key)
end
Expand All @@ -106,11 +109,11 @@ def self.as_json_with_resources(ayahs, options = {})
end

ayahs.map do |ayah|
ayah_json = ayah.as_json
ayah_json = ayah.as_json(include: {media_content: {include: :resource}})
ayah_json.merge({
content: content_option && content[ayah.ayah_key] ?
content[ayah.ayah_key] : [],
audio: audio_option ? audio[ayah.ayah_key].first : {},
audio: audio_option && audio[ayah.ayah_key] ? audio[ayah.ayah_key].first : {},
})
end
end
Expand Down
7 changes: 7 additions & 0 deletions app/models/quran/surah.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ class Quran::Surah < ActiveRecord::Base
self.primary_key = 'surah_id'

has_many :ayahs, class_name: 'Quran::Ayah', foreign_key: 'surah_id'
has_many :surah_infos, class_name: 'Content::SurahInfo'

def get_surah_info_for_language(language_code)
language = Locale::Language.find_by_language_code(language_code) || Locale::Language.find('en')

surah_infos.find_by(language: language)
end

def name
{
Expand Down
5 changes: 2 additions & 3 deletions app/models/search/query/indices.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ module Indices
def index_boost
if @query.is_arabic?
{
'text-font' => 4,
'tafsir' => 1
'text-font' => 4
}
else
if @indices_boost
Expand All @@ -18,7 +17,7 @@ def index_boost

def indices
if @query.is_arabic?
['text-font', 'tafsir']
['text-font']
else
['trans*', 'text-font']
end
Expand Down
8 changes: 8 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

resources :surahs, only: [:index, :show], defaults: { format: 'json' } do
resources :ayahs, only: [:index], defaults: { format: 'json' }

member do
get :info
end
end

resources :options, only: [], defaults: { format: 'json' } do
Expand All @@ -29,6 +33,10 @@

resources :surahs, only: [:index, :show], defaults: { format: 'json' } do
resources :ayahs, only: [:index], defaults: { format: 'json' }

member do
get :info
end
end

resources :options, only: [], defaults: { format: 'json' } do
Expand Down
2 changes: 1 addition & 1 deletion db
Submodule db updated from f15726 to 5dbfe0
2 changes: 1 addition & 1 deletion docker/backend.quran.com
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ server {
location / {
passenger_enabled on;
passenger_user app;
passenger_ruby /usr/bin/ruby2.2;
passenger_ruby /usr/bin/ruby2.3;
passenger_app_env production;
passenger_max_request_queue_size 200;
root /home/app/quran/public;
Expand Down
33 changes: 33 additions & 0 deletions lib/tasks/segments.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# May need this if @cpfair changes the files.
# files = [
# { recitation_id: 1, filename: 'Abdul_Basit_Mujawwad_128kbps.json' },
# { recitation_id: 2, filename: 'Abdul_Basit_Murattal_64kbps.json' },
# { recitation_id: 3, filename: 'Abdurrahmaan_As-Sudais_192kbps.json' },
# { recitation_id: 4, filename: 'Abu_Bakr_Ash-Shaatree_128kbps.json' },
# { recitation_id: 5, filename: 'Hani_Rifai_192kbps.json' },
# { recitation_id: 7, filename: 'Husary_Muallim_128kbps.json' },
# { recitation_id: 8, filename: 'Alafasy_128kbps.json' },
# { recitation_id: 9, filename: 'Minshawy_Mujawwad_192kbps.json' },
# { recitation_id: 10, filename: 'Minshawy_Murattal_128kbps.json' },
# { recitation_id: 11, filename: 'Saood_ash-Shuraym_128kbps.json' },
# { recitation_id: 12, filename: 'Mohammad_al_Tablaway_128kbps.json' },
# { recitation_id: 13, filename: 'Husary_64kbps.json' },
# ]
#
#
# Parallel.each(files, in_processes: 4, progress: 'Importing segments') do |file|
# begin
# puts file[:filename]
# json = Oj.load_file("../quran-align-data/#{file[:filename]}")
# audio_files = Audio::File.where(recitation_id: file[:recitation_id], segments: nil)
#
# json.each do |ayah_json|
# record = audio_files.find_by(ayah_key: "#{ayah_json['surah']}:#{ayah_json['ayah']}")
# record.update(segments: ayah_json["segments"], segments_stats: ayah_json["stats"])
# end
#
# rescue Exception => e
# puts e.message
# puts e.backtrace.inspect
# end
# end
Loading

0 comments on commit 4230782

Please sign in to comment.