Framework

Populate Rails + Redis with Realistic Mock Data

Task

# lib/tasks/seed_redis.rake
require "net/http"
require "json"
require "redis"

namespace :seed do
  task redis: :environment do
    redis = Redis.new(url: ENV["REDIS_URL"])

    body = {
      tables: [
        { name: "users", count: 100, fields: [
          { name: "id", type: "uuid" },
          { name: "name", type: "full_name" },
          { name: "email", type: "email" }
        ]}
      ]
    }

    res = Net::HTTP.post(
      URI("https://api.mockhero.dev/api/v1/generate"),
      body.to_json,
      "Content-Type" => "application/json",
      "x-api-key" => ENV["MOCKHERO_API_KEY"]
    )

    JSON.parse(res.body)["users"].each do |u|
      redis.set("user:#{u["id"]}", u.to_json)
    end
  end
end

Run with bin/rake seed:redis.

Get Started

Grab a free key.

M

MockHero Team

Guides and tutorials for generating realistic test data with the MockHero API.

Start generating test data for free

1,000 rows/month on the free tier. No credit card required.

Get Your API Key

Related Articles