Skip to content

Instantly share code, notes, and snippets.

has_attached_file :img,
styles: { small: '167x200#', medium: '325x390#', large: '625x750#' },
default_url: '/assets/images/missing_product.png',
s3_region: ENV['AWS_REGION'],
storage: :s3,
s3_protocol: :https,
s3_credentials: "#{Rails.root}/config/amazon_s3.yml",
url: ':s3_domain_url',
path: '/hyper-image/bumon-code/:style/:id/:filename',
s3_host_alias: 'https://s3-ap-northeast-1.amazonaws.com/'
SIZES_ARR = [[167, 200, 'S'], [325, 390, 'M'], [625, 750, 'L']]
SIZES_ARR.each do |size_data|
resize_image(cropped_img, size_data.first, size_data.second, size_data.last)
end
def resize_image(image, width, height, size)
image.resize_to_fill(width, height).write(Rails.public_path.join("#{size}_#{self.img_file_name}"))
end
SIZES = [%w[small S], %w[medium M], %w[large L], %w[extra_large EL]]
SIZES.each do |size|
upload_and_delete_local_file(key, image, size.first, size.last)
end
def upload_and_delete_local_file(key, image, size, file_size)
obj = S3.bucket('unikaihatsu-s3').object(key.gsub 'original', size)
obj.upload_file(Rails.public_path.join("#{file_size}_#{image.img_file_name}"), acl:'public-read')
File.delete(Rails.public_path.join("#{file_size}_#{image.img_file_name}"))
end
SIZES_ARR = [[167, 200, 'S'], [325, 390, 'M'], [625, 750, 'L']]
SIZES = [%w[small S], %w[medium M], %w[large L], %w[extra_large EL]]
def crop_and_resize
key = self.img.path[1..-1]
S3.bucket('unikaihatsu-s3').object(key).get(response_target: Rails.public_path.join(self.img_file_name))
original_file = Rails.public_path.join(self.img_file_name)
image = Magick::Image.read(Dir.glob(original_file).first)
cropped_img = image[0].crop(150, 0, 1500, 1800).write(Rails.public_path.join("EL_#{self.img_file_name}"))
File.delete(original_file) if File.exist? original_file
def token_catcher
tokens = Boxr::get_tokens(params['code'],
grant_type: 'authorization_code',
assertion: nil,
scope: nil,
username: nil,
client_id: ENV['CLIENT_ID'],
client_secret: ENV['CLIENT_SECRET'])
TokenCred.create!(access: tokens['access_token'],
refresh: tokens['refresh_token'])
def save_tokens(access, refresh)
TokenCred.create!(access: access, refresh: refresh)
end
def client
token_refresh_callback = lambda { |access, refresh, identifier| save_tokens(access, refresh) }
Boxr::Client.new(TokenCred.last.access,
refresh_token: TokenCred.last.refresh,
client_id: ENV['CLIENT_ID'],
client_secret: ENV['CLIENT_SECRET'],
def save_tokens(access, refresh)
TokenCred.create!(access: access, refresh: refresh)
end
def upload_zip_to_box
token_refresh_callback = lambda { |access, refresh, identifier| save_tokens(access, refresh) }
client = Boxr::Client.new(TokenCred.last.access,
refresh_token: TokenCred.last.refresh,
client_id: ENV['CLIENT_ID'],
client_secret: ENV['CLIENT_SECRET'],
@NaiyaShah-BTC
NaiyaShah-BTC / nested_hash.rb
Created March 13, 2019 07:30
Different levels of data of hash.
# Below is our complete hash example.
properties = { user: { profile: { name: 'Naiya' }, bank: { account: '123' } } }
# This is having nil in profile details.
properties = { user: { profile: nil } }
# This has no user data
properties = { user: nil }
# May be possible to have no data set to properties
# Below is our complete hash example.
properties = { user: { profile: { name: 'Naiya' },
bank: { account: '123' } } }
# This is having nil in profile details.
properties = { user: { profile: nil } }
# This has no user data.
properties = { user: nil }
# best
# Will return value of name or nil
properties.dig(:user, :profile, :name)
# Will return true or false
properties.dig(:user, :profile, :name).present?