Class: StackAgent::Instance

Inherits:
Object
  • Object
show all
Defined in:
lib/stack-agent/instance.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(instance_token = nil) ⇒ Instance

Returns a new instance of Instance.



8
9
10
# File 'lib/stack-agent/instance.rb', line 8

def initialize(instance_token = nil)
  self.instance_token = instance_token
end

Instance Attribute Details

#instance_tokenObject

Returns the value of attribute instance_token.



6
7
8
# File 'lib/stack-agent/instance.rb', line 6

def instance_token
  @instance_token
end

Class Method Details

.stacksObject



51
52
53
54
55
56
# File 'lib/stack-agent/instance.rb', line 51

def self.stacks
  config = StackAgent.configuration
  url = "#{config.api_host}/v1/apps/#{config.app_token}/stacks"
  response = RestClient::Resource.new(url, verify_ssl: OpenSSL::SSL::VERIFY_NONE).get
  JSON.parse(response.body)
end

Instance Method Details

#registerObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/stack-agent/instance.rb', line 12

def register
  config = StackAgent.configuration

  return false unless config.app_token && config.name && config.uri

  url = "#{config.api_host}/v1/apps/#{config.app_token}/stacks"

  data = {
    group: config.group,
    name: config.name,
    uri: config.uri
  }

  begin
    response = RestClient::Resource.new(url, verify_ssl: OpenSSL::SSL::VERIFY_NONE).post(data)
    return self.instance_token = JSON.parse(response.body)['id']
  rescue RestClient::BadRequest => ex
    false
  end
end

#registered?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/stack-agent/instance.rb', line 47

def registered?
  instance_token != nil
end

#unregisterObject



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/stack-agent/instance.rb', line 33

def unregister
  return false unless registered?

  config = StackAgent.configuration
  url = "#{config.api_host}/v1/apps/#{config.app_token}/stacks/#{instance_token}"

  begin
    RestClient::Resource.new(url, verify_ssl: OpenSSL::SSL::VERIFY_NONE).delete
    true
  rescue RestClient::BadRequest => ex
    false
  end
end