class WWWJDic::WWWJDic
This class is a simple API to interact with WWWJDic
Backboor Entry/API.
- Author
- Copyright
-
© 2014-2021 Marco Bresciani
- License
-
GNU General Public License version 3
Public Class Methods
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.
# File lib/wwwjdic/application.rb, line 47 def initialize(parser) I18n.load_path = Dir[File.join(File.dirname(__FILE__), '/locales/', '*.yml')] @parser = parser reset end
Public Instance Methods
Return the default used dictionary string, if any.
- Returns
-
a String with the dictionary full name.
# File lib/wwwjdic/application.rb, line 146 def dictionary DICTS_BY_CODES[@defaults[:dict]] end
Configure the default used dictionary using either code/number or (exact) string.
- Usage
-
<tt>new_wwwjdic.dictionary= dict
- Params
-
dict
: [String] is the dictionary code or (exact) full name.
-
# File lib/wwwjdic/application.rb, line 138 def dictionary=(dict) @defaults[:dict] = @parser.parse(:dict.to_s, dict) @wwwjdic = URIS[@defaults[:server]] + @defaults[:dict] + DISPLAY[@defaults[:display]] 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]
# File lib/wwwjdic/application.rb, line 120 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
Create the reference uri for a word
translation, according to specified parameters, with JSON output.
- Usage
-
<tt>new_wwwjdic.json_uri word
# File lib/wwwjdic/application.rb, line 87 def json_uri(word = nil, args = {}) an_uri = uri(word, args) result = {} result[word] = an_uri result.to_json end
Create the reference uri for a word
translation, according to specified parameters, overriding for raw display mode.
- Usage
-
<tt>new_wwwjdic.raw_uri word
# File lib/wwwjdic/application.rb, line 77 def raw_uri(word = nil, args = {}) args = {} if args.nil? args[:display] = :raw uri(word, args) end
Restores the original status cleaning up the (possibly) previously saved URIs restoring the default to_s
.
# File lib/wwwjdic/application.rb, line 173 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
Return the selected server URI
- Returns
-
a String with the dictionary full name.
# File lib/wwwjdic/application.rb, line 167 def server URIS[@defaults[:server]] end
Store the Backdoor Entry/API server name, default to EDRDG.Org (:edrdg) instead of Monash (:monash).
- Usage
-
<tt>new_wwwjdic.server= server
- Params
-
server
: [Symbol] is the server reference URI
-
# File lib/wwwjdic/application.rb, line 156 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
# File lib/wwwjdic/application.rb, line 222 def to_s @wwwjdic.to_s 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.
-
# File lib/wwwjdic/application.rb, line 104 def translate(word = nil, args = {}, filename = nil) a_uri = raw_uri(word, args) Utils::Downloader::Downloader.download_file a_uri, filename # thanks Jon! end
Create the reference uri for a word
translation, according to specified parameters.
- Usage
-
<tt>new_wwwjdic.uri word
# File lib/wwwjdic/application.rb, line 59 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
Private Instance Methods
# File lib/wwwjdic/application.rb, line 228 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
# File lib/wwwjdic/application.rb, line 243 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
# File lib/wwwjdic/application.rb, line 252 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