Class: Quickbooks::API

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

Constant Summary collapse

@@instances =
{}

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Support::QBXML

#cleanup_qbxml, #is_leaf_node?, #qbxml_class_defined?, #set_required_attributes

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.



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

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)
  @@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

returns the last created api instance



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

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

Instance Method Details

#clear_disk_cache(rebuild = false) ⇒ Object

Disk Cache



88
89
90
91
92
93
# File 'lib/quickbooks/api.rb', line 88

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



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

def container
  get_container_class
end

#find(class_name) ⇒ Object



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

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

#grep(keyword) ⇒ Object



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

def grep(keyword)
  keyword = keyword.to_s
  cached_classes.select { |c| to_attribute_name(c).include?(keyword) }
end

#hash_to_obj(data) ⇒ Object

RUBY 2 QBXML

Raises:

  • (RuntimeError)


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

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

  key_path = find_nested_key(container.template(true), key.to_s)
  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



82
83
84
# File 'lib/quickbooks/api.rb', line 82

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

#qbxml_classesObject



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

def qbxml_classes
  cached_classes
end

#qbxml_to_hash(qbxml, include_container = false) ⇒ Object



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

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



50
51
52
53
54
55
56
57
# File 'lib/quickbooks/api.rb', line 50

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