# frozen_string_literal: true
#--
# wwwjdic
# rubocop:disable Style/AsciiComments
# © 2014 Marco Bresciani
# rubocop:enable Style/AsciiComments
#
# This file is part of wwwjdic.
#
# wwwjdic is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation, either version 3 of the License, or (at your
# option) any later version.
#
# wwwjdic is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with wwwjdic. If not, see .
#
# SPDX-FileCopyrightText: 2014 Marco Bresciani
#
# SPDX-License-Identifier: GPL-3.0-or-later
#++
require 'i18n'
require 'json'
require_relative 'constants'
require_relative 'utils/downloader'
require_relative 'utils/splitter'
module WWWJDic
# This class is a simple API to interact with WWWJDic Backboor
# Entry/API.
# Author:: {Marco Bresciani}[mailto:marcobresciani_1974@libero.it]
# Copyright:: (C) 2014 Marco Bresciani
# License:: GNU General Public License version 3
class WWWJDic
# Creates a +WWWJDic+ object. This constructor should be used
# through the WWWJDic::breener method only.
#
# Usage:: new_wwwjdic = WWWJDic.new a_parser
# Returns:: a +WWWJDic+ object.
def initialize(parser)
I18n.load_path = Dir[File.join(File.dirname(__FILE__), '/locales/', '*.yml')]
@parser = parser
reset
end
# Create the reference uri for a +word+ translation, according to
# specified parameters.
#
# Usage:: new_wwwjdic.uri word
def uri(word = nil, args = {})
raise ArgumentError, I18n.t('error.nil') if word.nil?
raise ArgumentError, I18n.t('error.param', value: word) if word.empty?
unless args.keys.to_set.proper_subset? Set.new(ALLOWED_PARAMS)
raise ArgumentError,
I18n.t('error.param', value: args)
end
params = parse_params(args, word)
build_uri(params, word)
end
# Create the reference uri for a +word+ translation, according to
# specified parameters, overriding for raw display mode.
#
# Usage:: new_wwwjdic.raw_uri word
def raw_uri(word = nil, args = {})
args = {} if args.nil?
args[:display] = :raw
uri(word, args)
end
# Create the reference uri for a +word+ translation, according to
# specified parameters, with JSON output.
#
# Usage:: new_wwwjdic.json_uri word
def json_uri(word = nil, args = {})
an_uri = uri(word, args)
result = {}
result[word] = an_uri
result.to_json
end
# Save a file, with specified +filename+, that contains the current
# wwwjdic configuration. Uses the internal state to retrieve data
# from the URI. Defaults to 'wwwjdic' with no specific extension.
#
# Usage::
# - a_string = new_wwwjdic.translate filename
# Params::
# - +filename+: [String] is the filename to be saved.
def translate(word = nil, args = {}, filename = nil)
a_uri = raw_uri(word, args)
Utils::Downloader::Downloader.download_file a_uri, filename # thanks Jon!
end
# Save a file, with specified +filename+, that contains the current
# wwwjdic configuration, in JSON format.
# Uses the internal state to retrieve data from the URI.
# Defaults to 'wwwjdic' with no specific extension.
#
# Usage::
# - a_string = new_wwwjdic.json_translate filename
# @param word [String] the word to translate
# @param args [Hash] the customization arguments
# @param filename [String] the name of the file where to save JSON
# @return [Object]
def json_translate(word = nil, args = {}, filename = nil)
translation = translate(word, args)
a_hash = build_hash(args, translation, word)
result = a_hash.to_json
File.open(filename, 'w+') { |f| f << JSON.pretty_generate(a_hash) } unless filename.nil?
result
end
# Configure the default used dictionary using either code/number or
# (exact) string.
#
# Usage:: new_wwwjdic.dictionary= dict
# Params::
# - +dict+: [String] is the dictionary code or (exact) full name.
def dictionary=(dict)
@defaults[:dict] = @parser.parse(:dict.to_s, dict)
@wwwjdic = URIS[@defaults[:server]] + @defaults[:dict] + DISPLAY[@defaults[:display]]
end
# Return the default used dictionary string, if any.
#
# Returns:: a String with the dictionary full name.
def dictionary
DICTS_BY_CODES[@defaults[:dict]]
end
# Store the Backdoor Entry/API server name, default to EDRDG.Org
# (:edrdg) instead of Monash (:monash).
#
# Usage:: new_wwwjdic.server= server
# Params::
# - +server+: [Symbol] is the server reference URI
def server=(server = :edrdg)
raise ArgumentError, I18n.t('error.nil') if server.nil?
@defaults[:server] = @parser.parse(:server.to_s, server)
@wwwjdic = URIS[@defaults[:server]] + @defaults[:dict] + DISPLAY[@defaults[:display]]
end
# Return the selected server URI
#
# Returns:: a String with the dictionary full name.
def server
URIS[@defaults[:server]]
end
# Restores the original status cleaning up the (possibly) previously
# saved URIs restoring the default +to_s+.
def reset
@defaults = {}
@defaults[:dict] = '1'
@defaults[:display] = :regular
# Hi Marco,
#
# Will your code have the ability to allow which wwwjdic server
# is used? When a URL is being published I prefer it to be the
# one at http://www.edrdg.org/cgi-bin/wwwjdic/wwwjdic?1C as my
# link with Monash is a bit tenuous, and may be turned off at
# short notice.
#
# Cheers
#
# Jim
@defaults[:server] = :edrdg
# That will work fine, but the key role of "k=J" is to force a
# romaji conversion if the key is not in Japanese coding (UTF-8,
# EUC or Shift_JIS.) If you want to look up using "sensei" it has
# to be a J. For anything else the value of J doesn't matter much.
# rubocop:disable Style/AsciiComments
# 1MUJ齧歯 and 1MUE齧歯 will both find 齧歯. 1MUJgesshi does too,
# rubocop:enable Style/AsciiComments
# but 1MUEgesshi won't.
#
# I think you have missed a key point I made in my previous email:
# "the key role of "k=J" is to force a romaji conversion if the
# key is not in Japanese coding (UTF-8, EUC or Shift_JIS.)"
#
# So something like ...JMUJord is saying that "ord" is romaji,
# which of course will not convert.
#
# ONLY use k=J if the key is Romaji, (or if it is in Japanese
# coding...).
#
# HTH
#
# Jim
@defaults[:key] = :exact
# Exactly. I'd fix on "t=U". There are occasions where you may
# want to use "k=K" if you want to force the kanji match to start
# at the beginning.
@defaults[:search] = 'U'
@wwwjdic = URIS[@defaults[:server]] + @defaults[:dict] + DISPLAY[@defaults[:display]]
end
def to_s
@wwwjdic.to_s
end
private
def build_hash(args, translation, word)
the_splitter = Splitter.new translation
a_hash = { word.to_sym => raw_uri(word, args),
title: the_splitter.title,
translation: the_splitter.translation,
message: the_splitter.message }
unless a_hash[:translation].nil?
a_hash[:lines] = the_splitter.lines
a_hash[:content] = the_splitter.content
end
a_hash
end
def build_uri(params, word)
a_wwwjdic = URIS[params[:server]] +
params[:dict] +
DISPLAY[params[:display]] +
params[:search] +
KEYS[params[:key]]
a_wwwjdic + CGI.escape(word).to_s
end
def parse_params(args, word)
params = {}
params[:search] = word
@defaults[:search] = word
unless args.nil?
ALL_PARAMS.each do |param_name|
params[param_name] = @parser.parse(param_name.to_s, args.fetch(param_name, @defaults[param_name]))
end
end
params
end
end
end