Class: Zuora::Client

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

Constant Summary collapse

API_VERSION =
"37.0"
PROD_URL =
"https://www.zuora.com/apps/services/a/#{API_VERSION}"
SANDBOX_URL =
"https://apisandbox.zuora.com/apps/services/a/#{API_VERSION}"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url = nil) ⇒ Client

Returns a new instance of Client.



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/zuora_client.rb', line 79

def initialize(url=nil)
  $ZUORA_USER = self.class.config["username"]
  $ZUORA_PASSWORD = self.class.config["password"]
  $ZUORA_ENDPOINT = url || self.class.app_url

  @client = ZuoraInterface.new

  # add custom fields, if any
  custom_fields = self.class.parse_custom_fields
  @client.session_start(custom_fields)
end

Class Method Details

.app_urlObject



51
52
53
# File 'lib/zuora_client.rb', line 51

def self.app_url
  SANDBOX_URL
end

.configObject



55
56
57
58
59
# File 'lib/zuora_client.rb', line 55

def self.config
  return @config_hash if @config_hash
  @config_hash = Zuora::CONFIG if defined?(Zuora::CONFIG)
  @config_hash ||= {}
end

.custom_fieldsObject



75
76
77
# File 'lib/zuora_client.rb', line 75

def self.custom_fields
  @custom_fields = YAML.load_file(File.dirname(__FILE__) + '/../custom_fields.yml')
end

.parse_custom_fieldsObject



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/zuora_client.rb', line 61

def self.parse_custom_fields
  if self.custom_fields
    self.custom_fields.each do |zobject, field_names|
      fields = field_names.map { |e| "#{e.strip}__c" }
      type_class = Object.const_get('ZUORA').const_get(zobject)
      fields.each do |field|
        custom_field = field.gsub(/^\w/) { |i| i.downcase }
        type_class.send :attr_accessor, custom_field
      end
    end
  end
  custom_fields
end

Instance Method Details

#amend(obj) ⇒ Object



161
162
163
# File 'lib/zuora_client.rb', line 161

def amend(obj)
  @client.api_call(:amend, obj)
end

#create(obj) ⇒ Object



121
122
123
124
125
126
127
128
129
# File 'lib/zuora_client.rb', line 121

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

#delete(type, ids) ⇒ Object



151
152
153
154
155
156
157
158
159
# File 'lib/zuora_client.rb', line 151

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

#generate(obj) ⇒ Object



131
132
133
134
135
136
137
138
139
# File 'lib/zuora_client.rb', line 131

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

#query(query_string) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/zuora_client.rb', line 91

def query(query_string)
  # This will keep calling queryMore until all the results are retreived.

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

  responses = []
  begin
    responses << @client.api_call(:query, query_string)
    until responses.last.result.done
      responses << @client.api_call(:query_more, responses.last.result.queryLocator)
    end
  rescue Exception => e
    $stderr.puts e.message
  end
  # Capture all the records into one single array.
  responses.map{ |response| response.result.size > 0 ? response.result.records : [] }.flatten
end

#subscribe(obj) ⇒ Object



112
113
114
115
116
117
118
119
# File 'lib/zuora_client.rb', line 112

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

#update(obj) ⇒ Object



141
142
143
144
145
146
147
148
149
# File 'lib/zuora_client.rb', line 141

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