Class: SearchKit::Clients::Keys

Inherits:
Object
  • Object
show all
Defined in:
lib/search_kit/clients/keys.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeKeys

Returns a new instance of Keys.



9
10
11
12
13
# File 'lib/search_kit/clients/keys.rb', line 9

def initialize
  uri = [SearchKit.config.app_uri, "keys"].join("/")
  @connection = Faraday.new(uri)
  @token      = SearchKit.config.app_token
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



7
8
9
# File 'lib/search_kit/clients/keys.rb', line 7

def connection
  @connection
end

#tokenObject (readonly)

Returns the value of attribute token.



7
8
9
# File 'lib/search_kit/clients/keys.rb', line 7

def token
  @token
end

Instance Method Details

#create(name, options = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/search_kit/clients/keys.rb', line 15

def create(name, options = {})
  options = {
    token: token,
    data: { type: 'keys', attributes: { name: name }.merge(options) }
  }

  response = connection.post('', options)
  body     = JSON.parse(response.body, symbolize_names: true)

  fail Errors::BadRequest    if response.status == 400
  fail Errors::Unauthorized  if response.status == 401
  fail Errors::Unprocessable if response.status == 422

  body
end

#expire(id) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/search_kit/clients/keys.rb', line 31

def expire(id)
  response = connection.delete(id, token: token)
  body     = JSON.parse(response.body, symbolize_names: true)

  fail Errors::Unauthorized if response.status == 401
  fail Errors::KeyNotFound  if response.status == 404

  body
end

#indexObject



41
42
43
44
45
46
47
48
# File 'lib/search_kit/clients/keys.rb', line 41

def index
  response = connection.get("", token: token)
  body     = JSON.parse(response.body, symbolize_names: true)

  fail Errors::Unauthorized if response.status == 401

  body
end

#show(id) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/search_kit/clients/keys.rb', line 50

def show(id)
  response = connection.get(id, token: token)
  body     = JSON.parse(response.body, symbolize_names: true)

  fail Errors::Unauthorized if response.status == 401
  fail Errors::KeyNotFound  if response.status == 404

  body
end

#update(id, options) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/search_kit/clients/keys.rb', line 60

def update(id, options)
  options  = {
    token: token, data: { type: 'keys', attributes: options }
  }

  response = connection.patch(id, options)
  body     = JSON.parse(response.body, symbolize_names: true)

  fail Errors::BadRequest    if response.status == 400
  fail Errors::Unauthorized  if response.status == 401
  fail Errors::KeyNotFound   if response.status == 404
  fail Errors::Unprocessable if response.status == 422

  body
end