Class: Mousetrap::Resource

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/mousetrap/resource.rb

Direct Known Subclasses

Customer, Invoice, Plan, Subscription

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ Resource

Returns a new instance of Resource.



7
8
9
10
11
# File 'lib/mousetrap/resource.rb', line 7

def initialize(hash={})
  hash.each do |key, value|
    self.send("#{key}=", value)
  end
end

Class Method Details

.[](code) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/mousetrap/resource.rb', line 13

def self.[](code)
  # Example error message:
  #
  # { "error" => "Resource not found: Customer not found for
  #    code=cantfindme within productCode=MOUSETRAP_TEST"}

  response = get_resource plural_resource_name, code

  if response['error']
    if response['error'] =~ /not found/
      return nil
    else
      raise response['error']
    end
  end

  build_resource_from response
end

.destroy_allObject



32
33
34
# File 'lib/mousetrap/resource.rb', line 32

def self.destroy_all
  all.each { |object| object.destroy }
end

.exists?(code) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/mousetrap/resource.rb', line 36

def self.exists?(code)
  !self[code].nil?
end

.new_from_api(attributes) ⇒ Object



40
41
42
# File 'lib/mousetrap/resource.rb', line 40

def self.new_from_api(attributes)
  new(attributes_from_api(attributes))
end

Instance Method Details

#destroyObject



44
45
46
# File 'lib/mousetrap/resource.rb', line 44

def destroy
  member_action 'delete' unless new_record?
end

#exists?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/mousetrap/resource.rb', line 48

def exists?
  self.class.exists?(code)
end

#new?Boolean Also known as: new_record?

Returns:

  • (Boolean)


52
53
54
# File 'lib/mousetrap/resource.rb', line 52

def new?
  id.nil?
end