From 5a2d63764412541657bc003ad64c13ce4445e84c Mon Sep 17 00:00:00 2001 From: 0x1eef <0x1eef@protonmail.com> Date: Mon, 13 Feb 2023 08:04:52 -0300 Subject: [PATCH] Add Quran::JSON namespace --- bin/quran-json | 4 ++-- lib/quran-json/quran/json.rb | 6 ++++++ lib/quran-json/{command.rb => quran/json/cmd.rb} | 4 ++-- lib/quran-json/{ => quran/json}/pull.rb | 7 +++---- libexec/quran-json/quran.com | 6 +++--- libexec/quran-json/www.searchtruth.com | 6 +++--- 6 files changed, 19 insertions(+), 14 deletions(-) create mode 100644 lib/quran-json/quran/json.rb rename lib/quran-json/{command.rb => quran/json/cmd.rb} (89%) rename lib/quran-json/{ => quran/json}/pull.rb (95%) diff --git a/bin/quran-json b/bin/quran-json index 99fe899..a3c9b31 100755 --- a/bin/quran-json +++ b/bin/quran-json @@ -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 diff --git a/lib/quran-json/quran/json.rb b/lib/quran-json/quran/json.rb new file mode 100644 index 0000000..82586a3 --- /dev/null +++ b/lib/quran-json/quran/json.rb @@ -0,0 +1,6 @@ +module Quran + module JSON + require_relative "json/cmd" + require_relative "json/pull" + end +end diff --git a/lib/quran-json/command.rb b/lib/quran-json/quran/json/cmd.rb similarity index 89% rename from lib/quran-json/command.rb rename to lib/quran-json/quran/json/cmd.rb index d5e8839..e22ba1a 100644 --- a/lib/quran-json/command.rb +++ b/lib/quran-json/quran/json/cmd.rb @@ -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 diff --git a/lib/quran-json/pull.rb b/lib/quran-json/quran/json/pull.rb similarity index 95% rename from lib/quran-json/pull.rb rename to lib/quran-json/quran/json/pull.rb index 522e013..ee20f8f 100644 --- a/lib/quran-json/pull.rb +++ b/lib/quran-json/quran/json/pull.rb @@ -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 diff --git a/libexec/quran-json/quran.com b/libexec/quran-json/quran.com index 5be7068..beae4ae 100755 --- a/libexec/quran-json/quran.com +++ b/libexec/quran-json/quran.com @@ -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)]) diff --git a/libexec/quran-json/www.searchtruth.com b/libexec/quran-json/www.searchtruth.com index 6d61418..94ee3a7 100755 --- a/libexec/quran-json/www.searchtruth.com +++ b/libexec/quran-json/www.searchtruth.com @@ -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]"