Skip to content

Commit

Permalink
Refactor all V2 endpoints to use a base class
Browse files Browse the repository at this point in the history
This should have been done from the start, but here we are, doing it
now.
  • Loading branch information
swcraig committed Jun 23, 2019
1 parent 9dea838 commit 1a9f9ea
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 60 deletions.
14 changes: 2 additions & 12 deletions lib/oxford_dictionary/endpoints/entries.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
require 'oxford_dictionary/deserialize'
require 'oxford_dictionary/endpoints/endpoint'
require 'plissken'

module OxfordDictionary
module Endpoints
class Entries
class Entries < Endpoint
ENDPOINT = 'entries'.freeze

def initialize(request_client:)
@request_client = request_client
end

def entry(word:, dataset:, params: {})
query_string = "#{ENDPOINT}/#{dataset}/#{word}"
uri = URI(query_string)
Expand All @@ -34,12 +30,6 @@ def entry_snake_case(word:, dataset:, params: {})
snake_keys = JSON.parse(response.body).to_snake_keys
deserialize.call(JSON.generate(snake_keys))
end

private

def deserialize
@deserialize ||= OxfordDictionary::Deserialize.new
end
end
end
end
14 changes: 2 additions & 12 deletions lib/oxford_dictionary/endpoints/lemmas.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
require 'oxford_dictionary/deserialize'
require 'oxford_dictionary/endpoints/endpoint'

module OxfordDictionary
module Endpoints
class Lemmas
class Lemmas < Endpoint
ENDPOINT = 'lemmas'.freeze

def initialize(request_client:)
@request_client = request_client
end

def lemma(word:, language:, params: {})
query_string = "#{ENDPOINT}/#{language}/#{word}"
uri = URI(query_string)
Expand All @@ -20,12 +16,6 @@ def lemma(word:, language:, params: {})
response = @request_client.get(uri: uri)
deserialize.call(response.body)
end

private

def deserialize
@deserialize ||= OxfordDictionary::Deserialize.new
end
end
end
end
14 changes: 2 additions & 12 deletions lib/oxford_dictionary/endpoints/sentences.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
require 'oxford_dictionary/deserialize'
require 'oxford_dictionary/endpoints/endpoint'

module OxfordDictionary
module Endpoints
class Sentences
class Sentences < Endpoint
ENDPOINT = 'sentences'.freeze

def initialize(request_client:)
@request_client = request_client
end

def sentence(word:, language:, params: {})
query_string = "#{ENDPOINT}/#{language}/#{word}"
uri = URI(query_string)
Expand All @@ -20,12 +16,6 @@ def sentence(word:, language:, params: {})
response = @request_client.get(uri: uri)
deserialize.call(response.body)
end

private

def deserialize
@deserialize ||= OxfordDictionary::Deserialize.new
end
end
end
end
14 changes: 2 additions & 12 deletions lib/oxford_dictionary/endpoints/thesaurus.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
require 'oxford_dictionary/deserialize'
require 'oxford_dictionary/endpoints/endpoint'

module OxfordDictionary
module Endpoints
class Thesaurus
class Thesaurus < Endpoint
ENDPOINT = 'thesaurus'.freeze

def initialize(request_client:)
@request_client = request_client
end

def thesaurus(word:, language:, params: {})
query_string = "#{ENDPOINT}/#{language}/#{word}"
uri = URI(query_string)
Expand All @@ -20,12 +16,6 @@ def thesaurus(word:, language:, params: {})
response = @request_client.get(uri: uri)
deserialize.call(response.body)
end

private

def deserialize
@deserialize ||= OxfordDictionary::Deserialize.new
end
end
end
end
14 changes: 2 additions & 12 deletions lib/oxford_dictionary/endpoints/translations.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
require 'oxford_dictionary/deserialize'
require 'oxford_dictionary/endpoints/endpoint'

module OxfordDictionary
module Endpoints
class Translations
class Translations < Endpoint
ENDPOINT = 'translations'.freeze

def initialize(request_client:)
@request_client = request_client
end

def translation(word:, source_language:, target_language:, params: {})
query_string =
"#{ENDPOINT}/#{source_language}/#{target_language}/#{word}"
Expand All @@ -21,12 +17,6 @@ def translation(word:, source_language:, target_language:, params: {})
response = @request_client.get(uri: uri)
deserialize.call(response.body)
end

private

def deserialize
@deserialize ||= OxfordDictionary::Deserialize.new
end
end
end
end

0 comments on commit 1a9f9ea

Please sign in to comment.