Module: SkullIsland::Helpers::APIClient

Included in:
APIClientBase, RSpec::FakeAPIClient
Defined in:
lib/skull_island/helpers/api_client.rb

Overview

Simple helper methods for the API Client

Instance Method Summary collapse

Instance Method Details

#about_serviceObject



7
8
9
# File 'lib/skull_island/helpers/api_client.rb', line 7

def about_service
  get '/'
end

#cache(key) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/skull_island/helpers/api_client.rb', line 15

def cache(key)
  symbolized_key = key.to_sym
  if !@cache.has?(symbolized_key) && block_given?
    result = yield(self)
    @cache.store(symbolized_key, result)
  elsif !@cache.has?(symbolized_key)
    return nil
  end
  @cache.retrieve(symbolized_key)
end

#invalidate_cache_for(key) ⇒ Object



26
27
28
29
# File 'lib/skull_island/helpers/api_client.rb', line 26

def invalidate_cache_for(key)
  symbolized_key = key.to_sym
  @cache.invalidate(symbolized_key)
end

#json_escape(string) ⇒ String

Substitute characters with their JSON-supported versions

Returns:

  • (String)


37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/skull_island/helpers/api_client.rb', line 37

def json_escape(string)
  json_escape = {
    '&' => '\u0026',
    '>' => '\u003e',
    '<' => '\u003c',
    '%' => '\u0025',
    "\u2028" => '\u2028',
    "\u2029" => '\u2029'
  }
  json_escape_regex = /[\u2028\u2029&><%]/u

  string.to_s.gsub(json_escape_regex, json_escape)
end

#lru_cacheObject



31
32
33
# File 'lib/skull_island/helpers/api_client.rb', line 31

def lru_cache
  @cache
end

#rawRestClient::Resource

Provides access to the “raw” underlying rest-client

Returns:

  • (RestClient::Resource)


53
54
55
# File 'lib/skull_island/helpers/api_client.rb', line 53

def raw
  connection
end

#server_statusObject



11
12
13
# File 'lib/skull_island/helpers/api_client.rb', line 11

def server_status
  get '/status'
end

#versionString

The API Client version (uses Semantic Versioning)

Returns:

  • (String)


59
60
61
# File 'lib/skull_island/helpers/api_client.rb', line 59

def version
  VERSION
end