Class: QBWC::Request

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request, response_proc) ⇒ Request

Returns a new instance of Request.



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

def initialize(request, response_proc)
  #Handle Cases for a request passed in as a Hash or String
  #If it's a hash verify that it is properly wrapped with qbxml_msg_rq and xml_attributes for on_error events
  #Allow strings of QBXML to be passed in directly. 
  case
  when request.is_a?(Hash)

    unless request.keys.include?(:qbxml_msgs_rq)
      wrapped_request = { :qbxml_msgs_rq => {:xml_attributes => {"onError"=> QBWC::on_error } } } 
      wrapped_request[:qbxml_msgs_rq] = wrapped_request[:qbxml_msgs_rq].merge(request)
      request = wrapped_request
    end

    @request = QBWC.parser.hash_to_qbxml(request)
  when request.is_a?(String)
    @request = request
  end
  @response_proc = response_proc
end

Instance Attribute Details

#errorObject

Returns the value of attribute error.



4
5
6
# File 'lib/qbwc/request.rb', line 4

def error
  @error
end

#requestObject (readonly)

Returns the value of attribute request.



3
4
5
# File 'lib/qbwc/request.rb', line 3

def request
  @request
end

#responseObject

Returns the value of attribute response.



4
5
6
# File 'lib/qbwc/request.rb', line 4

def response
  @response
end

#response_procObject (readonly)

Returns the value of attribute response_proc.



3
4
5
# File 'lib/qbwc/request.rb', line 3

def response_proc
  @response_proc
end

Class Method Details

.from_array(requests, response_proc) ⇒ Object



44
45
46
# File 'lib/qbwc/request.rb', line 44

def from_array(requests, response_proc)
  Array(requests).map { |r| new(r, response_proc) } 
end

Instance Method Details

#process_responseObject



26
27
28
# File 'lib/qbwc/request.rb', line 26

def process_response
  @response_proc && @response && @response_proc.call(response) 
end

#to_hashObject



34
35
36
# File 'lib/qbwc/request.rb', line 34

def to_hash
  QBWC.parser.qbxml_to_hash @request.to_s
end

#to_qbxmlObject



30
31
32
# File 'lib/qbwc/request.rb', line 30

def to_qbxml
  QBWC.parser.hash_to_qbxml(request)
end

#to_sObject



38
39
40
# File 'lib/qbwc/request.rb', line 38

def to_s
  @request
end