lib/kpm/account.rb in kpm-0.5.0 vs lib/kpm/account.rb in kpm-0.5.1
- old
+ new
@@ -1,10 +1,11 @@
require 'net/http'
require 'tmpdir'
require 'yaml'
require 'date'
require 'securerandom'
+require 'killbill_client'
module KPM
class Account
@@ -63,11 +64,11 @@
:tag_definitions => {:id => :tag_definitions_id},:tag_definition_history => {:id => :tag_definition_history_id},
:tags => {:id => :tags_id, :object_id => nil},
:tag_history => {:id => :tag_history_id, :object_id => nil},
:audit_log => {:id => :audit_log_id}
}
-
+
#delimeters to sniff
DELIMITERS = [',','|']
DEFAULT_DELIMITER = "|"
def initialize(config_file = nil, killbill_api_credentials = nil, killbill_credentials = nil, killbill_url = nil,
@@ -119,40 +120,34 @@
if not File.exist?(source_file)
raise Interrupt, 'Need to specify a valid file'
end
- @delimiter = sniff_delimiter(source_file) || @delimiter
+ @delimiter = sniff_delimiter(source_file) || @delimiter
sanitize_and_import(source_file, skip_payment_methods)
end
private
# export helpers: fetch_export_data; export; process_export_data; remove_export_data;
def fetch_export_data(account_id)
- uri = URI("#{@killbill_url}/#{KILLBILL_API_VERSION}/kb/export/#{account_id}")
+ KillBillClient.url = @killbill_url
+ options = {
+ :username => @killbill_user,
+ :password => @killbill_password,
+ :api_key => @killbill_api_key,
+ :api_secret => @killbill_api_secrets
+ }
- request = Net::HTTP::Get.new(uri.request_uri)
- request.basic_auth(@killbill_user,@killbill_password)
- request['X-Killbill-ApiKey'] = @killbill_api_key;
- request['X-Killbill-ApiSecret'] = @killbill_api_secrets;
- request['X-Killbill-CreatedBy'] = WHO;
-
- response = Net::HTTP.start(uri.host,uri.port) do |http|
- http.request(request)
- end
-
- if response.to_s.include? 'HTTPUnauthorized'
- raise Interrupt, "User is unauthorized -> \e[93mUser[#{@killbill_user}],password[#{@killbill_password}],api_key[#{@killbill_api_key}],api_secret[#{@killbill_api_secrets}]\e[0m"
- end
-
- if not response.is_a?(Net::HTTPSuccess)
+ begin
+ account_data = KillBillClient::Model::Export.find_by_account_id(account_id, 'KPM', options)
+ rescue Exception => e
raise Interrupt, 'Account id not found'
end
- response.body
+ account_data
end
def export(export_data)
export_file = TMP_DIR + File::SEPARATOR + 'kbdump'
@@ -378,16 +373,16 @@
end
end
def fix_dates(value)
if !value.equal?(:DEFAULT)
-
+
dt = DateTime.parse(value)
return dt.strftime('%F %T').to_s
-
+
end
-
+
value
end
def fill_empty_column(value)
if value.to_s.strip.empty?
@@ -428,26 +423,26 @@
value
end
def sniff_delimiter(file)
-
+
return nil if File.size?(file).nil?
-
+
first_line = File.open(file) {|f| f.readline}
-
+
return nil if first_line.nil?
-
+
sniff = {}
-
+
DELIMITERS.each do |delimiter|
sniff[delimiter] = first_line.count(delimiter)
end
-
+
sniff = sniff.sort {|a,b| b[1]<=>a[1]}
sniff.size > 0 ? sniff[0][0] : nil
end
-
+
# helper methods that set up killbill and database options: load_config_from_file; set_config; set_database_options;
# set_killbill_options;
def load_config_from_file(config_file)
set_config(config_file)
\ No newline at end of file