Skip to content

Commit

Permalink
Merge pull request #15 from Sage/rails_4_branch
Browse files Browse the repository at this point in the history
Rails 4
  • Loading branch information
ianoxley committed Jun 4, 2024
2 parents e4bac6c + e6dd01f commit 32729d8
Show file tree
Hide file tree
Showing 31 changed files with 420 additions and 53 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ doc
gemfiles/*.gemfile.lock
zuora.log
Gemfile.lock
coverage
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
source :rubygems

gem 'wasabi', :git => 'git://github.com/skiz/wasabi.git'
gem 'wasabi', '2.3.0'

gemspec

16 changes: 16 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Version Next

### Changes
* SOP Upgrade - rails 4.0

### Dependencies
* None

### Deploy tasks
* None

### Migration Details
* None

### Obfuscation PR
* None
1 change: 1 addition & 0 deletions lib/zuora.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module Zuora
require 'zuora/fault'
require 'zuora/session'
require 'zuora/api'
require 'zuora/api_failure'
require 'zuora/validations'
require 'zuora/associations'
require 'zuora/attributes'
Expand Down
15 changes: 15 additions & 0 deletions lib/zuora/api.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'savon'
require 'net/http'

module Zuora

Expand Down Expand Up @@ -67,6 +68,20 @@ def request(method, xml_body=nil, &block)
raise Zuora::Fault.new(:message => e.message)
end

def download(export)
authenticate! unless authenticated?

uri = URI(URI.join(config.download_url, export.file_id))
req = Net::HTTP::Get.new(uri.request_uri)
req['Authorization'] = 'ZSession ' + session.try(:key)

http = Net::HTTP.new(uri.hostname, uri.port)
http.use_ssl = true

res = http.start { |http| http.request(req) }
res.body
end

# Attempt to authenticate against Zuora and initialize the Zuora::Session object
#
# @note that the Zuora API requires username to come first in the SOAP request so
Expand Down
4 changes: 4 additions & 0 deletions lib/zuora/api_failure.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module Zuora
class ApiFailure < Exception
end
end
10 changes: 9 additions & 1 deletion lib/zuora/attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,14 @@ def attributes
class_variable_get(:@@all_attributes).map(&:to_sym)
end

def api_attr(lower)
if lower =~ /__c$/
lower.to_s[0..-4].camelize + '__c'
else
lower.to_s.camelize
end
end

# the name to use when referencing remote Zuora objects
def remote_name
self.name.base_name
Expand All @@ -137,7 +145,7 @@ def inherited(subclass)
xpath = "//xs:complexType[@name='#{subclass.remote_name}']//xs:sequence/xs:element"
document = Zuora::Api.instance.client.wsdl.parser.instance_variable_get('@document')
q = document.xpath(xpath, 's0' => 'http://schemas.xmlsoap.org/wsdl/', 'xs' => 'http://www.w3.org/2001/XMLSchema')
wsdl_attrs = (q.map{|e| e.attributes['name'].value.underscore.to_sym }) << :id
wsdl_attrs = (q.map{|e| e.attributes['name'].to_s.underscore.to_sym }) << :id
subclass.send(:class_variable_set, :@@wsdl_attributes, wsdl_attrs)
subclass.send(:class_variable_set, :@@read_only_attributes, [])
subclass.send(:class_variable_set, :@@default_attributes, {})
Expand Down
7 changes: 7 additions & 0 deletions lib/zuora/core_ext/string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ def base_name
dup.scan(/\w+$/).first
end unless method_defined?(:base_name)

def zuora_camelize
if match(/__c$/)
self.gsub("__c","").zuora_camelize + "__c"
else
camelize
end
end unless method_defined?(:zuora_camelize)
end
end
end
Expand Down
5 changes: 4 additions & 1 deletion lib/zuora/objects.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ module Zuora
module Objects
autoload :Base, 'zuora/objects/base'
autoload :Account, 'zuora/objects/account'
autoload :Contact, 'zuora/objects/contact'
autoload :Amendment, 'zuora/objects/amendment'
autoload :CommunicationProfile, 'zuora/objects/communication_profile'
autoload :Contact, 'zuora/objects/contact'
autoload :Export, 'zuora/objects/export'
# autoload :CreditBalanceAdjustment, 'zuora/objects/credit_balance_adjustment'
autoload :Invoice, 'zuora/objects/invoice'
autoload :InvoiceAdjustment, 'zuora/objects/invoice_adjustment'
Expand All @@ -23,6 +24,8 @@ module Objects
autoload :RatePlan, 'zuora/objects/rate_plan'
autoload :RatePlanCharge, 'zuora/objects/rate_plan_charge'
autoload :RatePlanChargeTier, 'zuora/objects/rate_plan_charge_tier'
autoload :RatePlanData, 'zuora/objects/rate_plan_data'
autoload :RatePlanChargeData, 'zuora/objects/rate_plan_charge_data'
# autoload :Refund, 'zuora/objects/refund'
end
end
2 changes: 1 addition & 1 deletion lib/zuora/objects/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Account < Base
belongs_to :bill_to, :class_name => 'Contact'
belongs_to :sold_to, :class_name => 'Contact'

validates_presence_of :account_number, :name, :status, :payment_term, :batch, :currency
validates_presence_of :name, :status, :payment_term, :batch, :currency
validates_length_of :name, :maximum => 50
validates_length_of :purchase_order_number, :maximum => 100, :allow_nil => true
validates_inclusion_of :payment_term, :in => ['Due Upon Receipt','Net 15','Net 30','Net 45','Net 90']
Expand Down
29 changes: 28 additions & 1 deletion lib/zuora/objects/amendment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,37 @@ class Amendment < Base
validates_inclusion_of :status, :in => ["Completed", "Cancelled", "Draft", "Pending Acceptance", "Pending Activation"]
validates_inclusion_of :term_type, :in => ['TERMED', 'EVERGREEN'], :allow_nil => true
validates_inclusion_of :type, :in => ['Cancellation', 'NewProduct', 'OwnerTransfer', 'RemoveProduct', 'Renewal', 'UpdateProduct', 'TermsAndConditions']
validates_presence_of :rate_plan_data, :if => Proc.new { |a| ['NewProduct', 'RemoveProduct', 'UpdateProduct'].include?(a.type) }, :only => :apply_percentage_discount

attr_accessor :amendment_ids
attr_accessor :invoice_id
attr_accessor :payment_transaction_number

define_attributes do
read_only :created_by_id, :created_date, :updated_by_id, :updated_date
read_only :created_by_id, :created_date, :updated_by_id, :updated_date, :amendment_ids, :invoice_id, :payment_transaction_number
defaults :status => 'Draft'
end

def apply_percentage_discount
self.status = 'Completed'
result = self.connector.amend({ 'process_payments' => false })
apply_percentage_discount_response(result.to_hash, :amend_response)
end

private

def apply_percentage_discount_response(response_hash, type)
result = response_hash[type][:results]
if result[:success]
self.amendment_ids = result[:amendment_ids]
self.invoice_id = result[:invoice_id]
self.payment_transaction_number = result[:payment_transaction_number]
@previously_changed = changes
@changed_attributes.clear
return true
else
raise StandardError.new(result[:errors][:message])
end
end
end
end
18 changes: 15 additions & 3 deletions lib/zuora/objects/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,17 @@ def ons
self.class.ons
end

# select fields to retrieve from query
def self.select(select)
@select = select
self
end

# locate objects using a custom where clause, currently arel
# is not supported as it requires an actual db connection to
# generate the sql queries. This may be overcome in the future.
def self.where(where)
keys = (attributes - unselectable_attributes).map(&:to_s).map(&:camelcase)
keys = self.select_attributes
if where.is_a?(Hash)
# FIXME: improper inject usage.
where = where.inject([]){|t,v| t << "#{v[0].to_s.camelcase} = '#{v[1]}'"}.sort.join(' and ')
Expand All @@ -83,6 +89,8 @@ def self.where(where)

result = self.connector.query(sql)

# reset @select after query to not propagate value for next queries
@select = []
generate(result.to_hash, :query_response)
end

Expand Down Expand Up @@ -167,11 +175,15 @@ def apply_response(response_hash, type)
@changed_attributes.clear
return true
else
self.errors.add(:base, result[:errors][:message])
return false
raise StandardError.new(result[:errors][:message])
end
end

def self.select_attributes
select_attributes = @select.present? ? @select : attributes
(select_attributes - unselectable_attributes).map(&:to_s).map { |a| api_attr(a) }
end

end
end

15 changes: 15 additions & 0 deletions lib/zuora/objects/export.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Zuora::Objects
class Export < Base
def download
connector.download(self)
end

def ready?
self.status == 'Completed'
end

define_attributes do
defaults :format => 'csv'
end
end
end
9 changes: 4 additions & 5 deletions lib/zuora/objects/payment_method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,12 @@ class PaymentMethod < Base
:created_date, :credit_card_mask_number, :last_failed_sale_transaction_date,
:last_transaction_status, :name, :num_consecutive_failures, :updated_date,
:total_number_of_error_payments, :total_number_of_processed_payments, :active,
:bank_city, :bank_code, :bank_name, :bank_postal_code, :bank_street_name,
:bank_street_number, :bank_transfer_account_name, :bank_transfer_account_number,
:bank_transfer_account_type, :bank_transfer_account_number, :bank_transfer_account_type,
:bank_transfer_type, :last_transaction_datetime, :payment_method_status
:bank_city, :bank_name, :bank_postal_code, :bank_street_name,
:bank_street_number, :bank_transfer_account_type, :last_transaction_datetime,
:payment_method_status

write_only :ach_account_number, :credit_card_number, :credit_card_security_code,
:gateway_option_data, :skip_validation
:gateway_option_data, :skip_validation, :bank_transfer_account_number

default_attributes :use_default_retry_rule => true
end
Expand Down
2 changes: 1 addition & 1 deletion lib/zuora/objects/rate_plan.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class RatePlan < Base
has_many :rate_plan_charges
belongs_to :subscription

validates_presence_of :name, :product_rate_plan_id, :subscription_id
validates_presence_of :product_rate_plan_id

validates_inclusion_of :amendment_type, :in => %w(Cancellation NewProduct OwnerTransfer RemoveProduct Renewal TermsAndConditions UpdateProduct), :allow_nil => true
validates_length_of :created_by_id, :maximum => 32, :allow_nil => true
Expand Down
12 changes: 12 additions & 0 deletions lib/zuora/objects/rate_plan_charge_data.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module Zuora::Objects
class RatePlanChargeData < Base
attr_accessor :rate_plan_charge

has_many :rate_plan_charge_tiers
belongs_to :rate_plan_data

validates_presence_of :rate_plan_charge

define_attributes {}
end
end
12 changes: 12 additions & 0 deletions lib/zuora/objects/rate_plan_data.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module Zuora::Objects
class RatePlanData < Base
attr_accessor :rate_plan

has_many :rate_plan_charge_data
belongs_to :amendment

validates_presence_of :rate_plan

define_attributes {}
end
end
1 change: 1 addition & 0 deletions lib/zuora/objects/subscribe_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def apply_response(response_hash, type)
result = response_hash[type][:result]
if result[:success]
subscription.id = result[:subscription_id]
subscription.name = result[:subscription_number]
subscription.clear_changed_attributes!
@previously_changed = changes
@changed_attributes.clear
Expand Down
4 changes: 4 additions & 0 deletions lib/zuora/objects/subscription.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ class Subscription < Base
belongs_to :ancestor_account, :class_name => 'Account'
belongs_to :creator_account, :class_name => 'Account'
belongs_to :creator_invoice_owner, :class_name => 'Account'
belongs_to :invoice_owner, :class_name => 'Account'
has_many :rate_plans
has_many :amendments

validates_presence_of :contract_effective_date, :initial_term,
:renewal_term
Expand All @@ -30,6 +32,8 @@ class Subscription < Base
:original_created_date, :original_id, :previous_subscription_id, :status,
:subscription_end_date, :term_end_date, :version, :subscription_start_date

write_only :ancestor_account_id

defaults :auto_renew => false,
:initial_term => 1,
:is_invoice_separate => false,
Expand Down
Loading

0 comments on commit 32729d8

Please sign in to comment.