<?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" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Ahmad Hamza on Medium]]></title>
        <description><![CDATA[Stories by Ahmad Hamza on Medium]]></description>
        <link>https://medium.com/@ahmadhamza?source=rss-7d855d79d48a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*3bqKxmtsShDPPxFPNgeVXQ.jpeg</url>
            <title>Stories by Ahmad Hamza on Medium</title>
            <link>https://medium.com/@ahmadhamza?source=rss-7d855d79d48a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 19:57:37 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ahmadhamza/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Scalability through convention]]></title>
            <link>https://medium.com/@ahmadhamza/scalability-through-convention-5a3a1f7eb12d?source=rss-7d855d79d48a------2</link>
            <guid isPermaLink="false">https://medium.com/p/5a3a1f7eb12d</guid>
            <category><![CDATA[convention-over-config]]></category>
            <category><![CDATA[scalibility]]></category>
            <category><![CDATA[naming-conventions]]></category>
            <category><![CDATA[ruby-on-rails]]></category>
            <category><![CDATA[rails]]></category>
            <dc:creator><![CDATA[Ahmad Hamza]]></dc:creator>
            <pubDate>Tue, 22 Oct 2024 16:25:30 GMT</pubDate>
            <atom:updated>2025-04-04T07:27:35.208Z</atom:updated>
            <content:encoded><![CDATA[<h3>Scaling Smarter through conventions</h3><p>When your workload keeps increasing, but your resources are shrinking, how do you maintain efficiency? You’re still expected to deliver at a certain pace, and while it’s natural for efficiency to take a hit, with a few key strategies in place, you can not only keep up but also scale effectively. One of those strategies is <strong>Scalability through convention</strong> — synonymous to <strong>convention over configuration</strong> — a principle that lies at the heart of every Ruby on Rails developer.</p><h3>To illustrate this, let’s look at a real-life project where we put these principles into practice.</h3><p>We were building an analytical tool from scratch, and as you know, such tools involve metrics that need to be displayed through charts or graphs. These metrics like “test coverage” or “build success rate” were dependent on the data from Github or Jira.</p><p>So the following diagram sums the flow of the application:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/862/1*XzDwjP9_QcpgK1j-TbV7DQ.png" /></figure><h3>Technical requirements</h3><p>The idea was to display a few key metrics, with plans to gradually introduce more over time.</p><p>Each metric can have following type of graphs which are basically based on different type of data:</p><ol><li><strong>Latest data</strong> — single data — A <strong>gauge</strong> graph</li><li><strong>Historical data</strong> — Few weeks data or months may be — A <strong>time series</strong> graph</li><li><strong>Detailed data</strong> —Detailed view of a data (expanded form) — A <strong>table</strong> graph</li></ol><p>Therefore in order to understand the complexity, lets go through some of them to understand why these classes are different in nature:</p><ol><li>Classes which process the BQ data and calculate the value specific to metric type such as for test coverage metric it will calculate coverage value specific to time period. Such as for <strong>latest data,</strong> it will pick the last data set, for <strong>historical data</strong> it will pick the data set within the date range chosen. You may also include some other logic like calculating delta. Eventually it depends what you want to display on a single data point but lets assume we need <strong>date</strong>, <strong>value</strong> and <strong>delta. eg: MetricBuilders::TestCoverage</strong></li><li>Decorator classes to represent different types of data for different graph types. eg: <strong>GraphTypes::GaugeGraph</strong></li></ol><p>So you can probably think of a simple architecture that the shared implementation details can be abstracted into parent classes, while the unique logic can be handled in dedicated metric-specific classes, ensuring that our code remained both organized and adaptable.</p><p><strong>We identified one very important thing, the mapping between the metric record and its delegation to its respective ruby classes. Because they all are mapped based on their names.</strong></p><h3>Managing Metric Name Consistency</h3><p>For “Test Coverage” metric, the related builders, serializers and specs files looked like this:</p><pre># Service classes<br>app/services/metric_builders/test_coverage.rb<br>app/services/metric_details_builders/test_coverage/meta.rb<br>app/services/metric_details_builders/test_coverage/data.rb<br>app/services/metric_details_builders/test_coverage/packages.rb<br>app/services/metric_details_builders/test_coverage/response.rb<br><br># Serializers<br>app/serializers/dashboards/details/test_coverage/data_serializer.rb<br>app/serializers/dashboards/details/test_coverage/meta_serializer.rb<br>app/serializers/dashboards/details/test_coverage_serializer.rb<br><br># Specs<br>spec/services/metric_details_builders/test_coverage/packages_spec.rb<br>spec/services/metric_builders/test_coverage_spec.rb</pre><p>As you can see, there is a pattern of files here and it is very important for these files to have <strong>consistent class names</strong> for the respective metrics.</p><h3>Solution — Standardization of Metric Names</h3><p>To do this,</p><blockquote>Metric names were standardized and controlled throughout the application.</blockquote><p>By defining few ruby constants and making them available across the entire codebase:</p><ol><li>Constant for <strong>storing metric’s generic name</strong> (eg: “Test Coverage”)</li><li>Constant for <strong>mapping metric’s generic name to its code name</strong> (eg: “ test_coverage”) which is going to be used internally in the application.</li></ol><pre># config/initializers/constants.rb<br><br># Metrics<br>TEST_COVERAGE              = &#39;Test Coverage&#39;<br>BUILD_SUCCESS_RATE         = &#39;Build Success Rate&#39;<br>DEPLOYMENT_LEAD_TIME       = &#39;Deployment Lead Time&#39;<br>ERROR_RATE                 = &#39;Error Rate&#39;<br>FEATURE_COMPLETION_TIME    = &#39;Feature Completion Time&#39;<br><br>ALLOWED_METRICS = {<br>  TEST_COVERAGE              =&gt; &#39;test_coverage&#39;,<br>  BUILD_SUCCESS_RATE         =&gt; &#39;build_success_rate&#39;,<br>  DEPLOYMENT_LEAD_TIME       =&gt; &#39;deployment_lead_time&#39;,<br>  ERROR_RATE                 =&gt; &#39;error_rate&#39;,<br>  FEATURE_COMPLETION_TIME    =&gt; &#39;feature_completion_time&#39;<br>}.freeze</pre><p>and a <strong>validation</strong> for metric name in the Metric model to ensure the metric name was always correct and is among the expected ones:</p><pre>class Metric &lt; ApplicationRecord<br>  validates :name, presence: true, inclusion: { in: ALLOWED_METRICS.keys }<br>end</pre><p>Till this point, a <strong>convention has been established</strong> that in order for you to add a new metric, <strong>you have to add a constant for its generic name and a mapping for its generic name to code name.</strong></p><h3>Benefit</h3><p>The benefit of that convention is that <strong>we could reliably call the appropriate builders based on just the metric’s name</strong>. Here’s an example of how we used this to map metrics to their respective handlers:</p><pre>class MetricGraphBuilder<br>  METRIC_BUILDERS = {<br>    &#39;test_coverage&#39;           =&gt; MetricBuilders::TestCoverage,<br>    &#39;build_success_rate&#39;      =&gt; MetricBuilders::BuildSuccessRate,<br>    &#39;deployment_lead_time&#39;    =&gt; MetricBuilders::DeploymentLeadTime,<br>    &#39;error_rate&#39;              =&gt; MetricBuilders::ErrorRate,<br>    &#39;feature_completion_time&#39; =&gt; MetricBuilders::FeatureCompletionTime<br>  }.freeze<br><br>  def initialize(metric_id:)<br>    @metric_id = metric_id<br>  end<br><br>  def perform<br>    results = evaluate_and_build_graph(metric_name:)<br>    ::BuildGraphData.for_graph(results)<br>  end<br><br>  def evaluate_and_build_graph(metric_name:)<br>    metric_code_name = ALLOWED_METRICS[metric_name]<br>    metric_klass = METRIC_BUILDERS[metric_code_name]<br>    raise InvalidMetricHandler, &quot;No handler class found for metric: #{metric_name}&quot; if metric_klass.blank?<br><br>    metric_klass.new.perform<br>  end<br>end</pre><p>This approach not only ensured that the <strong>separation of concerns</strong> in the respective metric handlers but also significantly reduced the likelihood of <strong>mistakes during development</strong>.</p><p>By whitelisting metric names and using a centralised handler mapping, we <strong>streamlined the process, improved maintainability, and enhanced development efficiency</strong>.</p><h3>Reward — a custom Rails Generator for adding new metrics</h3><p>As we continued to scale the system, the consistent structure established by these conventions laid the groundwork for further automation.</p><p>The next logical step was to <strong>build a custom Rails generator</strong>. This generator <strong>automated</strong> the creation of new metrics by generating all the necessary files — service classes, serializers, and specs — complete with boilerplate code.</p><figure><img alt="Rails generator creates boiler plate code and files necessary for the new metric." src="https://cdn-images-1.medium.com/max/1024/1*TQxmI4G6uuoLs2z47HK94A.png" /><figcaption>Rails generator creates boiler plate code and files necessary for the new metric.</figcaption></figure><p>By adhering to our established conventions, the generator ensured that new metrics were seamlessly integrated into the system with minimal manual effort.</p><blockquote>This automation proved invaluable, particularly when the team size was reduced by half, allowing us to maintain our development pace and manage the increasing number of metrics with fewer resources.</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5a3a1f7eb12d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[I learned about Capybara matchers who obey wait time]]></title>
            <link>https://medium.com/@ahmadhamza/i-learned-about-capybara-matchers-who-obey-wait-time-4a2d0b6df287?source=rss-7d855d79d48a------2</link>
            <guid isPermaLink="false">https://medium.com/p/4a2d0b6df287</guid>
            <category><![CDATA[rails]]></category>
            <category><![CDATA[wait]]></category>
            <category><![CDATA[rspec]]></category>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[capybara]]></category>
            <dc:creator><![CDATA[Ahmad Hamza]]></dc:creator>
            <pubDate>Wed, 18 Mar 2020 10:43:22 GMT</pubDate>
            <atom:updated>2020-03-18T10:43:22.064Z</atom:updated>
            <content:encoded><![CDATA[<p>While writing the system tests for stripe, i noticed an intermittent test failure which seemed like test is not letting the asynchronous operation to complete.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8eoQsw8qQZqp_QqkwJUDIg.png" /></figure><p>The flow of test was like:</p><ol><li>Visit new subscription page</li></ol><pre>visit new_subscription_path(plan_id: plan.stripe_plan_id)</pre><p>2. Fill out form with card and personal details</p><pre>find_frame(&#39;#card-element &gt; div &gt; iframe&#39;) do<br>  card.to_s.chars.each do |piece|<br>    find_field(&#39;cardnumber&#39;).send_keys(piece)<br>  end</pre><pre>  find_field(&#39;exp-date&#39;).send_keys expiry<br>  find_field(&#39;cvc&#39;).send_keys cvc<br>  find_field(&#39;postal&#39;).send_keys postal<br>end</pre><pre>fill_in &#39;name_on_card&#39;, with: user.full_name<br>click_button &#39;Subscribe&#39;</pre><p>3. Check the redirection url after success</p><pre>expect(page.current_path).to eq(orders_path)</pre><p>Since this was the intermittent issue i ignored it at the beginning. But then the continuous integration was not letting it go and reporting the error again and again. The error was:</p><pre>1) subscriptions create successfully subscribes to a plan<br>     Failure/Error: expect(page.current_path).to eq(orders_path)<br>       expected &quot;/subscription/new?plan_id=lite&quot; to equal &quot;/orders&quot;<br>     # ./spec/features/subscription_spec.rb:20:in `block (3 levels) in &lt;top (required)&gt;&#39;</pre><p>So i rolled my sleeves up and decided to take a deep dive.</p><p>After debugging, I realised that the test was reporting the error even before the flow was even completed. I added a configuration in order to increase the wait time for capybara.</p><pre>Capybara.default_max_wait_time = 10 #default is 2</pre><p>This dint help and what i found is the most important thing and the gist of this article.</p><p>There are some capybara helpers which are specially written to obey the wait time and some don’t. So instead of</p><pre>expect(page.current_path).to eq(orders_path)</pre><p>i used <em>have_current_path </em>like this<em>:</em></p><pre>expect(page).to have_current_path(orders_path)</pre><p>And then my tests were passing.</p><p>Therefore it is important to know that if you are writing a test case which involves asynchronous operation like sending <em>ajax request, api calls, javascript animations </em>then you should use <em>capybara matchers</em> which waits till the process is completed and then move forward. Hope this will help others as well.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4a2d0b6df287" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>