class Fastbill::Automatic::UsageData
Constants
- ATTRIBUTES
Public Class Methods
find_by_usage_date(subscription_id, time)
click to toggle source
# File lib/fastbill-automatic/usage_data.rb, line 85 def self.find_by_usage_date(subscription_id, time) time = time.strftime("%Y-%m-%d %H:%M:%S") result = getusagedata(subscription_id: subscription_id, start: time, end: time) result_items = result['RESPONSE']['ITEMS'] last_item_attributes = result_items ? result_items.last : nil if last_item_attributes if last_item_attributes['CURRENCY_CODE'] == [] last_item_attributes['CURRENCY_CODE'] = '' end init_attributes = Hash[*last_item_attributes.map{|k,v| [k.downcase.to_sym, v]}.flatten] self.new(init_attributes) end end
get_remote_attributes(get_params)
click to toggle source
# File lib/fastbill-automatic/usage_data.rb, line 77 def self.get_remote_attributes(get_params) response = UsageData.getusagedata(get_params) binding.pry item_attributes = response['RESPONSE']['ITEMS'].last item_attributes end
Private Class Methods
deleteusagedata(delete_params)
click to toggle source
# File lib/fastbill-automatic/usage_data.rb, line 153 def deleteusagedata(delete_params) response = Fastbill::Automatic.request("subscription.deleteusagedata", delete_params) end
getusagedata(get_params)
click to toggle source
# File lib/fastbill-automatic/usage_data.rb, line 149 def getusagedata(get_params) response = Fastbill::Automatic.request("subscription.getusagedata", get_params) end
prepare_attributes(attributes)
click to toggle source
# File lib/fastbill-automatic/usage_data.rb, line 141 def prepare_attributes(attributes) Hash[*attributes.map{|k,v| [k.downcase.to_sym, v.to_s]}.flatten] end
setusagedata(set_params)
click to toggle source
# File lib/fastbill-automatic/usage_data.rb, line 145 def setusagedata(set_params) response = Fastbill::Automatic.request("subscription.setusagedata", set_params) end
where(get_params)
click to toggle source
# File lib/fastbill-automatic/usage_data.rb, line 157 def where(get_params) response = UsageData.getusagedata(get_params) result = [] items = response['RESPONSE']['ITEMS']||[] items.each do|item| result << UsageData.new(prepare_attributes(item)) end result end
Public Instance Methods
==(other)
click to toggle source
# File lib/fastbill-automatic/usage_data.rb, line 105 def ==(other) other.attributes == self.attributes end
attributes()
click to toggle source
# File lib/fastbill-automatic/usage_data.rb, line 21 def attributes Hash[*ATTRIBUTES.map{|a| [a, instance_variable_get("@#{a}")]}.flatten] end
create()
click to toggle source
# File lib/fastbill-automatic/usage_data.rb, line 49 def create save self end
created()
click to toggle source
# File lib/fastbill-automatic/usage_data.rb, line 33 def created Time.parse(@created) end
currency_code()
click to toggle source
# File lib/fastbill-automatic/usage_data.rb, line 41 def currency_code if @currency_code.is_a?(Array) @currency_code.join('') else @currency_code.to_s end end
delete()
click to toggle source
# File lib/fastbill-automatic/usage_data.rb, line 54 def delete UsageData.deleteusagedata(delete_params) end
quantity()
click to toggle source
# File lib/fastbill-automatic/usage_data.rb, line 37 def quantity @quantity.to_i end
save()
click to toggle source
# File lib/fastbill-automatic/usage_data.rb, line 64 def save UsageData.deleteusagedata(delete_params) if persisted? UsageData.setusagedata(set_params) if remote_attributes = UsageData.get_remote_attributes(get_params) initialize(remote_attributes) true else false end end
unit_price()
click to toggle source
# File lib/fastbill-automatic/usage_data.rb, line 25 def unit_price @unit_price.to_f end
update_attributes(attributes)
click to toggle source
# File lib/fastbill-automatic/usage_data.rb, line 58 def update_attributes(attributes) initialize(UsageData.prepare_attributes(attributes.merge(attributes))) save self end
usage_date()
click to toggle source
# File lib/fastbill-automatic/usage_data.rb, line 29 def usage_date Time.parse(@usage_date) end
Private Instance Methods
delete_params()
click to toggle source
# File lib/fastbill-automatic/usage_data.rb, line 133 def delete_params { usagedata_id: @usagedata_id } end
get_params()
click to toggle source
# File lib/fastbill-automatic/usage_data.rb, line 125 def get_params { subscription_id: self.subscription_id, start: @usage_date, end: @usage_date } end
persisted?()
click to toggle source
# File lib/fastbill-automatic/usage_data.rb, line 111 def persisted? if usagedata_id true else false end end
set_params()
click to toggle source
# File lib/fastbill-automatic/usage_data.rb, line 119 def set_params all_attributes = attributes all_attributes.delete(:usagedata_id) all_attributes end