Class: Quickbooks::Base

Inherits:
Object
  • Object
show all
Extended by:
Configuration
Includes:
Finders
Defined in:
lib/quickbooks/base.rb,
lib/quickbooks/base/finders.rb,
lib/quickbooks/base/configuration.rb

Defined Under Namespace

Modules: Configuration, Finders

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Configuration

configure, oauth_consumer, oauth_consumer=

Methods included from Finders

#display_name_sql, #find_by_display_name, #find_by_id, #qbuilder, #sql_builder

Constructor Details

#initialize(account, type = nil) ⇒ Base

Returns a new instance of Base.



11
12
13
14
# File 'lib/quickbooks/base.rb', line 11

def initialize(, type = nil)
  @account = 
  create_service_for(type) if type
end

Instance Attribute Details

#entityObject (readonly)

Returns the value of attribute entity.



7
8
9
# File 'lib/quickbooks/base.rb', line 7

def entity
  @entity
end

#serviceObject (readonly)

Returns the value of attribute service.



7
8
9
# File 'lib/quickbooks/base.rb', line 7

def service
  @service
end

Instance Method Details

#company_idObject



71
72
73
# File 'lib/quickbooks/base.rb', line 71

def company_id
  retrieve(:company_id)
end

#create_service_for(type) ⇒ Object



81
82
83
84
85
86
# File 'lib/quickbooks/base.rb', line 81

def create_service_for(type)
  @service = quickbooks_ruby_service(type)
  @service.access_token = oauth_client
  @service.company_id = company_id
  @service
end

#describing_methodObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/quickbooks/base.rb', line 48

def describing_method
  case @service.class.name
  when /(Item|TaxCode|PaymentMethod|Account)/
    'name'
  when /(Invoice|CreditMemo)/
    'doc_number'
  when /Payment/
    'total'
  when /(Vendor|Customer|Employee)/
    'display_name'
  else
    'txn_date'
  end
end

#description(e) ⇒ Object



42
43
44
45
46
# File 'lib/quickbooks/base.rb', line 42

def description(e)
  desc = (method = describing_method) =~ /(total)/ ? e.send(method).to_f : e.send(method)
rescue => e
  'nil'
end

#generate_quickbooks_ruby_namespace(entity, type = 'Model') ⇒ Object



16
17
18
19
# File 'lib/quickbooks/base.rb', line 16

def generate_quickbooks_ruby_namespace(entity, type = 'Model')
  @entity = entity.is_a?(Symbol) ? entity.to_s.camelcase : entity
  "Quickbooks::#{type}::#{@entity}"
end

#oauth_clientObject



31
32
33
# File 'lib/quickbooks/base.rb', line 31

def oauth_client
  @oauth_client ||= OAuth::AccessToken.new(Quickbooks::Base.oauth_consumer, token, secret)
end

#quickbooks_ruby_model(entity, *args) ⇒ Object Also known as: qr_model



21
22
23
# File 'lib/quickbooks/base.rb', line 21

def quickbooks_ruby_model(entity, *args)
  generate_quickbooks_ruby_namespace(entity, 'Model').constantize.new(*args)
end

#quickbooks_ruby_service(entity) ⇒ Object Also known as: qr_service



26
27
28
# File 'lib/quickbooks/base.rb', line 26

def quickbooks_ruby_service(entity)
  generate_quickbooks_ruby_namespace(entity, 'Service').constantize.new
end

#retrieve(type) ⇒ Object



75
76
77
78
79
# File 'lib/quickbooks/base.rb', line 75

def retrieve(type)
  meth = "persistent_#{type}"
  arr = Quickbooks::Base.send(meth).split('.')
  send_chain(arr)
end

#secretObject



67
68
69
# File 'lib/quickbooks/base.rb', line 67

def secret
  retrieve(:secret)
end

#show(options = {}) ⇒ Object



35
36
37
38
39
40
# File 'lib/quickbooks/base.rb', line 35

def show(options = {})
  options = { per_page: 20, page: 1 }.merge(options)
  @service.query(nil, options).entries.collect do |e|
    "QBID: #{e.id} DESC: #{description(e)}"
  end
end

#tokenObject



63
64
65
# File 'lib/quickbooks/base.rb', line 63

def token
  retrieve(:token)
end