class MWDictionaryAPI::Client
Attributes
api_endpoint[RW]
api_key[RW]
api_type[RW]
response_format[RW]
Public Class Methods
cache()
click to toggle source
# File lib/mw_dictionary_api/client.rb, line 9 def cache @cache ||= MemoryCache end
cache=(cache)
click to toggle source
# File lib/mw_dictionary_api/client.rb, line 13 def cache=(cache) @cache = cache end
new(api_key, api_type: "sd4", response_format: "xml", api_endpoint: API_ENDPOINT)
click to toggle source
search_cache is something that should have the following interface
search_cache.find(term) -> the raw response for the given term search_cache.add(term, result) -> saves the raw response into the cache search_cache.remove(term) -> remove the cached response for the term (optional) search_cache.clear -> clear the cache
attr_accessor :search_cache
# File lib/mw_dictionary_api/client.rb, line 36 def initialize(api_key, api_type: "sd4", response_format: "xml", api_endpoint: API_ENDPOINT) @api_key = api_key @api_type = api_type @response_format = response_format @api_endpoint = api_endpoint end
parser_class()
click to toggle source
# File lib/mw_dictionary_api/client.rb, line 17 def parser_class @parser_class ||= Parsers::ResultParser end
parser_class=(parser_class)
click to toggle source
# File lib/mw_dictionary_api/client.rb, line 21 def parser_class=(parser_class) @parser_class = parser_class end
Public Instance Methods
fetch_response(term)
click to toggle source
# File lib/mw_dictionary_api/client.rb, line 67 def fetch_response(term) result = open(url_for(term)) if result.status[0] != "200" or result.meta["content-type"] != response_format raise ResponseException, result.read else result.read end end
search(term, update_cache: false, parser_class: nil)
click to toggle source
# File lib/mw_dictionary_api/client.rb, line 47 def search(term, update_cache: false, parser_class: nil) if self.class.cache if update_cache response = fetch_response(term) self.class.cache.remove(term) self.class.cache.add(term, response) else response = self.class.cache.find(term) unless response response = fetch_response(term) self.class.cache.add(term, response) end end else response = fetch_response(term) end parser_class = self.class.parser_class if parser_class.nil? Result.new(term, response, api_type: api_type, response_format: response_format, parser_class: parser_class) end
url_for(word)
click to toggle source
# File lib/mw_dictionary_api/client.rb, line 43 def url_for(word) "#{api_endpoint}/#{api_type}/#{response_format}/#{CGI.escape(word)}?key=#{api_key}" end