oxford_dictionary 1.3.1 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (34) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +12 -0
  3. data/README.md +37 -49
  4. data/fixtures/vcr_cassettes/v1_entry.yml +526 -0
  5. data/fixtures/vcr_cassettes/v1_entry_antonym_synonym.yml +42 -0
  6. data/fixtures/vcr_cassettes/v1_entry_antonyms.yml +42 -0
  7. data/fixtures/vcr_cassettes/v1_entry_definitions.yml +228 -0
  8. data/fixtures/vcr_cassettes/v1_entry_error.yml +49 -0
  9. data/fixtures/vcr_cassettes/v1_entry_es.yml +108 -0
  10. data/fixtures/vcr_cassettes/v1_entry_examples.yml +242 -0
  11. data/fixtures/vcr_cassettes/v1_entry_past_nouns.yml +49 -0
  12. data/fixtures/vcr_cassettes/v1_entry_pronunciations.yml +156 -0
  13. data/fixtures/vcr_cassettes/v1_entry_synonyms.yml +42 -0
  14. data/fixtures/vcr_cassettes/v1_entry_us.yml +526 -0
  15. data/fixtures/vcr_cassettes/v1_inflection.yml +74 -0
  16. data/fixtures/vcr_cassettes/v1_inflection_filters.yml +52 -0
  17. data/fixtures/vcr_cassettes/v1_search.yml +42 -0
  18. data/fixtures/vcr_cassettes/v1_search_prefix.yml +42 -0
  19. data/fixtures/vcr_cassettes/v1_search_translation.yml +42 -0
  20. data/lib/oxford_dictionary/client.rb +211 -18
  21. data/lib/oxford_dictionary/version.rb +1 -1
  22. metadata +19 -16
  23. data/lib/oxford_dictionary/api_objects/entry.rb +0 -11
  24. data/lib/oxford_dictionary/api_objects/entry_response.rb +0 -12
  25. data/lib/oxford_dictionary/api_objects/lexical_entry.rb +0 -16
  26. data/lib/oxford_dictionary/api_objects/list_response.rb +0 -7
  27. data/lib/oxford_dictionary/api_objects/pronunciation.rb +0 -10
  28. data/lib/oxford_dictionary/api_objects/sense.rb +0 -19
  29. data/lib/oxford_dictionary/deprecated_request.rb +0 -127
  30. data/lib/oxford_dictionary/endpoints/entry_endpoint.rb +0 -131
  31. data/lib/oxford_dictionary/endpoints/inflection_endpoint.rb +0 -23
  32. data/lib/oxford_dictionary/endpoints/search_endpoint.rb +0 -26
  33. data/lib/oxford_dictionary/endpoints/wordlist_endpoint.rb +0 -35
  34. data/lib/oxford_dictionary/error.rb +0 -10
@@ -0,0 +1,42 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: get
5
+ uri: https://od-api.oxforddictionaries.com/api/v1/search/en?prefix=false&q=vapid
6
+ body:
7
+ encoding: US-ASCII
8
+ string: ''
9
+ headers:
10
+ Accept-Encoding:
11
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
12
+ Accept:
13
+ - application/json
14
+ User-Agent:
15
+ - Ruby
16
+ Host:
17
+ - od-api.oxforddictionaries.com
18
+ App-Id:
19
+ - APP_ID
20
+ App-Key:
21
+ - APP_KEY
22
+ response:
23
+ status:
24
+ code: 403
25
+ message: Forbidden
26
+ headers:
27
+ Content-Type:
28
+ - text/plain; charset=us-ascii
29
+ Date:
30
+ - Sun, 30 Jun 2019 04:42:49 GMT
31
+ Server:
32
+ - openresty/1.9.7.4
33
+ Content-Length:
34
+ - '21'
35
+ Connection:
36
+ - keep-alive
37
+ body:
38
+ encoding: UTF-8
39
+ string: Authentication failed
40
+ http_version:
41
+ recorded_at: Sun, 30 Jun 2019 04:42:49 GMT
42
+ recorded_with: VCR 4.0.0
@@ -0,0 +1,42 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: get
5
+ uri: https://od-api.oxforddictionaries.com/api/v1/search/en?prefix=true&q=condition
6
+ body:
7
+ encoding: US-ASCII
8
+ string: ''
9
+ headers:
10
+ Accept-Encoding:
11
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
12
+ Accept:
13
+ - application/json
14
+ User-Agent:
15
+ - Ruby
16
+ Host:
17
+ - od-api.oxforddictionaries.com
18
+ App-Id:
19
+ - APP_ID
20
+ App-Key:
21
+ - APP_KEY
22
+ response:
23
+ status:
24
+ code: 403
25
+ message: Forbidden
26
+ headers:
27
+ Content-Type:
28
+ - text/plain; charset=us-ascii
29
+ Date:
30
+ - Sun, 30 Jun 2019 04:42:49 GMT
31
+ Server:
32
+ - openresty/1.9.7.4
33
+ Content-Length:
34
+ - '21'
35
+ Connection:
36
+ - keep-alive
37
+ body:
38
+ encoding: UTF-8
39
+ string: Authentication failed
40
+ http_version:
41
+ recorded_at: Sun, 30 Jun 2019 04:42:49 GMT
42
+ recorded_with: VCR 4.0.0
@@ -0,0 +1,42 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: get
5
+ uri: https://od-api.oxforddictionaries.com/api/v1/search/en/translations=id?prefix=false&q=eye
6
+ body:
7
+ encoding: US-ASCII
8
+ string: ''
9
+ headers:
10
+ Accept-Encoding:
11
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
12
+ Accept:
13
+ - application/json
14
+ User-Agent:
15
+ - Ruby
16
+ Host:
17
+ - od-api.oxforddictionaries.com
18
+ App-Id:
19
+ - APP_ID
20
+ App-Key:
21
+ - APP_KEY
22
+ response:
23
+ status:
24
+ code: 403
25
+ message: Forbidden
26
+ headers:
27
+ Content-Type:
28
+ - text/plain; charset=us-ascii
29
+ Date:
30
+ - Sun, 30 Jun 2019 04:42:50 GMT
31
+ Server:
32
+ - openresty/1.9.7.4
33
+ Content-Length:
34
+ - '21'
35
+ Connection:
36
+ - keep-alive
37
+ body:
38
+ encoding: UTF-8
39
+ string: Authentication failed
40
+ http_version:
41
+ recorded_at: Sun, 30 Jun 2019 04:42:50 GMT
42
+ recorded_with: VCR 4.0.0
@@ -1,8 +1,3 @@
1
- require 'oxford_dictionary/endpoints/entry_endpoint'
2
- require 'oxford_dictionary/endpoints/inflection_endpoint'
3
- require 'oxford_dictionary/endpoints/search_endpoint'
4
- require 'oxford_dictionary/endpoints/wordlist_endpoint'
5
-
6
1
  require 'oxford_dictionary/endpoints/entries'
7
2
  require 'oxford_dictionary/endpoints/lemmas'
8
3
  require 'oxford_dictionary/endpoints/translations'
@@ -15,11 +10,6 @@ require 'oxford_dictionary/request'
15
10
  module OxfordDictionary
16
11
  # The client object to interact with
17
12
  class Client
18
- include OxfordDictionary::Endpoints::EntryEndpoint
19
- include OxfordDictionary::Endpoints::InflectionEndpoint
20
- include OxfordDictionary::Endpoints::SearchEndpoint
21
- include OxfordDictionary::Endpoints::WordlistEndpoint
22
-
23
13
  attr_reader :app_id, :app_key
24
14
 
25
15
  def initialize(params)
@@ -41,19 +31,171 @@ module OxfordDictionary
41
31
  )
42
32
  else
43
33
  warn '''
44
- The V1 interface for this library is DEPRECATED and will become
45
- non-functional on June 30, 2019. Use the new V2 interface for this
34
+ The V1 interface for this library is DEPRECATED.
35
+ Use the new V2 interface for this
46
36
  method instead. Reference github.com/swcraig/oxford-dictionary/pull/8
47
37
  for more information. Specifically check out
48
38
  OxfordDictionary::Endpoints::Entries#entry for the new interface.
49
39
  '''
50
- # Support V1 behaviour
51
- super(*args)
40
+ # Try our best
41
+ dataset = args[1].is_a?(Hash) && args[1][:lang] || 'en-gb'
42
+ has_lang = args[1].is_a?(Hash) && args[1][:lang]
43
+ args[1].delete(:lang) if has_lang
44
+
45
+ params = args[1]&.map do |key, value|
46
+ if value.is_a?(Array)
47
+ [key, value.join(',')]
48
+ else
49
+ [key, value]
50
+ end
51
+ end.to_h
52
+ parameters = params || {}
53
+ entry(word: args.first, dataset: dataset, params: parameters)
52
54
  end
53
55
  end
54
56
 
57
+ def entry_definitions(*args)
58
+ warn '''
59
+ Client#entry_defintions is DEPRECATED.
60
+ Use Client#entry instead. Reference
61
+ https://github.com/swcraig/oxford-dictionary/pull/8 for more
62
+ information. Check out OxfordDictionary::Endpoints::Entries for the
63
+ interface to use. Specifically use it with
64
+ params: { fields: \'definitions\' }
65
+ '''
66
+
67
+ dataset = args[1].is_a?(Hash) && args[1][:lang] || 'en-gb'
68
+ entry(
69
+ word: args.first,
70
+ dataset: dataset,
71
+ params: { fields: 'definitions' }
72
+ )
73
+ end
74
+
75
+ def entry_examples(*args)
76
+ warn '''
77
+ Client#entry_examples is DEPRECATED. Use Client#entry instead. Reference
78
+ https://github.com/swcraig/oxford-dictionary/pull/8 for more
79
+ information. Check out OxfordDictionary::Endpoints::Entries for the
80
+ interface to use. Specifically use it with
81
+ params: { fields: \'examples\' }
82
+ '''
83
+
84
+ dataset = args[1].is_a?(Hash) && args[1][:lang] || 'en-gb'
85
+ entry(
86
+ word: args.first,
87
+ dataset: dataset,
88
+ params: { fields: 'examples' }
89
+ )
90
+ end
91
+
92
+ def entry_pronunciations(*args)
93
+ warn '''
94
+ Client#entry_pronunciations is DEPRECATED.
95
+ Use Client#entry instead. Reference
96
+ https://github.com/swcraig/oxford-dictionary/pull/8 for more
97
+ information. Check out OxfordDictionary::Endpoints::Entries for the
98
+ interface to use. Specifically use it with
99
+ params: { fields: \'pronunciations\' }
100
+ '''
101
+
102
+ dataset = args[1].is_a?(Hash) && args[1][:lang] || 'en-gb'
103
+ entry(
104
+ word: args.first,
105
+ dataset: dataset,
106
+ params: { fields: 'pronunciations' }
107
+ )
108
+ end
109
+
110
+ def entry_sentences(*args)
111
+ warn '''
112
+ Client#entry_sentences is DEPRECATED.
113
+ Use Client#sentence instead. Reference
114
+ https://github.com/swcraig/oxford-dictionary/pull/13 for more
115
+ information. Check out OxfordDictionary::Endpoints::Sentences for the
116
+ interface to use.
117
+ '''
118
+
119
+ dataset = args[1].is_a?(Hash) && args[1][:lang] || 'en'
120
+ sentence(
121
+ word: args.first,
122
+ language: dataset,
123
+ params: {}
124
+ )
125
+ end
126
+
127
+ def entry_antonyms(*args)
128
+ warn '''
129
+ Client#entry_antonyms is DEPRECATED.
130
+ Use Client#thesaurus instead. Reference
131
+ https://github.com/swcraig/oxford-dictionary/pull/13 for more
132
+ information. Check out OxfordDictionary::Endpoints::Thesaurus for the
133
+ interface to use. Specifically use it with
134
+ params: { fields: \'antonyms\' }
135
+ '''
136
+
137
+ thesaurus(
138
+ word: args.first,
139
+ language: 'en',
140
+ params: { fields: 'antonyms' }
141
+ )
142
+ end
143
+
144
+ def entry_synonyms(*args)
145
+ warn '''
146
+ Client#entry_synonyms is DEPRECATED.
147
+ Use Client#thesaurus instead. Reference
148
+ https://github.com/swcraig/oxford-dictionary/pull/13 for more
149
+ information. Check out OxfordDictionary::Endpoints::Thesaurus for the
150
+ interface to use. Specifically use it with
151
+ params: { fields: \'synonyms\' }
152
+ '''
153
+
154
+ thesaurus(
155
+ word: args.first,
156
+ language: 'en',
157
+ params: { fields: 'synonyms' }
158
+ )
159
+ end
160
+
161
+ def entry_antonyms_synonyms(*args)
162
+ warn '''
163
+ Client#entry_antonyms_synonyms is DEPRECATED.
164
+ Use Client#thesaurus instead. Reference
165
+ https://github.com/swcraig/oxford-dictionary/pull/14 for more
166
+ information. Check out OxfordDictionary::Endpoints::Thesaurus for the
167
+ interface to use. Specifically use it with
168
+ params: { fields: \'synonyms,antonyms\' }
169
+ '''
170
+
171
+ thesaurus(
172
+ word: args.first,
173
+ language: 'en',
174
+ params: { fields: 'antonyms,synonyms' }
175
+ )
176
+ end
177
+
178
+ def entry_translations(*args)
179
+ warn '''
180
+ Client#entry_translations is DEPRECATED.
181
+ Use Client#translation instead. Reference
182
+ https://github.com/swcraig/oxford-dictionary/pull/12 for more
183
+ information. Check out OxfordDictionary::Endpoints::Translations for the
184
+ interface to use.
185
+ '''
186
+
187
+ language = args[1] && args[1][:translations] || 'es'
188
+ translation(
189
+ word: args.first,
190
+ source_language: 'en-us',
191
+ target_language: language,
192
+ params: {}
193
+ )
194
+ end
195
+
55
196
  def entry_snake_case(word:, dataset:, params: {})
56
197
  warn 'Client#entry_snake_case is DEPRECATED. Use Client#entry instead.'
198
+
57
199
  entry_endpoint.
58
200
  entry_snake_case(word: word, dataset: dataset, params: params)
59
201
  end
@@ -90,16 +232,43 @@ module OxfordDictionary
90
232
  else
91
233
  warn '''
92
234
  Client#search without parameters is DEPRECATED.
93
- Using the method without named parameters will become
94
- non-functional on June 30, 2019. Use the new V2 interface for this
235
+ Use the new V2 interface for this
95
236
  method instead. Reference github.com/swcraig/oxford-dictionary/pull/15
96
237
  for more information. Specifically check out
97
238
  OxfordDictionary::Endpoints::Search#search for the new interface.
98
239
  '''
240
+
241
+ language_parameter = args[1].is_a?(Hash) && args[1][:lang]
242
+ language = language_parameter || 'en-gb'
243
+ args[1].delete(:lang) if language_parameter
244
+
99
245
  if args[1].is_a?(Hash) && args[1][:translations]
100
- super(args[0], translations: args[1][:translations])
246
+ target_language = args[1][:translations]
247
+ args[1].delete(:translations)
248
+
249
+ params = args[1]&.map do |key, value|
250
+ if value.is_a?(Array)
251
+ [key, value.join(',')]
252
+ else
253
+ [key, value]
254
+ end
255
+ end.to_h
256
+ parameters = params&.merge(q: args[0]) || {}
257
+ search_translation(
258
+ source_language: language,
259
+ target_language: target_language,
260
+ params: parameters
261
+ )
101
262
  else
102
- super(*args)
263
+ params = args[1]&.map do |key, value|
264
+ if value.is_a?(Array)
265
+ [key, value.join(',')]
266
+ else
267
+ [key, value]
268
+ end
269
+ end.to_h
270
+ parameters = params&.merge(q: args[0]) || {}
271
+ search_endpoint.search(language: language, params: parameters)
103
272
  end
104
273
  end
105
274
  end
@@ -112,6 +281,30 @@ module OxfordDictionary
112
281
  )
113
282
  end
114
283
 
284
+ def inflection(*args)
285
+ warn '''
286
+ Client#inflection is DEPRECATED.
287
+ Use Client#lemma instead. Reference
288
+ github.com/swcraig/oxford-dictionary/pull/10 for for more information.
289
+ Check out OxfordDictionary::Endpoints::Lemmas#lemma for the interface
290
+ to use.
291
+ '''
292
+
293
+ language_parameter = args[1].is_a?(Hash) && args[1][:lang]
294
+ language = language_parameter || 'en'
295
+ args[1].delete(:lang) if language_parameter
296
+
297
+ params = args[1]&.map do |key, value|
298
+ if value.is_a?(Array)
299
+ [key, value.join(',')]
300
+ else
301
+ [key, value]
302
+ end
303
+ end.to_h
304
+ parameters = params || {}
305
+
306
+ lemma(word: args.first, language: language, params: parameters)
307
+ end
115
308
 
116
309
  private
117
310
 
@@ -1,3 +1,3 @@
1
1
  module OxfordDictionary
2
- VERSION = '1.3.1'.freeze
2
+ VERSION = '2.0.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: oxford_dictionary
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.1
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - swcraig
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-06-24 00:00:00.000000000 Z
11
+ date: 2019-06-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -161,28 +161,32 @@ files:
161
161
  - fixtures/vcr_cassettes/lemmas_lemma-es.yml
162
162
  - fixtures/vcr_cassettes/lemmas_lemma-verbs.yml
163
163
  - fixtures/vcr_cassettes/lemmas_lemma.yml
164
+ - fixtures/vcr_cassettes/v1_entry.yml
165
+ - fixtures/vcr_cassettes/v1_entry_antonym_synonym.yml
166
+ - fixtures/vcr_cassettes/v1_entry_antonyms.yml
167
+ - fixtures/vcr_cassettes/v1_entry_definitions.yml
168
+ - fixtures/vcr_cassettes/v1_entry_error.yml
169
+ - fixtures/vcr_cassettes/v1_entry_es.yml
170
+ - fixtures/vcr_cassettes/v1_entry_examples.yml
171
+ - fixtures/vcr_cassettes/v1_entry_past_nouns.yml
172
+ - fixtures/vcr_cassettes/v1_entry_pronunciations.yml
173
+ - fixtures/vcr_cassettes/v1_entry_synonyms.yml
174
+ - fixtures/vcr_cassettes/v1_entry_us.yml
175
+ - fixtures/vcr_cassettes/v1_inflection.yml
176
+ - fixtures/vcr_cassettes/v1_inflection_filters.yml
177
+ - fixtures/vcr_cassettes/v1_search.yml
178
+ - fixtures/vcr_cassettes/v1_search_prefix.yml
179
+ - fixtures/vcr_cassettes/v1_search_translation.yml
164
180
  - lib/oxford_dictionary.rb
165
- - lib/oxford_dictionary/api_objects/entry.rb
166
- - lib/oxford_dictionary/api_objects/entry_response.rb
167
- - lib/oxford_dictionary/api_objects/lexical_entry.rb
168
- - lib/oxford_dictionary/api_objects/list_response.rb
169
- - lib/oxford_dictionary/api_objects/pronunciation.rb
170
- - lib/oxford_dictionary/api_objects/sense.rb
171
181
  - lib/oxford_dictionary/client.rb
172
- - lib/oxford_dictionary/deprecated_request.rb
173
182
  - lib/oxford_dictionary/deserialize.rb
174
183
  - lib/oxford_dictionary/endpoints/endpoint.rb
175
184
  - lib/oxford_dictionary/endpoints/entries.rb
176
- - lib/oxford_dictionary/endpoints/entry_endpoint.rb
177
- - lib/oxford_dictionary/endpoints/inflection_endpoint.rb
178
185
  - lib/oxford_dictionary/endpoints/lemmas.rb
179
186
  - lib/oxford_dictionary/endpoints/search.rb
180
- - lib/oxford_dictionary/endpoints/search_endpoint.rb
181
187
  - lib/oxford_dictionary/endpoints/sentences.rb
182
188
  - lib/oxford_dictionary/endpoints/thesaurus.rb
183
189
  - lib/oxford_dictionary/endpoints/translations.rb
184
- - lib/oxford_dictionary/endpoints/wordlist_endpoint.rb
185
- - lib/oxford_dictionary/error.rb
186
190
  - lib/oxford_dictionary/request.rb
187
191
  - lib/oxford_dictionary/version.rb
188
192
  - oxford_dictionary.gemspec
@@ -205,8 +209,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
205
209
  - !ruby/object:Gem::Version
206
210
  version: '0'
207
211
  requirements: []
208
- rubyforge_project:
209
- rubygems_version: 2.7.4
212
+ rubygems_version: 3.0.3
210
213
  signing_key:
211
214
  specification_version: 4
212
215
  summary: A wrapper for the Oxford Dictionary API
@@ -1,11 +0,0 @@
1
- require 'virtus'
2
- require 'oxford_dictionary/api_objects/sense'
3
-
4
- # Entry
5
- class Entry
6
- include Virtus.model
7
- attribute :etymologies, Array[String]
8
- attribute :grammatical_features, Array[OpenStruct]
9
- attribute :homograph_number, String
10
- attribute :senses, Array[Sense]
11
- end
@@ -1,12 +0,0 @@
1
- require 'virtus'
2
- require 'oxford_dictionary/api_objects/lexical_entry'
3
-
4
- # Top level response (excluding metadata) from entries endpoints
5
- class EntryResponse
6
- include Virtus.model
7
- attribute :id, String
8
- attribute :language, String
9
- attribute :lexical_entries, Array[LexicalEntry]
10
- attribute :type, String
11
- attribute :word, String
12
- end
@@ -1,16 +0,0 @@
1
- require 'virtus'
2
- require 'oxford_dictionary/api_objects/entry'
3
- require 'oxford_dictionary/api_objects/pronunciation'
4
-
5
- # LexicalEntry
6
- class LexicalEntry
7
- include Virtus.model
8
- attribute :entries, Array[Entry]
9
- attribute :grammatical_features, Array[OpenStruct]
10
- attribute :inflection_of, Array[OpenStruct]
11
- attribute :language, String
12
- attribute :lexical_category, String
13
- attribute :pronunciations, Array[Pronunciation]
14
- attribute :sentences, Array[OpenStruct]
15
- attribute :text, String
16
- end
@@ -1,7 +0,0 @@
1
- require 'virtus'
2
-
3
- # Top level response (excluding metadata) from search and wordlist endpoints
4
- class ListResponse
5
- include Virtus.model
6
- attribute :results, Array[OpenStruct]
7
- end
@@ -1,10 +0,0 @@
1
- require 'virtus'
2
-
3
- # Pronunciation
4
- class Pronunciation
5
- include Virtus.model
6
- attribute :audio_file, String
7
- attribute :dialects, Array[String]
8
- attribute :phonetic_notation, String
9
- attribute :phonetic_spelling, String
10
- end
@@ -1,19 +0,0 @@
1
- require 'virtus'
2
-
3
- # Sense
4
- class Sense
5
- include Virtus.model
6
- attribute :id, String
7
- attribute :antonyms, Array[OpenStruct]
8
- attribute :cross_references, Array[OpenStruct]
9
- attribute :cross_reference_markers, Array[String]
10
- attribute :definitions, Array[String]
11
- attribute :domains, Array[String]
12
- attribute :examples, Array[OpenStruct]
13
- attribute :regions, Array[String]
14
- attribute :registers, Array[String]
15
- attribute :subsenses, Array[Sense]
16
- attribute :synonyms, Array[OpenStruct]
17
- attribute :translations, Array[OpenStruct]
18
- attribute :variant_forms, Array[OpenStruct]
19
- end
@@ -1,127 +0,0 @@
1
- require 'json'
2
- require 'net/http'
3
- require 'plissken'
4
- require 'oxford_dictionary/error'
5
-
6
- module OxfordDictionary
7
- # Handles all of the actual API calls
8
- module DeprecatedRequest
9
- BASE = 'https://od-api.oxforddictionaries.com/api/v1'.freeze
10
- HTTP_OK = '200'.freeze
11
- ACCEPT_TYPE = 'application/json'.freeze
12
- # May be used by the wordlist endpoint
13
- ADVANCED_FILTERS = [:exact, :exclude, :exclude_senses,
14
- :exclude_prime_senses, :limit, :offset,
15
- :prefix, :word_length].freeze
16
-
17
- def request(endpoint, q, params)
18
- url = URI(build_url(endpoint, q, params))
19
- response = Net::HTTP.start(url.host, url.port, use_ssl: true) do |http|
20
- request = build_get_request(url)
21
- http.request(request)
22
- end
23
- parse_body_or_raise(response)
24
- end
25
-
26
- private
27
-
28
- def parse_body_or_raise(response)
29
- unless response.code == HTTP_OK
30
- raise(Error.new(response.code), error_message(response.body))
31
- end
32
- JSON.parse(response.body).to_snake_keys
33
- end
34
-
35
- def build_url(endpoint, q, params)
36
- params[:lang] || params[:lang] = 'en'
37
- url_start = "#{BASE}/#{endpoint}/#{params[:lang]}"
38
- if params[:q]
39
- "#{url_start}#{search_endpoint_url(params)}".chomp('/')
40
- else
41
- unless q
42
- # The wordlist endpoint uses a slightly different url structure
43
- return "#{url_start}/#{build_advanced_url(params)}".chomp('/')
44
- end
45
- "#{url_start}/#{q}/#{finish_url(params)}".chomp('/')
46
- end
47
- end
48
-
49
- def build_advanced_url(params)
50
- advanced_params = {}
51
- params.each do |k, v|
52
- if ADVANCED_FILTERS.include?(k)
53
- params.delete(k)
54
- advanced_params[k] = v
55
- end
56
- end
57
- "#{create_query_string(params)}#{advanced_query(advanced_params)}"
58
- end
59
-
60
- def advanced_query(params)
61
- unless params.empty?
62
- params[:exact] || params[:exact] = false
63
- return "?#{create_query_string(params, '&')}"
64
- end
65
- ''
66
- end
67
-
68
- # The search endpoint has a slightly different url structure
69
- def search_endpoint_url(params)
70
- params[:prefix] || params[:prefix] = false
71
- append = ''
72
- if params[:translations]
73
- append = "/translations=#{params[:translations]}"
74
- params.delete(:translations)
75
- end
76
- "#{append}?#{create_query_string(params, '&')}"
77
- end
78
-
79
- def finish_url(params)
80
- params[:end] || create_query_string(params)
81
- end
82
-
83
- def create_query_string(params, seperator = ';')
84
- params.delete(:lang)
85
- count = 0
86
- query = ''
87
- params.each do |k, v|
88
- query += "#{k}=#{options(v)}"
89
- query += seperator if count < params.size - 1
90
- count += 1
91
- end
92
- query
93
- end
94
-
95
- def options(v)
96
- if v.is_a?(Array)
97
- hash_element?(v[0]) ? query_from_hash(v) : v.join(',')
98
- else
99
- v
100
- end
101
- end
102
-
103
- def query_from_hash(hash)
104
- query = ''
105
- hash.each { |h| query += create_query_string(h) }
106
- query
107
- end
108
-
109
- # Remove <p> and </p> in error message
110
- def error_message(response)
111
- response.lines.last.chomp[3..-5]
112
- end
113
-
114
- # The wordlist endpoint may nest filters
115
- def hash_element?(element)
116
- element.is_a?(Hash)
117
- end
118
-
119
- def build_get_request(url)
120
- request = Net::HTTP::Get.new(url)
121
- request['Accept'] = ACCEPT_TYPE
122
- request['app_id'] = app_id
123
- request['app_key'] = app_key
124
- request
125
- end
126
- end
127
- end