Skip to content
Browse files

Added where to connector, moved parse attrs to connector. SQL connect…

…or complete
  • Loading branch information...
1 parent 0ca40ab commit 944604a4a75a9295697b7cf66b41e57582201408 Jorge Alvarez committed Mar 20, 2012
Showing with 87 additions and 45 deletions.
  1. +3 −34 lib/zuora/objects/base.rb
  2. +32 −1 lib/zuora/soap_connector.rb
  3. +31 −8 lib/zuora/sqlite_connector.rb
  4. +21 −2 spec/zuora/sqlite_connector_spec.rb
View
37 lib/zuora/objects/base.rb
@@ -21,45 +21,14 @@ def self.generate(soap_hash, type)
result = soap_hash[type][:result]
return [] if result[:records] == nil
if result[:size].to_i == 1
- [(new parse_attributes(type, result[:records])).clear_changed_attributes!]
+ [(new self.connector.parse_attributes(type, result[:records])).clear_changed_attributes!]
else
result[:records].map do |record|
- (new parse_attributes(type, record)).clear_changed_attributes!
+ (new self.connector.parse_attributes(type, record)).clear_changed_attributes!
end
end
end
- # Remove empty attributes from response hash
- # and typecast any known types from the wsdl
- def self.parse_attributes(type, attrs={})
- # after quite a bit of upstream work, savon
- # still doesn't support using wsdl response
- # definitions, and only handles inline types.
- # This is a work in progress, and hopefully this
- # can be removed in the future via proper support.
- tdefs = Zuora::Api.instance.client.wsdl.type_definitions
- klass = attrs['@xsi:type'.to_sym].base_name
- if klass
- attrs.each do |a,v|
- ref = a.to_s.camelcase
- z = tdefs.find{|d| d[0] == [klass, ref] }
- if z
- case z[1]
- when 'integer', 'int' then
- attrs[a] = v.nil? ? nil : v.to_i
- when 'decimal' then
- attrs[a] = v.nil? ? nil : BigDecimal(v.to_s)
- when 'float', 'double' then
- attrs[a] = v.nil? ? nil : v.to_f
- end
- end
- end
- end
- #remove unknown attributes
- available = attributes.map(&:to_sym)
- attrs.delete_if {|k,v| !available.include?(k) }
- end
-
# find a record by the id
def self.find(id)
where(:id => id).first
@@ -112,7 +81,7 @@ def self.where(where)
end
sql = "select #{keys.join(', ')} from #{remote_name} where #{where}"
- result = self.connector.where(sql)
+ result = self.connector.query(sql)
generate(result.to_hash, :query_response)
end
View
33 lib/zuora/soap_connector.rb
@@ -7,7 +7,7 @@ def initialize(model)
@model = model
end
- def where(sql)
+ def query(sql)
Zuora::Api.instance.request(:query) do |xml|
xml.__send__(@model.zns, :queryString, sql)
end
@@ -46,6 +46,37 @@ def destroy
end
end
+ # Remove empty attributes from response hash
+ # and typecast any known types from the wsdl
+ def parse_attributes(type, attrs={})
+ # after quite a bit of upstream work, savon
+ # still doesn't support using wsdl response
+ # definitions, and only handles inline types.
+ # This is a work in progress, and hopefully this
+ # can be removed in the future via proper support.
+ tdefs = Zuora::Api.instance.client.wsdl.type_definitions
+ klass = attrs['@xsi:type'.to_sym].base_name
+ if klass
+ attrs.each do |a,v|
+ ref = a.to_s.camelcase
+ z = tdefs.find{|d| d[0] == [klass, ref] }
+ if z
+ case z[1]
+ when 'integer', 'int' then
+ attrs[a] = v.nil? ? nil : v.to_i
+ when 'decimal' then
+ attrs[a] = v.nil? ? nil : BigDecimal(v.to_s)
+ when 'float', 'double' then
+ attrs[a] = v.nil? ? nil : v.to_f
+ end
+ end
+ end
+ end
+ #remove unknown attributes
+ available = @model.attributes.map(&:to_sym)
+ attrs.delete_if {|k,v| !available.include?(k) }
+ end
+
protected
# generate complex objects for inclusion when creating and updating records
View
39 lib/zuora/sqlite_connector.rb
@@ -9,13 +9,27 @@ def initialize(model)
@model = model
end
+ def query(sql)
+ result = db.query sql
+ hashed_result = result.map {|r| hash_result_row(r, result) }
+ {
+ :query_response => {
+ :result => {
+ :success => true,
+ :size => result.count,
+ :records => hashed_result
+ }
+ }
+ }
+ end
+
def create
table = self.class.table_name(@model.class)
hash = @model.to_hash
keys = []
values = []
hash.each do |key, value|
- keys << key
+ keys << key.to_s.camelize
values << value
end
place_holder = ['?'] * keys.length
@@ -41,7 +55,7 @@ def update
keys = []
values = []
hash.each do |key, value|
- keys << "#{key}=?"
+ keys << "#{key.to_s.camelize}=?"
values << value
end
keys = keys.join(', ')
@@ -59,7 +73,7 @@ def update
def destroy
table = self.class.table_name(@model.class)
- destroy = "DELETE FROM '#{table}' WHERE id=?"
+ destroy = "DELETE FROM '#{table}' WHERE Id=?"
db.execute destroy, @model.id
{
:delete_response => {
@@ -71,19 +85,28 @@ def destroy
}
end
+ def parse_attributes(type, attrs = {})
+ data = attrs.to_a.map do |a|
+ key, value = a
+ [key.underscore, value]
+ end
+ Hash[data]
+ end
+
def self.build_schema
self.db = SQLite3::Database.new ":memory:"
self.generate_tables
end
def self.table_name(model)
- model.name.underscore.gsub '/', '_'
+ model.name.demodulize
end
protected
- def hash_result_row(row)
- Hash[row.columns.zip(row.first.to_a)]
+ def hash_result_row(row, result)
+ row = row.map {|r| r.nil? ? "" : r }
+ Hash[result.columns.zip(row.to_a)]
end
def self.generate_tables
@@ -96,9 +119,9 @@ def self.create_table(model)
table_name = self.table_name(model)
attributes = model.attributes - [:id]
attributes = attributes.map do |a|
- "'#{a}' text"
+ "'#{a.to_s.camelize}' text"
end
- autoid = "'id' integer PRIMARY KEY AUTOINCREMENT"
+ autoid = "'Id' integer PRIMARY KEY AUTOINCREMENT"
attributes.unshift autoid
attributes = attributes.join(", ")
schema = "CREATE TABLE 'main'.'#{table_name}' (#{attributes});"
View
23 spec/zuora/sqlite_connector_spec.rb
@@ -20,8 +20,9 @@
@models.each do |m|
table_name = described_class.table_name(m)
table = @db.table_info(table_name)
- columns = table.map {|t| t["name"].to_sym }
- (m.attributes - columns).should == []
+ columns = table.map {|t| t["name"] }
+ camel_attrs = m.attributes.map { |a| a.to_s.camelize }
+ (camel_attrs - columns).should == []
end
end
end
@@ -36,6 +37,24 @@
Zuora::Objects::Base.connector_class = old_class
end
+ describe :where do
+ before :each do
+ @model = Zuora::Objects::Product
+ @db = described_class.db
+
+ @product1 = @model.new :name => 'Product One'
+ @product1.create
+ @product2 = @model.new :name => 'Another One'
+ @product2.create
+ end
+
+ it "returns matching records" do
+ records = @model.where(:name => 'Product One')
+ records.first.name.should == @product1.name
+ records.first.id.should == @product1.id
+ end
+ end
+
describe :create do
before :each do
@model = Zuora::Objects::Product.new

0 comments on commit 944604a

Please sign in to comment.
Something went wrong with that request. Please try again.