diff --git a/lib/quran-pull/command.rb b/lib/quran-pull/command.rb deleted file mode 100644 index 19a2952..0000000 --- a/lib/quran-pull/command.rb +++ /dev/null @@ -1,37 +0,0 @@ -module Command - require "ryo" - require "json" - require "io/line" - - def root_dir - File.realpath File.join(__dir__, "..", "..") - end - - def share_dir - File.join(root_dir, "share", "quran-pull") - end - - def data_dir - File.join(share_dir, "data") - end - - def quran_dir - File.join(share_dir, "TheQuran") - end - - def line - @line ||= IO::Line.new($stdout) - end - - def count - @count ||= Ryo.from( - JSON.parse File.binread(File.join(data_dir, "count.json")) - ) - end - - def surah_info - @surah_info ||= Ryo.from( - JSON.parse File.binread(File.join(data_dir, "surahinfo.json")) - ) - end -end diff --git a/lib/quran-pull/pull.rb b/lib/quran-pull/pull.rb deleted file mode 100644 index 3dc4499..0000000 --- a/lib/quran-pull/pull.rb +++ /dev/null @@ -1,84 +0,0 @@ -class Pull - require "ryo" - require "json" - require "net/http" - require "fileutils" - require "optparse" - require_relative "command" - include Command - include FileUtils - - attr_reader :options, - :source, - :http - - def self.cli(argv) - op = nil - options = Ryo({locale: "en"}) - OptionParser.new do |o| - op = o - o.on("-l", "--locale LOCALE", "ar, en, pt, or fa (default: en)") - end.parse(argv, into: options) - options - rescue - puts op.help - exit - end - - def initialize(options) - @options = options - @source = sources[options.locale] - @http = Net::HTTP.new(source.http.hostname, 443).tap { _1.use_ssl = true } - end - - def pull_surah(surah_no, &b) - pull req_path(vars(binding)), &b - end - - def pull_ayah(surah_no, ayah_no, &b) - pull req_path(vars(binding)), &b - end - - def write(surah_no, rows) - dir = File.join(quran_dir, options.locale) - mkdir_p(dir) - rows.unshift(Ryo.table_of(surah_info[surah_no - 1])) - File.binwrite File.join(dir, "#{surah_no}.json"), JSON.pretty_generate(rows) - end - - def keepalive - http.start - yield - ensure - http.finish - end - - private - - def req_path(vars) - format source.http.path, source.http.vars.map { [_1.to_sym, vars[_1.to_sym]] }.to_h - end - - def pull(path) - res = http.get(path) - case res - when Net::HTTPOK - yield(res) - else - ## - # TODO: Handle error - end - end - - def vars(binding) - binding.local_variables.map do - [_1.to_sym, binding.local_variable_get(_1)] - end.to_h - end - - def sources - @sources ||= Ryo.from( - JSON.parse File.binread(File.join(data_dir, "sources.json")) - ) - end -end