Class: BcaStatement::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



13
14
15
16
17
# File 'lib/bca_statement/client.rb', line 13

def initialize
  @timestamp = Time.now.iso8601(3)
  @base_url = BcaStatement.configuration.base_url
  authentication
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



11
12
13
# File 'lib/bca_statement/client.rb', line 11

def access_token
  @access_token
end

Instance Method Details

#get_statement(start_date = '2016-08-29', end_date = '2016-09-01') ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/bca_statement/client.rb', line 19

def get_statement(start_date = '2016-08-29', end_date = '2016-09-01')
  @start_date = start_date.to_s
  @end_date = end_date.to_s
  @path = "/banking/v3/corporates/"
  @corporate_id = BcaStatement.configuration.corporate_id
  @account_number = BcaStatement.configuration.
  @relative_url = "#{@path}#{@corporate_id}/accounts/#{@account_number}/statements?EndDate=#{@end_date}&StartDate=#{@start_date}"
  response = RestClient.get("#{@base_url}#{@relative_url}",
    "Content-Type": 'application/json',
    "Authorization": "Bearer #{@access_token}",
    "Origin": BcaStatement.configuration.domain,
    "X-BCA-Key": BcaStatement.configuration.api_key,
    "X-BCA-Timestamp":  @timestamp,
    "X-BCA-Signature": signature)
  if response.code.eql?(200)
    elements = JSON.parse response.body
    statements = []

    elements['Data'].each do |element|
      year = Date.parse(@start_date).strftime('%m').to_i.eql?(12) ? Date.parse(@start_date).strftime('%Y') : Date.parse(@end_date).strftime('%Y')
      date = "#{element['TransactionDate']}/#{year}"
      attribute = {
        date: date,
        brance_code: element['BranchCode'],
        type: element['TransactionType'],
        amount: element['TransactionAmount'].to_f,
        name: element['TransactionName'],
        trailer: element['Trailer']
      }
      statements << BcaStatement::Entities::Statement.new(attribute)
    end
    statements
  else
    return
  end

end