Skip to content

Commit

Permalink
Add Entries endpoint to Client
Browse files Browse the repository at this point in the history
  • Loading branch information
swcraig committed Jun 22, 2019
1 parent 502b88f commit 9a37f04
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
23 changes: 23 additions & 0 deletions lib/oxford_dictionary/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,31 @@ def initialize(params)
end
end

def entry(*args)
if args.first.is_a?(Hash)
args = args.first
if args[:dataset_name]
entry_endpoint.entry(
word: args[:word],
dataset_name: args[:dataset_name],
params: args[:params]
)
else
entry_endpoint.entry(word: args[:word], params: args[:params])
end
else
# Support V1 behaviour
super(*args)
end
end

private

def entry_endpoint
@entry_endpoint ||=
OxfordDictionary::Endpoints::Entries.new(request_client: request_client)
end

def request_client
@request_client ||=
OxfordDictionary::Request.new(app_id: @app_id, app_key: @app_key)
Expand Down
22 changes: 21 additions & 1 deletion spec/client_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
require 'spec_helper'

describe 'Client' do
RSpec.describe OxfordDictionary::Client do
let(:app_id) { 'ID' }
let(:app_key) { 'KEY' }

describe '#new' do
it 'requires an argument' do
expect { OxfordDictionary::Client.new(nil) }
Expand All @@ -17,4 +20,21 @@
.to raise_error(ArgumentError, 'API id and key required.')
end
end

describe '#entry' do
let(:client) { described_class.new(app_id: app_id, app_key: app_key) }
subject { client.entry(args) }

context 'when the argument is a Hash' do
let(:args) { { word: 'ace', dataset_name: 'en-us', params: {} } }

it 'calls the Entries endpoint with correct arguments' do
expect_any_instance_of(OxfordDictionary::Endpoints::Entries).
to receive(:entry).
with(word: args[:word], dataset_name: args[:dataset_name], params: args[:params])

subject
end
end
end
end

0 comments on commit 9a37f04

Please sign in to comment.