Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Refactored Soap in Z:O:Base to use a connector which can be replaced …

…in tests
  • Loading branch information...
commit 7fceacc8a54aa5140aeb3c5ecdf4d8e634fc1de5 1 parent 7254832
Jorge Alvarez authored
View
1  .gitignore
@@ -1,4 +1,5 @@
doc
+*.swp
.yardoc
.rvmrc
zuora.log
View
2  .rspec
@@ -0,0 +1,2 @@
+--color
+--format progress
View
8 Guardfile
@@ -0,0 +1,8 @@
+# A sample Guardfile
+# More info at https://github.com/guard/guard#readme
+
+guard 'rspec', :version => 2, :spec_paths => ['spec/zuora'] do
+ watch(%r{^spec/.+_spec\.rb$})
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
+ watch('spec/spec_helper.rb') { "spec" }
+end
View
1  lib/zuora.rb
@@ -12,4 +12,5 @@
require 'zuora/associations'
require 'zuora/attributes'
require 'zuora/objects'
+require 'zuora/soap_connector'
View
72 lib/zuora/objects/base.rb
@@ -111,9 +111,9 @@ def self.where(where)
where = where.inject([]){|t,v| t << "#{v[0].to_s.camelcase} = '#{v[1]}'"}.sort.join(' and ')
end
sql = "select #{keys.join(', ')} from #{remote_name} where #{where}"
- result = Zuora::Api.instance.request(:query) do |xml|
- xml.__send__(zns, :queryString, sql)
- end
+
+ result = self.connector.where(sql)
+
generate(result.to_hash, :query_response)
end
@@ -139,30 +139,12 @@ def save!
# create the record remotely
def create
- result = Zuora::Api.instance.request(:create) do |xml|
- xml.__send__(zns, :zObjects, 'xsi:type' => "#{ons}:#{remote_name}") do |a|
- self.to_hash.each do |k,v|
- a.__send__(ons, k.to_s.camelize.to_sym, v) unless v.nil?
- end
- generate_complex_objects(a, :create)
- end
- end
+ result = self.connector.create
apply_response(result.to_hash, :create_response)
end
def update
- result = Zuora::Api.instance.request(:update) do |xml|
- xml.__send__(zns, :zObjects, 'xsi:type' => "#{ons}:#{remote_name}") do |a|
- obj_attrs = self.to_hash
- obj_id = obj_attrs.delete(:id)
- a.__send__(ons, :Id, obj_id)
- change_syms = changed.map(&:to_sym)
- obj_attrs.reject{|k,v| read_only_attributes.include?(k) }.each do |k,v|
- a.__send__(ons, k.to_s.camelize.to_sym, v) if change_syms.include?(k)
- end
- generate_complex_objects(a, :update)
- end
- end
+ result = self.connector.update
result = apply_response(result.to_hash, :update_response)
reset_complex_object_cache
return result
@@ -170,39 +152,28 @@ def update
# destroy the remote object
def destroy
- result = Zuora::Api.instance.request(:delete) do |xml|
- xml.__send__(zns, :type, remote_name)
- xml.__send__(zns, :ids, id)
- end
+ result = self.connector.destroy
apply_response(result.to_hash, :delete_response)
end
- protected
+ def self.connector_class
+ @@connector_class ||= Zuora::SoapConnector
+ end
- # generate complex objects for inclusion when creating and updating records
- def generate_complex_objects(builder, action)
- complex_attributes.each do |var, scope|
- scope_element = scope.to_s.singularize.classify.to_sym
- var_element = var.to_s.classify.pluralize.to_sym
- builder.__send__(ons, var_element) do |td|
- self.send(scope).each do |object|
- td.__send__(zns, scope_element, 'xsi:type' => "#{ons}:#{scope_element}") do
- case action
- when :create
- object.to_hash.each do |k,v|
- td.__send__(ons, k.to_s.camelize.to_sym, v) unless v.nil?
- end
- when :update
- object.to_hash.reject{|k,v| object.read_only_attributes.include?(k) || object.restrain_attributes.include?(k) }.each do |k,v|
- td.__send__(ons, k.to_s.camelize.to_sym, v) unless v.nil?
- end
- end
- end
- end
- end
- end
+ def self.connector_class=(connector)
+ @@connector_class = connector
+ end
+
+ def self.connector
+ self.connector_class.new(self)
end
+ def connector
+ self.class.connector_class.new(self)
+ end
+
+ protected
+
# When remote data is loaded, remove the locally cached version of the
# complex objects so that they may be cleanly reloaded on demand.
def reset_complex_object_cache
@@ -231,6 +202,7 @@ def apply_response(response_hash, type)
return false
end
end
+
end
end
View
75 lib/zuora/soap_connector.rb
@@ -0,0 +1,75 @@
+module Zuora
+ class SoapConnector
+ attr_reader :model
+ delegate :ons, :zns, :remote_name, :id, :to => :model
+
+ def initialize(model)
+ @model = model
+ end
+
+ def where(sql)
+ Zuora::Api.instance.request(:query) do |xml|
+ xml.__send__(@model.zns, :queryString, sql)
+ end
+ end
+
+ def create
+ Zuora::Api.instance.request(:create) do |xml|
+ xml.__send__(zns, :zObjects, 'xsi:type' => "#{ons}:#{remote_name}") do |a|
+ @model.to_hash.each do |k,v|
+ a.__send__(ons, k.to_s.camelize.to_sym, v) unless v.nil?
+ end
+ generate_complex_objects(a, :create)
+ end
+ end
+ end
+
+ def update
+ Zuora::Api.instance.request(:update) do |xml|
+ xml.__send__(zns, :zObjects, 'xsi:type' => "#{ons}:#{remote_name}") do |a|
+ obj_attrs = @model.to_hash
+ obj_id = obj_attrs.delete(:id)
+ a.__send__(ons, :Id, obj_id)
+ change_syms = @model.changed.map(&:to_sym)
+ obj_attrs.reject{|k,v| @model.read_only_attributes.include?(k) }.each do |k,v|
+ a.__send__(ons, k.to_s.camelize.to_sym, v) if change_syms.include?(k)
+ end
+ generate_complex_objects(a, :update)
+ end
+ end
+ end
+
+ def destroy
+ Zuora::Api.instance.request(:delete) do |xml|
+ xml.__send__(zns, :type, remote_name)
+ xml.__send__(zns, :ids, id)
+ end
+ end
+
+ protected
+
+ # generate complex objects for inclusion when creating and updating records
+ def generate_complex_objects(builder, action)
+ @model.complex_attributes.each do |var, scope|
+ scope_element = scope.to_s.singularize.classify.to_sym
+ var_element = var.to_s.classify.pluralize.to_sym
+ builder.__send__(ons, var_element) do |td|
+ @model.send(scope).each do |object|
+ td.__send__(zns, scope_element, 'xsi:type' => "#{ons}:#{scope_element}") do
+ case action
+ when :create
+ object.to_hash.each do |k,v|
+ td.__send__(ons, k.to_s.camelize.to_sym, v) unless v.nil?
+ end
+ when :update
+ object.to_hash.reject{|k,v| object.read_only_attributes.include?(k) || object.restrain_attributes.include?(k) }.each do |k,v|
+ td.__send__(ons, k.to_s.camelize.to_sym, v) unless v.nil?
+ end
+ end
+ end
+ end
+ end
+ end
+ end
+ end
+end
View
24 spec/zuora/objects/base_spec.rb
@@ -0,0 +1,24 @@
+require 'spec_helper'
+
+class ExampleObject < Zuora::Objects::Base
+end
+
+class ExampleConnector
+ def initialize(model)
+ end
+end
+
+describe Zuora::Objects::Base do
+ describe :connector do
+ it "uses SoapConnector by default" do
+ ExampleObject.connector.should be_a Zuora::SoapConnector
+ end
+
+ it "allows injecting different class for tests" do
+ described_class.connector_class = ExampleConnector
+ ExampleObject.connector.should be_a ExampleConnector
+ #reset for subsequent tests
+ described_class.connector_class = Zuora::SoapConnector
+ end
+ end
+end
View
1  zuora.gemspec
@@ -24,6 +24,7 @@ Gem::Specification.new do |s|
s.add_runtime_dependency(%q<libxml4r>, ['~> 0.2.6'])
s.add_development_dependency(%q<rake>, ["~> 0.8.7"])
+ s.add_development_dependency(%q<guard-rspec>, ["~> 0.6.0"])
s.add_development_dependency(%q<artifice>, ["~> 0.6.0"])
s.add_development_dependency(%q<yard>, ["~> 0.7.5"])
s.add_development_dependency(%q<rspec>, ["~> 2.8.0"])
Please sign in to comment.
Something went wrong with that request. Please try again.