Class: IntacctRuby::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/intacct_ruby/request.rb

Overview

An outgoing request to the Intacct API. Can have multiple functions. Complete with send method that gets (and wraps) response from Intacct.

Constant Summary collapse

DEFAULTS =
{
  uniqueid: false,
  dtdversion: 3.0,
  includewhitespace: false,
  transaction: true
}.freeze
REQUIRED_AUTHENTICATION_KEYS =
[
  :senderid,
  :sender_password,
  :userid,
  :companyid,
  :user_password
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*functions, request_params) ⇒ Request

Returns a new instance of Request.



31
32
33
34
35
36
37
38
# File 'lib/intacct_ruby/request.rb', line 31

def initialize(*functions, request_params)
  # request_params should contain all req'd authentication information. If
  # not, an error will be thrown on #send
  @opts = DEFAULTS.dup.merge request_params

  # If a hash is provided + popped, the remaining attrs are functions
  @functions = functions
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *arguments, &block) ⇒ Object (private)



68
69
70
71
72
73
# File 'lib/intacct_ruby/request.rb', line 68

def method_missing(method_name, *arguments, &block)
  super unless Function::ALLOWED_TYPES.include? method_name.to_s

  # object_type must be the first argument in arguments
  @functions << Function.new(method_name, arguments.shift, *arguments)
end

Instance Attribute Details

#functionsObject (readonly)

Returns the value of attribute functions.



14
15
16
# File 'lib/intacct_ruby/request.rb', line 14

def functions
  @functions
end

Instance Method Details

#send(opts = {}) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/intacct_ruby/request.rb', line 52

def send(opts = {})
  if opts.is_a? Hash
    api = opts[:api] || Api.new

    validate_keys!
    validate_functions!

    Response.new api.send_request(self)
  else
    # so that Request#send can play nice alongside Object#send
    super
  end
end

#to_xmlObject



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/intacct_ruby/request.rb', line 40

def to_xml
  @to_xml ||= begin
    @request = Builder::XmlMarkup.new

    @request.instruct!
    @request.request do
      control_block
      operation_block
    end
  end
end