Class: Myob::Api::Model::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/myob/api/models/base.rb

Constant Summary collapse

API_URL =
'https://api.myob.com/accountright/'

Instance Method Summary collapse

Constructor Details

#initialize(client, model_name) ⇒ Base

Returns a new instance of Base.



8
9
10
11
# File 'lib/myob/api/models/base.rb', line 8

def initialize(client, model_name)
  @client     = client
  @model_name = model_name || 'Base'
end

Instance Method Details

#all(query = nil) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/myob/api/models/base.rb', line 17

def all(query = nil)
  model_data = parse_response(@client.connection.get(self.url, {:headers => @client.headers}))
  if query
    return process_query(model_data, query)
  else
    return model_data
  end
end

#first(query = nil) ⇒ Object



30
31
32
33
# File 'lib/myob/api/models/base.rb', line 30

def first(query = nil)
  model_data = self.all(query)
  model_data[0] if model_data.length > 0
end

#get(query = nil) ⇒ Object



26
27
28
# File 'lib/myob/api/models/base.rb', line 26

def get(query = nil)
  all(query)
end

#model_routeObject



13
14
15
# File 'lib/myob/api/models/base.rb', line 13

def model_route
  @model_name.to_s
end

#new_record?(object) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/myob/api/models/base.rb', line 47

def new_record?(object)
  object["UID"].nil? || object["UID"] == ""
end

#save(object) ⇒ Object



35
36
37
# File 'lib/myob/api/models/base.rb', line 35

def save(object)
  new_record?(object) ? create(object) : update(object)
end

#url(object = nil) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/myob/api/models/base.rb', line 39

def url(object = nil)
  if self.model_route == ''
    "#{API_URL}"
  else
    "#{API_URL}#{@client.current_company_file[:id]}/#{self.model_route}#{"/#{object['UID']}" if object && object['UID']}"
  end
end