Class: Cloudpassage::Base

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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Base.



14
15
16
17
18
# File 'lib/cloudpassage/base.rb', line 14

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



36
37
38
39
40
41
42
# File 'lib/cloudpassage/base.rb', line 36

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



48
49
50
# File 'lib/cloudpassage/base.rb', line 48

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

#dataObject



20
21
22
23
24
25
# File 'lib/cloudpassage/base.rb', line 20

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

#headersObject



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

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

#object_symbolObject

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



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

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

#post(payload) ⇒ Object



52
53
54
# File 'lib/cloudpassage/base.rb', line 52

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

#reloadObject



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

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

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



64
65
66
67
68
69
# File 'lib/cloudpassage/base.rb', line 64

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