Class: Voicemaker::API
- Inherits:
-
Object
- Object
- Voicemaker::API
- Defined in:
- lib/voicemaker/api.rb
Constant Summary collapse
- ROOT =
'https://developer.voicemaker.in/voice'
Class Attribute Summary collapse
Class Method Summary collapse
- .cache ⇒ Object
-
.get(endpoint, params = {}) ⇒ Object
Performs HTTP GET and cache it Returns a parsed body on success Raises BadResponse on error.
-
.post(endpoint, params = {}) ⇒ Object
Performs HTTP POST and cache it Returns a parsed body on success Raises BadResponse on error.
Class Attribute Details
.cache_dir ⇒ Object
23 24 25 |
# File 'lib/voicemaker/api.rb', line 23 def cache_dir @cache_dir ||= ENV['VOICEMAKER_CACHE_DIR'] || 'cache' end |
.cache_life ⇒ Object
19 20 21 |
# File 'lib/voicemaker/api.rb', line 19 def cache_life @cache_life ||= ENV['VOICEMAKER_CACHE_LIFE'] || '4h' end |
.key ⇒ Object
15 16 17 |
# File 'lib/voicemaker/api.rb', line 15 def key @key ||= ENV['VOICEMAKER_API_KEY'] || raise(MissingAuth, "Please set the 'VOICEMAKER_API_KEY' environment variable") end |
Class Method Details
.cache ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/voicemaker/api.rb', line 47 def cache @cache ||= begin lightly = Lightly.new life: cache_life, dir: cache_dir lightly.disable if cache_life == 'off' lightly end end |
.get(endpoint, params = {}) ⇒ Object
Performs HTTP GET and cache it Returns a parsed body on success Raises BadResponse on error
30 31 32 33 34 35 |
# File 'lib/voicemaker/api.rb', line 30 def get(endpoint, params = {}) cache.get "#{root}/#{endpoint}+#{params}" do response = get! endpoint, params response.parse end end |
.post(endpoint, params = {}) ⇒ Object
Performs HTTP POST and cache it Returns a parsed body on success Raises BadResponse on error
40 41 42 43 44 45 |
# File 'lib/voicemaker/api.rb', line 40 def post(endpoint, params = {}) cache.get "#{root}/#{endpoint}+#{params}" do response = post! endpoint, params response.parse end end |