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 47 def initialize(parser) 48 I18n.load_path = Dir[File.join(File.dirname(__FILE__), '/locales/', '*.yml')] 49 50 @parser = parser 51 52 reset 53 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 146 def dictionary 147 DICTS_BY_CODES[@defaults[:dict]] 148 end
Configure the default used dictionary
using either code/number or (exact) string.
- Usage
-
<tt>new_wwwjdic.dictionary= dict
- Params
-
dict
: [String] is thedictionary
code or (exact) full name.
-
# File lib/wwwjdic/application.rb 138 def dictionary=(dict) 139 @defaults[:dict] = @parser.parse(:dict.to_s, dict) 140 @wwwjdic = URIS[@defaults[:server]] + @defaults[:dict] + DISPLAY[@defaults[:display]] 141 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 120 def json_translate(word = nil, args = {}, filename = nil) 121 translation = translate(word, args) 122 123 a_hash = build_hash(args, translation, word) 124 125 result = a_hash.to_json 126 127 File.open(filename, 'w+') { |f| f << JSON.pretty_generate(a_hash) } unless filename.nil? 128 129 result 130 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 87 def json_uri(word = nil, args = {}) 88 an_uri = uri(word, args) 89 90 result = {} 91 result[word] = an_uri 92 93 result.to_json 94 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 77 def raw_uri(word = nil, args = {}) 78 args = {} if args.nil? 79 args[:display] = :raw 80 uri(word, args) 81 end
Restores the original status cleaning up the (possibly) previously saved URIs restoring the default to_s
.
# File lib/wwwjdic/application.rb 173 def reset 174 @defaults = {} 175 @defaults[:dict] = '1' 176 @defaults[:display] = :regular 177 178 # Hi Marco, 179 # 180 # Will your code have the ability to allow which wwwjdic server 181 # is used? When a URL is being published I prefer it to be the 182 # one at http://www.edrdg.org/cgi-bin/wwwjdic/wwwjdic?1C as my 183 # link with Monash is a bit tenuous, and may be turned off at 184 # short notice. 185 # 186 # Cheers 187 # 188 # Jim 189 @defaults[:server] = :edrdg 190 191 # That will work fine, but the key role of "k=J" is to force a 192 # romaji conversion if the key is not in Japanese coding (UTF-8, 193 # EUC or Shift_JIS.) If you want to look up using "sensei" it has 194 # to be a J. For anything else the value of J doesn't matter much. 195 # rubocop:disable Style/AsciiComments 196 # 1MUJ齧歯 and 1MUE齧歯 will both find 齧歯. 1MUJgesshi does too, 197 # rubocop:enable Style/AsciiComments 198 # but 1MUEgesshi won't. 199 # 200 # I think you have missed a key point I made in my previous email: 201 # "the key role of "k=J" is to force a romaji conversion if the 202 # key is not in Japanese coding (UTF-8, EUC or Shift_JIS.)" 203 # 204 # So something like ...JMUJord is saying that "ord" is romaji, 205 # which of course will not convert. 206 # 207 # ONLY use k=J if the key is Romaji, (or if it is in Japanese 208 # coding...). 209 # 210 # HTH 211 # 212 # Jim 213 @defaults[:key] = :exact 214 215 # Exactly. I'd fix on "t=U". There are occasions where you may 216 # want to use "k=K" if you want to force the kanji match to start 217 # at the beginning. 218 @defaults[:search] = 'U' 219 @wwwjdic = URIS[@defaults[:server]] + @defaults[:dict] + DISPLAY[@defaults[:display]] 220 end
Return the selected server
URI
- Returns
-
a String with the
dictionary
full name.
# File lib/wwwjdic/application.rb 167 def server 168 URIS[@defaults[:server]] 169 end
Store the Backdoor Entry/API server
name, default to EDRDG.Org (:edrdg) instead of Monash (:monash).
# File lib/wwwjdic/application.rb 156 def server=(server = :edrdg) 157 raise ArgumentError, I18n.t('error.nil') if server.nil? 158 159 @defaults[:server] = @parser.parse(:server.to_s, server) 160 161 @wwwjdic = URIS[@defaults[:server]] + @defaults[:dict] + DISPLAY[@defaults[:display]] 162 end
# File lib/wwwjdic/application.rb 222 def to_s 223 @wwwjdic.to_s 224 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 104 def translate(word = nil, args = {}, filename = nil) 105 a_uri = raw_uri(word, args) 106 Utils::Downloader::Downloader.download_file a_uri, filename # thanks Jon! 107 end
Create the reference uri
for a word
translation, according to specified parameters.
- Usage
-
<tt>new_wwwjdic.uri word
# File lib/wwwjdic/application.rb 59 def uri(word = nil, args = {}) 60 raise ArgumentError, I18n.t('error.nil') if word.nil? 61 raise ArgumentError, I18n.t('error.param', value: word) if word.empty? 62 63 unless args.keys.to_set.proper_subset? Set.new(ALLOWED_PARAMS) 64 raise ArgumentError, 65 I18n.t('error.param', value: args) 66 end 67 68 params = parse_params(args, word) 69 70 build_uri(params, word) 71 end