Class: Cloudpassage::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudpassage/base.rb

Overview

Base class representing cloudpassage objects.

Instance Method Summary collapse

Constructor Details

#initialize(token, base_resource, data = nil) ⇒ Base

Returns a new instance of Base.



17
18
19
20
21
# File 'lib/cloudpassage/base.rb', line 17

def initialize(token, base_resource, data=nil)
  @token = token
  @base_resource = base_resource
  @data = data
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object

If method is missing, try to pass through to underlying data hash.



42
43
44
45
46
47
48
# File 'lib/cloudpassage/base.rb', line 42

def method_missing(sym, *args, &block)
  if (data && data[sym])
    data[sym]
  else
    super(sym, *args, &block)
  end
end

Instance Method Details

#[](key) ⇒ Object

Allows us to use any one of: object.id object object



54
55
56
# File 'lib/cloudpassage/base.rb', line 54

def [](key)
  data[key.to_sym]
end

#dataObject

Return data from sending http GET to underlying resource. Uses cached value if resource has been retrieved already.



25
26
27
28
29
30
# File 'lib/cloudpassage/base.rb', line 25

def data
  if @data.nil?
    @data = JSON.parse(@base_resource.get(headers), :symbolize_names=>true)[object_symbol]
  end
  @data
end

#exists?Boolean

Return true if object exists in the cloudpassage API, false otherwise.

Returns:

  • (Boolean)


75
76
77
78
79
80
# File 'lib/cloudpassage/base.rb', line 75

def exists?
  @base_resource.get(headers)
  true
rescue RestClient::ResourceNotFound
  false
end

#headersObject



37
38
39
# File 'lib/cloudpassage/base.rb', line 37

def headers
  {'Authorization'=>"Bearer #{@token}"}
end

#object_symbolObject

Convert class name to symbol. eg: CloudPassage::Users –> :users



68
69
70
71
72
# File 'lib/cloudpassage/base.rb', line 68

def object_symbol
  class_name = self.class.name
  index = class_name.rindex(/::/)
  class_name[index + 2 .. -1].underscore.to_sym
end

#post(payload) ⇒ Object



58
59
60
# File 'lib/cloudpassage/base.rb', line 58

def post(payload)
  JSON.parse(@base_resource.post payload.to_json, headers)
end

#put(payload) ⇒ Object



62
63
64
# File 'lib/cloudpassage/base.rb', line 62

def put(payload)
  JSON.parse(@base_resource.put payload.to_json, headers)
end

#reloadObject



32
33
34
35
# File 'lib/cloudpassage/base.rb', line 32

def reload
  @data = JSON.parse(@base_resource.get(headers), :symbolize_names=>true)[object_symbol]
  self
end

#wait_for(options = {}, &block) ⇒ Object

Wait for block to evaluate to true. If specified, options can be used to override default options. Options should conform to rubygems.org/gems/wait



85
86
87
88
89
# File 'lib/cloudpassage/base.rb', line 85

def wait_for(options={}, &block)
  Wait.new(Cloudpassage::wait_options.merge(options)).until do
    instance_eval &block
  end
end