Class: Vault::StatementStore
- Inherits:
-
Object
- Object
- Vault::StatementStore
- Defined in:
- lib/vault-tools/statement_store.rb
Overview
The StatementStore knows how to save and retrieve invoices from S3
Instance Method Summary collapse
-
#bucket_for(format, opts) ⇒ Object
Determine which bucket an invoice should live in.
-
#get_html(opts) ⇒ Object
Retrieve invoice HTML from S3.
-
#get_json(opts) ⇒ Object
Retrieve invoice JSON from S3.
-
#initialize(opts = {}) ⇒ StatementStore
constructor
A new instance of StatementStore.
-
#path_for(opts = {}) ⇒ Object
Determine the path on S3 for the given invoice.
-
#retrieve(format, opts) ⇒ Object
Retrieve the contents in a given format of a given file from S3.
-
#s3 ⇒ Object
Get an instance of the S3 client to work with.
-
#write(format, opts) ⇒ Object
Write the contents in the given format to S3.
-
#write_html(opts) ⇒ Object
Write contents as is to HTML bucket on S3.
-
#write_json(opts) ⇒ Object
Write as JSON to S3.
Constructor Details
#initialize(opts = {}) ⇒ StatementStore
Returns a new instance of StatementStore.
4 5 6 7 |
# File 'lib/vault-tools/statement_store.rb', line 4 def initialize(opts = {}) @key_id = opts.fetch(:key_id, ENV['AWS_ACCESS_KEY_ID']) @key = opts.fetch(:key, ENV['AWS_SECRET_ACCESS_KEY']) end |
Instance Method Details
#bucket_for(format, opts) ⇒ Object
Determine which bucket an invoice should live in
54 55 56 57 58 59 60 61 62 |
# File 'lib/vault-tools/statement_store.rb', line 54 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
26 27 28 |
# File 'lib/vault-tools/statement_store.rb', line 26 def get_html(opts) retrieve(:html, opts) end |
#get_json(opts) ⇒ Object
Retrieve invoice JSON from S3
10 11 12 13 14 15 16 17 |
# File 'lib/vault-tools/statement_store.rb', line 10 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
65 66 67 68 69 70 71 |
# File 'lib/vault-tools/statement_store.rb', line 65 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
36 37 38 |
# File 'lib/vault-tools/statement_store.rb', line 36 def retrieve(format, opts) s3.buckets[bucket_for(format, opts)].objects[path_for(opts)].read end |
#s3 ⇒ Object
Get an instance of the S3 client to work with
48 49 50 51 |
# File 'lib/vault-tools/statement_store.rb', line 48 def s3 @s3 ||= AWS::S3.new(access_key_id: @key_id, secret_access_key: @key, use_ssl: true) end |
#write(format, opts) ⇒ Object
Write the contents in the given format to S3
41 42 43 44 45 |
# File 'lib/vault-tools/statement_store.rb', line 41 def write(format, opts) obj = s3.buckets[bucket_for(format, opts)].objects[path_for(opts)] obj.write(opts[:contents]) obj end |
#write_html(opts) ⇒ Object
Write contents as is to HTML bucket on S3
31 32 33 |
# File 'lib/vault-tools/statement_store.rb', line 31 def write_html(opts) write(:html, opts) end |
#write_json(opts) ⇒ Object
Write as JSON to S3
20 21 22 23 |
# File 'lib/vault-tools/statement_store.rb', line 20 def write_json(opts) opts[:contents] = JSON.dump(opts[:contents]) write(:json, opts) end |