Class: Vault::StatementStore

Inherits:
Object
  • Object
show all
Defined in:
lib/vault-tools/statement_store.rb

Overview

The StatementStore knows how to save and retrieve invoices from S3

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ StatementStore

Returns a new instance of StatementStore.



4
5
6
7
8
# File 'lib/vault-tools/statement_store.rb', line 4

def initialize(opts = {})
  @credentials = Aws::Credentials.new(opts.fetch(:key_id, Config[:aws_access_key_id]),
                                      opts.fetch(:key, Config[:aws_secret_access_key]))
  @region = opts.fetch(:region, Config[:aws_region])
end

Instance Method Details

#bucket_for(format, opts) ⇒ Object

Determine which bucket an invoice should live in



59
60
61
62
63
64
65
66
67
# File 'lib/vault-tools/statement_store.rb', line 59

def bucket_for(format, opts)
  version = opts[:version]
  invoice_bucket = ENV['INVOICE_BUCKET_NAME'] || 'invoice-test'
  if format == :html
    "vault-v#{version}-#{invoice_bucket}"
  else
    "vault-v#{version}-json-#{invoice_bucket}"
  end
end

#get_html(opts) ⇒ Object

Retrieve invoice HTML from S3



27
28
29
# File 'lib/vault-tools/statement_store.rb', line 27

def get_html(opts)
  retrieve(:html, opts)
end

#get_json(opts) ⇒ Object

Retrieve invoice JSON from S3



11
12
13
14
15
16
17
18
# File 'lib/vault-tools/statement_store.rb', line 11

def get_json(opts)
  contents = retrieve(:json, opts)
  begin
    JSON.parse(contents)
  rescue
    contents
  end
end

#path_for(opts = {}) ⇒ Object

Determine the path on S3 for the given invoice



70
71
72
73
74
75
76
# File 'lib/vault-tools/statement_store.rb', line 70

def path_for(opts = {})
  validate_path_opts(opts)
  start = DateTime.parse(opts[:start_time].to_s).strftime('%Y-%m-%d')
  stop = DateTime.parse(opts[:stop_time].to_s).strftime('%Y-%m-%d')
  user_hid = opts[:user_hid] || "user#{opts[:user_id]}@heroku.com"
  sprintf('%s/%s/%s_v%s', start, stop, user_hid, opts[:version])
end

#retrieve(format, opts) ⇒ Object

Retrieve the contents in a given format of a given file from S3



37
38
39
40
41
42
# File 'lib/vault-tools/statement_store.rb', line 37

def retrieve(format, opts)
  s3.get_object({
    bucket: bucket_for(format, opts),
    key: path_for(opts)
  }).body.read
end

#s3Object

Get an instance of the S3 client to work with



54
55
56
# File 'lib/vault-tools/statement_store.rb', line 54

def s3
  @s3 ||= Aws::S3::Client.new(credentials: @credentials, region: @region)
end

#write(format, opts) ⇒ Object

Write the contents in the given format to S3



45
46
47
48
49
50
51
# File 'lib/vault-tools/statement_store.rb', line 45

def write(format, opts)
  s3.put_object({
    bucket: bucket_for(format, opts),
    key: path_for(opts),
    body: opts[:contents]
  })
end

#write_html(opts) ⇒ Object

Write contents as is to HTML bucket on S3



32
33
34
# File 'lib/vault-tools/statement_store.rb', line 32

def write_html(opts)
  write(:html, opts)
end

#write_json(opts) ⇒ Object

Write as JSON to S3



21
22
23
24
# File 'lib/vault-tools/statement_store.rb', line 21

def write_json(opts)
  opts[:contents] = JSON.dump(opts[:contents])
  write(:json, opts)
end