Class: ZuoraClient

Inherits:
Object
  • Object
show all
Defined in:
lib/zuora_client.rb

Constant Summary collapse

PROD_URL =
'https://www.zuora.com/apps/services/a/27.0'
SANDBOX_URL =
'https://apisandbox.zuora.com/apps/services/a/27.0'

Instance Method Summary collapse

Constructor Details

#initialize(username, password, url = PROD_URL) ⇒ ZuoraClient

Returns a new instance of ZuoraClient.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/zuora_client.rb', line 49

def initialize(username, password, url=PROD_URL)
  $ZUORA_USER = username
  $ZUORA_PASSWORD = password
  $ZUORA_ENDPOINT = url

  @client = ZuoraInterface.new

  # add custom fields, if any
  custom_fields = YAML.load_file(File.dirname(__FILE__) + '/../custom_fields.yml')
  if custom_fields
    custom_fields.each do |key, value|
      fields = value.strip.split(/\s+/).map { |e| "#{e}__c" }
      type_class = Object.const_get('ZUORA').const_get(key)
      fields.each do |field|
        custom_field = field.gsub(/^\w/) { |i| i.downcase }
        type_class.send :attr_accessor, custom_field
      end
    end
  end

  @client.session_start
end

Instance Method Details

#create(obj) ⇒ Object



107
108
109
110
111
112
113
114
115
# File 'lib/zuora_client.rb', line 107

def create(obj)
  begin
    response = @client.create(obj)
    result = save_results_to_hash(response)
  rescue Exception => e
    puts e.message
  end
  result || []
end

#delete(type, ids) ⇒ Object



137
138
139
140
141
142
143
144
145
# File 'lib/zuora_client.rb', line 137

def delete(type, ids)
  begin
    response = @client.delete(type, ids)
    result = save_results_to_hash(response)
  rescue Exception => e
    puts e.message
  end
  result || []
end

#generate(obj) ⇒ Object



117
118
119
120
121
122
123
124
125
# File 'lib/zuora_client.rb', line 117

def generate(obj)
  begin
    response = @client.generate(obj)
    result = save_results_to_hash(response)
  rescue Exception => e
    puts e.message
  end
  result || []
end

#query(query_string) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/zuora_client.rb', line 72

def query(query_string)

  query_string =~ /select\s+(.+)\s+from/i
  fields = ($1.split /,\s+/).map do |f|
    f.gsub!(/\b\w/) { $&.downcase }
  end

  begin
    response = @client.query(query_string)
  rescue Exception => e
    puts e.message
  end

  result = []
  if response && response.result && response.result.size > 0
    response.result.records.each do |record|
      row = {}
      fields.each do |f|
        row[f] = record.send(f)
      end
      result << row
    end
  end
  result
end

#subscribe(obj) ⇒ Object



98
99
100
101
102
103
104
105
# File 'lib/zuora_client.rb', line 98

def subscribe(obj)
  begin
    response = @client.subscribe(obj)
    return response
  rescue Exception => e
    puts e.message
  end
end

#update(obj) ⇒ Object



127
128
129
130
131
132
133
134
135
# File 'lib/zuora_client.rb', line 127

def update(obj)
  begin
    response = @client.update(obj)
    result = save_results_to_hash(response)
  rescue Exception => e
    puts e.message
  end
  result || []
end