Class: Quickbooks::API

Inherits:
Object
  • Object
show all
Includes:
Support, Support::API, Support::QBXML
Defined in:
lib/quickbooks/api.rb

Constant Summary

Constants included from Support::QBXML

Support::QBXML::COMMENT_END, Support::QBXML::COMMENT_MATCHER, Support::QBXML::COMMENT_START, Support::QBXML::XML_COMMENT, Support::QBXML::XML_DOCUMENT, Support::QBXML::XML_ELEMENT, Support::QBXML::XML_NODE, Support::QBXML::XML_NODE_SET, Support::QBXML::XML_TEXT

Constants included from Support::API

Support::API::API_ROOT, Support::API::DEFAULT_LOG_LEVEL, Support::API::RUBY_SCHEMA_PATH, Support::API::SCHEMA_MAP, Support::API::XML_SCHEMA_PATH

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Support::QBXML

#cleanup_qbxml, #is_leaf_node?, #qbxml_class_defined?

Methods included from Support

#inflector, #log, #simple_class_name, #to_attribute_name

Constructor Details

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

Returns a new instance of API.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/quickbooks/api.rb', line 8

def initialize(schema_type = nil, opts = {})
  @schema_type = schema_type
  use_disk_cache, log_level = opts.values_at(:use_disk_cache, :log_level)

  unless valid_schema_type?
    raise(ArgumentError, "schema type required: #{valid_schema_types.inspect}") 
  end

  @dtd_file = get_dtd_file
  @dtd_parser = DtdParser.new(schema_type)
  @qbxml_parser = QbxmlParser.new(schema_type)

  load_qb_classes(use_disk_cache)

  # load the container class template into memory (significantly speeds up wrapping of partial data hashes)
  get_container_class.template(true, use_disk_cache)
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

Instance Method Details

#clear_disk_cache(rebuild = false) ⇒ Object

Disk Cache



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

def clear_disk_cache(rebuild = false)
  qbxml_cache = Dir["#{get_disk_cache_path}/*.rb"]
  template_cache = Dir["#{get_template_cache_path}/*.yml"]
  File.delete(*(qbxml_cache + template_cache))
  load_qb_classes(rebuild)
end

#containerObject



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

def container
  get_container_class
end

#hash_to_obj(data) ⇒ Object

RUBY 2 QBXML

Raises:

  • (RuntimeError)


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

def hash_to_obj(data)
  key = data.keys.first
  value = data[key]

  key_path = find_nested_key(container.template(true), key)
  raise(RuntimeError, "#{key} class not found in api template") unless key_path

  wrapped_data = build_hash_wrapper(key_path, value)
  container.new(wrapped_data)
end

#hash_to_qbxml(data) ⇒ Object



68
69
70
# File 'lib/quickbooks/api.rb', line 68

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

#qbxml_classesObject



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

def qbxml_classes
  cached_classes
end

#qbxml_to_hash(qbxml, include_container = false) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/quickbooks/api.rb', line 45

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

#qbxml_to_obj(qbxml) ⇒ Object

QBXML 2 RUBY



36
37
38
39
40
41
42
43
# File 'lib/quickbooks/api.rb', line 36

def qbxml_to_obj(qbxml)
  case qbxml
  when IO
    qbxml_parser.parse_file(qbxml)
  else
    qbxml_parser.parse(qbxml)
  end
end