Class: Charging::Base

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

Direct Known Subclasses

ChargeAccount, Domain, Invoice

Constant Summary collapse

DEFAULT_PAGE =
1
DEFAULT_LIMIT =
10
COMMON_ATTRIBUTES =
[:uuid, :uri, :etag]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes, response) ⇒ Base

Returns a new instance of Base.



13
14
15
16
17
18
19
20
21
# File 'lib/charging/base.rb', line 13

def initialize(attributes, response)
  Helpers.load_variables(self, get_attributes, attributes)
  
  @last_response = response
  @errors = []
  @deleted = false
  
  normalize_etag!
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



10
11
12
# File 'lib/charging/base.rb', line 10

def errors
  @errors
end

#last_responseObject (readonly)

Returns the value of attribute last_response.



10
11
12
# File 'lib/charging/base.rb', line 10

def last_response
  @last_response
end

Class Method Details

.validate_attributes!(attributes) ⇒ Object

:nodoc:

Raises:

  • (ArgumentError)


72
73
74
75
76
# File 'lib/charging/base.rb', line 72

def self.validate_attributes!(attributes) # :nodoc:
  keys = attributes.keys.map(&:to_sym)
  diff = keys - (const_get(:ATTRIBUTES) + const_get(:READ_ONLY_ATTRIBUTES) + COMMON_ATTRIBUTES)
  raise ArgumentError, "Invalid attributes for #{self.name}: #{attributes.inspect}" if diff.any?
end

Instance Method Details

#attributesObject

Returns a hash with attributes



68
69
70
# File 'lib/charging/base.rb', line 68

def attributes
  Helpers.hashify(self, self.class::ATTRIBUTES)
end

#create!(&block) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/charging/base.rb', line 23

def create!(&block)
  execute_and_capture_raises_at_errors(201) do
    @last_response = block.call
  end
  
  self
end

#deleted?Boolean

Returns true if object already deleted on API

Returns:

  • (Boolean)


63
64
65
# File 'lib/charging/base.rb', line 63

def deleted?
  @deleted || false
end

#destroy!(&block) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/charging/base.rb', line 31

def destroy!(&block)
  execute_and_capture_raises_at_errors(204) do
    @last_response = block.call
  end
  
  if errors.empty?
    @deleted = true
    @persisted = false
  end
  
  self
end

#normalize_etag!Object



44
45
46
47
48
49
50
# File 'lib/charging/base.rb', line 44

def normalize_etag!
  if @etag.nil?
    @etag = last_response.headers[:etag] if last_response && last_response.code === 200
  else
    @etag = @etag.inspect
  end
end

#persisted?Boolean

Returns true if the object exists on Charging service.

Returns:

  • (Boolean)


53
54
55
# File 'lib/charging/base.rb', line 53

def persisted?
  (uuid && etag && uri && !deleted?) || false
end

#unpersisted?Boolean

Returns true if the object exists on Charging service.

Returns:

  • (Boolean)


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

def unpersisted?
  !persisted?
end