Class: Quickbooks::API

Inherits:
Object show all
Includes:
Config, Logger, Support::Inflection
Defined in:
lib/quickbooks/api.rb

Constant Summary collapse

@@instances =
{}

Constants included from Config

Config::API_ROOT, Config::RUBY_SCHEMA_PATH, Config::SCHEMA_MAP, Config::XML_SCHEMA_PATH

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Support::Inflection

#underscore

Methods included from Config

included

Methods included from Logger

#log

Constructor Details

#initialize(schema_type = nil, opts = {}) ⇒ API

Returns a new instance of API.



10
11
12
13
14
15
16
17
18
19
# File 'lib/quickbooks/api.rb', line 10

def initialize(schema_type = nil, opts = {})
  self.class.check_schema_type!(schema_type)
  @schema_type = schema_type

  @dtd_parser = Quickbooks::DtdParser.new(schema_type)
  @qbxml_parser = Quickbooks::QbxmlParser.new(schema_type)

  load_qb_classes
  @@instances[schema_type] = self
end

Instance Attribute Details

#dtd_parserObject (readonly)

Returns the value of attribute dtd_parser.



6
7
8
# File 'lib/quickbooks/api.rb', line 6

def dtd_parser
  @dtd_parser
end

#qbxml_parserObject (readonly)

Returns the value of attribute qbxml_parser.



6
7
8
# File 'lib/quickbooks/api.rb', line 6

def qbxml_parser
  @qbxml_parser
end

#schema_typeObject (readonly)

Returns the value of attribute schema_type.



6
7
8
# File 'lib/quickbooks/api.rb', line 6

def schema_type
  @schema_type
end

Class Method Details

.[](schema_type) ⇒ Object

simple singleton constructor without caching support



23
24
25
# File 'lib/quickbooks/api.rb', line 23

def self.[](schema_type)
  @@instances[schema_type] || new(schema_type)
end

.instance(schema_type = nil, opts = {}) ⇒ Object

full singleton constructor



29
30
31
# File 'lib/quickbooks/api.rb', line 29

def self.instance(schema_type = nil, opts = {})
  @@instances[schema_type] || new(schema_type, opts)
end

Instance Method Details

#containerObject

user friendly api decorators. Not used anywhere else.



35
36
37
# File 'lib/quickbooks/api.rb', line 35

def container
  container_class
end

#find(class_name) ⇒ Object

api introspection



45
46
47
# File 'lib/quickbooks/api.rb', line 45

def find(class_name)
  cached_classes.find { |c| underscore(c) == class_name.to_s }
end

#grep(pattern) ⇒ Object



49
50
51
# File 'lib/quickbooks/api.rb', line 49

def grep(pattern)
  cached_classes.select { |c| underscore(c).match(/#{pattern}/) }
end

#hash_to_obj(data) ⇒ Object

RUBY 2 QBXML

Raises:

  • (RuntimeError)


69
70
71
72
73
74
75
76
# File 'lib/quickbooks/api.rb', line 69

def hash_to_obj(data)
  key, value = data.detect { |name, value| name != 'xml_attributes' && name != :xml_attributes }
  key_path = container_class.template(true).path_to_nested_key(key.to_s)
  raise(RuntimeError, "#{key} class not found in api template") unless key_path

  wrapped_data = Hash.nest(key_path, value)
  container_class.new(wrapped_data)
end

#hash_to_qbxml(data) ⇒ Object



78
79
80
# File 'lib/quickbooks/api.rb', line 78

def hash_to_qbxml(data)
  hash_to_obj(data).to_qbxml
end

#qbxml_classesObject



39
40
41
# File 'lib/quickbooks/api.rb', line 39

def qbxml_classes
  cached_classes
end

#qbxml_to_hash(qbxml, include_container = false) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/quickbooks/api.rb', line 59

def qbxml_to_hash(qbxml, include_container = false)
  if include_container
    qbxml_to_obj(qbxml).attributes
  else
    qbxml_to_obj(qbxml).inner_attributes
  end
end

#qbxml_to_obj(qbxml) ⇒ Object

QBXML 2 RUBY



55
56
57
# File 'lib/quickbooks/api.rb', line 55

def qbxml_to_obj(qbxml)
  qbxml_parser.parse(qbxml)
end