cheddargetter_client_ruby 0.2.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.2.1
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{cheddargetter_client_ruby}
8
- s.version = "0.2.0"
8
+ s.version = "0.2.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Expected Behavior"]
12
- s.date = %q{2010-12-10}
12
+ s.date = %q{2010-12-11}
13
13
  s.description = %q{A CheddarGetter API wrapper for Ruby}
14
14
  s.email = %q{support@expectedbehavior.com}
15
15
  s.extra_rdoc_files = [
@@ -11,6 +11,11 @@ module CheddarGetter
11
11
  :errors => :error
12
12
  }
13
13
 
14
+ #not charges, because codes aren't unique
15
+ #not invoices, transactions, subscriptions, or errors because they don't have codes
16
+ ARRAY_TO_HASH_BY_CODE =
17
+ [:plans, :items, :customers]
18
+
14
19
  KEY_TO_DATA_TYPE = {
15
20
  :isActive => :boolean,
16
21
  :isFree => :boolean,
@@ -116,7 +121,7 @@ module CheddarGetter
116
121
  #
117
122
  #code must be provided if this response contains more than one customer.
118
123
  def customer_plan(code = nil)
119
- ((customer_subscription(code) || { })[:plans] || []).first
124
+ (customer_subscription(code) || { })[:plan]
120
125
  end
121
126
 
122
127
  #Returns the current open invoice for the given customer.
@@ -179,10 +184,8 @@ module CheddarGetter
179
184
  #
180
185
  #code must be provided if this response contains more than one customer.
181
186
  def customer_item(item_code = nil, code = nil)
182
- sub = customer_subscription(code)
183
- return nil unless sub
184
- sub_item = retrieve_item(sub, :items, item_code)
185
- plan_item = retrieve_item(retrieve_item(sub, :plans), :items, item_code)
187
+ sub_item = retrieve_item(customer_subscription(code), :items, item_code)
188
+ plan_item = retrieve_item(customer_plan(code), :items, item_code)
186
189
  return nil unless sub_item && plan_item
187
190
  item = plan_item.dup
188
191
  item[:quantity] = sub_item[:quantity]
@@ -231,12 +234,11 @@ module CheddarGetter
231
234
  end
232
235
 
233
236
  #access the root keys of the response directly, such as
234
- #:customers, :plans, or :error
237
+ #:customers, :plans, or :errors
235
238
  def [](value)
236
239
  self.clean_response[value]
237
240
  end
238
241
 
239
-
240
242
  private
241
243
  def deep_fix_array_keys!(data)
242
244
  if data.is_a?(Array)
@@ -290,60 +292,93 @@ module CheddarGetter
290
292
  end
291
293
  end
292
294
 
295
+ def make_plan_in_subscription_singular!(data)
296
+ return unless data[:customers]
297
+ data[:customers].each do |c|
298
+ c[:subscriptions].each do |s|
299
+ if s[:plans].size != 1
300
+ raise CheddarGetter::ResponseException.new("Should be exactly one plan in a subscription!")
301
+ end
302
+ s[:plan] = s[:plans].first
303
+ s.delete(:plans)
304
+ end
305
+ end
306
+ end
307
+
308
+ def switch_arrays_to_hashes!(data)
309
+ if data.is_a?(Array)
310
+ data.each do |v|
311
+ switch_arrays_to_hashes!(v)
312
+ end
313
+ elsif data.is_a?(Hash)
314
+ data.each do |k, v|
315
+ switch_arrays_to_hashes!(v)
316
+ if ARRAY_TO_HASH_BY_CODE.include?(k) && v.is_a?(Array)
317
+ new_hash = { }
318
+ v.each do |item|
319
+ new_hash[item[:code]] = item
320
+ end
321
+ data[k] = new_hash
322
+ end
323
+ end
324
+ end
325
+ end
326
+
293
327
  def reduce_clean_errors!(data)
294
328
  root_plural = [(data.delete(:errors) || { })[:error]].flatten
295
329
  root_singular = [data.delete(:error)]
296
330
  embed_singluar = []
297
331
  embed_plural = []
332
+
298
333
  embed = data[:customers] || data[:plans]
299
334
  if embed
300
335
  embed_singluar = [(embed.delete(:errors) || { })[:error]].flatten
301
336
  embed_plural = [embed.delete(:error)]
302
337
  end
338
+
303
339
  new_errors = (root_plural + root_singular + embed_plural + embed_singluar).compact
340
+
341
+ new_errors.each do |e|
342
+ aux_code = (e[:auxCode] || "")
343
+ if aux_code[":"]
344
+ split_aux_code = aux_code.split(':')
345
+ e[:fieldName] = split_aux_code.first
346
+ e[:errorType] = split_aux_code.last
347
+ end
348
+ end
349
+
304
350
  data[:errors] = new_errors
305
351
  end
306
352
 
307
353
  def create_clean_response
308
- data = self.raw_response.parsed_response.is_a?(Hash) ? self.raw_response.parsed_response : { }
309
- deep_symbolize_keys!(data)
310
- reduce_clean_errors!(data)
311
- deep_fix_array_keys!(data)
312
- deep_fix_data_types!(data)
313
- self.clean_response = data
354
+ data = { }
314
355
 
315
- #because Crack can:t get attributes and text at the same time. grrrr
316
- unless self.valid?
356
+ #if it isn't a hash, then we didn't get anything useful back.
357
+ if self.raw_response.parsed_response.is_a?(Hash)
358
+ #because Crack can:t get attributes and text at the same time.
359
+ #so we fix the error blocks and re-parse
317
360
  data = Crack::XML.parse(self.raw_response.body.gsub(/<error(.*)>(.*)<\/error>/,
318
361
  '<error\1><text>\2</text></error>'))
319
- deep_symbolize_keys!(data)
320
- reduce_clean_errors!(data)
321
- deep_fix_array_keys!(data)
322
- deep_fix_data_types!(data)
323
- self.clean_response = data
324
- self.errors.each do |e|
325
- aux_code = (e[:auxCode] || "")
326
- if aux_code[":"]
327
- split_aux_code = aux_code.split(':')
328
- e[:fieldName] = split_aux_code.first
329
- e[:errorType] = split_aux_code.last
330
- end
331
- end
332
-
333
362
  end
334
363
 
364
+ deep_symbolize_keys!(data)
365
+ reduce_clean_errors!(data)
366
+ deep_fix_array_keys!(data)
367
+ deep_fix_data_types!(data)
368
+ make_plan_in_subscription_singular!(data)
369
+ switch_arrays_to_hashes!(data)
370
+ self.clean_response = data
335
371
  end
336
372
 
337
- def retrieve_item(hash, type, code = nil)
338
- array = hash[type]
339
- if !array
373
+ def retrieve_item(root, type, code = nil)
374
+ hash = root[type]
375
+ if !hash
340
376
  raise CheddarGetter::ResponseException.new(
341
- "Can:t get #{type} from a response that doesn:t contain #{type}")
377
+ "Can:t get #{type} from a response that doesn't contain #{type}")
342
378
  elsif code
343
- code = code.to_s
344
- array.detect{ |p| p[:code].to_s == code }
345
- elsif array.size <= 1
346
- array.first
379
+ hash[code.to_s]
380
+ elsif hash.size <= 1
381
+ hash.first.last
347
382
  else
348
383
  raise CheddarGetter::ResponseException.new(
349
384
  "This response contains multiple #{type} so you need to provide the code for the one you wish to get")
@@ -291,7 +291,7 @@ class TestCheddargetterClientRuby < Test::Unit::TestCase
291
291
  assert_raises(CheddarGetter::ResponseException){ result.plan_items }
292
292
  assert_raises(CheddarGetter::ResponseException){ result.plan_item }
293
293
 
294
- assert_equal "Test Plan 2", result.customer_subscription(1)[:plans][0][:name]
294
+ assert_equal "Test Plan 2", result.customer_subscription(1)[:plan][:name]
295
295
  assert_equal 1, result.customer_subscriptions(1).count
296
296
  assert_equal "TEST_PLAN_2_RECURRING", result.customer_invoice(1)[:charges][0][:code]
297
297
  assert_equal 1, result.customer_invoices(1).count
@@ -348,7 +348,7 @@ class TestCheddargetterClientRuby < Test::Unit::TestCase
348
348
  assert_equal "5", result.customer(5)[:code]
349
349
  assert_equal "5", result.customer[:code]
350
350
 
351
- assert_equal "Test Plan 2", result.customer_subscription[:plans][0][:name]
351
+ assert_equal "Test Plan 2", result.customer_subscription[:plan][:name]
352
352
  assert_equal 1, result.customer_subscriptions.count
353
353
  assert_equal "Test Plan 2", result.customer_plan[:name]
354
354
  assert_equal "TEST_PLAN_2_RECURRING", result.customer_invoice[:charges][0][:code]
@@ -848,6 +848,14 @@ class TestCheddargetterClientRuby < Test::Unit::TestCase
848
848
  response.send(:deep_fix_data_types!, data)
849
849
  assert_equal "string", data[:test]
850
850
  CheddarGetter::Response::KEY_TO_DATA_TYPE.delete(:test)
851
+
852
+ assert_raises(CheddarGetter::ResponseException){
853
+ response.send(:make_plan_in_subscription_singular!,
854
+ {:customers => [:subscriptions => [:plans => []]]})}
855
+
856
+ assert_raises(CheddarGetter::ResponseException){
857
+ response.send(:make_plan_in_subscription_singular!,
858
+ {:customers => [:subscriptions => [:plans => [1,2]]]})}
851
859
  end
852
860
 
853
861
  should "test embedded errors" do
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cheddargetter_client_ruby
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 21
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 0
10
- version: 0.2.0
9
+ - 1
10
+ version: 0.2.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Expected Behavior
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-12-10 00:00:00 -05:00
18
+ date: 2010-12-11 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency