Class: MfCloud::Invoice::Api::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/mf_cloud/invoice/api/base.rb

Direct Known Subclasses

Billing, Item, Office, Partner

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Base

Returns a new instance of Base.



33
34
35
# File 'lib/mf_cloud/invoice/api/base.rb', line 33

def initialize(client)
  @client = client
end

Class Method Details

.allowed_methodsObject



22
23
24
# File 'lib/mf_cloud/invoice/api/base.rb', line 22

def allowed_methods
  @allowed_methods ||= []
end

.collection_classObject



18
19
20
# File 'lib/mf_cloud/invoice/api/base.rb', line 18

def collection_class
  Object.const_get(collection_name)
end

.collection_nameObject



14
15
16
# File 'lib/mf_cloud/invoice/api/base.rb', line 14

def collection_name
  "#{name.sub('Api', 'Collection')}Collection"
end

.model_classObject



10
11
12
# File 'lib/mf_cloud/invoice/api/base.rb', line 10

def model_class
  Object.const_get(model_name)
end

.model_nameObject



6
7
8
# File 'lib/mf_cloud/invoice/api/base.rb', line 6

def model_name
  name.sub('Api', 'Model')
end

Instance Method Details

#all(params = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/mf_cloud/invoice/api/base.rb', line 37

def all(params = {})
  fail MethodNotAllowed, "#{self.class.name} has not allowed :all" unless self.class.allowed_methods.include? :all

  response_body = @client.get(self.class::PATH, params)
  self.class.collection_class.new(
    response_body[self.class::COLLECTION_KEY],
    response_body["meta"]
  )
end

#create(params) ⇒ Object



54
55
56
57
58
59
# File 'lib/mf_cloud/invoice/api/base.rb', line 54

def create(params)
  fail MethodNotAllowed, "#{self.class.name} has not allowed :create" unless self.class.allowed_methods.include? :create

  response_body = @client.post(self.class::PATH, self.class::BASE_NAME => params)
  self.class.model_class.new(response_body)
end

#delete(id) ⇒ Object



68
69
70
71
72
# File 'lib/mf_cloud/invoice/api/base.rb', line 68

def delete(id)
  fail MethodNotAllowed, "#{self.class.name} has not allowed :delete" unless self.class.allowed_methods.include? :delete

  @client.delete("#{self.class::PATH}/#{id}")
end

#get(id) ⇒ Object



47
48
49
50
51
52
# File 'lib/mf_cloud/invoice/api/base.rb', line 47

def get(id)
  fail MethodNotAllowed, "#{self.class.name} has not allowed :get" unless self.class.allowed_methods.include? :get

  response_body = @client.get("#{self.class::PATH}/#{id}")
  self.class.model_class.new(response_body)
end

#update(id, params) ⇒ Object



61
62
63
64
65
66
# File 'lib/mf_cloud/invoice/api/base.rb', line 61

def update(id, params)
  fail MethodNotAllowed, "#{self.class.name} has not allowed :update" unless self.class.allowed_methods.include? :update

  response_body = @client.put("#{self.class::PATH}/#{id}", self.class::BASE_NAME => params)
  self.class.model_class.new(response_body)
end