Class: BadBill::BaseResource

Inherits:
Object
  • Object
show all
Extended by:
Resource
Includes:
ForwardMethods, Resource
Defined in:
lib/badbill/base_resource.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Resource

call, get, post, put

Methods included from ForwardMethods

#method_missing, #respond_to?

Constructor Details

#initialize(id, data) ⇒ Resource

Public: Create new resource object.

All resources have an ID and data. Methods are proxied to the data object.

Parameters:

  • id (String, Integer)

    The ID of the resource.

  • data (Hashie::Mash)

    Resource data.



23
24
25
26
# File 'lib/badbill/base_resource.rb', line 23

def initialize id, data
  @id   = id
  @data = data
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class BadBill::ForwardMethods

Instance Attribute Details

#dataObject

All data in a Hash.



13
14
15
# File 'lib/badbill/base_resource.rb', line 13

def data
  @data
end

#idObject

ID of the resource.



11
12
13
# File 'lib/badbill/base_resource.rb', line 11

def id
  @id
end

Class Method Details

.all(filter = {}) ⇒ Array<Resource>

Get all resources of this type.

Parameters:

  • filter (Hash) (defaults to: {})

    An Hash of filter parameters, see the online documentation for allowed values.

Returns:

  • (Array<Resource>)

    All found resources.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/badbill/base_resource.rb', line 34

def self.all filter={}
  all = get resource_name, filter
  return all if all.error

  resources = all.__send__(resource_name)
  case resources['@total'].to_i
  when 0
    []
  when 1
    data = resources.__send__(resource_name_singular)
    [new(data.id.to_i, data)]
  else
    resources.__send__(resource_name_singular).map do |res|
      new res.id.to_i, res
    end
  end
end

.create(params) ⇒ Resource

Create a new resource with the given parameters.

Parameters:

  • params (Hash)

    A Hash-like object of data. See the online documentation for allowed values.

Returns:

  • (Resource)

    New resource with id and data set.



78
79
80
81
82
83
84
# File 'lib/badbill/base_resource.rb', line 78

def self.create params
  res = post(resource_name, {resource_name_singular => params})
  return res if res.error

  res_data = res.__send__(resource_name_singular)
  new res_data.id.to_i, res_data
end

.find(id) ⇒ Resource

Get the resource with the given ID.

Parameters:

  • id (String, Integer)

    ID of the resource.

Returns:

  • (Resource)

    New resource with id and data set.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/badbill/base_resource.rb', line 57

def self.find id
  c = get resource_name, id
  if c.error
    if c.error.kind_of? Faraday::Error::ResourceNotFound
      return nil
    else
      return c
    end
  end

  data = c.__send__(resource_name_singular)
  return nil if data.nil?
  new id, data
end

Instance Method Details

#deleteBoolean

Delete this resource.

Returns:

  • (Boolean)

    True if successfull, false otherwise.



109
110
111
112
113
# File 'lib/badbill/base_resource.rb', line 109

def delete
  # Hack: We can't call #delete here, because we ARE #delete
  resp = call resource_name, id, nil, :delete
  !resp
end

#errorObject



122
123
124
125
126
127
128
# File 'lib/badbill/base_resource.rb', line 122

def error
  if @data.respond_to?(:error)
    @data.error
  else
    nil
  end
end

#saveResource

Save any changed data.

Returns:

  • (Resource)

    The instance with changed values or the error object.



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/badbill/base_resource.rb', line 89

def save
  # Only save
  if @__mutated__ && !@__mutated__.empty?
    data = Hash[@__mutated__.map { |k| [k, @data[k]] }]

    res = put resource_name, id, {resource_name_singular => data}

    return res if res.error

    res_data = res.__send__(resource_name_singular)
    @data.merge!(res_data)
    @__mutated__.clear
  end

  self
end