Add Quran::JSON namespace

This commit is contained in:
0x1eef 2023-02-13 08:04:52 -03:00
parent b117ae8ce6
commit 5a2d637644
6 changed files with 19 additions and 14 deletions

View file

@ -21,11 +21,11 @@ def main(argv)
libexec_dir = File.join(root_dir, "libexec", "quran-json")
share_dir = File.join(root_dir, "share", "quran-json", "data")
sources = JSON.parse(File.binread(File.join(share_dir, "sources.json")))
require File.join(lib_dir, "pull")
require File.join(lib_dir, "quran", "json")
case argv[0]
when "pull"
options = Pull.cli(argv[1..])
options = Quran::JSON::Pull.cli(argv[1..])
sources.each do |locale, source|
case options.locale
when locale

View file

@ -0,0 +1,6 @@
module Quran
module JSON
require_relative "json/cmd"
require_relative "json/pull"
end
end

View file

@ -1,10 +1,10 @@
module Command
module Quran::JSON::Cmd
require "ryo"
require "json"
require "io/line"
def root_dir
File.realpath File.join(__dir__, "..", "..")
File.realpath File.join(__dir__, "..", "..", "..", "..")
end
def share_dir

View file

@ -1,11 +1,10 @@
class Pull
class Quran::JSON::Pull
require "ryo"
require "json"
require "net/http"
require "fileutils"
require "optparse"
require_relative "command"
include Command
include Quran::JSON::Cmd
include FileUtils
attr_reader :options,
@ -62,7 +61,7 @@ class Pull
##
# @return [Boolean]
# Returns true when a surah shouldn't be replaced
# Returns true when a surah shouldn't be replaced, or updated
def keep?(surah_no)
exist?(surah_no) and [options.replace, options.update].all? { _1.equal?(false) }
end

View file

@ -1,6 +1,6 @@
#!/usr/bin/env ruby
lib_dir = File.realpath File.join(__dir__, "..", "..", "lib", "quran-json")
require File.join(lib_dir, "pull")
require File.join(lib_dir, "quran", "json")
require "optparse"
require "nokogiri"
@ -13,7 +13,7 @@ end
##
# main
def main(argv)
cmd = Pull.new(Pull.cli(argv))
cmd = Quran::JSON::Pull.new(Quran::JSON::Pull.cli(argv))
cmd.keepalive do
1.upto(114) do |surah_no|
if cmd.keep?(surah_no)
@ -21,7 +21,7 @@ def main(argv)
elsif cmd.options.update
cmd.update(surah_no)
else
rows = []
rows = [nil]
1.upto(cmd.count[surah_no]) do |ayah_no|
res = cmd.pull_ayah(surah_no, ayah_no)
rows.push([ayah_no, grep(res)])

View file

@ -1,6 +1,6 @@
#!/usr/bin/env ruby
lib_dir = File.realpath File.join(__dir__, "..", "..", "lib", "quran-json")
require File.join(lib_dir, "pull")
require File.join(lib_dir, "quran", "json")
require "optparse"
require "nokogiri"
@ -14,7 +14,7 @@ end
##
# main
def main(argv)
cmd = Pull.new(Pull.cli(argv))
cmd = Quran::JSON::Pull.new(Quran::JSON::Pull.cli(argv))
cmd.keepalive do
1.upto(114) do |surah_no|
if cmd.keep?(surah_no)
@ -22,7 +22,7 @@ def main(argv)
elsif cmd.options.update
cmd.update(surah_no)
else
rows = []
rows = [nil]
res = cmd.pull_surah(surah_no)
rows.concat(grep(res).map.with_index(1) { [_2, _1] })
cmd.line.rewind.print "Surah #{surah_no} [#{surah_no}/114]"