<?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[Building a CLI Data Gem Portfolio - Medium]]></title>
        <description><![CDATA[This article will shortly describe how to a build a CLI using Ruby. - Medium]]></description>
        <link>https://medium.com/building-a-cli-data-gem-portfolio?source=rss----82aef01e3adf---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Building a CLI Data Gem Portfolio - Medium</title>
            <link>https://medium.com/building-a-cli-data-gem-portfolio?source=rss----82aef01e3adf---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 16:25:01 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/building-a-cli-data-gem-portfolio" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Building a CLI Data Gem Portfolio]]></title>
            <link>https://medium.com/building-a-cli-data-gem-portfolio/building-a-cli-data-gem-portfolio-3ccbbf81fbfd?source=rss----82aef01e3adf---4</link>
            <guid isPermaLink="false">https://medium.com/p/3ccbbf81fbfd</guid>
            <category><![CDATA[ruby-on-rails]]></category>
            <category><![CDATA[gem]]></category>
            <category><![CDATA[scraping]]></category>
            <category><![CDATA[cli]]></category>
            <dc:creator><![CDATA[Jean-Kenel Dessources]]></dc:creator>
            <pubDate>Mon, 13 May 2019 18:13:54 GMT</pubDate>
            <atom:updated>2019-05-22T18:52:16.164Z</atom:updated>
            <content:encoded><![CDATA[<p>My name is Jean-Kenel Dessources. I have been a Student in FullStack Web Development at Flatiron School for a month and two weeks. This article will cover a part of what I did for my final project in Ruby.</p><p>This article describes how to build a CLI Data Gem Portfolio which will provide access to data from web page. First of all, you will need to configure your environment that means:</p><ol><li>Installing ruby on your computer.</li><li>Installing a text editor for code or IDE (<a href="https://www.sublimetext.com/">Sublime</a>, <a href="https://code.visualstudio.com/">VS Code</a>, <a href="https://atom.io/">Atom</a> or else).</li><li>If you intend to place your application in an online repository, you will need to configure Git on your computer.</li></ol><p>We do not cover the previous topics on this article.</p><p>Without further ado, let’s start our CLI by typing in the terminal:</p><p>bundle gem data_gem_portfolio</p><p>Note: <em>data_gem_portfolio</em> is the name of our application.</p><p>As soon as we are done typing this line of command, a bunch of files will be created and get everything ready to start our application as seen in the following picture.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FFW1KnoxZ759Ttu5qLxqPg.png" /></figure><h4>Let’s create the executable file</h4><p>In our <em>bin</em> directory we are going to create a file which will be responsible to execute our application. Let&#39;s call it data_gem.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Fbr6liGI27dmokU4ndnYlg.png" /></figure><h4>Load the Ruby interpreter</h4><p>Since it is not a ruby file, let’s add this following line of instructions so that the operating system can interpret it through ruby: #!/usr/bin/env ruby</p><p><em>Note:</em> Since it is the executable file, we do not want to put logic (code)in this file.</p><p>So, we are going to call the class where the logic of our application will be written.</p><p>Assuming that our class will be named <em>CLI</em> which will be written in the <em>cli.rb</em> file we will create later on.</p><p>We are adding this code (DataGemPortfolio::CLI.new.call) in the data_gem file.</p><h4>Let’s try to explain what it is doing.</h4><p>Above, we used the name space <em>DataGemPortfolio</em> from the file version.rb, we use the class CLI from the file cli.rb and we can instantiate and call the method call on it.</p><h4>Let’s create the controller that will allow to users to interact with our application.</h4><p>On the left we could clearly see all the different files that is generated for us. And in our directory lib/data_gem_portfolio a ruby file. We will call it cli.rb.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aq0uzXi4vfMVUaQGghJUjw.png" /></figure><p>Before continuing, let’s ask to the executable file to load all of the files that are in the lib directory.</p><p>require &#39;./lib/data_gem_portfolio&#39;</p><p>Now it becomes:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rvHVWXahg0fNtMLGPYXTnw.png" /></figure><p>So, we can start writing our controller codes in cli.rb .</p><pre><em>#CLI controller (User interactions)</em><br>class DataGemPortfolio::CLI</pre><pre>  def call<br>    <br>  end</pre><pre>end</pre><p>Before going further, let’s require some gems that will allow us to scrape data from the web.</p><p>In the file data_gem_portfolio located in <em>lib</em>, let’s add these following gems and also require some files from data_gem_portfolio folder:</p><p>In the file data_gem_portfolio located in <em>lib</em>, let&#39;s add these following gems:</p><pre>require &#39;open-uri&#39;<br>require &#39;nokogiri&#39;</pre><p>Let’s require these files:</p><pre>require_relative &#39;data_gem_portfolio/version&#39;<br>require_relative &#39;./data_gem_portfolio/cli&#39;<br>require_relative &#39;./data_gem_portfolio/category&#39;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GucR8cn-cgiN-RxI9XXQJw.png" /></figure><p>In the <em>data_gem_portfolio.gemspec</em> file, let&#39;s add these dependencies.</p><pre>spec.add_development_dependency &quot;pry&quot;<br>spec.add_dependency &quot;nokogiri&quot;</pre><h4>The whole structure of our Controller for User Input</h4><p>File: <em>cli.rb</em></p><p>In the <em>lib/data_gem_portfolio/cli.rb,</em> we are writing the following code for user inputs.</p><pre>class DataGemPortfolio::CLI</pre><pre>  def call<br>    list_category<br>    menu<br>  end</pre><pre>  def list_category<br>    info_item<br>    puts &quot;1. Batteries&quot;<br>    puts &quot;2. Inverters&quot;</pre><pre>    @courses = DataGemPortfolio::Category.today<br>  end</pre><pre>  def info_item<br>    puts &quot;Type 1 or 2 to see items, list to see the list or exit.&quot;<br>  end</pre><pre>  def menu<br>    input = nil<br>    count_instance = DataGemPortfolio::Category.today.size</pre><pre>    while input != &quot;exit&quot;<br>      input = gets.strip.downcase<br>      <br>        if input.to_i &gt; 0 &amp;&amp; input.to_i &lt;= count_instance<br>          the_category = @courses[input.to_i - 1]<br>          separator<br>          puts &quot;- Item: #{the_category.name}&quot;<br>          puts &quot;- Price: #{the_category.price}&quot;<br>          puts &quot;- Availability: #{the_category.availability}&quot;<br>          puts &quot;- Url: #{the_category.url}&quot;<br>          separator<br>          puts &quot; &quot;<br>          info_item<br>        elsif input == &quot;list&quot;<br>          list_category<br>        elsif input == &quot;exit&quot;<br>          thanking<br>        else<br>          puts &quot;Not sure whay you want. Type 1 or 2 to see items, list to see the list or exit.&quot;<br>        end<br>      end<br>    end</pre><pre>  def thanking<br>    puts &quot;Thank you for visiting our site!!&quot;<br>  end</pre><pre>  def separator <br>    puts &quot;*&quot; * 120<br>  end<br>end</pre><h4><strong>The code that shows how the application will access data from the web page.</strong></h4><p>File: <em>category.rb</em></p><p>We create a file called<em>category.rb</em> in <em>lib/data_gem_portfolio.</em></p><p>In the<em>lib/data_gem_portfolio/category.rb,</em> we are writing the following code for scraping from the web pages.</p><pre>class DataGemPortfolio::Category</pre><pre>  attr_accessor :name, :price, :availability, :url</pre><pre>  def self.today<br>    self.scrape_category<br>  end</pre><pre>  def self.scrape_category<br>    all_items = []<br>    all_items &lt;&lt; self.scrape_woot<br>    all_items &lt;&lt; self.scrape_inverter<br>    all_items<br>  end</pre><pre>  def self.batteries<br>    doc = Nokogiri::HTML(open(&quot;https://www.luminousindia.com/combo-deal/inverter-battery.html&quot;))<br>  end</pre><pre>  def self.scrape_woot<br>    battery_deal = self.new<br>    battery_deal.name = self.batteries.search(&quot;h2.product-name&quot;).first.inner_text<br>    battery_deal.price = self.batteries.css(&quot;span#product-price-1260.price&quot;).inner_text.strip<br>    battery_deal.url = self.batteries.css(&quot;.product-name&quot;).css(&quot;a&quot;).first.attr(&quot;href&quot;)<br>    battery_deal.availability = true<br>    battery_deal<br>  end<br>  <br>  def self.inverters<br>      doc = Nokogiri::HTML(open(&quot;https://theinverterstore.com/product/180-watt-pure-sine-power-inverter/&quot;))<br>  end</pre><pre>  def self.scrape_inverter<br>    inverter_deal = self.new<br>    inverter_deal.name = self.inverters.search(&quot;h3.product-title&quot;).first.text.strip<br>    inverter_deal.price = self.inverters.css(&quot;span.woocommerce-Price-amount&quot;).first.text<br>    inverter_deal.url = self.inverters.css(&quot;.product-title&quot;).css(&quot;a&quot;).first.attr(&quot;href&quot;)<br>    inverter_deal.availability = true<br>    inverter_deal<br>  end<br>  <br>end</pre><p><strong><em>Test the application</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Qglj7ddQUhHmVccS-S55Yg.png" /></figure><p><strong>A short video on how it works.</strong></p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FFihQNU7v5rk%3Ffeature%3Doembed&amp;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DFihQNU7v5rk&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FFihQNU7v5rk%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="640" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/6ef9c2b098db1cb04783bd2280ccf384/href">https://medium.com/media/6ef9c2b098db1cb04783bd2280ccf384/href</a></iframe><p><a href="https://github.com/djken/data_gem_portfolio">Click here to download or clone from GitHub.</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3ccbbf81fbfd" width="1" height="1" alt=""><hr><p><a href="https://medium.com/building-a-cli-data-gem-portfolio/building-a-cli-data-gem-portfolio-3ccbbf81fbfd">Building a CLI Data Gem Portfolio</a> was originally published in <a href="https://medium.com/building-a-cli-data-gem-portfolio">Building a CLI Data Gem Portfolio</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>