Class: SkullIsland::APIClientBase

Inherits:
Object
  • Object
show all
Includes:
Helpers::APIClient, Validations::APIClient
Defined in:
lib/skull_island/api_client_base.rb

Overview

The API Client Base class

Direct Known Subclasses

APIClient, SimpleAPIClient

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers::APIClient

#about_service, #cache, #invalidate_cache_for, #json_escape, #lru_cache, #raw, #server_status, #version

Methods included from Validations::APIClient

#validate_creds, #validate_opts, #validate_server

Instance Attribute Details

#base_uriObject (readonly)

Returns the value of attribute base_uri.



6
7
8
# File 'lib/skull_island/api_client_base.rb', line 6

def base_uri
  @base_uri
end

#passwordObject

Returns the value of attribute password.



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

def password
  @password
end

#serverObject (readonly)

Returns the value of attribute server.



6
7
8
# File 'lib/skull_island/api_client_base.rb', line 6

def server
  @server
end

#usernameObject

Returns the value of attribute username.



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

def username
  @username
end

Instance Method Details

#api_uriObject



12
13
14
15
16
# File 'lib/skull_island/api_client_base.rb', line 12

def api_uri
  @api_uri ||= URI.parse(server)
  @api_uri.path = base_uri if base_uri
  @api_uri
end

#authenticated?Boolean

Returns:

  • (Boolean)

Raises:



18
19
20
21
22
# File 'lib/skull_island/api_client_base.rb', line 18

def authenticated?
  raise Exceptions::APIClientNotConfigured unless configured?

  @username && @password ? true : false
end

#configured?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/skull_island/api_client_base.rb', line 24

def configured?
  @configured ? true : false
end

#delete(uri) ⇒ Object



70
71
72
73
74
# File 'lib/skull_island/api_client_base.rb', line 70

def delete(uri)
  client_action do |client|
    client[uri].delete(json_headers)
  end
end

#get(uri, data = nil) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/skull_island/api_client_base.rb', line 32

def get(uri, data = nil)
  client_action do |client|
    # TODO: Support the API's pagination through the "next" top-level key
    if data
      JSON.parse client[uri].get(json_headers.merge(params: data))
    else
      JSON.parse client[uri].get(json_headers)
    end
  end
end

#json_headersObject



28
29
30
# File 'lib/skull_island/api_client_base.rb', line 28

def json_headers
  { content_type: :json, accept: :json }
end

#patch(uri, data) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# File 'lib/skull_island/api_client_base.rb', line 53

def patch(uri, data)
  client_action do |client|
    response = client[uri].patch(json_escape(data.to_json), json_headers)
    if response && !response.empty?
      JSON.parse(response)
    else
      true
    end
  end
end

#post(uri, data = nil) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/skull_island/api_client_base.rb', line 43

def post(uri, data = nil)
  client_action do |client|
    if data
      JSON.parse client[uri].post(json_escape(data.to_json), json_headers)
    else
      JSON.parse client[uri].post(nil, json_headers)
    end
  end
end

#put(uri, data) ⇒ Object



64
65
66
67
68
# File 'lib/skull_island/api_client_base.rb', line 64

def put(uri, data)
  client_action do |client|
    client[uri].put(json_escape(data.to_json), json_headers)
  end
end