Class: Zuora::ZObject

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/active_zuora/zobject.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ ZObject



3
4
5
6
7
8
9
# File 'lib/active_zuora/zobject.rb', line 3

def initialize(attributes={})
  super(self.class.zobject_class.new).tap do |zobject|
    attributes.each do |attr, value|
      zobject.send("#{attr}=", self.class.convert_date(value))
    end
  end
end

Class Method Details

.all(options = {}) ⇒ Object



140
141
142
143
# File 'lib/active_zuora/zobject.rb', line 140

def self.all(options={})
  zobjects = client.query("select #{query_attribute_names(options).join(", ")} from #{self.name.gsub(/Zuora::/,"")}")
  zobjects.map{|zobject| self.new_from_zobject zobject }
end

.attribute_namesObject

TODO: This sucks attributes need to be clearly defined



88
89
90
# File 'lib/active_zuora/zobject.rb', line 88

def self.attribute_names
  @attribute_names ||= zobject_class.instance_variable_get("@attributes")
end

.build_filter_statments(filter_statments) ⇒ Object



120
121
122
123
124
125
126
# File 'lib/active_zuora/zobject.rb', line 120

def self.build_filter_statments(filter_statments)
  filter_statments.map{|key, value|
    value = "'#{value}'" if value.kind_of?(String)
    value = "null" if value.nil?
    "#{key} = #{value}"
  }.join(" and ")
end

.clientObject



145
146
147
148
149
# File 'lib/active_zuora/zobject.rb', line 145

def self.client
  return @client if @client && self.valid_session?
  @session_start_time = Time.now
  @client = Zuora::Client.new
end

.convert_date(value) ⇒ Object

Any attributes that are Dates should be created as a DateTime in the PST timezone. Zuora doesn’t actually use the time portion of any Datetime field. All that matters is the date.



45
46
47
48
49
50
51
# File 'lib/active_zuora/zobject.rb', line 45

def self.convert_date(value)
  if value.is_a?(Date) || value.is_a?(DateTime) || value.is_a?(Time)
    DateTime.new(value.year, value.month, value.day, 0, 0, 0, "-0800")
  else
    value
  end
end

.create(attributes = {}) ⇒ Object



61
62
63
# File 'lib/active_zuora/zobject.rb', line 61

def self.create(attributes={})
  self.client.create([self.new(attributes).to_zobject])
end

.destroy(ids) ⇒ Object



57
58
59
# File 'lib/active_zuora/zobject.rb', line 57

def self.destroy(ids)
  self.client.delete(self.klass_name, [ids].flatten)
end

.exclude_query_attributes(*attributes) ⇒ Object



99
100
101
# File 'lib/active_zuora/zobject.rb', line 99

def self.exclude_query_attributes(*attributes)
  excluded_query_attributes.concat attributes
end

.excluded_query_attributesObject



103
104
105
# File 'lib/active_zuora/zobject.rb', line 103

def self.excluded_query_attributes
  @excluded_query_attributes ||= [:fieldsToNull]
end

.extra_attributes(attributes = []) ⇒ Object



107
108
109
# File 'lib/active_zuora/zobject.rb', line 107

def self.extra_attributes(attributes=[])
  attributes
end

.find(id) ⇒ Object



128
129
130
131
132
133
134
# File 'lib/active_zuora/zobject.rb', line 128

def self.find(id)
  nil unless valid_id(id)
  query = "select #{query_attribute_names(:include_extras => true).join(", ")} from #{self.name.gsub(/Zuora::/,"")} where Id = '#{id}'"
  puts query if $DEBUG
  zobject = client.query(query).first
  new_from_zobject zobject if zobject
end

.klass_nameObject



83
84
85
# File 'lib/active_zuora/zobject.rb', line 83

def self.klass_name
  @klass_name ||= name.split("::").last
end

.new_from_zobject(zobject) ⇒ Object



11
12
13
14
15
# File 'lib/active_zuora/zobject.rb', line 11

def self.new_from_zobject(zobject)
  self.new.tap do |active_zobject|
    active_zobject.__setobj__(zobject)
  end
end

.query_attribute_names(options = {}) ⇒ Object



92
93
94
95
96
97
# File 'lib/active_zuora/zobject.rb', line 92

def self.query_attribute_names(options={})
  excluded_attributes = []
  excluded_attributes.concat excluded_query_attributes unless options[:include_excluded]
  excluded_attributes.concat extra_attributes unless options[:include_extras]
  attribute_names - excluded_attributes
end

.update_attributes(attributes = {}) ⇒ Object



65
66
67
# File 'lib/active_zuora/zobject.rb', line 65

def self.update_attributes(attributes={})
  self.client.update([self.new(attributes).to_zobject])
end

.valid_id(id) ⇒ Object



136
137
138
# File 'lib/active_zuora/zobject.rb', line 136

def self.valid_id(id)
  id.to_s.size == 32 && id.hex.to_s(16) == id
end

.valid_session?Boolean



151
152
153
154
# File 'lib/active_zuora/zobject.rb', line 151

def self.valid_session?
  #session is valid if it has been running for less than 8 hours
  @session_start_time + 28800 > Time.now
end

.where(conditions = {}, options = {}) ⇒ Object



111
112
113
114
115
116
117
118
# File 'lib/active_zuora/zobject.rb', line 111

def self.where(conditions={}, options={})
  # You can give a hash or a string for the conditions
  conditions = build_filter_statments(conditions) if conditions.is_a? Hash
  query = "select #{self.query_attribute_names(options).join(", ")} from #{self.name.gsub(/Zuora::/,"")} where #{conditions}"
  puts query if $DEBUG
  zobjects = self.client.query(query)
  zobjects.map{|zobject| self.new_from_zobject zobject }
end

.zobject_classObject



69
70
71
72
73
74
75
76
77
# File 'lib/active_zuora/zobject.rb', line 69

def self.zobject_class
  return @zobject_class if @zobject_class

  if ZUORA.const_defined?(self.klass_name)
    @zobject_class = ZUORA.const_get(self.klass_name)
  else
    @zobject_class = self.superclass.respond_to?(:zobject_class) ? self.superclass.zobject_class : ZUORA.const_missing(self.klass_name)
  end
end

Instance Method Details

#==(another) ⇒ Object



29
30
31
# File 'lib/active_zuora/zobject.rb', line 29

def ==(another)
  id == another.id && another.respond_to?(:zobject_class) && zobject_class == another.zobject_class
end

#attributesObject



33
34
35
36
37
38
39
# File 'lib/active_zuora/zobject.rb', line 33

def attributes
  Hash.new.tap do |hash|
    self.class.attribute_names.each do |attr|
      hash[attr] = __getobj__.send(attr)
    end
  end
end

#destroyObject



53
54
55
# File 'lib/active_zuora/zobject.rb', line 53

def destroy
  self.class.destroy(id)
end

#idObject



21
22
23
# File 'lib/active_zuora/zobject.rb', line 21

def id
  __getobj__.id
end

#to_zobjectObject



17
18
19
# File 'lib/active_zuora/zobject.rb', line 17

def to_zobject
  __getobj__
end

#typeObject



25
26
27
# File 'lib/active_zuora/zobject.rb', line 25

def type
  __getobj__.type
end

#zobject_classObject



79
80
81
# File 'lib/active_zuora/zobject.rb', line 79

def zobject_class
  self.class.zobject_class
end