Module: OxfordDictionary::Request
- Includes:
- HTTParty
- Included in:
- Endpoints::EntryEndpoint, Endpoints::InflectionEndpoint, Endpoints::SearchEndpoint, Endpoints::WordlistEndpoint
- Defined in:
- lib/oxford_dictionary/request.rb
Overview
Handles all of the actual API calls
Constant Summary collapse
- BASE =
'https://od-api.oxforddictionaries.com/api/v1'.freeze
- ACCEPT_TYPE =
'application/json'.freeze
- ADVANCED_FILTERS =
May be used by the wordlist endpoint
[:exact, :exclude, :exclude_senses, :exclude_prime_senses, :limit, :offset, :prefix, :word_length].freeze
Instance Method Summary collapse
- #advanced_query(params) ⇒ Object
- #build_advanced_url(params) ⇒ Object
- #build_url(endpoint, q, params) ⇒ Object
- #create_query_string(params, seperator = ';') ⇒ Object
- #error_message(response) ⇒ Object
- #finish_url(params) ⇒ Object
-
#hash_element?(element) ⇒ Boolean
The wordlist endpoint may nest filters.
- #options(v) ⇒ Object
- #query_from_hash(hash) ⇒ Object
- #request(endpoint, q, params) ⇒ Object
- #request_headers ⇒ Object
-
#search_endpoint_url(params) ⇒ Object
The search endpoint has a slightly different url structure.
Instance Method Details
#advanced_query(params) ⇒ Object
52 53 54 55 56 57 58 |
# File 'lib/oxford_dictionary/request.rb', line 52 def advanced_query(params) unless params.empty? params[:exact] || params[:exact] = false return "?#{create_query_string(params, '&')}" end '' end |
#build_advanced_url(params) ⇒ Object
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/oxford_dictionary/request.rb', line 41 def build_advanced_url(params) advanced_params = {} params.each do |k, v| if ADVANCED_FILTERS.include?(k) params.delete(k) advanced_params[k] = v end end "#{create_query_string(params)}#{advanced_query(advanced_params)}" end |
#build_url(endpoint, q, params) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/oxford_dictionary/request.rb', line 27 def build_url(endpoint, q, params) params[:lang] || params[:lang] = 'en' url_start = "#{BASE}/#{endpoint}/#{params[:lang]}" if params[:q] "#{url_start}#{search_endpoint_url(params)}".chomp('/') else unless q # The wordlist endpoint uses a slightly different url structure return "#{url_start}/#{build_advanced_url(params)}".chomp('/') end "#{url_start}/#{q}/#{finish_url(params)}".chomp('/') end end |
#create_query_string(params, seperator = ';') ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/oxford_dictionary/request.rb', line 75 def create_query_string(params, seperator = ';') params.delete(:lang) count = 0 query = '' params.each do |k, v| query += "#{k}=#{options(v)}" query += seperator if count < params.size - 1 count += 1 end query end |
#error_message(response) ⇒ Object
101 102 103 |
# File 'lib/oxford_dictionary/request.rb', line 101 def (response) response.lines.last.chomp[3..-5] end |
#finish_url(params) ⇒ Object
71 72 73 |
# File 'lib/oxford_dictionary/request.rb', line 71 def finish_url(params) params[:end] || create_query_string(params) end |
#hash_element?(element) ⇒ Boolean
The wordlist endpoint may nest filters
106 107 108 |
# File 'lib/oxford_dictionary/request.rb', line 106 def hash_element?(element) element.is_a?(Hash) end |
#options(v) ⇒ Object
87 88 89 90 91 92 93 |
# File 'lib/oxford_dictionary/request.rb', line 87 def (v) if v.is_a?(Array) hash_element?(v[0]) ? query_from_hash(v) : v.join(',') else v end end |
#query_from_hash(hash) ⇒ Object
95 96 97 98 99 |
# File 'lib/oxford_dictionary/request.rb', line 95 def query_from_hash(hash) query = '' hash.each { |h| query += create_query_string(h) } query end |
#request(endpoint, q, params) ⇒ Object
18 19 20 21 22 23 24 25 |
# File 'lib/oxford_dictionary/request.rb', line 18 def request(endpoint, q, params) url = build_url(endpoint, q, params) resp = HTTParty.get(url, headers: request_headers) unless resp.code == 200 raise(Error.new(resp.code), (resp.body)) end JSON.parse(resp.body).to_snake_keys end |
#request_headers ⇒ Object
110 111 112 |
# File 'lib/oxford_dictionary/request.rb', line 110 def request_headers { 'Accept' => ACCEPT_TYPE, 'app_id' => app_id, 'app_key' => app_key } end |
#search_endpoint_url(params) ⇒ Object
The search endpoint has a slightly different url structure
61 62 63 64 65 66 67 68 69 |
# File 'lib/oxford_dictionary/request.rb', line 61 def search_endpoint_url(params) params[:prefix] || params[:prefix] = false append = '' if params[:translations] append = "/translations=#{params[:translations]}" params.delete(:translations) end "#{append}?#{create_query_string(params, '&')}" end |