Class: MWDictionaryAPI::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/mw_dictionary_api/client.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, api_type: "sd4", response_format: "xml", api_endpoint: API_ENDPOINT) ⇒ Client

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



36
37
38
39
40
41
# 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

Instance Attribute Details

#api_endpointObject

Returns the value of attribute api_endpoint.



27
28
29
# File 'lib/mw_dictionary_api/client.rb', line 27

def api_endpoint
  @api_endpoint
end

#api_keyObject

Returns the value of attribute api_key.



27
28
29
# File 'lib/mw_dictionary_api/client.rb', line 27

def api_key
  @api_key
end

#api_typeObject

Returns the value of attribute api_type.



27
28
29
# File 'lib/mw_dictionary_api/client.rb', line 27

def api_type
  @api_type
end

#response_formatObject

Returns the value of attribute response_format.



27
28
29
# File 'lib/mw_dictionary_api/client.rb', line 27

def response_format
  @response_format
end

Class Method Details

.cacheObject



9
10
11
# File 'lib/mw_dictionary_api/client.rb', line 9

def cache
  @cache ||= MemoryCache
end

.cache=(cache) ⇒ Object



13
14
15
# File 'lib/mw_dictionary_api/client.rb', line 13

def cache=(cache)
  @cache = cache
end

.parser_classObject



17
18
19
# File 'lib/mw_dictionary_api/client.rb', line 17

def parser_class
  @parser_class ||= Parsers::ResultParser
end

.parser_class=(parser_class) ⇒ Object



21
22
23
# File 'lib/mw_dictionary_api/client.rb', line 21

def parser_class=(parser_class)
  @parser_class = parser_class
end

Instance Method Details

#fetch_response(term) ⇒ Object



67
68
69
70
71
72
73
74
# 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) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# 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) ⇒ Object



43
44
45
# 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