This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Report | |
def pdf_url | |
Rails.cache.fetch(["v1/report-pdf-url", self]) do | |
asset_host = Rails.env.production? ? Rails.application.config.action_controller.asset_host : "http://localhost:3000" | |
metadata = { width: 8.5, width: 11 } | |
renderer = ReportsController.renderer.new(https: true) | |
html = renderer.render(template: "reports/show", assigns: { report: self }) | |
pdf = BreezyPDF::HTML2PDF.new(asset_host, html, metadata) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Retriable.retriable(tries: 10, on: [RateLimitReached], intervals: Rails.application.config.retry_intervals) do | |
res = make_request | |
if res.status == 429 | |
raise RateLimitReached | |
else | |
# process returned invoice | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<style> | |
body, html { | |
margin: 0; | |
height: 100%; | |
} | |
.flex-column { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# spec/support/have_size.rb | |
require "rspec/expectations" | |
RSpec::Matchers.define :have_size do |expected| | |
match do |actual| | |
actual.size == expected | |
end | |
failure_message do |actual| |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CompressedRequests | |
def initialize(app) | |
@app = app | |
end | |
def method_handled?(env) | |
!!(env['REQUEST_METHOD'] =~ /(POST|PUT)/) | |
end | |
def encoding_handled?(env) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document).on('change', 'input[type="file"]', function(delegatedEvent) { | |
var event = delegatedEvent.originalEvent; | |
var csrfToken = $('[name="authenticity_token"]').val(); | |
var presignBaseUrl = $('form').attr('action'); | |
[].forEach.call(event.target.files, function(file) { | |
var filename = encodeURIComponent(file.name); | |
var size = encodeURIComponent(file.size); | |
var uploader = new S3(csrfToken); | |
var presignUrl = presignBaseUrl + |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
S3 = function(csrfToken) { | |
var handlers = []; | |
var obj = this; | |
var isSuccess = function(xhr) { | |
return (xhr.status >= 200 && xhr.status < 300) || xhr.status === 304; | |
}; | |
var formData = function(file, fields) { | |
var data = new FormData(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Handle file uploads to S3 | |
class UploadsController < ApplicationController | |
def new | |
@upload = Upload.new | |
end | |
# Step 1: POST to app to get the presignature URL | |
def create | |
@upload = Upload.create!(upload_params) | |
render json: signature # Step 2: Return the presigned URL after doing some validations |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# config/initializers/aws.rb | |
require "aws-sdk" | |
Aws.config.update({ | |
region: ENV["AWS_REGION"], | |
credentials: Aws::Credentials.new(ENV["AWS_ACCESS_KEY_ID"], ENV["AWS_SECRET_ACCESS_KEY"]) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export AWS_ACCESS_KEY_ID=KEY | |
export AWS_SECRET_ACCESS_KEY=SECRET | |
export AWS_S3_BUCKET=BUCKET_NAME | |
export AWS_REGION=REGION |