Module: Quickbooks::Service::ServiceCrud
- Included in:
- BaseService
- Defined in:
- lib/quickbooks/service/service_crud.rb
Instance Method Summary collapse
-
#all(object_query = nil, options = {}) ⇒ Object
fetch all records, returns an array of models.
- #create(entity, options = {}) ⇒ Object (also: #update)
- #delete(entity) ⇒ Object
- #delete_by_query_string(entity, options = {}) ⇒ Object
- #fetch_by_id(id, params = {}) ⇒ Object
- #find_by(field, selector, options = {}) ⇒ Object
- #query(object_query = nil, options = {}) ⇒ Object
- #query_in_batches(object_query = nil, options = {}) ⇒ Object
Instance Method Details
#all(object_query = nil, options = {}) ⇒ Object
fetch all records, returns an array of models
10 11 12 13 14 15 16 |
# File 'lib/quickbooks/service/service_crud.rb', line 10 def all(object_query=nil, ={}) collection = [] self.query_in_batches(object_query, ) do |batch| collection << batch.entries end collection.flatten end |
#create(entity, options = {}) ⇒ Object Also known as: update
41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/quickbooks/service/service_crud.rb', line 41 def create(entity, = {}) raise Quickbooks::InvalidModelException.new(entity.errors..join(',')) unless entity.valid? xml = entity.to_xml_ns() response = do_http_post(url_for_resource(model.resource_for_singular), valid_xml_document(xml), [:query]) if response.code.to_i == 200 model.from_xml(parse_singular_entity_response(model, response.plain_body)) else nil end end |
#delete(entity) ⇒ Object
54 55 56 |
# File 'lib/quickbooks/service/service_crud.rb', line 54 def delete(entity) raise NotImplementedError end |
#delete_by_query_string(entity, options = {}) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/quickbooks/service/service_crud.rb', line 58 def delete_by_query_string(entity, = {}) url = "#{url_for_resource(model::REST_RESOURCE)}?operation=delete" xml = entity.to_xml_ns() response = do_http_post(url, valid_xml_document(xml)) if response.code.to_i == 200 parse_singular_entity_response_for_delete(model, response.plain_body) else false end end |
#fetch_by_id(id, params = {}) ⇒ Object
36 37 38 39 |
# File 'lib/quickbooks/service/service_crud.rb', line 36 def fetch_by_id(id, params = {}) url = "#{url_for_resource(model.resource_for_singular)}/#{id}" fetch_object(model, url, params) end |
#find_by(field, selector, options = {}) ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/quickbooks/service/service_crud.rb', line 28 def find_by(field, selector, ={}) if field.class == Symbol field = field.to_s.camelcase end q = "select * from %s where %s = '%s'" % [model.resource_for_singular, field, selector] self.query(q, ) end |
#query(object_query = nil, options = {}) ⇒ Object
5 6 7 |
# File 'lib/quickbooks/service/service_crud.rb', line 5 def query(object_query = nil, = {}) fetch_collection(object_query, model, ) end |
#query_in_batches(object_query = nil, options = {}) ⇒ Object
18 19 20 21 22 23 24 25 26 |
# File 'lib/quickbooks/service/service_crud.rb', line 18 def query_in_batches(object_query=nil, ={}) page = 0 per_page = .delete(:per_page) || 1_000 begin page += 1 results = query(object_query, page: page, per_page: per_page) yield results if results.count > 0 end until results.count < per_page end |