oxford_dictionary 1.0.1 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
@@ -0,0 +1,9 @@
1
+ require 'json'
2
+
3
+ module OxfordDictionary
4
+ class Deserialize
5
+ def call(payload)
6
+ JSON.parse(payload, object_class: OpenStruct)
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,45 @@
1
+ require 'oxford_dictionary/deserialize'
2
+ require 'plissken'
3
+
4
+ module OxfordDictionary
5
+ module Endpoints
6
+ class Entries
7
+ ENDPOINT = 'entries'.freeze
8
+
9
+ def initialize(request_client:)
10
+ @request_client = request_client
11
+ end
12
+
13
+ def entry(word:, dataset:, params: {})
14
+ query_string = "#{ENDPOINT}/#{dataset}/#{word}"
15
+ uri = URI(query_string)
16
+
17
+ unless params.empty?
18
+ uri.query = URI.encode_www_form(params)
19
+ end
20
+
21
+ response = @request_client.get(uri: uri)
22
+ deserialize.call(response.body)
23
+ end
24
+
25
+ def entry_snake_case(word:, dataset:, params: {})
26
+ query_string = "#{ENDPOINT}/#{dataset}/#{word}"
27
+ uri = URI(query_string)
28
+
29
+ unless params.empty?
30
+ uri.query = URI.encode_www_form(params)
31
+ end
32
+
33
+ response = @request_client.get(uri: uri)
34
+ snake_keys = JSON.parse(response.body).to_snake_keys
35
+ deserialize.call(JSON.generate(snake_keys))
36
+ end
37
+
38
+ private
39
+
40
+ def deserialize
41
+ @deserialize ||= OxfordDictionary::Deserialize.new
42
+ end
43
+ end
44
+ end
45
+ end
@@ -1,11 +1,11 @@
1
- require 'oxford_dictionary/request'
1
+ require 'oxford_dictionary/deprecated_request'
2
2
  require 'oxford_dictionary/api_objects/entry_response'
3
3
 
4
4
  module OxfordDictionary
5
5
  module Endpoints
6
6
  # Interface to '/entries' endpoint
7
7
  module EntryEndpoint
8
- include OxfordDictionary::Request
8
+ include OxfordDictionary::DeprecatedRequest
9
9
  ENDPOINT = 'entries'.freeze
10
10
 
11
11
  def entry(query, params = {})
@@ -1,11 +1,11 @@
1
- require 'oxford_dictionary/request'
1
+ require 'oxford_dictionary/deprecated_request'
2
2
  require 'oxford_dictionary/api_objects/entry_response'
3
3
 
4
4
  module OxfordDictionary
5
5
  module Endpoints
6
6
  # Interface to '/inflections' endpoint
7
7
  module InflectionEndpoint
8
- include OxfordDictionary::Request
8
+ include OxfordDictionary::DeprecatedRequest
9
9
  ENDPOINT = 'inflections'.freeze
10
10
 
11
11
  def inflection(query, params = {})
@@ -1,11 +1,11 @@
1
- require 'oxford_dictionary/request'
1
+ require 'oxford_dictionary/deprecated_request'
2
2
  require 'oxford_dictionary/api_objects/list_response'
3
3
 
4
4
  module OxfordDictionary
5
5
  module Endpoints
6
6
  # Interface to '/search' endpoint
7
7
  module SearchEndpoint
8
- include OxfordDictionary::Request
8
+ include OxfordDictionary::DeprecatedRequest
9
9
  ENDPOINT = 'search'.freeze
10
10
 
11
11
  def search(query, params = {})
@@ -1,12 +1,12 @@
1
- require 'oxford_dictionary/request'
1
+ require 'oxford_dictionary/deprecated_request'
2
2
  require 'oxford_dictionary/api_objects/list_response'
3
3
 
4
4
  module OxfordDictionary
5
5
  module Endpoints
6
6
  # Interface to '/wordlist' endpoint
7
7
  module WordlistEndpoint
8
- include OxfordDictionary::Request
9
8
  extend Gem::Deprecate
9
+ include OxfordDictionary::DeprecatedRequest
10
10
 
11
11
  ENDPOINT = 'wordlist'.freeze
12
12
  ADVANCED_FILTERS = [:exact, :exclude, :exclude_senses,
@@ -1,127 +1,30 @@
1
- require 'json'
2
1
  require 'net/http'
3
- require 'plissken'
4
- require 'oxford_dictionary/error'
5
2
 
6
3
  module OxfordDictionary
7
- # Handles all of the actual API calls
8
- module Request
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
4
+ class Request
5
+ BASE_URL = 'https://od-api.oxforddictionaries.com/api/v2'.freeze
16
6
 
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
7
+ def initialize(app_id:, app_key:)
8
+ @app_id = app_id
9
+ @app_key = app_key
47
10
  end
48
11
 
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
12
+ def get(uri:)
13
+ uri = URI("#{BASE_URL}/#{uri}")
67
14
 
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)
15
+ Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |https|
16
+ https.request(request_object(uri))
75
17
  end
76
- "#{append}?#{create_query_string(params, '&')}"
77
18
  end
78
19
 
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
20
+ private
94
21
 
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
22
+ def request_object(uri)
23
+ Net::HTTP::Get.new(uri).tap do |request|
24
+ request['Accept'] = 'application/json'
25
+ request['app_id'] = @app_id
26
+ request['app_key'] = @app_key
100
27
  end
101
28
  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
29
  end
127
30
  end
@@ -1,3 +1,3 @@
1
1
  module OxfordDictionary
2
- VERSION = '1.0.1'.freeze
2
+ VERSION = '1.1.0'.freeze
3
3
  end
@@ -26,6 +26,8 @@ Gem::Specification.new do |spec|
26
26
  spec.add_development_dependency 'rspec', '~> 3.0'
27
27
  spec.add_development_dependency 'webmock', '~> 2.1.0'
28
28
  spec.add_development_dependency 'rubocop', '~> 0.45.0'
29
+ spec.add_development_dependency 'pry'
30
+ spec.add_development_dependency 'vcr', '~> 4.0.0'
29
31
 
30
32
  spec.add_runtime_dependency 'virtus', '~> 1.0.5'
31
33
  spec.add_runtime_dependency 'plissken', '~> 0.1.0'
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.0.1
4
+ version: 1.1.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-05-08 00:00:00.000000000 Z
11
+ date: 2019-06-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -80,6 +80,34 @@ dependencies:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: 0.45.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: vcr
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 4.0.0
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 4.0.0
83
111
  - !ruby/object:Gem::Dependency
84
112
  name: virtus
85
113
  requirement: !ruby/object:Gem::Requirement
@@ -126,6 +154,10 @@ files:
126
154
  - Rakefile
127
155
  - bin/console
128
156
  - bin/setup
157
+ - fixtures/vcr_cassettes/entries_entry-en-us.yml
158
+ - fixtures/vcr_cassettes/entries_entry-verbs.yml
159
+ - fixtures/vcr_cassettes/entries_entry.yml
160
+ - fixtures/vcr_cassettes/entries_entry_snake_case.yml
129
161
  - lib/oxford_dictionary.rb
130
162
  - lib/oxford_dictionary/api_objects/entry.rb
131
163
  - lib/oxford_dictionary/api_objects/entry_response.rb
@@ -134,6 +166,9 @@ files:
134
166
  - lib/oxford_dictionary/api_objects/pronunciation.rb
135
167
  - lib/oxford_dictionary/api_objects/sense.rb
136
168
  - lib/oxford_dictionary/client.rb
169
+ - lib/oxford_dictionary/deprecated_request.rb
170
+ - lib/oxford_dictionary/deserialize.rb
171
+ - lib/oxford_dictionary/endpoints/entries.rb
137
172
  - lib/oxford_dictionary/endpoints/entry_endpoint.rb
138
173
  - lib/oxford_dictionary/endpoints/inflection_endpoint.rb
139
174
  - lib/oxford_dictionary/endpoints/search_endpoint.rb