Class: CheddarGetter::Response

Inherits:
Object
  • Object
show all
Includes:
LibXML
Defined in:
lib/cheddargetter-client.rb

Instance Method Summary collapse

Constructor Details

#initialize(xml) ⇒ Response

Returns a new instance of Response.



110
111
112
113
# File 'lib/cheddargetter-client.rb', line 110

def initialize(xml)
	@document = XML::Document.string(xml)
	@responseType = @document.root.name
end

Instance Method Details

#customer(code = nil) ⇒ Object



157
158
159
160
161
162
163
164
165
# File 'lib/cheddargetter-client.rb', line 157

def customer(code = nil)
	raise ArgumentException 'Can\'t get a customer from a response that isn\'t of type \'customers\'' if @responseType != 'customers'
	raise ArgumentException 'This response contains more than one customer so you need to provide the code for the customer you wish to get' if code.blank? && @document.root.children.length > 1
	
	if code.blank?
		return to_a.first
	end
	return to_a[code]
end

#customerInvoice(code = nil) ⇒ Object



177
178
179
180
# File 'lib/cheddargetter-client.rb', line 177

def customerInvoice(code = nil)
	subscription = customerSubscription(code)
	return subscription['invoices'].first
end

#customerItemQuantity(code = nil, itemCode = nil) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/cheddargetter-client.rb', line 182

def customerItemQuantity(code = nil, itemCode = nil)
	subscription = customerSubscription(code)
	raise ArgumentException 'This customer\'s subscription contains more than one item so you need to provide the code for the item you wish to get' if itemCode.blank? && subscription['items'].length > 1
	plan = customerPlan(code)
	if !itemCode.blank?
		return {
			'item' => plan['items'][itemCode],
			'quantity' => subscription['items'][itemCode]
		}
	else
		return {
			'item' => plan['items'].first,
			'quantity' => subscription['items'].first
		}
	end
end

#customerPlan(code = nil) ⇒ Object



172
173
174
175
# File 'lib/cheddargetter-client.rb', line 172

def customerPlan(code = nil)
	subscription = customerSubscription(code)
	return subscription['plans'].first
end

#customerSubscription(code = nil) ⇒ Object



167
168
169
170
# File 'lib/cheddargetter-client.rb', line 167

def customerSubscription(code = nil)
	customer = customer(code)
	return customer['subscriptions'].first
end

#plan(code = nil) ⇒ Object



137
138
139
140
141
142
143
144
145
# File 'lib/cheddargetter-client.rb', line 137

def plan(code = nil)
	raise ArgumentException 'Can\'t get a plan from a response that isn\'t of type \'plans\'' if @responseType != 'plans'
	raise 'This response contains more than one plan so you need to provide the code for the plan you wish to get' if code.blank? && @document.root.children.length > 1
	
	if code.blank?
		return to_a.first
	end
	return to_a[code]
end

#planItem(code = nil, itemCode = nil) ⇒ Object



147
148
149
150
151
152
153
154
155
# File 'lib/cheddargetter-client.rb', line 147

def planItem(code = nil, itemCode = nil)
	plan = plan(code)
	raise ArgumentException 'This plan contains more than one item so you need to provide the code for the item you wish to get' if itemCode.blank? && plan['items'].length > 1
	
	if itemCode.blank?
		return plan['items'].first
	end
	return plan['items'][itemCode]
end

#to_aObject



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/cheddargetter-client.rb', line 119

def to_a
	if (@array != nil)
		return @array
	end
	
	if (@responseType == 'error') 
		return [
			{
				'code' => @document.root.attributes.get_attribute('code').value,
				'message' => @document.root.first.content
			}
		]
	end
	@array = _toArray(@document.root.children);
	return @array;
	
end

#to_sObject



115
116
117
# File 'lib/cheddargetter-client.rb', line 115

def to_s
	return @document.to_s
end