Skip to content

Instantly share code, notes, and snippets.

@allanbatista
Last active August 28, 2018 16:47
Show Gist options
  • Save allanbatista/c3b3e9a66fca2049cea45926bf4eebbd to your computer and use it in GitHub Desktop.
Save allanbatista/c3b3e9a66fca2049cea45926bf4eebbd to your computer and use it in GitHub Desktop.
Comandos para facilitar a inicialização de um projeto limpo na skyhub

Criação de Contas e usuários

Cria Account Admin e Suporte

admin = Admin.create(email: "[email protected]", password: "password", password_confirmation: "password")
Tenant.current = account_test = Account.find_by(name: "account_test") || Account.create(name: "account_test", email: "[email protected]")
suporte = User.last || User.create(email: "[email protected]", support: true, password: "password", password_confirmation: "password", name: "support", account: account) rescue User.last

Cria uma nova conta com todas as permissoes

account_name = "IAS-46"
Tenant.current = account = Account.find_by(name: account_name) || Account.create(name: account_name, email: "#{account_name}@example.com")
user = User.last || User.create(email: "#{account.name}@example.com", password: "password", password_confirmation: "password", name: "#{account.name}-user", account: account) rescue User.last

Permission.new.subject_enum.each do |subject|
  account.permissions.create(subject: subject)
end

Cria uma API como Source

api_client = ApiClient.last
api_client ||= ApiClient.create(account: account, behave_as: :source, name: "Source API") rescue ApiClient.last

Criação de produtos

Para a criação de produtos, inicialize 2 terminais. Um terminal com rails c e o outro com rails s.

Generate products

Inicialização das variáveis para criação dos produtos

brands = ["Samsung", "Apple", "DELL", "Asus", "Avaya"]
names  = ["Notebook", "Celular", "Smartphone", "Telefone", "Fone de Ouvido", "Headset", "Garrafa"]
adjetivos = ["Small", "Medium", "Great", "Bigger", "Large", "Super", "Hyper"]
colors = ["FF4848", "FF68DD", "FF62B0", "FE67EB", "E469FE", "D568FD", "9669FE", "FF7575", "FF79E1", "FF73B9", "FE67EB", "E77AFE", "D97BFD", "A27AFE", "FF8A8A", "FF86E3", "FF86C2", "FE8BF0", "EA8DFE", "DD88FD", "AD8BFE", "FF9797", "FF97E8", "FF97CB", "FE98F1", "ED9EFE", "E29BFD", "B89AFE", "FFA8A8", "FFACEC", "FFA8D3", "FEA9F3", "EFA9FE", "E7A9FE", "C4ABFE", "FFBBBB", "FFACEC", "FFBBDD", "FFBBF7", "F2BCFE", "EDBEFE", "D0BCFE", "FFCECE", "FFC8F2", "FFC8E3", "FFCAF9", "F5CAFF", "F0CBFE", "DDCEFF", "FFDFDF", "FFDFF8", "FFDFEF", "FFDBFB", "F9D9FF", "F4DCFE", "E6DBFF", "FFECEC", "FFEEFB", "FFECF5", "FFEEFD", "FDF2FF", "FAECFF", "F1ECFF", "FFF2F2", "FFFEFB", "FFF9FC", "FFF9FE", "FFFDFF", "FDF9FF", "FBF9FF", "800080", "872187", "9A03FE", "892EE4", "3923D6", "2966B8", "23819C", "BF00BF", "BC2EBC", "A827FE", "9B4EE9", "6755E3", "2F74D0", "2897B7", "DB00DB", "D54FD5", "B445FE", "A55FEB", "8678E9", "4985D6", "2FAACE", "F900F9", "DD75DD", "BD5CFE", "AE70ED", "9588EC", "6094DB", "44B4D5", "FF4AFF", "DD75DD", "C269FE", "AE70ED", "A095EE", "7BA7E1", "57BCD9", "FF86FF", "E697E6", "CD85FE", "C79BF2", "B0A7F1", "8EB4E6", "7BCAE1", "FFA4FF", "EAA6EA", "D698FE", "CEA8F4", "BCB4F3", "A9C5EB", "8CD1E6", "FFBBFF", "EEBBEE", "DFB0FF", "DBBFF7", "CBC5F5", "BAD0EF", "A5DBEB", "FFCEFF", "F0C4F0", "E8C6FF", "E1CAF9", "D7D1F8", "CEDEF4", "B8E2EF", "FFDFFF", "F4D2F4", "EFD7FF", "EDDFFB", "E3E0FA", "E0EAF8", "C9EAF3", "FFECFF", "F4D2F4", "F9EEFF", "F5EEFD", "EFEDFC", "EAF1FB", "DBF0F7", "FFF9FF", "FDF9FD", "FEFDFF", "FEFDFF", "F7F5FE", "F8FBFE", "EAF7FB", "5757FF", "62A9FF", "62D0FF", "06DCFB", "01FCEF", "03EBA6", "01F33E", "6A6AFF", "75B4FF", "75D6FF", "24E0FB", "1FFEF3", "03F3AB", "0AFE47", "7979FF", "86BCFF", "8ADCFF", "3DE4FC", "5FFEF7", "33FDC0", "4BFE78", "8C8CFF", "99C7FF", "99E0FF", "63E9FC", "74FEF8", "62FDCE", "72FE95", "9999FF", "99C7FF", "A8E4FF", "75ECFD", "92FEF9", "7DFDD7", "8BFEA8", "AAAAFF", "A8CFFF", "BBEBFF", "8CEFFD", "A5FEFA", "8FFEDD", "A3FEBA", "BBBBFF", "BBDAFF", "CEF0FF", "ACF3FD", "B5FFFC", "A5FEE3", "B5FFC8", "CACAFF", "D0E6FF", "D9F3FF", "C0F7FE", "CEFFFD", "BEFEEB", "CAFFD8", "E1E1FF", "DBEBFF", "ECFAFF", "C0F7FE", "E1FFFE", "BDFFEA", "EAFFEF", "EEEEFF", "ECF4FF", "F9FDFF", "E6FCFF", "F2FFFE", "CFFEF0", "EAFFEF", "F9F9FF", "F9FCFF", "FDFEFF", "F9FEFF", "FDFFFF", "F7FFFD", "F9FFFB", "1FCB4A", "59955C", "48FB0D", "2DC800", "59DF00", "9D9D00", "B6BA18", "27DE55", "6CA870", "79FC4E", "32DF00", "61F200", "C8C800", "CDD11B", "4AE371", "80B584", "89FC63", "36F200", "66FF00", "DFDF00", "DFE32D", "7CEB98", "93BF96", "99FD77", "52FF20", "95FF4F", "FFFFAA", "EDEF85", "93EEAA", "A6CAA9", "AAFD8E", "6FFF44", "ABFF73", "FFFF84", "EEF093", "A4F0B7", "B4D1B6", "BAFEA3", "8FFF6F", "C0FF97", "FFFF99", "F2F4B3", "BDF4CB", "C9DECB", "CAFEB8", "A5FF8A", "D1FFB3", "FFFFB5", "F5F7C4", "D6F8DE", "DBEADC", "DDFED1", "B3FF99", "DFFFCA", "FFFFC8", "F7F9D0", "E3FBE9", "E9F1EA", "EAFEE2", "D2FFC4", "E8FFD9", "FFFFD7", "FAFBDF", "E3FBE9", "F3F8F4", "F1FEED", "E7FFDF", "F2FFEA", "FFFFE3", "FCFCE9", "FAFEFB", "FBFDFB", "FDFFFD", "F5FFF2", "FAFFF7", "FFFFFD", "FDFDF0"]
conn = Faraday.new("http://localhost:3000")

Criação de produtos com variação

  100.times do |n|
    price = rand(100.0..1000.0).round(2)
    cat = names.sample
    sku = "#{DateTime.now.to_i.to_s(16)}#{SecureRandom.hex(2)}".upcase
    product = {
      sku: sku,
      name: "#{names.sample} #{adjetivos.sample}",
      description: "Lorem Ipsum é simplesmente uma simulação de texto da indústria tipográfica e de impressos, e vem sendo utilizado desde o século XVI, quando um impressor desconhecido pegou uma bandeja de tipos e os embaralhou para fazer um livro de modelos de tipos. Lorem Ipsum sobreviveu não só a cinco séculos, como também ao salto para a editoração eletrônica, permanecendo essencialmente inalterado. Se popularizou na década de 60, quando a Letraset lançou decalques contendo passagens de Lorem Ipsum, e mais recentemente quando passou a ser integrado a softwares de editoração eletrônica como Aldus PageMaker.",
      brand: brands.sample,
      enabled: true,
      qty: rand(100..500),
      price: price,
      promotional_price: price * 0.9,
      weight: rand(10.0..100.0),
      height: rand(10.0..100.0),
      width: rand(10.0..100.0),
      length: rand(10.0..100.0),
      categories: [
        {
          code: cat.downcase,
          name: cat
        }
      ],
      images: [
        "https://placehold.it/1000x1000/#{colors.sample}",
        "https://placehold.it/1000x1000/#{colors.sample}",
        "https://placehold.it/1000x1000/#{colors.sample}"
      ],
      specifications: [
        {
          key: "Attr1",
          value: rand(10.0..100.0)
        },
        {
          key: "Attr2",
          value: SecureRandom.hex(2)
        },
        {
          key: "Attr3",
          value: rand(10.0..100.0)
        },
        {
          key: "Attr4",
          value: SecureRandom.hex(2)
        },
        {
          key: "Attr5",
          value: rand(10.0..100.0)
        },
        {
          key: "Attr6",
          value: SecureRandom.hex(2)
        }
      ],
      variations: [
        {
          sku: "#{sku}-1",
          specifications: [
            {
              key: "Color",
              value: colors.sample
            },
            {
              key: "Attr1",
              value: rand(10.0..100.0)
            },
            {
              key: "Attr2",
              value: SecureRandom.hex(2)
            },
            {
              key: "Attr3",
              value: rand(10.0..100.0)
            },
            {
              key: "Attr4",
              value: SecureRandom.hex(2)
            }
          ]
        },
        {
          sku: "#{sku}-2",
          specifications: [
            {
              key: "Color",
              value: colors.sample
            },
            {
              key: "Attr3",
              value: rand(10.0..100.0)
            },
            {
              key: "Attr4",
              value: SecureRandom.hex(2)
            },
            {
              key: "Attr5",
              value: rand(10.0..100.0)
            },
            {
              key: "Attr6",
              value: SecureRandom.hex(2)
            }
          ]
        }
      ],
      variation_attributes: [
        "Color"
      ]
    }
  
  
    r = conn.post do |builder|
      builder.url "/products"
      builder.headers["Content-type"] = "application/json"
      builder.headers["Accept"] = "application/json",
      builder.headers["X-User-Token"] = api_client.auth_token
      builder.headers["X-User-Email"] = user.email
      builder.body = { product: product }.to_json
    end
  end

Criação de produtos simples

100.times do |n|
  price = rand(100.0..1000.0).round(2)
  cat = names.sample
  sku = "#{DateTime.now.to_i.to_s(16)}#{SecureRandom.hex(2)}".upcase
  product = {
    sku: sku,
    name: "#{names.sample} #{adjetivos.sample}",
    description: "Lorem Ipsum é simplesmente uma simulação de texto da indústria tipográfica e de impressos, e vem sendo utilizado desde o século XVI, quando um impressor desconhecido pegou uma bandeja de tipos e os embaralhou para fazer um livro de modelos de tipos. Lorem Ipsum sobreviveu não só a cinco séculos, como também ao salto para a editoração eletrônica, permanecendo essencialmente inalterado. Se popularizou na década de 60, quando a Letraset lançou decalques contendo passagens de Lorem Ipsum, e mais recentemente quando passou a ser integrado a softwares de editoração eletrônica como Aldus PageMaker.",
    brand: brands.sample,
    enabled: true,
    qty: rand(100..500),
    price: price,
    promotional_price: price * 0.9,
    weight: rand(10.0..100.0),
    height: rand(10.0..100.0),
    width: rand(10.0..100.0),
    length: rand(10.0..100.0),
    categories: [
      {
        code: cat.downcase,
        name: cat
      }
    ],
    images: [
      "https://placehold.it/1000x1000/#{colors.sample}",
      "https://placehold.it/1000x1000/#{colors.sample}",
      "https://placehold.it/1000x1000/#{colors.sample}"
    ],
    specifications: [
      {
        key: "Attr1",
        value: rand(10.0..100.0)
      },
      {
        key: "Attr2",
        value: SecureRandom.hex(2)
      },
      {
        key: "Attr3",
        value: rand(10.0..100.0)
      },
      {
        key: "Attr4",
        value: SecureRandom.hex(2)
      },
      {
        key: "Attr5",
        value: rand(10.0..100.0)
      },
      {
        key: "Attr6",
        value: SecureRandom.hex(2)
      }
    ]
  }
  
  r = conn.post do |builder|
    builder.url "/products"
    builder.headers["Content-type"] = "application/json"
    builder.headers["Accept"] = "application/json",
    builder.headers["X-User-Token"] = api_client.auth_token
    builder.headers["X-User-Email"] = user.email
    builder.body = { product: product }.to_json
  end
end
Product.where(account: account).each do |product|
  product.update_attributes!(qty: rand(10..100))
end

Criação de remote_marketstructure

data = JSON.parse(File.read("remote_marketstructure.json"))
data.each do |d|
  RemoteMarketstructure.create(d.except("id"))
end

Criação de Pending Classifications

Product.all.each do |p|
  suggested_category = {}
  data.sample["code"].split("-").each_with_index do |code, index|
    suggested_category["lvl#{index+1}"] = { category_code: code, percentage: rand(0.5..1.0).round(2) }
  end
  PendingClassification.create({
    sku: p.sku,
    name: p.name,
    product: p,
    account: p.account,
    description: p.description,
    suggested_category: suggested_category
  })
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment