Class: SkullIsland::RSpec::FakeAPIClient

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

Overview

A Fake API Client for RSpec testing

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

Constructor Details

#initialize(opts = {}) ⇒ FakeAPIClient

Returns a new instance of FakeAPIClient.



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/skull_island/rspec/fake_api_client.rb', line 13

def initialize(opts = {})
  # validations
  validate_opts(opts)

  # Set up the client's state
  @server     = opts[:server] || 'http://localhost:8001'
  @username   = opts[:username] || 'admin'
  @password   = opts[:password] || 'admin'
  @cache      = LRUCache.new(100) # LRU cache of up to 100 items
  @configured = true
end

Instance Attribute Details

#base_uriObject (readonly)

Returns the value of attribute base_uri.



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

def base_uri
  @base_uri
end

#passwordObject

Returns the value of attribute password.



8
9
10
# File 'lib/skull_island/rspec/fake_api_client.rb', line 8

def password
  @password
end

#serverObject (readonly)

Returns the value of attribute server.



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

def server
  @server
end

#usernameObject

Returns the value of attribute username.



8
9
10
# File 'lib/skull_island/rspec/fake_api_client.rb', line 8

def username
  @username
end

Instance Method Details

#get(uri, _data = nil) ⇒ Object



40
41
42
43
# File 'lib/skull_island/rspec/fake_api_client.rb', line 40

def get(uri, _data = nil)
  @responses ||= {}
  @responses.dig('get', uri.to_s)
end

#hash(data) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/skull_island/rspec/fake_api_client.rb', line 25

def hash(data)
  if data
    Digest::MD5.hexdigest(data.sort.to_s)
  else
    ''
  end
end

#patch(uri, data) ⇒ Object



50
51
52
53
# File 'lib/skull_island/rspec/fake_api_client.rb', line 50

def patch(uri, data)
  @responses ||= {}
  @responses.dig('patch', uri.to_s + hash(data))
end

#post(uri, data = nil) ⇒ Object



45
46
47
48
# File 'lib/skull_island/rspec/fake_api_client.rb', line 45

def post(uri, data = nil)
  @responses ||= {}
  @responses.dig('post', uri.to_s + hash(data))
end

#put(uri, data) ⇒ Object



55
56
57
58
# File 'lib/skull_island/rspec/fake_api_client.rb', line 55

def put(uri, data)
  @responses ||= {}
  @responses.dig('put', uri.to_s + hash(data))
end

#response_for(type, uri, data: nil, response: {}) ⇒ Object



33
34
35
36
37
38
# File 'lib/skull_island/rspec/fake_api_client.rb', line 33

def response_for(type, uri, data: nil, response: {})
  @responses ||= {}
  @responses[type.to_s] ||= {}
  key = data ? uri.to_s + hash(data) : uri.to_s
  @responses[type.to_s][key] = response
end