oxford_dictionary 1.0.1 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,95 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: get
5
+ uri: https://od-api.oxforddictionaries.com/api/v2/lemmas/es/fuego
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: 200
25
+ message: OK
26
+ headers:
27
+ Api-Version:
28
+ - v2
29
+ Code-Version:
30
+ - v2.3.2-g02aa52c
31
+ Content-Type:
32
+ - application/json;charset=utf-8
33
+ Date:
34
+ - Sat, 22 Jun 2019 23:43:20 GMT
35
+ Server:
36
+ - openresty/1.13.6.2
37
+ Content-Length:
38
+ - '1429'
39
+ Connection:
40
+ - keep-alive
41
+ body:
42
+ encoding: UTF-8
43
+ string: |-
44
+ {
45
+ "metadata": {
46
+ "provider": "Oxford University Press"
47
+ },
48
+ "results": [
49
+ {
50
+ "id": "fuego",
51
+ "language": "es",
52
+ "lexicalEntries": [
53
+ {
54
+ "grammaticalFeatures": [
55
+ {
56
+ "id": "masculine",
57
+ "text": "Masculine",
58
+ "type": "Gender"
59
+ }
60
+ ],
61
+ "inflectionOf": [
62
+ {
63
+ "id": "fuego",
64
+ "text": "fuego"
65
+ }
66
+ ],
67
+ "language": "es",
68
+ "lexicalCategory": {
69
+ "id": "noun",
70
+ "text": "Noun"
71
+ },
72
+ "text": "fuego"
73
+ },
74
+ {
75
+ "inflectionOf": [
76
+ {
77
+ "id": "fuego",
78
+ "text": "fuego"
79
+ }
80
+ ],
81
+ "language": "es",
82
+ "lexicalCategory": {
83
+ "id": "interjection",
84
+ "text": "Interjection"
85
+ },
86
+ "text": "fuego"
87
+ }
88
+ ],
89
+ "word": "fuego"
90
+ }
91
+ ]
92
+ }
93
+ http_version:
94
+ recorded_at: Sat, 22 Jun 2019 23:43:20 GMT
95
+ recorded_with: VCR 4.0.0
@@ -0,0 +1,81 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: get
5
+ uri: https://od-api.oxforddictionaries.com/api/v2/lemmas/en/ace?lexicalCategory=verb
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: 200
25
+ message: OK
26
+ headers:
27
+ Api-Version:
28
+ - v2
29
+ Code-Version:
30
+ - v2.3.2-g02aa52c
31
+ Content-Type:
32
+ - application/json;charset=utf-8
33
+ Date:
34
+ - Sat, 22 Jun 2019 23:43:19 GMT
35
+ Server:
36
+ - openresty/1.13.6.2
37
+ Content-Length:
38
+ - '964'
39
+ Connection:
40
+ - keep-alive
41
+ body:
42
+ encoding: UTF-8
43
+ string: |-
44
+ {
45
+ "metadata": {
46
+ "provider": "Oxford University Press"
47
+ },
48
+ "results": [
49
+ {
50
+ "id": "ace",
51
+ "language": "en",
52
+ "lexicalEntries": [
53
+ {
54
+ "grammaticalFeatures": [
55
+ {
56
+ "id": "transitive",
57
+ "text": "Transitive",
58
+ "type": "Subcategorization"
59
+ }
60
+ ],
61
+ "inflectionOf": [
62
+ {
63
+ "id": "ace",
64
+ "text": "ace"
65
+ }
66
+ ],
67
+ "language": "en",
68
+ "lexicalCategory": {
69
+ "id": "verb",
70
+ "text": "Verb"
71
+ },
72
+ "text": "ace"
73
+ }
74
+ ],
75
+ "word": "ace"
76
+ }
77
+ ]
78
+ }
79
+ http_version:
80
+ recorded_at: Sat, 22 Jun 2019 23:43:19 GMT
81
+ recorded_with: VCR 4.0.0
@@ -0,0 +1,109 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: get
5
+ uri: https://od-api.oxforddictionaries.com/api/v2/lemmas/en/ace
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: 200
25
+ message: OK
26
+ headers:
27
+ Api-Version:
28
+ - v2
29
+ Code-Version:
30
+ - v2.3.2-g02aa52c
31
+ Content-Type:
32
+ - application/json;charset=utf-8
33
+ Date:
34
+ - Sat, 22 Jun 2019 23:42:51 GMT
35
+ Server:
36
+ - openresty/1.13.6.2
37
+ Content-Length:
38
+ - '1866'
39
+ Connection:
40
+ - keep-alive
41
+ body:
42
+ encoding: UTF-8
43
+ string: |-
44
+ {
45
+ "metadata": {
46
+ "provider": "Oxford University Press"
47
+ },
48
+ "results": [
49
+ {
50
+ "id": "ace",
51
+ "language": "en",
52
+ "lexicalEntries": [
53
+ {
54
+ "inflectionOf": [
55
+ {
56
+ "id": "ace",
57
+ "text": "ace"
58
+ }
59
+ ],
60
+ "language": "en",
61
+ "lexicalCategory": {
62
+ "id": "noun",
63
+ "text": "Noun"
64
+ },
65
+ "text": "ace"
66
+ },
67
+ {
68
+ "inflectionOf": [
69
+ {
70
+ "id": "ace",
71
+ "text": "ace"
72
+ }
73
+ ],
74
+ "language": "en",
75
+ "lexicalCategory": {
76
+ "id": "adjective",
77
+ "text": "Adjective"
78
+ },
79
+ "text": "ace"
80
+ },
81
+ {
82
+ "grammaticalFeatures": [
83
+ {
84
+ "id": "transitive",
85
+ "text": "Transitive",
86
+ "type": "Subcategorization"
87
+ }
88
+ ],
89
+ "inflectionOf": [
90
+ {
91
+ "id": "ace",
92
+ "text": "ace"
93
+ }
94
+ ],
95
+ "language": "en",
96
+ "lexicalCategory": {
97
+ "id": "verb",
98
+ "text": "Verb"
99
+ },
100
+ "text": "ace"
101
+ }
102
+ ],
103
+ "word": "ace"
104
+ }
105
+ ]
106
+ }
107
+ http_version:
108
+ recorded_at: Sat, 22 Jun 2019 23:42:51 GMT
109
+ recorded_with: VCR 4.0.0
@@ -3,6 +3,9 @@ require 'oxford_dictionary/endpoints/inflection_endpoint'
3
3
  require 'oxford_dictionary/endpoints/search_endpoint'
4
4
  require 'oxford_dictionary/endpoints/wordlist_endpoint'
5
5
 
6
+ require 'oxford_dictionary/endpoints/entries'
7
+ require 'oxford_dictionary/endpoints/lemmas'
8
+
6
9
  module OxfordDictionary
7
10
  # The client object to interact with
8
11
  class Client
@@ -21,5 +24,53 @@ module OxfordDictionary
21
24
  instance_variable_set("@#{key}", value)
22
25
  end
23
26
  end
27
+
28
+ def entry(*args)
29
+ if args.first.is_a?(Hash)
30
+ args = args.first
31
+ entry_endpoint.entry(
32
+ word: args[:word],
33
+ dataset: args[:dataset],
34
+ params: args[:params]
35
+ )
36
+ else
37
+ warn '''
38
+ The V1 interface for this library is DEPRECATED and will become
39
+ non-functional on June 30, 2019. Use the new V2 interface for this
40
+ method instead. Reference github.com/swcraig/oxford-dictionary/pull/8
41
+ for more information. Specifically check out
42
+ OxfordDictionary::Endpoints::Entries#entry for the new interface.
43
+ '''
44
+ # Support V1 behaviour
45
+ super(*args)
46
+ end
47
+ end
48
+
49
+ def entry_snake_case(word:, dataset:, params: {})
50
+ warn 'Client#entry_snake_case is DEPRECATED. Use Client#entry instead.'
51
+ entry_endpoint.
52
+ entry_snake_case(word: word, dataset: dataset, params: params)
53
+ end
54
+
55
+ def lemma(word:, language:, params: {})
56
+ lemma_endpoint.lemma(word: word, language: language, params: params)
57
+ end
58
+
59
+ private
60
+
61
+ def lemma_endpoint
62
+ @lemma_endpoint ||=
63
+ OxfordDictionary::Endpoints::Lemmas.new(request_client: request_client)
64
+ end
65
+
66
+ def entry_endpoint
67
+ @entry_endpoint ||=
68
+ OxfordDictionary::Endpoints::Entries.new(request_client: request_client)
69
+ end
70
+
71
+ def request_client
72
+ @request_client ||=
73
+ OxfordDictionary::Request.new(app_id: @app_id, app_key: @app_key)
74
+ end
24
75
  end
25
76
  end
@@ -0,0 +1,127 @@
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