Skip to content

Instantly share code, notes, and snippets.

@joshuachestang
Created June 7, 2022 23:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshuachestang/fac5be2d5f13f431e98c4ca2ea308b31 to your computer and use it in GitHub Desktop.
Save joshuachestang/fac5be2d5f13f431e98c4ca2ea308b31 to your computer and use it in GitHub Desktop.
class Brand < ActiveRecord::Base
# Include default devise modules.
require 'open-uri'
require 'uri'
require 'net/http'
require 'rest_client'
require 'active_support/all'
require 'csv'
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :omniauthable
include DeviseTokenAuth::Concerns::User
include Rails.application.routes.url_helpers
has_many :products #need to archive products in a users closet if the brand is being destroyed
has_many :customer_feedbacks
has_many :promo_codes
has_many :retail_locations
has_many :loyalty_statuses, dependent: :destroy
has_many :daily_reports
has_many :events
has_many :invitations
belongs_to :user
validates_presence_of :name #:user_id, :street_address_1, :city, :state, :zip_code, :founding_month, :founding_year
has_attached_file :brand_logo, styles: { medium: "300x300>", thumb: "150x150" }, default_url: "https://s3.amazonaws.com/threadest/missing.png", :storage => :s3
validates_attachment_content_type :brand_logo, content_type: /\Aimage\/.*\Z/
default_scope { where(:disabled => false) }
default_scope { where(:disabled => nil) }
acts_as_followable
validates_acceptance_of :terms
include AlgoliaSearch
algoliasearch do
attribute :name, :state, :city, :founding_year, :id, :position, :disabled
attributesToIndex ['name']
add_attribute :brand_logo_image_url
add_attribute :products_count
add_attribute :emerging_brand
end
def name_and_id
self.name + " - " + self.id.to_s
end
# validates_uniqueness_of :name
scope :alphabetically, -> {
order("name ASC")
}
scope :top50, -> {
order("products_count DESC")
}
def has_shipping_address
if street_address_1.nil? || city.nil? || state.nil? || zip_code.nil?
return false
else
return true
end
end
def self.import_products(file)
#1. Open up csv
CSV.foreach(file.path, headers: true) do |row|
#binding.pry
Product.create(
title: row["title"],
url: link,
brand_id: self.id,
user_id: 3,
approved: true,
approved_for_date: Date.today,
description: @description,
default_price: @default_price,
sale_price: @sale_price,
street_address_1: self.street_address_1,
city: self.city,
state: self.state,
zip_code: self.zip_code,
shipping_option_id: 3,
ships_within: "4 - 7 Days",
gender_classification: row["gender"],
# closet_category: incoming_product["category"].downcase,
tag_list: tag_list + self.name.titleize + ", " + row["title"].downcase.gsub(" ", ", "),
managed: false,
# retailer: "Saks 5th Avenue",
# return_policy: incoming_product["no-returns"]
)
#loop through each product group id
# ProductVariation - create a product variation for each row with the same product group id
# end
ProductImageGallery.create
#loop through the product gallery images to create them
ProductGalleryImage.create
# Loop through each product gallery image within each row
end
#2. Look for certain columns in CSV
#3. Look for existence of the product name in our database
#4. If it does not exist import the products with each column name
#5. If there are picture urls and size variations, upload each in their respective models connected to the current product being imported
#6. After everything is imported, delete the csv from the file system
end
def has_connected_stripe_account
self.access_token.present?
end
def get_access_token(code)
brand = ActiveSupport::JSON.decode(`curl -X POST https://connect.stripe.com/oauth/token -d client_secret=#{ENV['SECRET_KEY']} -d code=#{code} -d grant_type=authorization_code`)
if self.update(:access_token => brand["access_token"], :refresh_token => brand["refresh_token"], :stripe_user_id => brand["stripe_user_id"])
return true
else
return false
end
end
def product_count
self.products.count
end
def emerging_brand
if self.stripe_user_id.present?
return true
else
return false
end
end
def brand_logo_image_url
if self.brand_logo.present?
self.brand_logo.url(:medium)
else
"https://s3.amazonaws.com/threadest/missing.png"
end
end
def brand_url
if Rails.env == "production"
"https://www.threadest.com/#{brand_path(self)}"
else
brand_path(self).to_s
end
end
def self.delete_brands_with_all_caps
Brand.all.each do |brand|
if brand.name == ( brand.name.upcase || brand.name.downcase )
brand.destroy
end
end
end
def tell_followers_that_new_thread_was_posted
NotificationsMailer.new_item_posted_to_brand(self.id).deliver
end
def self.import_new_logos
file = URI.parse('https://s3.amazonaws.com/threadest/brands-logo-search-7.json').read
data_hash = JSON.parse(file)
incoming_brands = data_hash
incoming_brands.each do |incoming_brand|
begin
if !Brand.find(incoming_brand["id"]).brand_logo.present?
if incoming_brand["logo-src"].include?("http")
brand = Brand.find(incoming_brand["id"])
brand_logo = URI.parse(incoming_brand["logo-src"]).open
brand.update(brand_logo: brand_logo)
end
if incoming_brand["logo-src"] == nil || incoming_brand["logo-src"] == "N/A" || incoming_brand["logo-src"] == "not found" || incoming_brand["logo-src"] == "Not found" || incoming_brand["logo-src"] == "n/a"
brand = Brand.find(incoming_brand["id"])
brand.destroy
end
end
rescue
next
end
end
SearchWorker.perform_async("Brand")
end
def self.top_50_brands_import
file = URI.parse('https://s3.amazonaws.com/threadest/top-50-brands.json').read
data_hash = JSON.parse(file)
incoming_brands = data_hash
incoming_brands.each do |incoming_brand|
# unless incoming_product["title"].blank?
if !Brand.find_by_name(incoming_brand["name"].titleize).present?
brand_logo = URI.parse(incoming_brand["logo-src"]).open
brand = Brand.create(
name: incoming_brand["name"].titleize,
brand_logo: brand_logo,
email: "hello@threadest.com",
uid: SecureRandom.hex(15),
provider: "threadest",
password: "TheClosetOfTheFuture",
password_confirmation: "TheClosetOfTheFuture"
)
end
end
SearchWorker.perform_async("Brand")
end
def self.nordstrom_brands_import
file = URI.parse('https://s3.amazonaws.com/threadest/nordstrom-brands-3.json').read
data_hash = JSON.parse(file)
incoming_brands = data_hash
incoming_brands.each do |incoming_brand|
# unless incoming_product["title"].blank?
if !Brand.find_by_name(incoming_brand["name"].titleize).present?
# brand_logo = URI.parse(incoming_brand["logo-src"]).open
brand = Brand.create(
name: incoming_brand["name"].titleize,
# brand_logo: brand_logo,
email: "brands+#{incoming_brand["name"].parameterize}@threadest.com",
uid: SecureRandom.hex(15),
provider: "threadest",
password: "TheClosetOfTheFuture",
password_confirmation: "TheClosetOfTheFuture"
)
end
end
SearchWorker.perform_async("Brand")
end
def self.saks_brands_import
file = URI.parse('https://s3.amazonaws.com/threadest/saks-brands-import-1.json').read
data_hash = JSON.parse(file)
incoming_brands = data_hash
incoming_brands.each do |incoming_brand|
# unless incoming_product["title"].blank?
if !Brand.find_by_name(incoming_brand["name"].titleize).present?
# brand_logo = URI.parse(incoming_brand["logo-src"]).open
brand = Brand.create(
name: incoming_brand["name"].titleize,
# brand_logo: brand_logo,
email: "brands+#{incoming_brand["name"].parameterize}@threadest.com",
uid: SecureRandom.hex(15),
provider: "threadest",
password: "TheClosetOfTheFuture",
password_confirmation: "TheClosetOfTheFuture"
)
end
end
SearchWorker.perform_async("Brand")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment