Class: Voicemaker::API

Inherits:
Object
  • Object
show all
Defined in:
lib/voicemaker/api.rb

Constant Summary collapse

ROOT =
'https://developer.voicemaker.in/voice'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.cache_dirObject



23
24
25
# File 'lib/voicemaker/api.rb', line 23

def cache_dir
  @cache_dir ||= ENV['VOICEMAKER_CACHE_DIR'] || 'cache'
end

.cache_lifeObject



19
20
21
# File 'lib/voicemaker/api.rb', line 19

def cache_life
  @cache_life ||= ENV['VOICEMAKER_CACHE_LIFE'] || '4h'
end

.keyObject



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

.rootObject



11
12
13
# File 'lib/voicemaker/api.rb', line 11

def root
  @root ||= ENV['VOICEMAKER_API_ROOT'] || ROOT
end

Class Method Details

.cacheObject



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