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, RSpec::FakeAPIClient, 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



83
84
85
86
87
# File 'lib/skull_island/api_client_base.rb', line 83

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
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/skull_island/api_client_base.rb', line 32

def get(uri, data = nil)
  client_action do |client|
    results = nil
    params = {}
    params.merge(data) if data
    # OPTIMIZE: Move to an Enumerable
    loop do
      follow_up = JSON.parse client[uri].get(json_headers.merge(params: params))

      if results
        results['data'] += follow_up['data']
      else
        results = follow_up.dup
        results.delete('offset')
        results.delete('next')
      end

      params = params.merge('offset' => follow_up['offset']) if follow_up.key?('offset')
      raise StopIteration unless follow_up.key?('offset')
    end
    results
  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



66
67
68
69
70
71
72
73
74
75
# File 'lib/skull_island/api_client_base.rb', line 66

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



56
57
58
59
60
61
62
63
64
# File 'lib/skull_island/api_client_base.rb', line 56

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



77
78
79
80
81
# File 'lib/skull_island/api_client_base.rb', line 77

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