Class: LegacyInfusionsoftApi::Model::Base

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

Instance Method Summary collapse

Constructor Details

#initialize(client, model_name) ⇒ Base

Returns a new instance of Base.



3
4
5
6
# File 'lib/legacy_infusionsoft_api/models/base.rb', line 3

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

Instance Method Details

#all(query = {}, paginate = true, per_page = 1000, page_number = 0) ⇒ Object



8
9
10
11
12
# File 'lib/legacy_infusionsoft_api/models/base.rb', line 8

def all(query = {}, paginate = true, per_page = 1000, page_number = 0)
  per_page = [per_page, 1000].min
  results = @client.connection.call("DataService.query", @client.api_key, self.table_name, per_page, page_number, query, self.fields)
  results.length > 0 && paginate ? results + self.all(query, true, per_page, page_number + 1) : results
end

#count(query = {}) ⇒ Object



39
40
41
# File 'lib/legacy_infusionsoft_api/models/base.rb', line 39

def count(query={})
  @client.connection.call("DataService.count", @client.api_key, self.table_name, query)
end

#create(data = {}) ⇒ Object



26
27
28
29
# File 'lib/legacy_infusionsoft_api/models/base.rb', line 26

def create(data = {})
  data["Id"] = @client.connection.call("DataService.add", @client.api_key, self.table_name, data)
  data
end

#delete(query) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/legacy_infusionsoft_api/models/base.rb', line 31

def delete(query)
  if query.is_a?(Hash)
    self.all(query).each { |data| self.delete(data["Id"]) }
  else
    @client.connection.call("DataService.delete", @client.api_key, self.table_name, query)
  end
end

#fieldsObject



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

def fields
  []
end

#find_each(query = {}, paginate = true, per_page = 1000, page_number = 0) {|results| ... } ⇒ Object

Yields:

  • (results)


14
15
16
17
18
19
20
# File 'lib/legacy_infusionsoft_api/models/base.rb', line 14

def find_each(query = {}, paginate = true, per_page = 1000, page_number = 0, &block)
  per_page = [per_page, 1000].min
  results = @client.connection.call("DataService.query", @client.api_key, self.table_name, per_page, page_number, query, self.fields)
  yield results
  return self.find_each(query, true, per_page, page_number + 1, &block) if results.length > 0 && paginate
  true
end

#first(query = {}) ⇒ Object



22
23
24
# File 'lib/legacy_infusionsoft_api/models/base.rb', line 22

def first(query = {})
  self.all(query, false)[0]
end

#table_nameObject



43
44
45
# File 'lib/legacy_infusionsoft_api/models/base.rb', line 43

def table_name
  @model_name
end