class IntacctRuby::Request

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

Constants

DEFAULTS
REQUIRED_AUTHENTICATION_KEYS

Attributes

functions[R]

Public Class Methods

new(*functions, request_params) click to toggle source
# 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

Public Instance Methods

send(opts = {}) click to toggle source
Calls superclass method
# 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_xml() click to toggle source
# 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

Private Instance Methods

authentication_block() click to toggle source
# File lib/intacct_ruby/request.rb, line 117
def authentication_block
  @request.authentication do
    @request.login do
      @request.userid    @opts[:userid]
      @request.companyid @opts[:companyid]
      @request.password  @opts[:user_password]
    end
  end
end
control_block() click to toggle source
# File lib/intacct_ruby/request.rb, line 103
def control_block
  @request.control do
    @request.senderid @opts[:senderid]
    @request.password @opts[:sender_password]

    # As recommended by Intacct API reference. This ID should be unique
    # to the call: it's used to associate a response with a request.
    @request.controlid          timestamp
    @request.uniqueid           @opts[:uniqueid]
    @request.dtdversion         @opts[:dtdversion]
    @request.includewhitespace  @opts[:includewhitespace]
  end
end
method_missing(method_name, *arguments, &block) click to toggle source
Calls superclass method
# 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
operation_block() click to toggle source
# File lib/intacct_ruby/request.rb, line 127
def operation_block
  @request.operation transaction: @opts[:transaction] do
    authentication_block
    @request.content do
      functions.each do |function|
        @request << function.to_xml
      end
    end
  end
end
respond_to_missing?(method_name, include_private = false) click to toggle source
Calls superclass method
# File lib/intacct_ruby/request.rb, line 75
def respond_to_missing?(method_name, include_private = false)
  Function::ALLOWED_TYPES.include?(method_name) || super
end
timestamp() click to toggle source
# File lib/intacct_ruby/request.rb, line 99
def timestamp
  @timestamp ||= Time.now.utc.to_s
end
validate_functions!() click to toggle source
# File lib/intacct_ruby/request.rb, line 79
def validate_functions!
  unless functions.any?
    raise Exceptions::EmptyRequestException,
          'a successful request must contain at least one function'
  end
end
validate_keys!() click to toggle source
# File lib/intacct_ruby/request.rb, line 86
def validate_keys!
  missing_keys = REQUIRED_AUTHENTICATION_KEYS - @opts.keys

  unless missing_keys.empty?
    missing_keys.map! { |s| ":#{s}" } # so they appear as symbols in output

    raise Exceptions::InsufficientCredentialsException,
          "[#{missing_keys.join(', ')}] required for a valid request. "
          'All authentication-related keys should be provided on ' \
          'instantiation in IntacctRuby::Request#new'
  end
end