-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add OxfordDictionary::Endpoints::Search
Oxford Dictionaries is updating their API to a new version which includes quite a few changes: https://developer.oxforddictionaries.com/version2 There have been updates to the search functionality which is documented in the migration guide linked above. As mentioned in the tests, this only unit tests the endpoint. If someone would like to contribute, please feel free to update and run the specs (similar to what was done for the Entries and Lemmas endpoints). Entries: #8 Lemmas: #10
- Loading branch information
Showing
2 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
require 'oxford_dictionary/endpoints/endpoint' | ||
|
||
module OxfordDictionary | ||
module Endpoints | ||
class Search < Endpoint | ||
ENDPOINT = 'search'.freeze | ||
|
||
def search(language:, params: {}) | ||
query_string = "#{ENDPOINT}/#{language}" | ||
uri = URI(query_string) | ||
|
||
unless params.empty? | ||
uri.query = URI.encode_www_form(params) | ||
end | ||
|
||
response = @request_client.get(uri: uri) | ||
deserialize.call(response.body) | ||
end | ||
|
||
def search_translation(source_language:, target_language:, params: {}) | ||
query_string = | ||
"#{ENDPOINT}/translations/#{source_language}/#{target_language}" | ||
uri = URI(query_string) | ||
|
||
unless params.empty? | ||
uri.query = URI.encode_www_form(params) | ||
end | ||
|
||
response = @request_client.get(uri: uri) | ||
deserialize.call(response.body) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
require 'spec_helper' | ||
require 'oxford_dictionary/endpoints/search' | ||
|
||
# Spec dependencies | ||
require 'oxford_dictionary/request' | ||
|
||
RSpec.describe OxfordDictionary::Endpoints::Search do | ||
let(:request_client) do | ||
OxfordDictionary::Request. | ||
new(app_id: ENV['APP_ID'], app_key: ENV['APP_KEY']) | ||
end | ||
|
||
let(:endpoint) { described_class.new(request_client: request_client) } | ||
|
||
# The search endpoint is only avaiable to the paid tier | ||
# If someone with a paid tier account would like to contribute, please | ||
# feel free remove this double (and the stub in the tests), uncomment the | ||
# sections that run VCR against the live endpoint, and PR the resulting files | ||
let(:response_double) { double(body: {}.to_json) } | ||
|
||
describe '#search' do | ||
subject do | ||
endpoint.search(language: language, params: params) | ||
end | ||
|
||
let(:language) { 'en-gb' } | ||
let(:params) { { q: 'an' } } | ||
|
||
|
||
it 'calls API as expected', :aggregate_failures do | ||
expected_uri = URI("search/#{language}?q=an") | ||
|
||
expect(request_client).to receive(:get). | ||
with(uri: expected_uri). | ||
and_return(response_double) | ||
|
||
subject | ||
|
||
# VCR.use_cassette('search#search') do | ||
# response = subject | ||
# expect(response).to be_an(OpenStruct) | ||
# expect(response.results.first.id).to eq(word) | ||
# expect(response.results.first.lexicalEntries).to all(be_an(OpenStruct)) | ||
# end | ||
end | ||
end | ||
|
||
describe '#search_translation' do | ||
subject do | ||
endpoint.search_translation( | ||
source_language: source_language, | ||
target_language: target_language, | ||
params: params | ||
) | ||
end | ||
|
||
let(:source_language) { 'en' } | ||
let(:target_language) { 'es' } | ||
let(:params) { { q: 'an' } } | ||
|
||
it 'calls API as expected', :aggregate_failures do | ||
expected_uri = | ||
URI("search/translations/#{source_language}/#{target_language}?q=an") | ||
|
||
expect(request_client).to receive(:get). | ||
with(uri: expected_uri). | ||
and_return(response_double) | ||
|
||
subject | ||
|
||
# VCR.use_cassette('search#search_translation') do | ||
# response = subject | ||
# expect(response).to be_an(OpenStruct) | ||
# expect(response.results.first.id).to eq(word) | ||
# expect(response.results.first.lexicalEntries).to all(be_an(OpenStruct)) | ||
# end | ||
end | ||
end | ||
end |