Class: Fastbill::Automatic::UsageData

Inherits:
Base
  • Object
show all
Defined in:
lib/fastbill-automatic/usage_data.rb

Constant Summary collapse

ATTRIBUTES =
[
  :subscription_id,
  :customer_id,
  :article_number,
  :unit_price,
  :description,
  :currency_code,
  :status,
  :usage_date,
  :created,
  :quantity,
  :usagedata_id
]

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

clear_request_infos, #initialize, request_infos, request_infos=, #set_attributes

Methods included from Services::Create

included

Methods included from Services::Get

included

Constructor Details

This class inherits a constructor from Fastbill::Automatic::Base

Class Method Details

.deleteusagedata(delete_params) ⇒ Object



153
154
155
# File 'lib/fastbill-automatic/usage_data.rb', line 153

def deleteusagedata(delete_params)
  response = Fastbill::Automatic.request("subscription.deleteusagedata", delete_params)
end

.find_by_usage_date(subscription_id, time) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# 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) ⇒ Object



77
78
79
80
81
82
# 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

.getusagedata(get_params) ⇒ Object



149
150
151
# 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) ⇒ Object



141
142
143
# 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) ⇒ Object



145
146
147
# 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) ⇒ Object



157
158
159
160
161
162
163
164
165
# 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

Instance Method Details

#==(other) ⇒ Object



105
106
107
# File 'lib/fastbill-automatic/usage_data.rb', line 105

def ==(other)
  other.attributes == self.attributes
end

#attributesObject



21
22
23
# File 'lib/fastbill-automatic/usage_data.rb', line 21

def attributes
  Hash[*ATTRIBUTES.map{|a| [a, instance_variable_get("@#{a}")]}.flatten]
end

#createObject



49
50
51
52
# File 'lib/fastbill-automatic/usage_data.rb', line 49

def create
  save
  self
end

#createdObject



33
34
35
# File 'lib/fastbill-automatic/usage_data.rb', line 33

def created
  Time.parse(@created)
end

#currency_codeObject



41
42
43
44
45
46
47
# 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

#deleteObject



54
55
56
# File 'lib/fastbill-automatic/usage_data.rb', line 54

def delete
  UsageData.deleteusagedata(delete_params)
end

#quantityObject



37
38
39
# File 'lib/fastbill-automatic/usage_data.rb', line 37

def quantity
  @quantity.to_i
end

#saveObject



64
65
66
67
68
69
70
71
72
73
74
75
# 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_priceObject



25
26
27
# File 'lib/fastbill-automatic/usage_data.rb', line 25

def unit_price
  @unit_price.to_f
end

#update_attributes(attributes) ⇒ Object



58
59
60
61
62
# File 'lib/fastbill-automatic/usage_data.rb', line 58

def update_attributes(attributes)
  initialize(UsageData.prepare_attributes(attributes.merge(attributes)))
  save
  self
end

#usage_dateObject



29
30
31
# File 'lib/fastbill-automatic/usage_data.rb', line 29

def usage_date
  Time.parse(@usage_date)
end