<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Xauvkub Alex Yang</title>
    <description>The latest articles on DEV Community by Xauvkub Alex Yang (@xauvk).</description>
    <link>https://dev.to/xauvk</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F890035%2Fa139ae84-1312-4bc5-8572-e030375f613c.jpg</url>
      <title>DEV Community: Xauvkub Alex Yang</title>
      <link>https://dev.to/xauvk</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/xauvk"/>
    <language>en</language>
    <item>
      <title>How To Use an API from your Ruby Server</title>
      <dc:creator>Xauvkub Alex Yang</dc:creator>
      <pubDate>Mon, 03 Oct 2022 13:05:49 +0000</pubDate>
      <link>https://dev.to/xauvk/how-to-use-an-api-from-your-ruby-server-1om</link>
      <guid>https://dev.to/xauvk/how-to-use-an-api-from-your-ruby-server-1om</guid>
      <description>&lt;p&gt;&lt;strong&gt;Step 1&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;USE RESTCLIENT TO FETCH FROM API&lt;/strong&gt;&lt;br&gt;
The first thing we want to do is fetch from the API.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def from_api
 resp = RestClient.get("https://exampleapi.com/?api_key=1241241515151")
 render json: resp.body, status: :ok
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 2&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;HIDE YOUR API KEY&lt;/strong&gt;&lt;br&gt;
If you need to hide your key you can do so by using a gem called Figaro.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gem "figaro"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add figaro to to your gemfile, bundle install figaro and install figaro.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ bundle exec figaro install
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This creates a config/application.yml file and adds it to your .gitignore. You can use this to create Environment variables such as your key.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;api_key: 1241241515151 (example)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And you can call it like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ENV['api_key'] 

&amp;gt; 1241241515151
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now use that key in your fetch request.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def from_api
    resp = RestClient.get("https://exampleapi.com/
?api_key=#{ENV['api_key']}")
    render json: resp.body, status: :ok
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>Creating Active Record Custom Validations</title>
      <dc:creator>Xauvkub Alex Yang</dc:creator>
      <pubDate>Mon, 12 Sep 2022 02:57:40 +0000</pubDate>
      <link>https://dev.to/xauvk/creating-active-record-custom-validations-5dk4</link>
      <guid>https://dev.to/xauvk/creating-active-record-custom-validations-5dk4</guid>
      <description>&lt;p&gt;In order to have an efficient back end server, you need to make sure only valid data is stored into the database. The way we do this in rails is using validations.&lt;/p&gt;

&lt;p&gt;Writing our validations is quite simple. Here’s a built in rails validation that checks if the name is present.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class User &amp;lt; ApplicationRecord
  validates :name, presence: true
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This line of code validates the name checking if the User that instance has a name before entering it into the database. An advantage of using the built in Active Record validations is that it can create errors for you.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;irb&amp;gt; u = User.create
=&amp;gt; #&amp;lt;User id: nil, name: nil&amp;gt;
irb&amp;gt; u.errors.objects.first.full_message
=&amp;gt; "Name can't be blank"

irb&amp;gt; u.save
=&amp;gt; false

irb&amp;gt; u.save!
ActiveRecord::RecordInvalid: Validation failed: Name can't be blank

irb&amp;gt; User.create!
ActiveRecord::RecordInvalid: Validation failed: Name can't be blank
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There’s many useful pre-definded active record validations and can be found on the Ruby on Rails guides. If you wanted your validation to be something unique and specific you can create your own. To start you'd write validate with a symbol of a method's name.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;validate :is_over_eightteen
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then you can write the method returning a custom error message.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def is_over_eightteen
  if age &amp;lt;= 18
    errors.add(:age, "Sorry User is too Young!")
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>How Ruby Macros Can Write Our Code For Us</title>
      <dc:creator>Xauvkub Alex Yang</dc:creator>
      <pubDate>Tue, 23 Aug 2022 12:53:00 +0000</pubDate>
      <link>https://dev.to/xauvk/how-ruby-macros-can-write-our-code-for-us-2p07</link>
      <guid>https://dev.to/xauvk/how-ruby-macros-can-write-our-code-for-us-2p07</guid>
      <description>&lt;p&gt;During my time at Flatiron Coding Bootcamp we've learned so much material in such short amount of time. More recently we've learned a language called Ruby. Using Ruby I've learned there's many things to make the coding process a lot faster and simpler. One way to do that is to use macros. A macro is like a method or set of instructions that writes your code for you. We use them a lot when using Active Record and they make the whole process a lot faster.&lt;/p&gt;

&lt;p&gt;For example, I wrote a class instance getter and setter for a variable called name which took about six lines.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Person
 def initialize(name)
    @name=name
 end

 def name
    @name
 end

 def name=(new_name)
    @name=new_name
 end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But you could use a macro to write this much faster. To write our getter and setter methods we can use &lt;code&gt;attr_writer&lt;/code&gt; and &lt;code&gt;att_reader&lt;/code&gt; and assign them the symbol of name.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Person
...
 attr_reader :name
 attr_writer :name
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That was much simpler! &lt;code&gt;attr_writer&lt;/code&gt; created our setter method for us and &lt;code&gt;att_reader&lt;/code&gt; created a getter method. But what's even better these two lines can actually be simplified to one line.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;attr_accessor :name
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Macros are used so that we don't have to repeat writing the same actions over and over making work much simpler. It allows us to automate our tasks so that we don't have to spend more time remembering or writing some line of code. I'd highly recommend to research and use macros and shortcuts available to you to improve your productivity.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Making a Search Bar with React</title>
      <dc:creator>Xauvkub Alex Yang</dc:creator>
      <pubDate>Mon, 01 Aug 2022 02:40:00 +0000</pubDate>
      <link>https://dev.to/xauvk/making-a-search-bar-with-react-28b8</link>
      <guid>https://dev.to/xauvk/making-a-search-bar-with-react-28b8</guid>
      <description>&lt;p&gt;When using a computer, you want the computer to do exactly what you want when and how you need it. Finding a the right file or post quickly is essential to boosting productivity. Search engines are extremely useful in finding the data users need in a shorter amount of time. My objective for this post is to explain how I make simple search engines for sites in JavaScript and React.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Grabbing the Data&lt;/strong&gt;&lt;br&gt;
For this example we’re gonna grab data from a .json file using the fetch function. The data grabbed from the .json is stored in an array that we can use to display to our screen.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/* App.js */
import React, { useState, useEffect } from "react"
import Header from "./Header"
import SearchBar from "./SearchBar"
import DataContainer from "./DataContainer"

function App() {
const [data, setData] = useState([])

useEffect(() =&amp;gt; {
 fetch(url)
  .then(resp =&amp;gt; resp.json())
  .then(dataArray =&amp;gt; setData(dataArray))
},[])

return (
    &amp;lt;&amp;gt;
      &amp;lt;Header /&amp;gt;
      &amp;lt;SearchBar /&amp;gt;
      &amp;lt;DataContainer data={data}/&amp;gt;
    &amp;lt;/&amp;gt;
  )
}

export default App
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Create a useState&lt;/strong&gt;&lt;br&gt;
useState is a react tool that can make your web app very dynamic. The useState variable updates everything that is using that variable anytime it is changed. We can use that to change the data whenever the input field is changed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/* App.js */
const [searchString, setSearchString] = useState('')

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then we can create a function to change searchString whenever you change the text input.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/* App.js */
const handleSearch = (string) =&amp;gt; {
 setSearchString(string)
}

...
  &amp;lt;SearchBar handleSearch={handleSearch} /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/* SearchBar.js */
function SearchBar({handleSearch}) {
  return (
    &amp;lt;div&amp;gt;
        &amp;lt;input
          onChange={handleSearch(e.target.value)}
          type="text"
        /&amp;gt;
    &amp;lt;/div&amp;gt;
  )
}

export default SearchBar
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every time the input is changed, handleSearch updates our useState to the same value. &lt;/p&gt;

&lt;p&gt;We can use data to create a separate array that changes every time you enter a new query. You can do this by using .filter() and use .includes() on the strings to filter them. Since we used a useState, the array will change after we change the input.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/* App.js */
const searchArray = data.filter((item) =&amp;gt; { 
 return item.name.toLowerCase().includes(searchString.toLowerCase())
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Make sure to create the searchArray after the handleSearch or else the data will still be showing the previous input.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/* App.js */
   &amp;lt;DataContainer data={searchArray}/&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Lastly we pass the data to our DataContainer component. Now all our data has been filtered and is ready to be used in DataContainer.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>react</category>
    </item>
    <item>
      <title>My Coding Journey Begins</title>
      <dc:creator>Xauvkub Alex Yang</dc:creator>
      <pubDate>Mon, 11 Jul 2022 05:42:19 +0000</pubDate>
      <link>https://dev.to/xauvk/my-coding-journey-begins-19ne</link>
      <guid>https://dev.to/xauvk/my-coding-journey-begins-19ne</guid>
      <description>&lt;p&gt;We are just finishing the first phase in our coding bootcamp and I am already feeling the pressure. In high school, I started learning basic JavaScript for the first time and loved solving the problems. When I went to college, I decided on a Computer Science major just because that was something I already had experience in. &lt;/p&gt;

&lt;p&gt;As the courses got harder, I started doubting whether I was meant to code or not. I failed my classes and headed back home to wait tables for my parents again. I was just waiting for a sign that something or someone was just meant to be.&lt;/p&gt;

&lt;p&gt;Not until recently, I realized I wanted more. My best future wasn’t just going to come to me. I decided I want more responsibility. I want to finish what I start. I want to find purpose. That’s why I’ve taken time from helping the family restaurant to start FlatIron School.&lt;/p&gt;

&lt;p&gt;I am really excited to grow as a person and become disciplined though my work. I plan to be a role model for my community and give back to them. I want to develop skills that can help me give and show that it’s possible to achieve your goals. I want to be confident in myself and become the person my family and friends have always seen in me as. I am determined to become a different person in the end.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>productivity</category>
      <category>webdev</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
