<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Complete Ruby on Rails]]></title><description><![CDATA[Documented journey of my own learnings in Ruby on Rails.]]></description><link>https://imlakshay08-complete-ruby-on-rails.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Thu, 18 Jun 2026 23:46:42 GMT</lastBuildDate><atom:link href="https://imlakshay08-complete-ruby-on-rails.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[From Polling to Production: How I Upgraded My Biometric Integration with Gate Control, Auto-Enrollment, and 24/7 Reliability]]></title><description><![CDATA[👉 Read the original post first: Connecting a Biometric Fingerprint Device to a Rails Web App Using Python
The Problem with "Good Enough"
When I published the first biometric post, the system worked. ]]></description><link>https://imlakshay08-complete-ruby-on-rails.hashnode.dev/from-polling-to-production-how-i-upgraded-my-biometric-integration-with-gate-control-auto-enrollment-and-24-7-reliability</link><guid isPermaLink="true">https://imlakshay08-complete-ruby-on-rails.hashnode.dev/from-polling-to-production-how-i-upgraded-my-biometric-integration-with-gate-control-auto-enrollment-and-24-7-reliability</guid><category><![CDATA[Ruby on Rails]]></category><category><![CDATA[Python]]></category><category><![CDATA[software development]]></category><category><![CDATA[api]]></category><dc:creator><![CDATA[Lakshay Tyagi]]></dc:creator><pubDate>Sat, 30 May 2026 12:28:04 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/62cdc1a71d29fac83b6d2d9c/9368adbb-f3d5-4a8d-bac8-07e46615c407.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>👉 Read the original post first: <a href="https://imlakshay08-complete-ruby-on-rails.hashnode.dev/connecting-a-biometric-fingerprint-device-to-a-rails-web-app-using-python">Connecting a Biometric Fingerprint Device to a Rails Web App Using Python</a></p>
<h2>The Problem with "Good Enough"</h2>
<p>When I published the first biometric post, the system worked. The Python bridge polled the device, forwarded punches to Rails, attendance appeared on the dashboard.</p>
<p>But "worked" and "production-ready" are different things.</p>
<p>Here's what the system looked like in practice after a few weeks of real gym use:</p>
<ul>
<li><strong>The bridge only ran when staff remembered to start it.</strong> They didn't always remember.</li>
<li><strong>The bridge sent every historical punch on every restart.</strong> January records were being re-sent in March.</li>
<li><strong>Expired members could still walk in.</strong> The <code>DENIED</code> label in the dashboard was cosmetic — the door opened anyway.</li>
<li><strong>Enrolling a new member required two separate manual operations</strong> — entering them in the software, then going to the device and registering their fingerprint, then manually creating a mapping record in the database.</li>
<li><strong>The gym's spare Android phone running the bridge had adware</strong> that would randomly kill background processes.</li>
<li><strong>ADMS (the device's built-in cloud push protocol) wouldn't connect</strong> — SSL/HTTP mismatch between the ZK firmware and Cloudflare/Render.</li>
</ul>
<p>None of these were bugs in the traditional sense. The code did what I wrote it to do. The problem was the gap between what the code did and what the business actually needed.</p>
<p>This post is about closing that gap.</p>
<hr />
<h2>What I Built (The Upgrade Summary)</h2>
<table>
<thead>
<tr>
<th>Feature</th>
<th>Before</th>
<th>After</th>
</tr>
</thead>
<tbody><tr>
<td>Bridge startup</td>
<td>Manual</td>
<td>Auto-starts silently on Windows boot via VBScript</td>
</tr>
<tr>
<td>Historical re-sends</td>
<td>Happened on every restart</td>
<td>Filtered by <code>date.today()</code> — today's punches only</td>
</tr>
<tr>
<td>Expired member gate access</td>
<td>Door opened, just logged DENIED</td>
<td>Finger template deleted from device — door physically stays closed</td>
</tr>
<tr>
<td>Member enrollment</td>
<td>3 manual steps across 2 systems</td>
<td>Single "Enroll Fingerprint" button in member profile</td>
</tr>
<tr>
<td>Bridge reliability</td>
<td>Gym laptop (unreliable, staff-dependent)</td>
<td>Dual: laptop (primary) + Android phone (backup)</td>
</tr>
<tr>
<td>Cloudflare/Render SSL</td>
<td>ADMS blocked by SSL mismatch</td>
<td>Flexible SSL on Cloudflare — HTTP from device, HTTPS to Render</td>
</tr>
<tr>
<td>Finger template backup</td>
<td>None</td>
<td>Templates stored as JSON in DB before deletion</td>
</tr>
<tr>
<td>Auto-restore on renewal</td>
<td>None</td>
<td>Sync script restores templates from DB on next run</td>
</tr>
</tbody></table>
<hr />
<h2>Part 1: Fixing the "Today Only" Problem</h2>
<p>The original bridge used an in-memory <code>last_sent</code> set to deduplicate. This worked within a single session but had a critical flaw: every time the script restarted, the set was empty, so all historical records were re-sent to Rails.</p>
<p>The fix is one line, but it changes everything:</p>
<pre><code class="language-python"># bridge.py

from datetime import date

while True:
    today = date.today()  # re-evaluated every loop iteration
    attendances = conn.get_attendance()
    device_sn = conn.get_serialnumber()

    for att in attendances:
        # Only process today's records
        if att.timestamp.date() != today:
            continue

        key = f"{att.user_id}-{att.timestamp}"
        if key in last_sent:
            continue

        payload = {
            "compcode": COMP_CODE,
            "user_id": att.user_id,
            "timestamp": att.timestamp.strftime("%Y-%m-%d %H:%M:%S"),
            "device_sn": device_sn
        }
        send_to_rails(payload)
        last_sent.add(key)

    time.sleep(POLL_INTERVAL_SECONDS)
</code></pre>
<p>Two important details:</p>
<ol>
<li><code>date.today()</code> is called <strong>inside</strong> the loop, not outside it. This means if the script runs past midnight, it automatically shifts to the new date without a restart.</li>
<li>The deduplication set <code>last_sent</code> still prevents duplicate sends within a session. Rails-side deduplication handles the cross-session case.</li>
</ol>
<p>The result: restart the bridge at any point during the day and it picks up exactly from today's records. No January punches appearing in May.</p>
<hr />
<h2>Part 2: Auto-Start on Windows — The Silent Background Process</h2>
<p>The original <code>.bat</code> file approach had a visible terminal window. Staff would close it. Attendance would stop. Nobody would notice until the owner called.</p>
<p>The fix is a <code>.vbs</code> file placed in the Windows Startup folder:</p>
<pre><code class="language-vb">' start_bridge.vbs
' Place in: shell:startup

Set objShell = CreateObject("WScript.Shell")
objShell.Run "cmd /c cd C:\Users\Admin\Desktop\biometric_bridge &amp;&amp; python bridge.py", 0, False
objShell.Run "cmd /c cd C:\Users\Admin\Desktop\biometric_bridge &amp;&amp; python enroll_api.py", 0, False
</code></pre>
<p>The <code>0</code> as the third parameter means <strong>hidden window</strong> — no terminal, no visible process. Staff can't accidentally close what they can't see.</p>
<p>Both the attendance bridge and the enrollment API (covered later) start silently on every boot.</p>
<p>To verify it's running without a terminal window, check Task Manager → Details tab → look for <code>python.exe</code>. If it's there, the bridge is alive.</p>
<h3>Preventing Sleep from Killing the Process</h3>
<p>Windows sleep/hibernate will kill background processes even with the VBScript approach. Set these before deploying:</p>
<pre><code>Settings → System → Power &amp; Sleep
  Screen: Never
  Sleep: Never

Control Panel → Power Options → Choose what closing the lid does
  When I close the lid: Do nothing
</code></pre>
<p>This is also where you tell staff: <em>"This laptop is the gym's server. Do not close the lid. Do not shut it down."</em> The mystique helps.</p>
<hr />
<h2>Part 3: The Gate Control Problem — Why DENIED Wasn't Enough</h2>
<p>The original system logged <code>DENIED</code> in the database when an expired member scanned their finger. But the door still opened. The DENIED label was purely informational — the device itself didn't know anything had changed.</p>
<p>To physically prevent the door from opening for expired members, <strong>the change has to happen on the device itself</strong>, not in the database.</p>
<p>The ZK device controls the door relay. When a fingerprint is matched, the relay fires and the door opens. If no fingerprint template exists for a user, the device shows a red failure screen and the relay doesn't fire. <strong>No template = no access. No middleware required.</strong></p>
<p>This is the cleanest possible implementation: delete the finger template from the device when a subscription expires, and restore it when they renew.</p>
<h3>The Database Change</h3>
<p>First, add two columns to <code>trn_member_biometric_mappings</code> to store the template data before deletion:</p>
<pre><code class="language-sql">ALTER TABLE trn_member_biometric_mappings
  ADD COLUMN mbm_finger_template LONGTEXT,
  ADD COLUMN mbm_uid INT DEFAULT NULL;
</code></pre>
<p><code>mbm_uid</code> stores the ZK internal UID (different from <code>device_user_id</code>). <code>mbm_finger_template</code> stores the raw finger template bytes as a JSON array so we can restore them later.</p>
<h3>The Sync Script</h3>
<pre><code class="language-python"># sync_access.py

from zk import ZK
from zk.user import User
from zk.finger import Finger
import requests
from config import *

RAILS_API_BASE = "https://spine-fitness.com"
DEVICE_SN = "NFZ8253402448"


def get_access_status():
    """
    Fetch all mapped members and their subscription status from Rails.
    Returns list of {device_user_id, access, name, user_info} dicts.
    """
    try:
        # Wake Render from idle before the real request
        requests.get(RAILS_API_BASE, timeout=30)
    except:
        pass

    try:
        response = requests.get(
            f"{RAILS_API_BASE}/api/access_status",
            params={"compcode": COMP_CODE, "device_sn": DEVICE_SN},
            timeout=60
        )
        return response.json().get("users", [])
    except Exception as e:
        print(f"Could not fetch access status: {e}")
        return []


def save_template_to_rails(device_user_id, uid, templates):
    """
    Before deleting a user from the device, save their finger templates
    to the Rails DB so we can restore them on renewal.
    """
    try:
        template_data = [
            {
                "uid": t.uid,
                "fid": t.fid,
                "valid": t.valid,
                "template": list(t.template)  # bytes → list for JSON serialization
            }
            for t in templates
        ]
        requests.post(
            f"{RAILS_API_BASE}/api/biometric_mappings/save_template",
            json={
                "compcode": COMP_CODE,
                "device_user_id": device_user_id,
                "device_sn": DEVICE_SN,
                "uid": uid,
                "templates": template_data
            },
            timeout=60
        )
    except Exception as e:
        print(f"    Could not save template to Rails: {e}")


def restore_user_to_device(conn, user_info):
    """
    Re-add a previously blocked user to the device using their saved templates.
    Called when their subscription is renewed.
    """
    uid = user_info.get("uid")
    device_user_id = user_info.get("device_user_id")
    name = user_info.get("name", "Member")
    templates_data = user_info.get("templates")

    if not uid or not templates_data:
        print(f"    No stored template for {name} — needs manual re-enrollment")
        return

    try:
        user_obj = User(
            uid=int(uid),
            name=name[:24],
            privilege=0,
            password='',
            group_id='',
            user_id=str(device_user_id),
            card=0
        )
        fingers = [
            Finger(
                uid=int(uid),
                fid=t["fid"],
                valid=t["valid"],
                template=bytes(t["template"])
            )
            for t in templates_data
        ]
        conn.save_user_template(user=user_obj, fingers=fingers)
        print(f"    Restored: {name} (device_user_id={device_user_id})")
    except Exception as e:
        print(f"    Could not restore {name}: {e}")


def sync_device_access():
    print("Starting access sync...")

    users_status = get_access_status()
    if not users_status:
        print("No data from Rails — skipping sync")
        return

    # Build lookup keyed by device_user_id
    access_map = {str(u["device_user_id"]): u for u in users_status}

    zk = ZK(DEVICE_IP, port=DEVICE_PORT, timeout=DEVICE_TIMEOUT,
            password=0, force_udp=False, ommit_ping=False)

    try:
        conn = zk.connect()
        conn.disable_device()

        # Snapshot of current device state
        device_users = {str(u.user_id): u for u in conn.get_users()}
        templates = conn.get_templates()
        templates_by_uid = {}
        for t in templates:
            templates_by_uid.setdefault(t.uid, []).append(t)

        for device_user_id, status in access_map.items():
            access = status["access"]       # "ALLOW" or "DENY"
            name = status.get("name", "Unknown")
            user_info = status.get("user_info", {})

            if device_user_id in device_users:
                user = device_users[device_user_id]

                # Never touch admin/staff accounts (privilege=14)
                if user.privilege == 14:
                    continue

                if access == "DENY":
                    # Save templates to DB before deletion
                    user_templates = templates_by_uid.get(user.uid, [])
                    if user_templates:
                        save_template_to_rails(device_user_id, user.uid, user_templates)
                    conn.delete_user(uid=user.uid)
                    print(f"  BLOCKED: {name} (device_user_id={device_user_id})")

                # access == "ALLOW" and user exists on device → nothing to do
                else:
                    pass

            else:
                # User not currently on device
                if access == "ALLOW" and user_info.get("uid") and user_info.get("templates"):
                    # Previously blocked, subscription renewed → restore
                    restore_user_to_device(conn, user_info)

        conn.enable_device()
        print("Access sync complete.")

    except Exception as e:
        print(f"Sync error: {e}")
        import traceback
        traceback.print_exc()
    finally:
        try:
            conn.enable_device()
            conn.disconnect()
        except:
            pass


if __name__ == "__main__":
    sync_device_access()
</code></pre>
<h3>The Rails API Endpoint</h3>
<p>The sync script needs to know who is active and who isn't:</p>
<pre><code class="language-ruby"># app/controllers/api/access_status_controller.rb
class Api::AccessStatusController &lt; ApplicationController
  skip_before_action :verify_authenticity_token

  def index
    compcode  = params[:compcode]
    device_sn = params[:device_sn]
    today     = Date.today

    mappings = TrnMemberBiometricMapping.where(
      mbm_compcode:  compcode,
      mbm_device_sn: device_sn,
      mbm_is_active: 'Y'
    )

    users = mappings.map do |m|
      has_active = TrnMemberSubscription
        .where(ms_compcode: compcode, ms_member_id: m.mbm_member_id)
        .where('ms_end_date &gt;= ?', today)
        .exists?

      member = MstMembersList.find_by(id: m.mbm_member_id)

      {
        device_user_id: m.mbm_device_user_id,
        access:         has_active ? "ALLOW" : "DENY",
        name:           member&amp;.mmbr_name || "Unknown",
        user_info: {
          uid:            m.mbm_uid,
          device_user_id: m.mbm_device_user_id,
          name:           member&amp;.mmbr_name || "Unknown",
          templates:      m.mbm_finger_template ? JSON.parse(m.mbm_finger_template) : nil
        }
      }
    end

    render json: { status: true, users: users }
  end
end
</code></pre>
<p>And the template save endpoint:</p>
<pre><code class="language-ruby"># app/controllers/api/biometric_mappings_controller.rb
def save_template
  mapping = TrnMemberBiometricMapping.find_by(
    mbm_compcode:       params[:compcode],
    mbm_device_user_id: params[:device_user_id],
    mbm_device_sn:      params[:device_sn]
  )

  return render json: { status: false, message: "Mapping not found" } unless mapping

  mapping.update!(
    mbm_uid:             params[:uid],
    mbm_finger_template: params[:templates].to_json
  )

  render json: { status: true }
end
</code></pre>
<h3>Running Sync Automatically</h3>
<p>The sync runs once on bridge startup and once every midnight — embedded as a background thread inside bridge.py:</p>
<pre><code class="language-python"># bridge.py (updated)

import threading
from sync_access import sync_device_access

SYNC_HOUR = 0  # midnight

def sync_scheduler():
    print("Running initial access sync...")
    sync_device_access()

    while True:
        now = datetime.now()
        if now.hour == SYNC_HOUR and now.minute == 0:
            print("Midnight sync starting...")
            sync_device_access()
            time.sleep(61)  # prevent double-trigger within same minute
        time.sleep(30)

def main():
    sync_thread = threading.Thread(target=sync_scheduler, daemon=True)
    sync_thread.start()

    # ... rest of bridge.py unchanged
</code></pre>
<h3>What the Member Experiences</h3>
<pre><code>Member with expired subscription scans finger:
  → Device shows red ✗ screen
  → "Unregistered ID" or "Access Denied"  
  → Door relay does NOT fire
  → Gate stays closed

Member renews subscription in Rails:
  → Next midnight sync runs
  → Rails returns "ALLOW" for this member
  → Sync finds stored templates in DB
  → Templates re-uploaded to device
  → Next scan: green ✓, door opens
</code></pre>
<p>The gate control is <strong>entirely device-side</strong>. Even if the laptop is off, the internet is down, or the Rails app is unreachable — if a template was deleted, the door stays closed. The device enforces access independently.</p>
<hr />
<h2>Part 4: Fingerprint Enrollment from the Web UI</h2>
<p>The original system required three separate manual steps to enroll a new member's fingerprint:</p>
<ol>
<li>Add the member in the web app</li>
<li>Go to the biometric device, navigate the menu, register their finger manually</li>
<li>Note the device-assigned user ID, come back to the computer, manually insert a row into <code>trn_member_biometric_mappings</code></li>
</ol>
<p>Step 3 was where it always broke. Staff would forget the device ID. They'd write it on a scrap of paper and lose it. The mapping table would get out of sync.</p>
<p>The fix: a <strong>local Flask API</strong> running on the gym laptop alongside bridge.py. The web app's member profile page sends an HTTP request to <code>localhost:5000/enroll</code>, which creates the device user and triggers the enrollment screen — all from a single button click.</p>
<h3>Why localhost?</h3>
<p>The enrollment API only needs to be reachable from the gym laptop, because enrollment always happens in person at the gym. The browser making the request and the Flask server are on the same machine. No ngrok, no tunnels, no external exposure.</p>
<pre><code class="language-python"># enroll_api.py

from flask import Flask, request, jsonify
from flask_cors import CORS
from zk import ZK
from zk.user import User
import requests as req
from config import *

app = Flask(__name__)
CORS(app)

RAILS_API_BASE = "https://spine-fitness.com"
DEVICE_SN      = "NFZ8253402448"


def get_next_uid_and_user_id(conn):
    users = conn.get_users()
    if not users:
        return 1, '1'
    max_uid      = max(u.uid for u in users)
    existing_ids = [int(u.user_id) for u in users if str(u.user_id).isdigit()]
    max_user_id  = max(existing_ids) if existing_ids else 0
    return max_uid + 1, str(max_user_id + 1)


@app.route('/health', methods=['GET'])
def health():
    return jsonify({'status': True, 'message': 'Enrollment API running'})


@app.route('/enroll', methods=['POST'])
def enroll():
    data        = request.json
    member_id   = data.get('member_id')
    member_name = data.get('member_name', 'Member')
    compcode    = data.get('compcode', 'SF')

    zk = ZK(DEVICE_IP, port=DEVICE_PORT, timeout=DEVICE_TIMEOUT,
            password=0, force_udp=False, ommit_ping=False)

    try:
        conn = zk.connect()
        conn.disable_device()

        new_uid, new_device_user_id = get_next_uid_and_user_id(conn)

        # Create user record on device (no finger template yet)
        user_obj = User(
            uid=new_uid,
            name=member_name[:24],
            privilege=0,
            password='',
            group_id='',
            user_id=new_device_user_id,
            card=0
        )
        conn.save_user_template(user=user_obj, fingers=[])

        # Trigger fingerprint enrollment screen on device
        conn.enable_device()
        conn.enroll_user(uid=new_uid, temp_id=0)

        # Save mapping to Rails
        req.post(
            f"{RAILS_API_BASE}/api/biometric_mappings",
            json={
                'compcode':       compcode,
                'member_id':      str(member_id),
                'device_user_id': new_device_user_id,
                'device_sn':      DEVICE_SN,
                'uid':            new_uid
            },
            timeout=30
        )

        return jsonify({
            'status':         True,
            'message':        f'Device ready — ask {member_name} to scan finger now!',
            'device_user_id': new_device_user_id
        })

    except Exception as e:
        return jsonify({'status': False, 'message': str(e)}), 500
    finally:
        try:
            conn.enable_device()
            conn.disconnect()
        except:
            pass


if __name__ == '__main__':
    app.run(host='127.0.0.1', port=5000, debug=False)
</code></pre>
<h3>The UI: Fingerprint Section on Member Profile</h3>
<p>The member edit page now has a biometric section that shows the current enrollment status and relevant actions:</p>
<pre><code class="language-erb">&lt;%# In member_list/add_member.html.erb %&gt;

&lt;% if @member &amp;&amp; @member.id %&gt;
  &lt;% mapping = TrnMemberBiometricMapping.find_by(
       mbm_compcode: session[:loggedUserCompCode],
       mbm_member_id: @member.id.to_s,
       mbm_is_active: 'Y'
     ) %&gt;

  &lt;div class="form-group row" style="margin-top: 20px;"&gt;
    &lt;div class="col-md-12"&gt;
      &lt;div class="card" style="padding: 15px;"&gt;
        &lt;h5&gt;Biometric Fingerprint&lt;/h5&gt;

        &lt;% if mapping %&gt;
          &lt;p&gt;
            &lt;span class="badge badge-success"&gt;✓ Enrolled&lt;/span&gt;
            Device User ID: &lt;strong&gt;&lt;%= mapping.mbm_device_user_id %&gt;&lt;/strong&gt;
          &lt;/p&gt;
          &lt;button type="button"
            onclick="enrollFinger(&lt;%= @member.id %&gt;, '&lt;%= @member.mmbr_name %&gt;')"
            class="btn btn-warning btn-sm"&gt;
            Re-enroll Finger
          &lt;/button&gt;
          &lt;button type="button"
            onclick="removeMapping(&lt;%= mapping.id %&gt;)"
            class="btn btn-danger btn-sm"&gt;
            Remove Mapping
          &lt;/button&gt;
        &lt;% else %&gt;
          &lt;p class="text-muted"&gt;No fingerprint enrolled.&lt;/p&gt;
          &lt;button type="button"
            onclick="enrollFinger(&lt;%= @member.id %&gt;, '&lt;%= @member.mmbr_name %&gt;')"
            class="btn btn-primary btn-sm"&gt;
            👆 Enroll Fingerprint
          &lt;/button&gt;

          &lt;%# Manual mapping for members already on device %&gt;
          &lt;div style="margin-top: 10px;"&gt;
            &lt;input type="text" id="manual_device_uid"
              class="form-control form-control-sm"
              placeholder="Device User ID (manual)" style="width: 200px; display: inline;"/&gt;
            &lt;button type="button"
              onclick="saveManualMapping(&lt;%= @member.id %&gt;, '&lt;%= @member.mmbr_name %&gt;')"
              class="btn btn-secondary btn-sm"&gt;
              Save Manual Mapping
            &lt;/button&gt;
          &lt;/div&gt;
        &lt;% end %&gt;

        &lt;div id="enroll-status" class="mt-2"&gt;&lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;% end %&gt;
</code></pre>
<pre><code class="language-javascript">function enrollFinger(memberId, memberName) {
  $("#enroll-status").html('&lt;span class="text-info"&gt;⏳ Checking enrollment service...&lt;/span&gt;');

  // First ping localhost to confirm we're on the gym laptop
  $.ajax({
    url: 'http://localhost:5000/health',
    type: 'GET',
    timeout: 3000,
    success: function() {
      doEnroll(memberId, memberName);
    },
    error: function() {
      $("#enroll-status").html(
        '&lt;span class="text-warning"&gt;⚠ Enrollment only works from the gym laptop.&lt;/span&gt;'
      );
    }
  });
}

function doEnroll(memberId, memberName) {
  $.ajax({
    url: 'http://localhost:5000/enroll',
    type: 'POST',
    contentType: 'application/json',
    data: JSON.stringify({ member_id: memberId, member_name: memberName, compcode: 'SF' }),
    timeout: 30000,
    success: function(resp) {
      if (resp.status) {
        $("#enroll-status").html('&lt;span class="text-success"&gt;✓ ' + resp.message + '&lt;/span&gt;');
        setTimeout(function(){ location.reload(); }, 4000);
      } else {
        $("#enroll-status").html('&lt;span class="text-danger"&gt;Error: ' + resp.message + '&lt;/span&gt;');
      }
    }
  });
}
</code></pre>
<p>The health check before enrollment is important — it gives a clear message when someone tries to enroll from home rather than a confusing network error.</p>
<hr />
<h2>Part 5: The Android Backup Bridge</h2>
<p>The gym's spare Android phone was already on 24/7 (used for YouTube/music). I put it to work as a backup bridge using Termux.</p>
<pre><code class="language-bash"># Termux setup
pkg update &amp;&amp; pkg upgrade -y
pkg install python -y
pip install pyzk requests   # note: pyzk, NOT zk

# Create startup script (runs on phone boot via Termux:Boot)
mkdir -p ~/.termux/boot
cat &gt; ~/.termux/boot/start-bridge.sh &lt;&lt; 'EOF'
#!/data/data/com.termux/files/usr/bin/sh
termux-wake-lock
cd /data/data/com.termux/files/home
python3 bridge.py &amp;
EOF
chmod +x ~/.termux/boot/start-bridge.sh
</code></pre>
<p>The key insight: <code>pip install zk</code> installs <strong>Zipkin</strong> (a distributed tracing library). The correct package is <code>pip install pyzk</code>. Easy mistake to make, hard to debug if you don't know to look for it.</p>
<p>The <code>termux-wake-lock</code> command prevents Android from killing Termux when it's backgrounded — critical for a bridge that needs to run continuously while YouTube plays in the foreground.</p>
<p>Set battery optimization to "Unrestricted" for Termux in Android Settings to prevent aggressive power management from terminating the process.</p>
<h3>Dual Bridge Architecture</h3>
<p>With both bridges running:</p>
<pre><code>Phone on  + Laptop off → attendance works via phone ✓
Phone off + Laptop on  → attendance works via laptop ✓
Both on               → both send, Rails deduplication ignores duplicates ✓
</code></pre>
<p>The Rails duplicate check (<code>same member + same minute</code>) means double-sends are silently ignored. Running two bridges simultaneously is safe.</p>
<hr />
<h2>Part 6: The ADMS Investigation (And Why I Abandoned It)</h2>
<p>I spent significant time trying to make ADMS (the device's built-in cloud push protocol) work. ADMS would have eliminated the need for the Python bridge entirely — the device would push attendance directly to Rails.</p>
<p>The investigation revealed the exact failure chain:</p>
<ol>
<li><strong>DNS resolution worked</strong> — after changing the device's DNS from the router (<code>192.168.31.1</code>) to Google's (<code>8.8.8.8</code>), the device could resolve <code>spine-fitness.com</code></li>
<li><strong>Port 80 was reachable</strong> — <code>curl http://spine-fitness.com/iclock/getrequest</code> from the same network returned <code>OK</code></li>
<li><strong>Cloudflare activated successfully</strong> — Flexible SSL mode set, domain proxied through Cloudflare</li>
<li><strong>Rails ADMS controller was deployed</strong> — routes for <code>/iclock/cdata</code> and <code>/iclock/getrequest</code> were live and returning <code>OK</code></li>
<li><strong>The device still never connected</strong> — zero <code>/iclock/</code> requests appeared in Render logs even after DNS fix and reboot</li>
</ol>
<p>The most likely root cause: the router has per-device firewall rules blocking outbound HTTP from IoT/embedded devices (MAC address filtering or device category classification). The laptop and phone on the same WiFi could reach the internet fine; the ZK device could not — despite identical network configuration.</p>
<p>The ADMS controller code remains in the codebase for future use if the router configuration ever changes:</p>
<pre><code class="language-ruby"># app/controllers/api/adms_controller.rb
class Api::AdmsController &lt; ApplicationController
  skip_before_action :verify_authenticity_token

  def getrequest
    render plain: "OK"
  end

  def handshake
    render plain: "OK"
  end

  def receive
    body = request.body.read
    Rails.logger.info "ADMS received: #{body}"

    sn_match  = body.match(/SN=([^\s&amp;\r\n]+)/i)
    device_sn = sn_match ? sn_match[1] : 'NFZ8253402448'

    body.each_line do |line|
      line = line.strip
      next if line.empty?
      next if line.start_with?("ATTLOG")
      next unless line.split("\t")[0].to_s.match?(/\A\d+\z/)

      parts          = line.split("\t")
      device_user_id = parts[0].to_s.strip
      timestamp      = parts[1].to_s.strip
      punch_time     = Time.zone.parse(timestamp) rescue nil

      next unless punch_time
      next if punch_time.to_date &lt; Date.today

      process_attendance(device_user_id, punch_time, device_sn)
    end

    render plain: "OK"
  end

  private

  def process_attendance(device_user_id, punch_time, device_sn)
    # same logic as BiometricAttendancesController
  end
end
</code></pre>
<p>If you're starting fresh and your router doesn't filter IoT devices, ADMS is the cleaner solution — no bridge needed at all.</p>
<hr />
<h2>The Updated Architecture</h2>
<pre><code>                    ┌─────────────────────────────┐
                    │   ZK Fingerprint Device      │
                    │   192.168.31.151:4370        │
                    │   Door relay connected        │
                    └──────────┬──────────────────┘
                               │ pyzk TCP
              ┌────────────────┼────────────────┐
              │                │                │
              ▼                ▼                │
 ┌────────────────┐  ┌──────────────────┐       │
 │  Python Bridge │  │  Enroll API      │       │
 │  bridge.py     │  │  enroll_api.py   │       │
 │  (gym laptop)  │  │  Flask :5000     │       │
 │                │  │  (gym laptop)    │       │
 │  + sync_access │  └────────┬─────────┘       │
 │    thread      │           │ localhost POST   │
 └───────┬────────┘           │                 │
         │                    │                 │
         │ HTTPS POST         │                 │
         ▼                    ▼                 │
 ┌───────────────────────────────────────────┐  │
 │        Cloudflare (Flexible SSL)          │  │
 └───────────────────┬───────────────────────┘  │
                     │ HTTPS                     │
                     ▼                           │
 ┌───────────────────────────────────────────┐  │
 │     Ruby on Rails (Render)                │  │
 │                                           │  │
 │  POST /api/biometric_attendances          │  │
 │  POST /api/biometric_mappings             │  │
 │  POST /api/biometric_mappings/save_template│  │
 │  GET  /api/access_status                  │  │
 └───────────────────┬───────────────────────┘  │
                     │                           │
                     ▼                           │
 ┌───────────────────────────────────────────┐  │
 │     MySQL (CleverCloud)                   │  │
 │                                           │  │
 │  trn_member_attendances                   │  │
 │  trn_member_biometric_mappings            │  │
 │    + mbm_finger_template (LONGTEXT)       │  │
 │    + mbm_uid (INT)                        │  │
 │  trn_member_subscriptions                 │  │
 └───────────────────────────────────────────┘  │
                                                 │
 ┌───────────────────────────────────────────┐  │
 │  Android Phone (Termux backup bridge)     │──┘
 │  Same WiFi, same device, different host   │
 └───────────────────────────────────────────┘
</code></pre>
<hr />
<h2>Key Engineering Decisions in Retrospect</h2>
<p><strong>1. Delete the template, not just a flag.</strong>
The first instinct is to add an <code>is_blocked</code> flag somewhere and check it in a middleware layer. But that still involves a network call at scan time — and the network can be down. Deleting the template makes the device itself the enforcement layer. No network, no middleware, no latency. The device just can't match a finger that isn't there.</p>
<p><strong>2. Store templates before deletion.</strong>
This is the part that's easy to skip and catastrophic if you do. Always save the template bytes to the database before calling <code>conn.delete_user()</code>. The sync script saves first, deletes second — if the save fails, the delete doesn't happen.</p>
<p><strong>3. Never touch admin accounts (privilege=14).</strong>
The sync script explicitly skips any user with <code>privilege == 14</code>. These are device admins — staff, trainers, the owner. They should always have access regardless of subscription status. One unguarded <code>delete_user</code> on an admin account during a sync run would be a very bad day.</p>
<p><strong>4. The enrollment API health check.</strong>
Before sending the enroll command, the browser pings <code>localhost:5000/health</code>. If it doesn't respond, the user sees "Enrollment only works from the gym laptop" instead of a generic network error. Small UX detail, but it prevents a lot of confused support calls.</p>
<p><strong>5. Render cold starts are real.</strong>
The sync script pings the Rails root URL before the actual <code>access_status</code> request. Render's free tier sleeps after inactivity. Without the warmup ping, the first real request would time out, the sync would abort, and nobody would get blocked or restored. The warmup adds ~2 seconds but makes the sync reliable.</p>
<hr />
<h2>Current State</h2>
<p>The system has been running in production for several months. The gym has:</p>
<ul>
<li>Zero attendance records missed due to bridge downtime (dual bridge coverage)</li>
<li>Members with expired subscriptions physically blocked at the door</li>
<li>New member enrollment taking under 30 seconds from web form to working fingerprint</li>
<li>The gym owner checking the live attendance dashboard from home on their phone</li>
</ul>
<p>The notebook is in a drawer somewhere. Nobody's opened it.</p>
<hr />
<p><strong>🔗 Live:</strong> <a href="https://spine-fitness.com">spine-fitness.com</a>
<strong>💻 Source:</strong> <a href="https://github.com/imlakshay08/spine-fitness-gym-management-system">github.com/imlakshay08/spine-fitness-gym-management-system</a></p>
<p><em>If you've dealt with biometric hardware integration or the ZK device ecosystem — I'd love to compare notes in the comments.</em></p>
]]></content:encoded></item><item><title><![CDATA[How I Built a Rails App That Syncs Empire Flippers Listings to HubSpot and Google Sheets]]></title><description><![CDATA[The Origin Story
This project started as a coding challenge for a job interview at Empire Flippers — the #1 marketplace for buying and selling online businesses. The challenge was simple on paper:

"U]]></description><link>https://imlakshay08-complete-ruby-on-rails.hashnode.dev/how-i-built-a-rails-app-that-syncs-empire-flippers-listings-to-hubspot-and-google-sheets</link><guid isPermaLink="true">https://imlakshay08-complete-ruby-on-rails.hashnode.dev/how-i-built-a-rails-app-that-syncs-empire-flippers-listings-to-hubspot-and-google-sheets</guid><category><![CDATA[Ruby]]></category><category><![CDATA[Ruby on Rails]]></category><category><![CDATA[Web Development]]></category><category><![CDATA[api]]></category><category><![CDATA[Software Engineering]]></category><dc:creator><![CDATA[Lakshay Tyagi]]></dc:creator><pubDate>Mon, 25 May 2026 01:17:22 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/62cdc1a71d29fac83b6d2d9c/9ab69e59-2486-4f5f-b1e2-f33d3c2b82e9.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2>The Origin Story</h2>
<p>This project started as a coding challenge for a job interview at Empire Flippers — the #1 marketplace for buying and selling online businesses. The challenge was simple on paper:</p>
<blockquote>
<p>"Use our Public Listings API as an input data source, store listings in a database, create Deal objects in HubSpot, and run the sync once per day."</p>
</blockquote>
<p>What followed was five days of learning Ruby on Rails from scratch, debugging SSL certificates on Windows, fighting libcurl DLLs, pagination loops that ran forever, and ultimately shipping a working integration with 213 real HubSpot deals and a live Google Sheet — all with a full RSpec test suite.</p>
<p>This is that story.</p>
<hr />
<h2>What the App Does</h2>
<p>Empire Flippers lists online businesses for sale — Amazon FBA stores, SaaS products, content sites, eCommerce businesses. Their platform is essentially a marketplace for digital assets, similar to a real estate agency but for online businesses.</p>
<p>The challenge was to build a Rails app that:</p>
<ol>
<li>Fetches all "For Sale" listings from Empire Flippers' public API</li>
<li>Stores listing data in a PostgreSQL database</li>
<li>Creates a corresponding Deal in HubSpot CRM for each listing</li>
<li>Exports all listings to a Google Sheet</li>
<li>Runs the entire sync automatically once per day via background jobs</li>
<li>Never creates duplicate listings or duplicate HubSpot deals — even if the sync runs multiple times</li>
</ol>
<pre><code>SyncListingsJob (runs daily at midnight via Sidekiq + sidekiq-scheduler)
    ↓
ListingSyncService (orchestrator)
    ├── EmpireFlippersService  →  fetches all listings from EF API (paginated)
    ├── HubspotService         →  creates deals in HubSpot CRM
    └── GoogleSheetsService    →  exports listings to Google Sheet
</code></pre>
<hr />
<h2>The Tech Stack</h2>
<table>
<thead>
<tr>
<th>Layer</th>
<th>Technology</th>
</tr>
</thead>
<tbody><tr>
<td><strong>Framework</strong></td>
<td>Ruby on Rails 7.1</td>
</tr>
<tr>
<td><strong>Database</strong></td>
<td>PostgreSQL</td>
</tr>
<tr>
<td><strong>Background Jobs</strong></td>
<td>Sidekiq + sidekiq-scheduler</td>
</tr>
<tr>
<td><strong>EF API Client</strong></td>
<td>HTTParty</td>
</tr>
<tr>
<td><strong>HubSpot</strong></td>
<td>hubspot-api-client gem</td>
</tr>
<tr>
<td><strong>Google Sheets</strong></td>
<td>google-apis-sheets_v4 gem</td>
</tr>
<tr>
<td><strong>Testing</strong></td>
<td>RSpec + factory_bot</td>
</tr>
<tr>
<td><strong>Environment</strong></td>
<td>dotenv-rails</td>
</tr>
</tbody></table>
<hr />
<h2>Architecture: Service Objects</h2>
<p>The first design decision was choosing service objects over fat controllers or fat models.</p>
<p>Each service has exactly one responsibility:</p>
<pre><code class="language-ruby"># EmpireFlippersService — knows how to talk to the EF API
# HubspotService        — knows how to talk to HubSpot
# GoogleSheetsService   — knows how to talk to Google Sheets
# ListingSyncService    — orchestrates all three, touches nothing else
</code></pre>
<p>This matters more than it seems. When the EF API added pagination requirements mid-project, only <code>EmpireFlippersService</code> needed to change. When HubSpot needed a different property format, only <code>HubspotService</code> changed. Nothing else was touched.</p>
<p>This is the <strong>Single Responsibility Principle</strong> in practice — each class has exactly one reason to change.</p>
<hr />
<h2>The Database Layer: Preventing Duplicates</h2>
<p>The <code>Listing</code> model is simple but has two layers of duplicate prevention:</p>
<pre><code class="language-ruby">class Listing &lt; ApplicationRecord
  validates :listing_number, presence: true, uniqueness: true
  validates :listing_price, presence: true
end
</code></pre>
<p>And at the database level:</p>
<pre><code class="language-ruby">add_index :listings, :listing_number, unique: true
</code></pre>
<p><strong>Why both?</strong> The model validation catches duplicates in Ruby before touching the database. The database index is the last line of defense — even if two requests arrive simultaneously, PostgreSQL rejects the second one.</p>
<p>The database columns:</p>
<pre><code>listing_number              — EF's unique ID for each business
listing_price               — sale price in USD
summary                     — full business description
listing_status              — "For Sale"
hubspot_deal_id             — stored after creating the HubSpot deal
average_monthly_net_profit  — monthly earnings
</code></pre>
<hr />
<h2>Fetching Listings: The Pagination Problem</h2>
<p>The first version of <code>EmpireFlippersService</code> was simple:</p>
<pre><code class="language-ruby">def self.fetch_listings
  response = HTTParty.get(BASE_URL, query: {
    listing_status: "For Sale",
    limit: 100
  })
  response["data"]["listings"]
end
</code></pre>
<p>This returned 100 listings. During the interview, the interviewer noted: "There are 213 listings — I want all of them."</p>
<p>The fix required pagination:</p>
<pre><code class="language-ruby">def self.fetch_listings
  listings = []
  page = 1

  loop do
    response = HTTParty.get(BASE_URL, query: {
      listing_status: "For Sale",
      limit: 100,
      page: page
    })

    raise "Empire Flippers API error: #{response.code}" unless response.success?

    page_listings = response["data"]["listings"]
    break if page_listings.empty?

    listings.concat(page_listings)
    page += 1
  end

  listings
rescue StandardError =&gt; e
  Rails.logger.error "Failed to fetch EF listings: #{e.message}"
  raise
end
</code></pre>
<p><strong>How it works:</strong> Page 1 returns 100 listings, page 2 returns 100 more, page 3 returns 13, page 4 returns empty — loop breaks. Total: 213.</p>
<p>The <code>raise</code> at the end of the rescue is intentional. Re-raising the error tells Sidekiq the job failed, triggering automatic retries. If the EF API is temporarily down, Sidekiq retries up to 25 times with exponential backoff — no manual intervention required.</p>
<hr />
<h2>The Sync Orchestrator: Idempotent by Design</h2>
<p>The <code>ListingSyncService</code> is the heart of the app:</p>
<pre><code class="language-ruby">class ListingSyncService
  def self.sync
    listings = EmpireFlippersService.fetch_listings

    listings.each do |listing_data|
      listing = Listing.find_or_create_by(listing_number: listing_data["listing_number"]) do |l|
        l.listing_price = listing_data["listing_price"]
        l.summary = listing_data["summary"]
        l.listing_status = listing_data["listing_status"]
        l.average_monthly_net_profit = listing_data["average_monthly_net_profit"]
      end

      if listing.hubspot_deal_id.nil?
        deal_id = HubspotService.create_deal(listing)
        listing.update(hubspot_deal_id: deal_id)
      end
    end

    GoogleSheetsService.export(Listing.all)
  end
end
</code></pre>
<p>Three things worth noting:</p>
<p><strong><code>find_or_create_by</code></strong> — This is the duplicate prevention for the database. If listing #91258 already exists, return it. If not, create it. The <code>do |l|</code> block only runs for new records.</p>
<p><strong><code>if listing.hubspot_deal_id.nil?</code></strong> — After saving to the database, check if we already created a HubSpot deal. If yes, skip. If no, create and save the returned deal ID. This prevents duplicate HubSpot deals even if the sync runs ten times.</p>
<p><strong><code>GoogleSheetsService.export(Listing.all)</code></strong> — We pass all listings from the database, not just new ones. The challenge requires clearing and rewriting the sheet completely each run.</p>
<p>This makes the entire sync <strong>idempotent</strong> — running it 10 times produces the same result as running it once.</p>
<hr />
<h2>HubSpot Integration: The libcurl Adventure</h2>
<p>The HubSpot service itself is straightforward:</p>
<pre><code class="language-ruby">class HubspotService
  def self.create_deal(listing)
    client = Hubspot::Client.new(access_token: ENV['HUBSPOT_ACCESS_TOKEN'])

    properties = {
      dealname: "Listing ##{listing.listing_number}",
      amount: listing.listing_price.to_s,
      closedate: (Time.now + 30.days).strftime("%Y-%m-%dT%H:%M:%S.%LZ"),
      description: listing.summary,
      average_monthly_net_profit: listing.average_monthly_net_profit
    }

    response = client.crm.deals.basic_api.create(
      simple_public_object_input_for_create: { properties: properties }
    )

    response.id
  end
end
</code></pre>
<p>But getting here on Windows was not straightforward.</p>
<p>The HubSpot gem uses <code>typhoeus</code> internally, which requires <code>libcurl</code>. On Windows, typhoeus looks for <code>libcurl.dll</code> specifically — not the curl executable that ships with Windows 10+.</p>
<p>The fix:</p>
<pre><code class="language-powershell"># Download the curl DLL
Invoke-WebRequest -Uri "https://curl.se/windows/dl-8.11.1_2/curl-8.11.1_2-win64-mingw.zip" -OutFile "$env:TEMP\curl.zip"
Expand-Archive "\(env:TEMP\curl.zip" -DestinationPath "\)env:TEMP\curl"

# Copy to Ruby's bin directory
Copy-Item "$env:TEMP\curl\curl-8.11.1_2-win64-mingw\bin\libcurl-x64.dll" "C:\Ruby31-x64\bin\"

# Create a copy with the exact name typhoeus looks for
Copy-Item "C:\Ruby31-x64\bin\libcurl-x64.dll" "C:\Ruby31-x64\bin\libcurl.dll"
</code></pre>
<p>Then SSL verification on Windows required one more fix — a Typhoeus initializer:</p>
<pre><code class="language-ruby"># config/initializers/hubspot.rb
require 'typhoeus'

Typhoeus.before do |request|
  request.options[:ssl_verifypeer] = false
  request.options[:ssl_verifyhost] = 0
end
</code></pre>
<p>This is a Windows-only workaround. On a Linux production server, neither fix is needed.</p>
<hr />
<h2>Google Sheets Integration: The Permission Problem</h2>
<p>The Google Sheets service authenticates via a service account:</p>
<pre><code class="language-ruby">def client
  @client ||= begin
    service = Google::Apis::SheetsV4::SheetsService.new
    service.authorization = Google::Auth::ServiceAccountCredentials.make_creds(
      json_key_io: File.open(ENV['GOOGLE_SHEETS_CREDENTIALS_PATH']),
      scope: SCOPE
    )
    service
  end
end
</code></pre>
<p>The <code>@client ||=</code> is memoization — create the authenticated client once per object instance, reuse it for all API calls. No re-authenticating on every sheet operation.</p>
<p>The first attempt to create a sheet from the service account returned a <code>403 PERMISSION_DENIED</code>. The reason: service accounts live in their own Google Drive, separate from your personal Drive. They can't create sheets in your Drive without being explicitly granted access.</p>
<p>The clean solution: create the sheet manually in Google Sheets, share it with the service account email (<code>empire-sync@project-id.iam.gserviceaccount.com</code>), and store the sheet ID in <code>.env</code>. The service account then has Editor access to write data.</p>
<pre><code class="language-ruby">def find_or_create_sheet
  sheet_id = GoogleSheetReference.first&amp;.sheet_id

  if sheet_id.nil?
    sheet_id = ENV['GOOGLE_SHEET_ID']
    GoogleSheetReference.create!(sheet_id: sheet_id)
  end

  sheet_id
end
</code></pre>
<p><code>GoogleSheetReference</code> stores the sheet ID in the database — the sheet is created once and reused on every subsequent sync run.</p>
<p>Writing the data:</p>
<pre><code class="language-ruby">def write_rows(sheet_id, listings)
  values = [["Listing #", "Listing Price", "Summary"]]

  listings.each do |listing|
    values &lt;&lt; [
      listing.listing_number.to_s,
      listing.listing_price.to_s,
      listing.summary
    ]
  end

  value_range = Google::Apis::SheetsV4::ValueRange.new(values: values)

  client.update_spreadsheet_value(
    sheet_id,
    'Sheet1',
    value_range,
    value_input_option: 'RAW'
  )
end
</code></pre>
<p>Each run: clear the sheet, write the header row, write one row per listing. Simple and deterministic.</p>
<hr />
<h2>Testing with RSpec: TDD in Practice</h2>
<p>Every service has a corresponding spec. The testing philosophy: never hit real APIs in tests. Stub everything external, test your logic in isolation.</p>
<pre><code class="language-ruby">RSpec.describe ListingSyncService do
  describe ".sync" do
    let(:fake_listings) do
      [{
        "listing_number" =&gt; 12345,
        "listing_price" =&gt; 50000,
        "summary" =&gt; "A great business",
        "listing_status" =&gt; "For Sale"
      }]
    end

    before do
      allow(EmpireFlippersService).to receive(:fetch_listings).and_return(fake_listings)
      allow(HubspotService).to receive(:create_deal).and_return("hs_deal_123")
      allow(GoogleSheetsService).to receive(:export)
    end

    it "syncs listings from Empire Flippers" do
      ListingSyncService.sync
      expect(Listing.count).to eq(1)
      expect(Listing.first.listing_number).to eq(12345)
    end

    it "does not create duplicate listings" do
      ListingSyncService.sync
      ListingSyncService.sync
      expect(Listing.count).to eq(1)
    end

    it "creates a hubspot deal for new listings" do
      ListingSyncService.sync
      expect(HubspotService).to have_received(:create_deal)
      expect(Listing.first.hubspot_deal_id).to eq("hs_deal_123")
    end

    it "does not create hubspot deal if one already exists" do
      ListingSyncService.sync
      allow(HubspotService).to receive(:create_deal)
      ListingSyncService.sync
      expect(HubspotService).to have_received(:create_deal).once
    end
  end
end
</code></pre>
<p>The <code>allow(...).to receive(...).and_return(...)</code> pattern is <strong>stubbing</strong> — replacing the real method with a fake that returns predetermined data. Tests run in milliseconds and never depend on the internet.</p>
<p>The duplicate prevention test is particularly important: run the sync twice, expect only one listing in the database. If <code>find_or_create_by</code> is broken, this test catches it immediately.</p>
<hr />
<h2>The Daily Scheduler</h2>
<p>Sidekiq handles background job processing. sidekiq-scheduler adds cron-like scheduling on top:</p>
<pre><code class="language-yaml"># config/sidekiq.yml
:scheduler:
  :schedule:
    sync_listings:
      :cron: "0 0 * * *"
      :class: SyncListingsJob
</code></pre>
<p><code>0 0 * * *</code> = minute 0, hour 0, every day = midnight.</p>
<p>The job itself is intentionally thin:</p>
<pre><code class="language-ruby">class SyncListingsJob &lt; ApplicationJob
  queue_as :default

  def perform
    ListingSyncService.sync
  end
end
</code></pre>
<p>Business logic lives in the service. The job is just a trigger. This makes the service independently testable and independently callable — from the console, from a rake task, from anywhere.</p>
<hr />
<h2>End-to-End Results</h2>
<p>After running <code>ListingSyncService.sync</code> in the Rails console:</p>
<pre><code>Listing.count                                    # =&gt; 213
Listing.where.not(hubspot_deal_id: nil).count    # =&gt; 213
</code></pre>
<p>213 listings saved to PostgreSQL. 213 HubSpot deals created with names like "Listing #91258", amounts like $1,293,233, and 30-day close dates. 213 rows in the Google Sheet with headers.</p>
<p>Running the sync a second time:</p>
<pre><code>Listing.count                                    # =&gt; 213  (no duplicates)
HubSpot deals created                            # =&gt; 0    (all already exist)
</code></pre>
<p>Idempotency confirmed.</p>
<hr />
<h2>Key Learnings</h2>
<p><strong>1. Service objects make testing trivial.</strong> When each service has one job, you stub one thing and test one thing. The alternative — fat models or fat controllers — makes tests brittle and slow.</p>
<p><strong>2. Idempotency is a design decision, not an afterthought.</strong> <code>find_or_create_by</code> + <code>hubspot_deal_id</code> nil check = a sync that can run 100 times safely. Design for repeated execution from the start.</p>
<p><strong>3. Windows is a special case.</strong> Most Rails documentation assumes Linux. libcurl, SSL certificates, PowerShell aliases — none of these bite you on a Linux server. If you're developing on Windows, budget time for platform-specific debugging.</p>
<p><strong>4. Memoize expensive operations.</strong> The <code>@client ||=</code> pattern in <code>GoogleSheetsService</code> creates one authenticated connection per sync run. Without it, every API call would re-authenticate — unnecessary and slower.</p>
<p><strong>5. Re-raise errors in background jobs.</strong> Catching an error and logging it silently means Sidekiq thinks the job succeeded. Re-raising means Sidekiq retries automatically. For production background jobs, retries are almost always what you want.</p>
<hr />
<h2>What I'd Add Next</h2>
<ul>
<li><strong>Pagination for listings over 1000</strong> — loop until empty works but could be parallelized with <code>ProcessListingJob</code> workers for scale</li>
<li><strong>Sentry for error alerting</strong> — know within minutes when a midnight sync fails</li>
<li><strong>Webhook from EF</strong> — instead of polling once per day, react to listing changes in real time</li>
<li><strong>Soft deletes for sold listings</strong> — currently sold listings stay in the database indefinitely; they should be marked and excluded from HubSpot</li>
</ul>
<hr />
<h2>The Bigger Picture</h2>
<p>Empire Flippers facilitates the sale of online businesses in the 5 to 8 figure USD range. Every listing in that Google Sheet and every deal in that HubSpot account represents a real business someone built — an Amazon FBA store, a content site, a SaaS product — and is now ready to sell.</p>
<p>Building integrations between systems is what Rails is genuinely good at. Not flashy frontends or complex algorithms — clean, maintainable code that connects APIs together and runs reliably in the background while you sleep.</p>
<p>That's what this project is.</p>
<hr />
<p><strong>💻 Source Code:</strong> <a href="https://github.com/imlakshay08/empire-sync">github.com/imlakshay08/empire-sync</a></p>
<p><em>If you've built HubSpot or Google Sheets integrations with Rails and ran into different issues — I'd love to hear about it in the comments.</em></p>
]]></content:encoded></item><item><title><![CDATA[Connecting a Biometric Fingerprint Device to a Rails Web App Using Python — A Complete Walkthrough]]></title><description><![CDATA[👉 Part of my Spine Fitness system — a production gym management platform built with Rails.Read the full system overview: Spine Fitness System Overview
Introduction
When I was building a gym managemen]]></description><link>https://imlakshay08-complete-ruby-on-rails.hashnode.dev/connecting-a-biometric-fingerprint-device-to-a-rails-web-app-using-python-a-complete-walkthrough</link><guid isPermaLink="true">https://imlakshay08-complete-ruby-on-rails.hashnode.dev/connecting-a-biometric-fingerprint-device-to-a-rails-web-app-using-python-a-complete-walkthrough</guid><category><![CDATA[Ruby on Rails]]></category><category><![CDATA[Python]]></category><category><![CDATA[Backend Development]]></category><category><![CDATA[System Design]]></category><category><![CDATA[api integration]]></category><category><![CDATA[webhooks]]></category><category><![CDATA[iot]]></category><dc:creator><![CDATA[Lakshay Tyagi]]></dc:creator><pubDate>Thu, 23 Apr 2026 02:15:44 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/62cdc1a71d29fac83b6d2d9c/683880d5-b1a4-41f7-bc99-589a492092ea.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>👉 Part of my Spine Fitness system — a production gym management platform built with Rails.<br />Read the full system overview: <a href="https://imlakshay08-complete-ruby-on-rails.hashnode.dev/gym-management-system-with-ruby-on-rails">Spine Fitness System Overview</a></p>
<h2>Introduction</h2>
<p>When I was building a <a href="https://github.com/imlakshay08/spine-fitness-gym-management-system">gym management system</a> for a real gym in New Delhi, one of the most interesting challenges was <strong>connecting a physical biometric fingerprint device to my cloud-hosted Ruby on Rails app</strong>.</p>
<p>The gym wanted to:</p>
<ul>
<li><p>Track member attendance via <strong>fingerprint scanning</strong></p>
</li>
<li><p><strong>Automatically deny access</strong> to members with expired subscriptions</p>
</li>
<li><p>Prevent <strong>duplicate check-ins</strong></p>
</li>
<li><p>See attendance on the <strong>admin dashboard</strong> in real-time</p>
</li>
</ul>
<p>The catch? The biometric device only speaks to the <strong>local network</strong>. My Rails app is hosted on <strong>Render</strong> (cloud). They can't talk to each other directly.</p>
<p>The solution? A <strong>Python bridge script</strong> running on the gym's laptop that reads fingerprint punches from the device and forwards them to the Rails API via HTTP.</p>
<p>This post covers the <strong>entire pipeline</strong> — Python bridge → Rails API → Database — with real code from production.</p>
<hr />
<h2>Architecture: The Full Pipeline</h2>
<pre><code class="language-plaintext">┌─────────────────────┐
│  ZK Fingerprint      │
│  Biometric Device    │
│  (192.168.1.201)     │
└──────────┬──────────┘
           │ pyzk SDK (TCP)
           ▼
┌──────────────────────────┐
│  Python Bridge Script    │
│  (bridge.py)             │
│  Runs on gym laptop      │
│  Polls every 20 seconds  │
│  Deduplicates locally    │
└──────────┬───────────────┘
           │ HTTP POST (JSON)
           ▼
┌──────────────────────────────────────┐
│  Ruby on Rails API (Render cloud)    │
│  POST /api/biometric_attendances     │
│                                      │
│  1. Find biometric mapping           │
│  2. Check for duplicate punches      │
│  3. Validate subscription            │
│  4. Store attendance                 │
│  5. Return ALLOWED / DENIED          │
└──────────┬───────────────────────────┘
           │
           ▼
┌──────────────────────────────────────┐
│  MySQL Database (CleverCloud)        │
│                                      │
│  trn_member_biometric_mappings       │
│  trn_member_attendances              │
│  trn_member_subscriptions            │
└──────────────────────────────────────┘
</code></pre>
<p>Three components, two languages, one seamless flow.</p>
<hr />
<h2>Part 1: The Python Bridge (Gym Laptop Side)</h2>
<h3>The Problem</h3>
<p>The biometric device (a ZK-based fingerprint scanner) is on the gym's <strong>local network</strong> at <code>192.168.1.201</code>. It stores fingerprints and punch records internally. It has no concept of "calling a web API."</p>
<p>My Rails app is hosted on Render — a <strong>cloud server</strong> that the device can't reach directly.</p>
<p><strong>Solution:</strong> A Python script that acts as the <strong>middleman</strong> — reads from the device using the <code>pyzk</code> SDK, and forwards punches to Rails via HTTP.</p>
<h3>Configuration</h3>
<pre><code class="language-python"># biometric_bridge/config.py

DEVICE_IP = "192.168.1.201"
DEVICE_PORT = 4370
DEVICE_TIMEOUT = 5

RAILS_API_URL = "https://spine-fitness.com/api/biometric_attendances"
COMP_CODE = "SF"

POLL_INTERVAL_SECONDS = 20
</code></pre>
<p>Key decisions:</p>
<ul>
<li><p><strong>Port 4370</strong> — Standard ZK biometric device communication port</p>
</li>
<li><p><strong>20-second polling</strong> — Balances real-time feel vs. not overwhelming the device</p>
</li>
<li><p><strong>Company code "SF"</strong> — Supports multi-tenant architecture (future-proof for multiple gyms)</p>
</li>
</ul>
<h3>The Bridge Script</h3>
<pre><code class="language-python"># biometric_bridge/bridge.py

from zk import ZK
import requests
import time
from datetime import datetime
from config import *

def send_to_rails(payload):
    try:
        response = requests.post(
            RAILS_API_URL,
            json=payload,
            timeout=5
        )
        print(f"Sent: {payload} | Response: {response.status_code}")
    except Exception as e:
        print("Rails API error:", e)

def main():
    zk = ZK(
        DEVICE_IP,
        port=DEVICE_PORT,
        timeout=DEVICE_TIMEOUT,
        password=0,
        force_udp=False,
        ommit_ping=False
    )

    print("Connecting to biometric device...")

    try:
        conn = zk.connect()
        conn.disable_device()

        print("Connected to device")
        print("Fetching attendance logs...")

        last_sent = set()

        while True:
            attendances = conn.get_attendance()

            for att in attendances:
                key = f"{att.user_id}-{att.timestamp}"

                # Prevent duplicate sending
                if key in last_sent:
                    continue

                payload = {
                    "compcode": COMP_CODE,
                    "user_id": att.user_id,
                    "timestamp": att.timestamp.strftime("%Y-%m-%d %H:%M:%S"),
                    "device_sn": conn.serial_number
                }

                send_to_rails(payload)
                last_sent.add(key)

            time.sleep(POLL_INTERVAL_SECONDS)

    except Exception as e:
        print("Device connection error:", e)

    finally:
        try:
            conn.enable_device()
            conn.disconnect()
        except:
            pass

if __name__ == "__main__":
    main()
</code></pre>
<h3>How It Works Step by Step</h3>
<p><strong>1. Connect to the device</strong></p>
<pre><code class="language-python">zk = ZK(DEVICE_IP, port=DEVICE_PORT, timeout=DEVICE_TIMEOUT, ...)
conn = zk.connect()
conn.disable_device()  # Prevents new operations while reading
</code></pre>
<p>The <code>pyzk</code> library connects to the ZK device via TCP on port 4370. We temporarily disable the device during reads to prevent data corruption.</p>
<p><strong>2. Poll every 20 seconds</strong></p>
<pre><code class="language-python">while True:
    attendances = conn.get_attendance()
    # ... process ...
    time.sleep(POLL_INTERVAL_SECONDS)
</code></pre>
<p>The script runs in an <strong>infinite loop</strong>, fetching all attendance records from the device. The device stores punches internally, so we get the full history each time.</p>
<p><strong>3. Deduplicate locally</strong></p>
<pre><code class="language-python">last_sent = set()

key = f"{att.user_id}-{att.timestamp}"
if key in last_sent:
    continue
</code></pre>
<p>Since <code>get_attendance()</code> returns <strong>all historical records</strong>, we use an in-memory set to track what's already been sent. Only new punches get forwarded. This is <strong>deduplication layer 1</strong> — the Rails API has its own deduplication as <strong>layer 2</strong>.</p>
<p><strong>4. Forward to Rails API</strong></p>
<pre><code class="language-python">payload = {
    "compcode": COMP_CODE,
    "user_id": att.user_id,
    "timestamp": att.timestamp.strftime("%Y-%m-%d %H:%M:%S"),
    "device_sn": conn.serial_number
}
send_to_rails(payload)
</code></pre>
<p>Each punch becomes a clean JSON payload with all the context Rails needs.</p>
<h3>Auto-Start on Windows</h3>
<p>The gym staff shouldn't need to "start the bridge" manually. A <code>.bat</code> file handles this:</p>
<pre><code class="language-bat">:: biometric_bridge/start_biometric.bat
cd C:\biometric_bridge
python bridge.py
</code></pre>
<p>This can be placed in the Windows <strong>Startup folder</strong> so the bridge starts automatically when the gym opens and the laptop powers on.</p>
<h3>Dependencies</h3>
<pre><code class="language-text"># biometric_bridge/requirements.txt
pyzk
requests
</code></pre>
<p>Just two dependencies — <code>pyzk</code> for ZK device communication and <code>requests</code> for HTTP calls. Minimal and reliable.</p>
<hr />
<h2>Part 2: The Rails API (Cloud Server Side)</h2>
<h3>The Route</h3>
<pre><code class="language-ruby"># config/routes.rb
Rails.application.routes.draw do
  namespace :api do
    resources :biometric_attendances, only: [:create]
  end
end
</code></pre>
<p>This gives us: <code>POST /api/biometric_attendances</code></p>
<h3>The Controller</h3>
<p>When the Python bridge sends a punch, the Rails API processes it through a 4-step pipeline:</p>
<pre><code class="language-ruby"># app/controllers/api/biometric_attendances_controller.rb

class Api::BiometricAttendancesController &lt; ApplicationController
  skip_before_action :verify_authenticity_token

  def create
    compcode       = params[:compcode].to_s
    device_user_id = params[:user_id].to_i
    device_sn      = params[:device_sn].to_s
    punch_time     = Time.zone.parse(params[:timestamp]) rescue Time.current

    # ── STEP 1: Find biometric mapping ──
    mapping = TrnMemberBiometricMapping.active.find_by(
      mbm_compcode:       compcode,
      mbm_device_user_id: device_user_id,
      mbm_device_sn:      device_sn
    )

    unless mapping
      render json: {
        status: false,
        message: "Biometric user not mapped"
      }, status: 404
      return
    end

    member = mapping.member

    # ── STEP 2: Ignore duplicate punches (same member, same minute) ──
    if duplicate_punch?(member.id, punch_time)
      render json: { status: true, message: "Duplicate ignored" }
      return
    end

    # ── STEP 3: Validate subscription ──
    subscription = latest_subscription(member.id, compcode)

    if subscription &amp;&amp; subscription.ms_end_date &gt;= Date.today
      att_status = "ALLOWED"
      reason     = nil
    else
      att_status = "DENIED"
      reason     = "Subscription expired"
    end

    # ── STEP 4: Store attendance ──
    TrnMemberAttendance.create!(
      att_compcode:       compcode,
      att_member_id:      member.id,
      att_device_user_id: device_user_id,
      att_device_sn:      device_sn,
      att_punch_time:     punch_time,
      att_punch_date:     punch_time.to_date,
      att_status:         att_status,
      att_reason:         reason
    )

    render json: { status: true, access: att_status }
  end

  private

  def duplicate_punch?(member_id, time)
    TrnMemberAttendance.where(
      att_member_id: member_id,
      att_punch_time: time.beginning_of_minute..time.end_of_minute
    ).exists?
  end

  def latest_subscription(member_id, compcode)
    TrnMemberSubscription
      .where(ms_compcode: compcode, ms_member_id: member_id)
      .order(ms_end_date: :desc)
      .first
  end
end
</code></pre>
<hr />
<h2>Two Layers of Deduplication</h2>
<p>This is important — deduplication happens at <strong>both levels</strong>:</p>
<table>
<thead>
<tr>
<th>Layer</th>
<th>Where</th>
<th>How</th>
<th>Why</th>
</tr>
</thead>
<tbody><tr>
<td><strong>Layer 1</strong></td>
<td>Python bridge</td>
<td>In-memory <code>set</code> of <code>{user_id}-{timestamp}</code> keys</td>
<td>Prevents resending the same device record on every poll cycle</td>
</tr>
<tr>
<td><strong>Layer 2</strong></td>
<td>Rails API</td>
<td>Database query for same member + same minute</td>
<td>Catches duplicates if the bridge restarts (set resets), or if the device sends duplicate records</td>
</tr>
</tbody></table>
<pre><code class="language-plaintext">Member scans finger
       │
       ▼
Python bridge: "Already in last_sent?" ──YES──▶ Skip
       │ NO
       ▼
Rails API: "Punch in same minute?" ──YES──▶ Return "Duplicate ignored"
       │ NO
       ▼
Store attendance 
</code></pre>
<p>This double-layer approach means the system is resilient to:</p>
<ul>
<li><p>Bridge restarts (set clears → Layer 2 catches it)</p>
</li>
<li><p>Device quirks (some ZK devices record multiple entries per scan)</p>
</li>
<li><p>Network retries (if the bridge retries a failed request)</p>
</li>
</ul>
<hr />
<h2>Database Design</h2>
<h3>The Bridge Table: <code>trn_member_biometric_mappings</code></h3>
<table>
<thead>
<tr>
<th>Column</th>
<th>Type</th>
<th>Purpose</th>
</tr>
</thead>
<tbody><tr>
<td><code>mbm_compcode</code></td>
<td>string</td>
<td>Company code (multi-tenant)</td>
</tr>
<tr>
<td><code>mbm_device_user_id</code></td>
<td>integer</td>
<td>User ID on the biometric device</td>
</tr>
<tr>
<td><code>mbm_device_sn</code></td>
<td>string</td>
<td>Device serial number</td>
</tr>
<tr>
<td><code>mbm_member_id</code></td>
<td>integer</td>
<td>FK → <code>mst_members_lists.id</code></td>
</tr>
<tr>
<td><code>mbm_status</code></td>
<td>string</td>
<td><code>ACTIVE</code> / <code>INACTIVE</code></td>
</tr>
</tbody></table>
<p><strong>Why is this needed?</strong> The biometric device assigns its own user IDs (1, 2, 3...). These don't match your database. This table says <em>"Device user #42 on device SN-ABC = Member Rahul Sharma (ID: 156)"</em>.</p>
<h3>The Attendance Table: <code>trn_member_attendances</code></h3>
<table>
<thead>
<tr>
<th>Column</th>
<th>Type</th>
<th>Purpose</th>
</tr>
</thead>
<tbody><tr>
<td><code>att_member_id</code></td>
<td>integer</td>
<td>FK → member</td>
</tr>
<tr>
<td><code>att_device_user_id</code></td>
<td>integer</td>
<td>Device user ID</td>
</tr>
<tr>
<td><code>att_device_sn</code></td>
<td>string</td>
<td>Device serial</td>
</tr>
<tr>
<td><code>att_punch_time</code></td>
<td>datetime</td>
<td>Exact punch time</td>
</tr>
<tr>
<td><code>att_punch_date</code></td>
<td>date</td>
<td>For easy date-based queries</td>
</tr>
<tr>
<td><code>att_status</code></td>
<td>string</td>
<td><code>ALLOWED</code> or <code>DENIED</code></td>
</tr>
<tr>
<td><code>att_reason</code></td>
<td>string</td>
<td>Why denied (if applicable)</td>
</tr>
</tbody></table>
<p><strong>Key design decision:</strong> Even <code>DENIED</code> attempts are stored. This lets the gym owner see which expired members are still trying to come — useful for renewal follow-ups.</p>
<hr />
<h2>Testing the Full Pipeline</h2>
<h3>Test with cURL (bypassing the Python bridge)</h3>
<pre><code class="language-bash"># Active member
curl -X POST https://spine-fitness.com/api/biometric_attendances \
  -H "Content-Type: application/json" \
  -d '{
    "compcode": "SF",
    "user_id": 42,
    "device_sn": "CRT5200-SN001",
    "timestamp": "2026-03-12 07:30:00"
  }'
# → {"status":true,"access":"ALLOWED"}

# Expired member
# → {"status":true,"access":"DENIED"}

# Unknown biometric ID
# → {"status":false,"message":"Biometric user not mapped"}
</code></pre>
<h3>Test the Python bridge locally</h3>
<pre><code class="language-bash">cd biometric_bridge
pip install -r requirements.txt
python bridge.py
# → Connecting to biometric device...
# → Connected to device
# → Fetching attendance logs...
# → Sent: {...} | Response: 200
</code></pre>
<hr />
<h2>Edge Cases I Solved</h2>
<table>
<thead>
<tr>
<th>Edge Case</th>
<th>Solution</th>
</tr>
</thead>
<tbody><tr>
<td>Device returns ALL historical records every poll</td>
<td>Python-side <code>last_sent</code> set filters to only new records</td>
</tr>
<tr>
<td>Member scans finger 3 times rapidly</td>
<td>Rails-side 1-minute deduplication window</td>
</tr>
<tr>
<td>Bridge script crashes / laptop restarts</td>
<td><code>.bat</code> file in Startup folder auto-restarts; Rails Layer 2 catches re-sent duplicates</td>
</tr>
<tr>
<td>Gym has multiple devices</td>
<td><code>device_sn</code> is part of the mapping — same user_id on different devices = different members</td>
</tr>
<tr>
<td>Timestamp timezone mismatch</td>
<td><code>Time.zone.parse</code> with <code>rescue Time.current</code> fallback</td>
</tr>
<tr>
<td>Member renews subscription mid-day</td>
<td><code>ms_end_date &gt;= Date.today</code> check means renewal takes effect immediately</td>
</tr>
<tr>
<td>Member leaves permanently</td>
<td>Set <code>mbm_status</code> to <code>INACTIVE</code> — <code>.active</code> scope blocks without deleting data</td>
</tr>
<tr>
<td>Network/API timeout</td>
<td>Python <code>requests.post(timeout=5)</code> with try/except — failed sends are logged, not fatal</td>
</tr>
</tbody></table>
<hr />
<h2>How This Powers the Dashboard</h2>
<p>All attendance data flows to the admin dashboard in real-time:</p>
<ul>
<li><p>🟢 <strong>Active members</strong> — subscription valid, attendance tracked</p>
</li>
<li><p>🟡 <strong>Expiring soon</strong> — auto-triggers WhatsApp reminders</p>
</li>
<li><p>🔴 <strong>Expired</strong> — DENIED attendance logged, visible to gym owner</p>
</li>
</ul>
<p>The gym owner sees a member scan their finger, and within seconds the dashboard reflects it — all without touching a single register.</p>
<hr />
<h2>Key Takeaways</h2>
<ol>
<li><p><strong>Use a bridge pattern</strong> when hardware can't talk to cloud directly. A simple Python script solved the hardware↔cloud gap.</p>
</li>
<li><p><strong>Deduplicate at every layer.</strong> Don't trust any single layer to handle it perfectly.</p>
</li>
<li><p><strong>Store denied attempts.</strong> They're not failures — they're business intelligence.</p>
</li>
<li><p><strong>Keep the bridge minimal.</strong> Two dependencies (<code>pyzk</code> + <code>requests</code>), one config file, one script. Less can go wrong.</p>
</li>
<li><p><strong>Auto-start everything.</strong> The gym staff shouldn't need to know Python exists. A <code>.bat</code> file in Startup and it just works.</p>
</li>
<li><p><strong>Multi-language is fine.</strong> Python is better at hardware communication (pyzk), Rails is better at web apps. Use the right tool for each layer.</p>
</li>
</ol>
<hr />
<h2>Conclusion</h2>
<p>This feature taught me that <strong>production software often lives at the intersection of hardware and software</strong>. The biometric device, the Python bridge, the Rails API, and the MySQL database — four different technologies working together to create a seamless experience: member scans finger → attendance appears on dashboard.</p>
<p>The most satisfying moment? Watching the gym owner check the dashboard and see real-time attendance without touching a single notebook.</p>
<hr />
<p><strong>🔗 Live App:</strong> <a href="https://spine-fitness.com">spine-fitness.com</a> <strong>💻 Full Source Code:</strong> <a href="https://github.com/imlakshay08/spine-fitness-gym-management-system">GitHub</a> <strong>📂 Python Bridge Code:</strong> <a href="https://github.com/imlakshay08/spine-fitness-gym-management-system/tree/main/biometric_bridge">biometric_bridge/</a></p>
<p><em>Found this useful? Drop a ❤️! Got questions about biometric integration or the Python bridge? Let's chat in the comments!</em></p>
]]></content:encoded></item><item><title><![CDATA[How I Ditched Interakt and Built a Direct WhatsApp Automation Pipeline with Meta Cloud API]]></title><description><![CDATA[A follow-up to my previous post on building Spine Fitness — a production gym management system used by 200+ members daily.
👉 In this post, I’ll walk through why I replaced Interakt with Meta Cloud AP]]></description><link>https://imlakshay08-complete-ruby-on-rails.hashnode.dev/how-i-ditched-interakt-and-built-a-direct-whatsapp-automation-pipeline-with-meta-cloud-api</link><guid isPermaLink="true">https://imlakshay08-complete-ruby-on-rails.hashnode.dev/how-i-ditched-interakt-and-built-a-direct-whatsapp-automation-pipeline-with-meta-cloud-api</guid><category><![CDATA[Ruby on Rails]]></category><category><![CDATA[Backend Development]]></category><category><![CDATA[System Design]]></category><category><![CDATA[webhooks]]></category><category><![CDATA[whatsapp-api]]></category><category><![CDATA[api integration]]></category><category><![CDATA[Software Engineering]]></category><dc:creator><![CDATA[Lakshay Tyagi]]></dc:creator><pubDate>Thu, 23 Apr 2026 01:33:48 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/62cdc1a71d29fac83b6d2d9c/0cd2d32b-edc4-490e-8be7-09833e88ad07.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p><em>A follow-up to my previous post on building Spine Fitness — a production gym management system used by 200+ members daily.</em></p>
<p>👉 In this post, I’ll walk through why I replaced Interakt with Meta Cloud API, the issues I faced, and how I built a reliable, production-ready WhatsApp automation pipeline.</p>
<hr />
<h2>The Backstory</h2>
<p>A few months ago, I published a post about building <strong>Spine Fitness</strong> — a full-stack gym management system I deployed for a real gym in Dwarka, New Delhi. The system replaced physical notebooks with a Rails app that handled member management, biometric attendance, payments, and WhatsApp notifications.</p>
<p>In that post, I mentioned using <strong>Interakt</strong> as the WhatsApp API provider.</p>
<p>I no longer use Interakt.</p>
<p>This is the story of why I switched, the absolute nightmare of dealing with Meta's ecosystem, and how I eventually built a clean, direct integration with <strong>Meta Cloud API</strong> — from scratch — that now reliably sends automated membership reminders to gym members every morning at 10 AM IST.</p>
<hr />
<h2>What Was Working Before (And What Wasn't)</h2>
<p>The Interakt setup seemed fine on paper. I had:</p>
<ul>
<li><p>A connected WhatsApp number (8920)</p>
</li>
<li><p>Approved message templates</p>
</li>
<li><p>A Rails service (<code>Interakt::SendWhatsapp</code>) making API calls</p>
</li>
<li><p>A cron job firing daily at 10 AM</p>
</li>
<li><p>A <code>trn_whatsapp_logs</code> table logging every attempt</p>
</li>
</ul>
<p>But when I checked the logs, something was wrong.</p>
<p>Every single message had <code>wl_status = 'QUEUED'</code>. No delivered. No read. Just QUEUED — forever.</p>
<pre><code class="language-sql">SELECT wl_status, COUNT(*) FROM trn_whatsapp_logs GROUP BY wl_status;
-- QUEUED: 13
-- DELIVERED: 0
-- READ: 0
</code></pre>
<p>Interakt's dashboard showed messages going out. Single tick on WhatsApp. Members never received anything.</p>
<p>I contacted Interakt support. They said the number would be removed from their system. Weeks passed. It wasn't removed. Meanwhile, messages kept getting queued and silently dropped.</p>
<p>I decided to move on.</p>
<hr />
<h2>The Plan: Go Direct with Meta Cloud API</h2>
<p>Instead of relying on a BSP (Business Solution Provider) like Interakt, I'd connect directly to Meta's WhatsApp Cloud API. Same infrastructure the big players use — no middleman, full control.</p>
<p>The plan was simple:</p>
<ol>
<li><p>Register 8920 on Meta Cloud API</p>
</li>
<li><p>Create message templates</p>
</li>
<li><p>Replace <code>Interakt::SendWhatsapp</code> with <code>Meta::SendWhatsapp</code> in Rails</p>
</li>
<li><p>Set up a webhook for delivery status updates</p>
</li>
</ol>
<p>Simple in theory. Absolutely chaotic in practice.</p>
<hr />
<h2>Week One: The Meta Setup Maze</h2>
<h3>Haptik Was Everywhere</h3>
<p>When I logged into Meta Business Manager, I found that both my numbers (7011 and 8920) had <strong>Haptik</strong> (Interakt's parent company) as a partner with full control. Even after Interakt said they removed 8920, Haptik still appeared in the partners tab.</p>
<p>This meant any attempt to register 8920 directly on Meta Cloud API failed with:</p>
<pre><code class="language-plaintext">"Unsupported post request. Object with ID '1073316579198774' does not exist,
cannot be loaded due to missing permissions..."
</code></pre>
<p>The number existed. It just wasn't mine yet.</p>
<h3>The Payment Method Loop</h3>
<p>After finally getting Interakt to release 8920, I tried adding a payment method to the WhatsApp Business Account. Meta charged my card ₹3 as verification — four separate times — and never actually saved it.</p>
<p>The Developer Console kept screaming "Missing valid payment method" even after the WABA settings clearly showed Visa ****4009 as default. I eventually realized this was a Meta UI bug specific to India accounts. The payment was there. The console just couldn't see it.</p>
<h3>The Display Name That Wouldn't Approve</h3>
<p>I registered 8920 on Meta Cloud API and submitted "Spine Fitness" as the display name. It went into <code>PENDING_REVIEW</code>.</p>
<p>It stayed there for <strong>two days</strong>.</p>
<p>Then it came back as <code>DECLINED</code>.</p>
<p>Apparently "Spine Fitness" was too generic. Meta's guidelines require the display name to clearly and uniquely represent your business. I resubmitted as <strong>"Spine Fitness Gym Dwarka"</strong> — specific enough to pass their guidelines — and it was approved within hours.</p>
<hr />
<h2>The Code Change: Surprisingly Clean</h2>
<p>Once the Meta side was sorted, the Rails code change was actually minimal. I created a new service file:</p>
<pre><code class="language-ruby"># app/services/meta/send_whatsapp.rb
module Meta
  class SendWhatsapp
    API_URL = "https://graph.facebook.com/v19.0"

    def self.send_template(phone:, template:, body_values:)
      phone = phone.to_s.gsub(/\D/, "").last(10)
      return { http_code: 0, body: {}, raw: "Invalid phone" } unless phone.length == 10

      uri = URI("#{API_URL}/#{ENV['WHATSAPP_PHONE_ID']}/messages")

      payload = {
        messaging_product: "whatsapp",
        to: "91#{phone}",
        type: "template",
        template: {
          name: template,
          language: { code: "en" },
          components: [{
            type: "body",
            parameters: body_values.map { |v| { type: "text", text: v.to_s } }
          }]
        }
      }

      http = Net::HTTP.new(uri.host, uri.port)
      http.use_ssl = true
      request = Net::HTTP::Post.new(uri)
      request["Authorization"] = "Bearer #{ENV['WHATSAPP_TOKEN']}"
      request["Content-Type"] = "application/json"
      request.body = payload.to_json

      response = http.request(request)
      parsed_body = JSON.parse(response.body) rescue {}
      { http_code: response.code.to_i, body: parsed_body, raw: response.body }
    end
  end
end
</code></pre>
<p>And changed one line in the job:</p>
<pre><code class="language-ruby"># Before
response = Interakt::SendWhatsapp.send_template(...)

# After
response = Meta::SendWhatsapp.send_template(...)
</code></pre>
<p>Two environment variables on Render:</p>
<pre><code class="language-plaintext">WHATSAPP_PHONE_ID=1073316579198774
WHATSAPP_TOKEN=&lt;permanent system user token&gt;
</code></pre>
<p>That was it for the sending side.</p>
<hr />
<h2>The Template Problem: Meta Hates "Renew Now"</h2>
<p>My original template said:</p>
<blockquote>
<p><em>"Hi {{1}}, your membership at Spine Fitness expires on {{2}}. Renew now to avoid interruption. Contact us or visit the gym."</em></p>
</blockquote>
<p>Meta flagged it as Marketing. Every rewrite got flagged. Anything with "renew", "visit us", or "contact us" triggered the marketing classifier.</p>
<p>The fix was to make it purely transactional — no call to action, no promotional language:</p>
<blockquote>
<p><em>"Your membership at Spine Fitness (ID: {{1}}) will expire on {{2}}. This is an automated notification."</em></p>
</blockquote>
<p>Boring? Yes. Approved as Utility? Also yes. And Utility templates are cheaper and have fewer delivery restrictions than Marketing ones.</p>
<hr />
<h2>The Delivery Problem: App Was Unpublished</h2>
<p>After all of this, messages were still only delivering to numbers that had previously messaged 8920 first. My number worked. My mother's worked (after she sent "Hi" to 8920 first). Everyone else got accepted by the API but never actually received the message.</p>
<p>I spent a long time chasing the wrong culprits — display name status, payment method, credit lines, contact book theory. The actual reason was simpler and more embarrassing:</p>
<p><strong>My Meta Developer App was unpublished.</strong></p>
<p>In development mode, WhatsApp API has severe restrictions on who can receive messages. The moment I published the app (which required adding a privacy policy URL, an app icon, and completing app review), messages started going to everyone — no prior interaction required.</p>
<p>That single toggle fixed what two weeks of debugging couldn't.</p>
<hr />
<h2>Adding Webhook Delivery Tracking</h2>
<p>With Interakt, I never got real delivery status back. With Meta, I could set up a webhook to receive <code>sent</code>, <code>delivered</code>, and <code>read</code> status updates in real time.</p>
<pre><code class="language-ruby"># app/controllers/webhooks/meta_controller.rb
class Webhooks::MetaController &lt; ApplicationController
  skip_before_action :verify_authenticity_token

  def verify
    mode      = params['hub.mode']
    token     = params['hub.verify_token']
    challenge = params['hub.challenge']

    if mode == 'subscribe' &amp;&amp; token == ENV['WHATSAPP_WEBHOOK_TOKEN']
      render plain: challenge, status: :ok
    else
      head :forbidden
    end
  end

  def receive
    body = JSON.parse(request.body.read)
    entries = body.dig('entry') || []

    entries.each do |entry|
      entry.dig('changes')&amp;.each do |change|
        change.dig('value', 'statuses')&amp;.each do |status|
          process_status(status)
        end
      end
    end

    head :ok
  rescue =&gt; e
    Rails.logger.error "[MetaWebhook] Error: #{e.message}"
    head :ok
  end

  private

  def process_status(status)
    message_id = status['id']
    status_val = status['status']&amp;.upcase
    return unless message_id.present?
    return unless %w[DELIVERED READ FAILED SENT].include?(status_val)

    log = TrnWhatsappLog.find_by(wl_interakt_msg_id: message_id)
    return unless log

    case status_val
    when 'DELIVERED'
      log.update!(wl_status: 'DELIVERED', wl_delivered_at: Time.current)
    when 'READ'
      log.update!(wl_status: 'READ', wl_read_at: Time.current)
    when 'FAILED'
      error = status.dig('errors', 0, 'message') || 'Unknown error'
      log.update!(wl_status: 'FAILED', wl_failed_reason: error)
    end

    Rails.logger.info "[MetaWebhook] Updated log #{log.id} → #{status_val}"
  end
end
</code></pre>
<p>One important step that wasn't obvious from the docs — I had to explicitly subscribe my app to the correct WABA via API:</p>
<pre><code class="language-bash">curl -X POST \
  "https://graph.facebook.com/v19.0/1603252984268401/subscribed_apps" \
  -H "Authorization: Bearer YOUR_TOKEN"
</code></pre>
<p>Without this, the webhook configuration in the Developer Console subscribes to the test WABA, not your production one. Real delivery events never arrive.</p>
<p>Once subscribed correctly, the logs updated in real time:</p>
<pre><code class="language-plaintext">[MetaWebhook] Updated log 30 → READ
[MetaWebhook] Updated log 32 → READ
</code></pre>
<hr />
<h2>The Final State</h2>
<p>Here's what the full pipeline looks like now:</p>
<pre><code class="language-plaintext">cron-job.org (daily 4:30 UTC / 10:00 AM IST)
    │
    ▼
GET /cron/send_expiry_whatsapp
    │
    ▼
MembershipExpiryWhatsappJob (:expiring / :expired)
    │
    ├── Query members expiring in 3 days (or already expired)
    ├── Skip if already DELIVERED or READ
    ├── Call Meta Cloud API
    ├── Log response to trn_whatsapp_logs (status: QUEUED)
    │
    ▼
Meta WhatsApp Cloud API
    │
    ▼
Member's WhatsApp (message delivered)
    │
    ▼
POST /webhooks/meta (delivery status webhook)
    │
    ▼
trn_whatsapp_logs updated: QUEUED → DELIVERED → READ
</code></pre>
<p>And the numbers after going live:</p>
<table>
<thead>
<tr>
<th>Metric</th>
<th>Interakt</th>
<th>Meta Cloud API</th>
</tr>
</thead>
<tbody><tr>
<td>Messages delivered</td>
<td>0</td>
<td>✅ All</td>
</tr>
<tr>
<td>Status tracking</td>
<td>❌ Never updated</td>
<td>✅ Real-time</td>
</tr>
<tr>
<td>Cost per message</td>
<td>~₹0.30 + BSP fee</td>
<td>₹0.12 (utility)</td>
</tr>
<tr>
<td>Cold outreach</td>
<td>❌ Broken</td>
<td>✅ Works</td>
</tr>
<tr>
<td>Setup pain</td>
<td>Low</td>
<td>Very high</td>
</tr>
</tbody></table>
<hr />
<h2>What I'd Tell Myself Before Starting</h2>
<p><strong>1. Go direct from the start.</strong> BSPs like Interakt add cost and a dependency. If you're building something custom, Meta Cloud API gives you full control and better pricing.</p>
<p><strong>2. Publish your app early.</strong> The development mode restriction is the least documented and most impactful limitation. You'll waste days debugging delivery issues that disappear the moment you go live.</p>
<p><strong>3. Display names matter more than you think.</strong> Generic names get declined. Be specific — include your city, your category, something that makes the name uniquely yours.</p>
<p><strong>4. Subscribe your webhook to the right WABA.</strong> The Developer Console subscribes to the test WABA by default. Make the API call to subscribe your production WABA explicitly.</p>
<p><strong>5. Use Utility templates, not Marketing.</strong> Avoid action words. Make it sound like a system notification. It's cheaper and has fewer delivery restrictions.</p>
<hr />
<h2>What's Next</h2>
<p>The automation is live and running daily. Members are receiving expiry reminders automatically. The gym owner stopped making manual phone calls.</p>
<p>Next up: a member-facing view so members can check their own subscription status, and SMS fallback for members not on WhatsApp.</p>
<hr />
<p><em>Spine Fitness is live at</em> <a href="https://spine-fitness.com"><em>spine-fitness.com</em></a><em>. Source code on</em> <a href="https://github.com/imlakshay08/spine-fitness-gym-management-system"><em>GitHub</em></a><em>.</em></p>
<p><em>If you found this useful — or if you've been through the Meta API maze yourself — drop a comment. I'd love to hear your war stories.</em></p>
]]></content:encoded></item><item><title><![CDATA[Building a Production Gym Management System with Ruby on Rails]]></title><description><![CDATA[Hey there!
I'm Lakshay, and I built Spine Fitness — a full-stack gym management system using Ruby on Rails that's currently deployed in production and used daily by a real gym in Dwarka, New Delhi.
Th]]></description><link>https://imlakshay08-complete-ruby-on-rails.hashnode.dev/gym-management-system-with-ruby-on-rails</link><guid isPermaLink="true">https://imlakshay08-complete-ruby-on-rails.hashnode.dev/gym-management-system-with-ruby-on-rails</guid><category><![CDATA[Ruby]]></category><category><![CDATA[Ruby on Rails]]></category><category><![CDATA[backend]]></category><category><![CDATA[rubyonrails]]></category><category><![CDATA[Software Engineering]]></category><category><![CDATA[Web Development]]></category><category><![CDATA[GYM Management System ]]></category><dc:creator><![CDATA[Lakshay Tyagi]]></dc:creator><pubDate>Fri, 13 Mar 2026 04:09:22 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/62cdc1a71d29fac83b6d2d9c/d2c090b8-a4f9-4507-aa3f-d2e94d8f4087.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2>Hey there!</h2>
<p>I'm Lakshay, and I built <strong>Spine Fitness</strong> — a full-stack gym management system using Ruby on Rails that's currently <strong>deployed in production and used daily</strong> by a real gym in Dwarka, New Delhi.</p>
<p>The gym was managing <strong>200+ members using physical notebooks</strong>. Membership renewals were missed, payments were scattered, and attendance tracking was a mess. I built a web platform to replace all of it.</p>
<p><strong>🔗 Live at:</strong> <a href="https://spine-fitness.com">spine-fitness.com</a> <strong>💻 Source Code:</strong> <a href="https://github.com/imlakshay08/spine-fitness-gym-management-system">GitHub Repository</a></p>
<hr />
<h2>The Problem: Death by Notebooks</h2>
<p>Before the software, the gym staff dealt with:</p>
<ul>
<li><p>Member registrations handwritten in registers</p>
</li>
<li><p>Membership renewals forgotten — lost revenue</p>
</li>
<li><p>Attendance tracked inconsistently (or not at all)</p>
</li>
<li><p>Payment history scattered across different books</p>
</li>
<li><p>Inventory (equipment, supplements) completely untracked</p>
</li>
<li><p>Member communication required manual phone calls</p>
</li>
</ul>
<p><strong>Result?</strong> Human errors, missed renewals, zero visibility, and hours of admin work daily.</p>
<hr />
<h2>The Solution: Spine Fitness</h2>
<p>A centralized web platform where gym administrators manage everything digitally — from member onboarding to automated WhatsApp expiry reminders.</p>
<h3>Tech Stack at a Glance</h3>
<table>
<thead>
<tr>
<th>Layer</th>
<th>Technology</th>
</tr>
</thead>
<tbody><tr>
<td><strong>Backend</strong></td>
<td>Ruby on Rails 7.1, Ruby 3.1.4</td>
</tr>
<tr>
<td><strong>Database</strong></td>
<td>MySQL (CleverCloud)</td>
</tr>
<tr>
<td><strong>Frontend</strong></td>
<td>HTML, CSS, JavaScript (Hotwire/Turbo + Stimulus)</td>
</tr>
<tr>
<td><strong>Hosting</strong></td>
<td>Render</td>
</tr>
<tr>
<td><strong>Biometric Bridge</strong></td>
<td>Python 3 (pyzk + requests) — runs on gym laptop</td>
</tr>
<tr>
<td><strong>WhatsApp API</strong></td>
<td>Interakt</td>
</tr>
<tr>
<td><strong>PDF Reports</strong></td>
<td>Prawn</td>
</tr>
<tr>
<td><strong>Scheduling</strong></td>
<td>cron-job.com</td>
</tr>
<tr>
<td><strong>Hardware</strong></td>
<td>ZK Fingerprint biometric device</td>
</tr>
</tbody></table>
<blockquote>
<p>⚠️ <strong>Update (2026):</strong> This system originally used Interakt for WhatsApp messaging. Due to reliability issues, I later replaced it with a direct integration using Meta Cloud API.</p>
<p>👉 Full migration breakdown: <a href="https://imlakshay08-complete-ruby-on-rails.hashnode.dev/how-i-ditched-interakt-and-built-a-direct-whatsapp-automation-pipeline-with-meta-cloud-api">How I Replaced Interakt with Meta Cloud API</a></p>
</blockquote>
<h3>Architecture Overview</h3>
<pre><code class="language-plaintext">Gym Admin (Browser)
   │
   ▼
Ruby on Rails Application (Render)
   │
   ├── MySQL Database (CleverCloud)
   │
   ├── WhatsApp Messaging (Interakt API)
   │
   ├── Scheduled Jobs (cron-job.com)
   │
   └── REST API: POST /api/biometric_attendances
                    ▲
                    │ HTTP POST (every 20 seconds)
                    │
         ┌─────────┴──────────┐
         │  Python Bridge      │
         │  (bridge.py)        │
         │  Runs on gym laptop │
         └─────────┬──────────┘
                    │ pyzk SDK
                    ▼
         ┌─────────────────────┐
         │  ZK Fingerprint     │
         │  Biometric Device   │
         │  (192.168.1.201)    │
         └─────────────────────┘
</code></pre>
<p>The system is <strong>multi-language</strong> — Ruby on Rails handles the web app and business logic, while a <strong>Python script running on the gym's local laptop</strong> bridges the physical biometric device to the cloud-hosted Rails API.</p>
<h3>Python Biometric Bridge (Runs on Gym Laptop)</h3>
<p>The biometric fingerprint device sits on the gym's local network. It doesn't natively talk to cloud APIs. So I wrote a <strong>Python bridge script</strong> that runs continuously on the gym's Windows laptop:</p>
<pre><code class="language-python"># biometric_bridge/bridge.py

from zk import ZK
import requests
import time
from config import *

def send_to_rails(payload):
    try:
        response = requests.post(
            RAILS_API_URL,
            json=payload,
            timeout=5
        )
        print(f"Sent: {payload} | Response: {response.status_code}")
    except Exception as e:
        print("Rails API error:", e)

def main():
    zk = ZK(
        DEVICE_IP,
        port=DEVICE_PORT,
        timeout=DEVICE_TIMEOUT,
        password=0,
        force_udp=False,
        ommit_ping=False
    )

    print("Connecting to biometric device...")

    try:
        conn = zk.connect()
        conn.disable_device()

        last_sent = set()

        while True:
            attendances = conn.get_attendance()

            for att in attendances:
                key = f"{att.user_id}-{att.timestamp}"

                if key in last_sent:
                    continue

                payload = {
                    "compcode": COMP_CODE,
                    "user_id": att.user_id,
                    "timestamp": att.timestamp.strftime("%Y-%m-%d %H:%M:%S"),
                    "device_sn": conn.serial_number
                }

                send_to_rails(payload)
                last_sent.add(key)

            time.sleep(POLL_INTERVAL_SECONDS)

    except Exception as e:
        print("Device connection error:", e)
    finally:
        try:
            conn.enable_device()
            conn.disconnect()
        except:
            pass

if __name__ == "__main__":
    main()
</code></pre>
<p><strong>How it works:</strong></p>
<ol>
<li><p>Connects to the ZK biometric device on the local network (<code>192.168.1.201:4370</code>) using the <code>pyzk</code> library</p>
</li>
<li><p>Polls attendance logs <strong>every 20 seconds</strong></p>
</li>
<li><p>Deduplicates locally using an in-memory set (<code>last_sent</code>)</p>
</li>
<li><p>Sends each new punch as an HTTP POST to <code>https://spine-fitness.com/api/biometric_attendances</code></p>
</li>
<li><p>Runs as a background process via a <code>.bat</code> file on Windows startup</p>
</li>
</ol>
<pre><code class="language-bat">:: biometric_bridge/start_biometric.bat
cd C:\biometric_bridge
python bridge.py
</code></pre>
<p>This makes the system <strong>truly full-stack</strong> — from hardware on the gym floor, through a Python bridge on a local laptop, to a Rails API in the cloud.</p>
<h2>🗄️ Database Design: Master-Transaction Pattern</h2>
<p>I structured the database into <strong>Master tables</strong> (static data) and <strong>Transaction tables</strong> (operational data):</p>
<p><strong>Master Tables:</strong></p>
<ul>
<li><p><code>mst_members_lists</code> — Member profiles</p>
</li>
<li><p><code>mst_membership_plans</code> — Plan types (monthly, quarterly, annual)</p>
</li>
<li><p><code>mst_staff_lists</code> / <code>mst_trainer_lists</code> — Staff records</p>
</li>
<li><p><code>mst_stock_lists</code> — Equipment &amp; inventory</p>
</li>
</ul>
<p><strong>Transaction Tables:</strong></p>
<ul>
<li><p><code>trn_member_subscriptions</code> — Active subscriptions with dates</p>
</li>
<li><p><code>trn_member_attendances</code> — Biometric punch records</p>
</li>
<li><p><code>trn_payments</code> — Payment records linked to subscriptions</p>
</li>
<li><p><code>trn_whatsapp_logs</code> — Message delivery tracking</p>
</li>
<li><p><code>trn_audit_trials</code> — Full audit log of all actions</p>
</li>
</ul>
<p>This separation keeps queries efficient and business logic clean.</p>
<hr />
<h2>Key Features</h2>
<h3>1. Member Management</h3>
<p>Members get auto-generated codes using a custom <code>GlobalCodeGenerator</code>:</p>
<pre><code class="language-ruby">@Lastcode = generate_code(
  table: MstMembersList,
  column: "mmbr_code",
  prefix: "M",
  compcode: session[:loggedUserCompCode]
)
</code></pre>
<p>Each member links to subscriptions, payments, and biometric mappings.</p>
<h3>2. Subscription &amp; Payment Tracking</h3>
<p>The system supports <strong>partial payments</strong> — a real-world requirement:</p>
<pre><code class="language-ruby">def payment_status(subscription)
  paid  = total_paid(subscription)
  final = subscription.ms_final_amount.to_f

  return "PAID"    if paid &gt;= final
  return "PARTIAL" if paid &gt; 0
  "DUE"
end
</code></pre>
<h3>3. Biometric Attendance</h3>
<p>A fingerprint device sends punch data to a REST API endpoint. The system maps biometric IDs to members, deduplicates punches, and validates subscriptions in real-time.</p>
<blockquote>
<p>📝 <strong>I wrote a full deep-dive on this</strong> → [Link to your biometric blog post]</p>
</blockquote>
<h3>4. WhatsApp Automation</h3>
<p>Members automatically receive WhatsApp messages for expiry reminders through a 3-stage pipeline:</p>
<p><strong>Cron Trigger → Background Job → Interakt API → Webhook Delivery Tracking</strong></p>
<pre><code class="language-ruby"># Daily cron hits this endpoint
def send_expiry_whatsapp
  return head :unauthorized unless params[:token] == ENV['CRON_SECRET']

  MembershipExpiryWhatsappJob.perform_later(:expiring)
  MembershipExpiryWhatsappJob.perform_later(:expired)
end
</code></pre>
<p>Messages are tracked through their full lifecycle:</p>
<pre><code class="language-plaintext">QUEUED → SENT → DELIVERED → READ (or FAILED)
</code></pre>
<h3>5. Admin Dashboard</h3>
<p>Single-screen overview: Active/Expiring/Expired counts, today's collections, due payments, inventory status — all preloaded in bulk to avoid N+1 queries:</p>
<pre><code class="language-ruby">def preload_members
  member_ids = @latest_subs.map(&amp;:ms_member_id).uniq
  @members_map = MstMembersList
    .where(mmbr_compcode: @compcode, id: member_ids)
    .each_with_object({}) { |m, h| h[m.id.to_s] = m }
end
</code></pre>
<hr />
<h2>Challenges &amp; Solutions</h2>
<table>
<thead>
<tr>
<th>Challenge</th>
<th>Solution</th>
</tr>
</thead>
<tbody><tr>
<td>Biometric device sends raw punch data</td>
<td>Built REST API with mapping, deduplication, and subscription validation</td>
</tr>
<tr>
<td>WhatsApp duplicate messages</td>
<td>Check <code>trn_whatsapp_logs</code> before sending</td>
</tr>
<tr>
<td>Dashboard slow with 200+ members</td>
<td>Bulk preloading with hash maps</td>
</tr>
<tr>
<td>Tracking message delivery</td>
<td>Interakt webhook controller for real-time status</td>
</tr>
<tr>
<td>Multi-user access control</td>
<td>Module-based permission system</td>
</tr>
</tbody></table>
<hr />
<h2>Key Learnings</h2>
<ol>
<li><p><strong>Real business requirements are messy.</strong> Partial payments, expired members punching in, duplicate scans — you don't think about these in tutorials.</p>
</li>
<li><p><strong>Background jobs are essential.</strong> WhatsApp notifications can't block the request cycle.</p>
</li>
<li><p><strong>Audit logging is non-negotiable.</strong> When real money is involved, every action needs a trail.</p>
</li>
<li><p><strong>N+1 queries become real</strong> when you have 200+ members on a dashboard.</p>
</li>
<li><p><strong>External APIs need defensive coding.</strong> Always handle failures, log responses, and plan for timeouts.</p>
</li>
</ol>
<hr />
<h2>What's Next</h2>
<ul>
<li><p>SMS fallback for members not on WhatsApp</p>
</li>
<li><p>Member-facing mobile view for self-service</p>
</li>
<li><p>Revenue analytics and trend reports</p>
</li>
<li><p>Online payment collection</p>
</li>
</ul>
<hr />
<h2>Final Thoughts</h2>
<p>Building Spine Fitness taught me that <strong>production software for a real business</strong> is fundamentally different from side projects. The requirements come from real workflows, bugs have real consequences, and watching a gym owner ditch their notebooks for your app is incredibly satisfying.</p>
<p><strong>If you're looking to build something meaningful with Rails, start by finding a real problem around you.</strong> The best portfolio projects are the ones that actually get used.</p>
<hr />
<p><strong>🔗 Live:</strong> <a href="https://spine-fitness.com">spine-fitness.com</a> <strong>💻 Source:</strong> <a href="https://github.com/imlakshay08/spine-fitness-gym-management-system">github.com/imlakshay08/spine-fitness-gym-management-system</a></p>
<p><em>If you found this helpful, drop a ❤️ and follow me for more real-world Rails content!</em></p>
]]></content:encoded></item><item><title><![CDATA[Step-by-step Installation guide for Ruby | Complete Ruby on Rails]]></title><description><![CDATA[Installing Ruby on Xubuntu (My Notes from The Odin Project)
Before jumping into Ruby on Rails, we first need to set up Ruby itself.It sounds simple, but trust me — this step can get messy if you skip details.
I went through this recently while follow...]]></description><link>https://imlakshay08-complete-ruby-on-rails.hashnode.dev/step-by-step-installation-guide-for-ruby</link><guid isPermaLink="true">https://imlakshay08-complete-ruby-on-rails.hashnode.dev/step-by-step-installation-guide-for-ruby</guid><category><![CDATA[Ruby]]></category><category><![CDATA[Ruby on Rails]]></category><category><![CDATA[rubyonrails]]></category><category><![CDATA[ruby on rails developer]]></category><dc:creator><![CDATA[Lakshay Tyagi]]></dc:creator><pubDate>Sat, 01 Nov 2025 16:34:32 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1762014835821/98f41cb4-2e71-477b-b89e-83664f98dfa3.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h1 id="heading-installing-ruby-on-xubuntu-my-notes-from-the-odin-project">Installing Ruby on Xubuntu (My Notes from The Odin Project)</h1>
<p>Before jumping into Ruby on Rails, we first need to set up Ruby itself.<br />It sounds simple, but trust me — this step can get messy if you skip details.</p>
<p>I went through this recently while following <strong>The Odin Project</strong>, and here’s a cleaner, beginner-friendly walkthrough based on my own experience.</p>
<hr />
<h2 id="heading-why-you-need-ruby-first">Why You Need Ruby First</h2>
<p>Rails runs on Ruby, so before creating your first app, you need the correct Ruby version and a version manager to switch between them easily.</p>
<p>I’ll be using <strong>rbenv</strong>, which helps install and manage Ruby versions safely without touching system files.</p>
<hr />
<h2 id="heading-step-1-update-your-system">Step 1: Update Your System</h2>
<p>Always start fresh!<br />Open your terminal (<strong>Ctrl + Alt + T</strong> on Xubuntu) and update existing packages:</p>
<pre><code class="lang-bash">sudo apt update &amp;&amp; sudo apt upgrade
</code></pre>
<p>This ensures your system’s libraries are current. You’ll be asked for your password and to confirm updates — press Y when prompted.</p>
<h2 id="heading-step-2-install-required-dependencies">Step 2: Install Required Dependencies</h2>
<p>Ruby needs a few libraries and tools to build properly:</p>
<pre><code class="lang-bash">sudo apt install gcc make libssl-dev libreadline-dev zlib1g-dev libsqlite3-dev libyaml-dev
</code></pre>
<p>💡 Linux Tip: Ctrl + Shift + C → Copy from terminal Ctrl + Shift + V → Paste into terminal</p>
<h2 id="heading-step-3-install-rbenv">Step 3: Install rbenv</h2>
<p>rbenv lets you install and switch between Ruby versions easily.</p>
<p>Clone it from GitHub:</p>
<pre><code class="lang-bash">git <span class="hljs-built_in">clone</span> https://github.com/rbenv/rbenv.git ~/.rbenv
</code></pre>
<p>Then initialize it:</p>
<pre><code class="lang-bash">~/.rbenv/bin/rbenv init
</code></pre>
<p>Now close your terminal and reopen it to refresh your environment variables.</p>
<p>Verify installation:</p>
<pre><code class="lang-bash">rbenv -v
</code></pre>
<p>You should see something like: rbenv 1.x.x</p>
<h2 id="heading-step-4-add-ruby-build-plugin">Step 4: Add ruby-build Plugin</h2>
<p>This plugin helps rbenv compile and install Ruby versions.</p>
<pre><code class="lang-bash">mkdir -p <span class="hljs-string">"<span class="hljs-subst">$(rbenv root)</span>"</span>/plugins
git <span class="hljs-built_in">clone</span> https://github.com/rbenv/ruby-build.git <span class="hljs-string">"<span class="hljs-subst">$(rbenv root)</span>"</span>/plugins/ruby-build
</code></pre>
<p>Now rbenv can handle Ruby installations.</p>
<h2 id="heading-step-5-install-ruby">Step 5: Install Ruby</h2>
<p>We’re finally ready! Run:</p>
<pre><code class="lang-bash">rbenv install 3.4.2 --verbose
</code></pre>
<p>This might take 10–15 minutes. The --verbose flag lets you see progress so you know it hasn’t frozen.</p>
<p>If you get an error like:</p>
<pre><code class="lang-makefile"><span class="hljs-section">ruby-build: definition not found: 3.4.2</span>
</code></pre>
<p>Update ruby-build and try again:</p>
<pre><code class="lang-bash">git -C <span class="hljs-string">"<span class="hljs-subst">$(rbenv root)</span>"</span>/plugins/ruby-build pull
</code></pre>
<h2 id="heading-step-6-set-default-ruby-version">Step 6: Set Default Ruby Version</h2>
<p>Once installation completes, set it as your global Ruby version:</p>
<pre><code class="lang-bash">rbenv global 3.4.2
</code></pre>
<p>Verify with:</p>
<pre><code class="lang-bash">ruby -v
</code></pre>
<p>If everything worked, you’ll see something like:</p>
<pre><code class="lang-scss"><span class="hljs-selector-tag">ruby</span> 3<span class="hljs-selector-class">.4</span><span class="hljs-selector-class">.2pXXX</span> (20XX-XX-XX revision XXXXX) <span class="hljs-selector-attr">[x86_64-linux]</span>
</code></pre>
<p>🎉 Congrats — you’ve officially installed Ruby!</p>
<h2 id="heading-whats-next">What’s Next</h2>
<p>With Ruby set up, you’re ready to install Rails and start building your first app. You can also watch my video walkthrough of this process here — [<a target="_blank" href="https://www.youtube.com/watch?v=L0275goPFYc">YouTube Link</a>].</p>
<h2 id="heading-credits">Credits</h2>
<p>This post is inspired by The Odin Project’s “Installing Ruby” lesson. I followed their guidance but rewrote this post based on my own notes and experience using Xubuntu Linux.</p>
]]></content:encoded></item></channel></rss>