Module: ServiceCRUD

Instance Method Summary collapse

Instance Method Details

#action_url(object) ⇒ Object



52
53
54
# File 'lib/quickeebooks/common/service_crud.rb', line 52

def action_url(object)
  url = "#{url_for_resource(model.resource_for_singular)}/#{object.id.value}"
end

#create(object) ⇒ Object



3
4
5
6
7
8
9
10
11
12
# File 'lib/quickeebooks/common/service_crud.rb', line 3

def create(object)
  raise InvalidModelException.new(object.errors.full_messages.join(',')) unless object.valid?
  xml = object.to_xml_ns
  response = do_http_post(url_for_resource(model.resource_for_singular), valid_xml_document(xml))
  if response.code.to_i == 200
    model.from_xml(response.body)
  else
    nil
  end
end

#delete(object) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/quickeebooks/common/service_crud.rb', line 36

def delete(object)
  raise InvalidModelException.new("Missing required parameters for delete") unless object.valid_for_deletion?
  xml = valid_xml_document(object.to_xml_ns(:fields => ['Id', 'SyncToken']))
  url = action_url(object)
  response = do_http_post(url, xml, {:methodx => "delete"})
  response.code.to_i == 200
end

#fetch_by_id(id) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/quickeebooks/common/service_crud.rb', line 14

def fetch_by_id(id)
  url = "#{url_for_resource(model.resource_for_singular)}/#{id}"
  response = do_http_get(url)
  if response && response.code.to_i == 200
    model.from_xml(response.body)
  else
    nil
  end
end

#list(filters = [], page = 1, per_page = 20, sort = nil, options = {}) ⇒ Object



44
45
46
# File 'lib/quickeebooks/common/service_crud.rb', line 44

def list(filters = [], page = 1, per_page = 20, sort = nil, options = {})
  fetch_collection(model, filters, page, per_page, sort, options)
end

#modelObject



48
49
50
# File 'lib/quickeebooks/common/service_crud.rb', line 48

def model
  self.class.to_s.sub(/::Service::/,'::Model::').constantize
end

#update(object) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/quickeebooks/common/service_crud.rb', line 24

def update(object)
  raise InvalidModelException.new("Missing required parameters for update") unless object.valid_for_update?
  url = action_url(object)
  xml = object.to_xml_ns
  response = do_http_post(url, valid_xml_document(xml))
  if response.code.to_i == 200
    model.from_xml(response.body)
  else
    nil
  end
end