Class: RubyPaypalNvp::Model::Statement

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_paypal_nvp/model/statement.rb

Constant Summary collapse

IGNORED_STATUSES =
%w[Cleared Placed Removed].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(result) ⇒ Statement

rubocop:disable Metrics/AbcSize rubocop:disable Metrics/MethodLength



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/ruby_paypal_nvp/model/statement.rb', line 14

def initialize(result)
  @timestamp = result[:meta]['timestamp']
  @start_date = result[:meta]['start_date']
  @end_date = result[:meta]['end_date']
  @subject = result[:meta]['subject']
  @currency_code = result[:meta]['currency_code']
  @items = result[:values].map do |value|
    Item.new(value) unless IGNORED_STATUSES.include?(value['L_STATUS'])
  end.compact.uniq(&:transaction_id)
  @items_count = @items.count
  @amount_sum = @items.sum(&:amount)
  @fee_amount_sum = @items.sum(&:fee_amount)
  @net_amount_sum = @items.sum(&:net_amount)
  @raw = result
end

Instance Attribute Details

#amount_sumObject

Returns the value of attribute amount_sum.



6
7
8
# File 'lib/ruby_paypal_nvp/model/statement.rb', line 6

def amount_sum
  @amount_sum
end

#currency_codeObject

Returns the value of attribute currency_code.



6
7
8
# File 'lib/ruby_paypal_nvp/model/statement.rb', line 6

def currency_code
  @currency_code
end

#end_dateObject

Returns the value of attribute end_date.



6
7
8
# File 'lib/ruby_paypal_nvp/model/statement.rb', line 6

def end_date
  @end_date
end

#fee_amount_sumObject

Returns the value of attribute fee_amount_sum.



6
7
8
# File 'lib/ruby_paypal_nvp/model/statement.rb', line 6

def fee_amount_sum
  @fee_amount_sum
end

#itemsObject

Returns the value of attribute items.



6
7
8
# File 'lib/ruby_paypal_nvp/model/statement.rb', line 6

def items
  @items
end

#items_countObject

Returns the value of attribute items_count.



6
7
8
# File 'lib/ruby_paypal_nvp/model/statement.rb', line 6

def items_count
  @items_count
end

#net_amount_sumObject

Returns the value of attribute net_amount_sum.



6
7
8
# File 'lib/ruby_paypal_nvp/model/statement.rb', line 6

def net_amount_sum
  @net_amount_sum
end

#start_dateObject

Returns the value of attribute start_date.



6
7
8
# File 'lib/ruby_paypal_nvp/model/statement.rb', line 6

def start_date
  @start_date
end

#subjectObject

Returns the value of attribute subject.



6
7
8
# File 'lib/ruby_paypal_nvp/model/statement.rb', line 6

def subject
  @subject
end

#timestampObject

Returns the value of attribute timestamp.



6
7
8
# File 'lib/ruby_paypal_nvp/model/statement.rb', line 6

def timestamp
  @timestamp
end

Instance Method Details

#generate_csv(filename = nil) ⇒ Object

rubocop:disable Metrics/AbcSize rubocop:disable Metrics/MethodLength



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/ruby_paypal_nvp/model/statement.rb', line 34

def generate_csv(filename = nil)
  raise 'Missing filename/path' unless filename
  ::CSV.open(filename, 'w') do |csv|
    csv << ['HEADER']
    csv << ['', 'timestamp', @timestamp]
    csv << ['', 'start_date', @start_date]
    csv << ['', 'end_date', @end_date]
    csv << ['', 'subject', @subject]
    csv << ['', 'currency_code', @currency_code]
    csv << ['', 'items_count', @items_count]
    csv << ['', 'amount_sum', @amount_sum]
    csv << ['', 'fee_amount_sum', @fee_amount_sum]
    csv << ['', 'net_amount_sum', @net_amount_sum]
    csv << ['ITEMS']
    csv << RubyPaypalNvp::Model::Item.attributes
    @items.each do |item|
      csv << item.to_csv
    end
  end
end

#to_iis_jsonObject



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

def to_iis_json
  {
    'meta': {
      'timestamp': @timestamp,
      'start_date': @start_date,
      'end_date': @end_date,
      'subject': @subject,
      'currency_code': @currency_code
    },
    'values': raw_items
  }.to_json
end

#to_jsonObject

rubocop:enable Metrics/AbcSize rubocop:enable Metrics/MethodLength



57
58
59
# File 'lib/ruby_paypal_nvp/model/statement.rb', line 57

def to_json
  raw_items.to_json
end