Module: Commerce

Defined in:
lib/commerce.rb

Overview

The Commerce module is used for quickly building documents and dispatching inbound requests

Defined Under Namespace

Modules: CommerceError Classes: BlockObject, Response

Class Method Summary collapse

Class Method Details

.debug(*args) ⇒ Object

Send debug messages



84
85
86
87
88
89
# File 'lib/commerce.rb', line 84

def self.debug(*args)
  if defined?(Rails)
    Rails.logger.debug *args if Rails.logger
    p *args if Rails.env && Rails.env.development?
  end
end

.dispatch(xml, &block) ⇒ Object

Dispatch can be used to handle incoming cXML requests

E.g. Commerce.dispatch(request.raw_post) do

order_request do |order_request|
  # Process order request
  render xml: Commerce::Response.success
end

end



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/commerce.rb', line 54

def self.dispatch(xml, &block)
  raise CommerceError::CommerceError, "Missing xml" if xml.blank?
  raise CommerceError::CommerceError, "Missing dispatch block" unless block_given?

  cxml = CXML.parse(xml)
  request = cxml['Request']

  raise CommerceError::InvalidRequestError, "No request element" if request.nil?
  
  deployment_mode = request.delete('deploymentMode')
  id = request.delete('Id')

  raise CommerceError::InvalidRequestError, "Invalid request object: #{request}" if request.keys.count != 1

  request_type, request_item = request.first

  Commerce.debug [ 'Commerce::Dispatch', 'Received request item', request_type, request_item ]
  
  block_object = Commerce::BlockObject.new
  block_object.instance_eval(&block)
  
  processor = block_object.procs[request_type.underscore.to_sym]
  
  raise CommerceError::CommerceError, "Missing handler for #{request_type.underscore}" unless processor
  
  obj = block.binding.eval("self") # Grab self of caller
  obj.instance_exec(request_item, &processor)
end