CLI Project Review day

Today is my CLI project review and I reserved my day to review for the last time my code and check that everything is working in my interface.

As soon as I opened the web page that I am scraping I noticed that they have changed their content. Glup! I checked to see if my code was still working….and…not, that new exhibition was not included in my list of exhibitions.

PANIC! I am nervous because is my first project review and I really don’t know how is going to be. And  now I found that some exhibitions were not included and… Oh! Wait, I found out that that web page has links to more pages. I should scrape those pages too to get a full list of the exhibitions and I don’t have much time.

I started to make all the changes to get everything working well. I was inspecting the page but I din’t see why my CSS selector was not working. It seems that only the recommended exhibitions are listed, when I wanted all of them. I tried like 20 different selectors and I can not get that new exhibition! I struggle with CSS selectors… I find them very difficult!

After 4 hours and with a mental speech to explain why my code is not working properly and I didn’t do anything to fix it I found out that I was looking to a different web page. I was looking to the archive page and not to the “whats on” page, the one I wanted…

OK, everything is fine after all!

But I want to share this experience. I have learnt a few new things like  how to get attribute info with a CSS selector in a Nokogiri document.

 

 

And finally I have learnt another important lesson like, when you are nervous and you don’t have much time left, don’t change your code. Don’t break everything in the last minute like I did. Keep calm and later explain what you have found and try to fix it, but not in a rush. Go for a walk, breath some fresh air and take it easy. Chocolate cookies help too.

Programming my classes

After all the previous steps now I have pretty clear how I am going to structure my code.

I want a CLI that displays art exhibitions and gives to the user more info  about the one it chooses. I am going to scrape from a public website all the info concerning the art events (Artlyst).

I want three classes: a CLI, an exhibition one and a scraper.

This three classes will live inside a lib folder.

I finally changed the CLI class name for artify in case I want to publish my gem in RubyGems.org. So I have threes classes: artify, exhibition and scraper.

Let’s start coding!

To check my code go to my Github repository.

After programming my classes I require them inside the environment.rb file.

And that’s it!

If you want to play with this gem please check this link: readme page.

Hope it was helpful!

The executable file

I create a folder bin and create inside of this folder an executable file called in this case as the gem, ‘artify’.

When I type: $ruby bin/artify,  everything run but I don’t want this. I want that the user from the bash can type: $ ./bin/artify.

So I type:

$ cd bin/

$ ls -lah

Ad I can see that artify doesn’t have executable permissions. To change this I type:

$ chmod +x artify

$ ls -lah

This executable file is not a ruby file so I have to include at the top the following:

#!/usr/bin/env ruby

Then I require our environment file to use all the gems already installed.

In this file I want to run for my still imaginary CLI class. I want to write here something like: CLI.new.run.

So, at this point, I know I want a class CLI, where I am going to build all the methods and keep all data to run my CLI.

I finally changed this name for ‘artify’ in case I want to publish it in RubyGems.org.

How to build a CLI Ruby Gem

Steps followed:

  • Planning my gem.

I would like to build a command line interface gem where I can check art exhibitions and get more info about the ones I liked.

How it would work:

– User types “exhibitions”.

– And ‘Artify’ will show a list of exhibitions like this:

Exhibition title – where

dates

-Which exhibition do you want to learn more about?

-And it will display more info about the exhibition selected.

Walkthrough video

Artify Environment

Setting my own environment: Visual Studio Code, Git and Bundler

First, I create a repository in Github.

Then, I create a new folder in my laptop where I want to clone the repository.

I open this folder in my VS code and open the terminal (ctrl + `).

I run: git clone + the https from the repository.

When I make changes:

First, I stage this changes.

Second, I write commit message and commit.

Third, I push my changes.

Installing Bundler

First I make sure I have the latest version of RubyGems. I run in the VS code terminal $gem update –system

Then I instal Bundler: gem install bundler but I got an error message

I don’t have write permissions. I googled this error output and I am trying something  I found in stack overflow: sudo gem install bundler. I gave the personal password for my computer and it worked!

The purpose of sudo is to execute the command given to it with root privileges.

Adding Gems to gemfile

I created a new file gemfile and I am adding my gems to it, bundler.

‘~> 2.0’

~> is called “Twiddle-Wakka.” '~> 2.0' means any minor version above 2.0,  2.1, 2.2, 2.3 … and 2.9 included would work (including patches); but version 3.0 wouldn’t work because it indicates a new major version.

The second specification '>= 2.0.1'means any version greater than or equal to 2.0.1. Both specifications have to be true, so this gem couldn’t use version 2.0because it’s lower than 2.0.1.

My Gemfile.lock file

I run in the VS code terminal $bundle installAutomatically a file was created in my directory: Gemfile.lock

Reviewing my installed gems

I run in the VS code terminal $gem list

Checking where my Bundler is

I run in the VS code terminal $bundle show [bundler]

Adding Gemfile and Gemfile.lock to my repository

I run in the VS code terminal $git add Gemfile Gemfile.lock.

I could staged the changes in my VS code, they do the same thing that the git add command.

I check my Github repository….and it didn’t work.

Ok! I forgot to push my changes, so I ran: $git push origin master

Done! I got my two new files in my repository.

Now, I want to install Nokogiri

Installing Nokogiri

I run in the VS code terminal $sudo gem install nokogiri

And after the installation was successfully  I add my new gem to the Gemfile

I check if my changes are in Github and…no! They are not! Why? BecauseI forgot to push them.

My work flow in VSC:

Save changes – stage changes – commit changes – push changes

Installing pry

I run in the VS code terminal $sudo gem install pry

And then I add the new gem to the Gemfile

And I don’t forget about my VSC workflow! Save, stage, commit and push.

Installing ‘open-uri’

I was trying to install it and got this error:

I googled it and…

OpenUri is part of Ruby standard library, I just need to require it.

After all this installations I checked my Gemfile.lock and I couldn’t see anything about nokogiri or pry. I run $bundle install and my Gemfile.lock is updated.

Installing ‘colorize’

I run in the VS code terminal $sudo gem install colorize

I add my new gem to the Gemfile.

And run $bundle install to update my Gemfile.lock.

 

I open my Gemfile and do this with my pry gem:

This is Group Syntax and I am specifying the environment where it is going to be used pry. I am going to change it for a shorter syntax, the hash syntax: gem ‘pry’, :group => :development

After installing all my gems I need to require them where I am going to use them.

config/environment.rb

Now, I create a folder config and a file environment.rb inside. In this file I will be loading, requiring, all of my app’s dependencies like my gems.

With this code all the gems that are inside Gemfile are required.

I need to require ‘open-uri’ and ‘colorize’ too in my environment.

Rubber duck debugging

Last week I was working in the Music Library CLI Lab and I was not able to solve the last method I had to pass all the tests. I do not think it was more difficult than all the previous ones but even so I was not able to see what I was doing wrong. Maybe I was tired because I had been studying for several hours and I needed to clear my mind so I took a break. Talking to a friend about this situation, he told me why I did not try to explain my problem to a rubber duck.

Yap. I thought he was kidding but when I started working in my Lab I tried it and while I was explaining to the duck my problem I realised what was happening in 5 minutes!

Rubber duck debugging is a method of debugging code and a very effective one.

Here I leave a picture of my best study partner!Rubber Duck Debugging

What I have learned so far about OO Ruby

From what I’ve understood Object-Oriented programming is very popular and it has lots of advantages. The programmer may have a closer feeling of working with real-life entities due to the multitask nature of the objects in OOP. From my point of view OOP is a good way to keep all your code organise. Each object contains its data and functionality within its scope and if its methods are written in an abstract way they can be reused for other objects or for a different project.

I am still working in the OO Ruby section and it is taking me some time to understand all the concepts and why or when to use them. Here it is what I have learned so far:

Creating a class

Class names begin with capital letters because they are stored in Ruby constants. If our class name contains two words, the name should be CamelCased.

Instantiation

Every time we want to represent a new individual in our program we should instantiate an object of that type (Dog.new). We call these individuals instances.

Instances variables

An instance variable represents a singular property of an individual, of an instance. This variable is accessible in any instance method in a particular instance of a class. The name of the instance variable is written with a @ at the beginning of the word: @instance_variable_name.

Instance methods

We call the methods defined within the object’s class instance methods because they are methods that belong to any instance of the class.

They provide a mechanism for an object to expose to the outside world its data. They can manipulate the internal state of an object and provide some functionality for an individual object too.

Attribute readers, writers and accesors

Our object’s instances can have attributes, like a name or hair colour in the case of our Dog class. In order to set these attributes we have to use a writer and in order to have these data available in the outside world we have to have a reader. It is very important to use an instance variable (@name), otherwise the getter method couldn’t read the data that we set in the writer method because it is out of scope.

This is the explicit method for the attribute @name. There is another faster way to do it using a macro: the attr_writer, the attr_reader and the attr_accessor that does the work of the two previous macros. In Ruby a macro is like a method that instead of returning a Ruby datatype returns more Ruby code. The implementation of macros is considered metaprogramming.

When to use attr_accessor vs a custom reader/writer.

The attr_accessor macro will provide a naive interface for reading and writing to an instance variable. Anytime we need something conceptually different or more complex, we should build our own reader or writer.

Self

Whenever an object needs to refer to itself, the keyword “self” is used.

In an instance method, self will always refer to the instance itself and in a class method, self will always refer to the class itself.

Class variable

Class variables are for properties that belong to the entire class. A class level variable can be used for example for instance memoization, for keeping track of all instances of a class or for collaborating data. The name of the class variable is written with a double @ at the beginning of the word: @@class_variable_name.

#self.all is a class method for reading data stored in the class variable @@all. This is a class reader, very similar to an instance reader method that reads an instance property.

Class methods

With these methods we can access any class variable. A class method operate on the entire class and expose the class’ scope to the rest of our program. They don’t have access to the instance scope.

The most common class methods are:

Class finders

A class finder method returns existing instances.

Custom class constructors

A class constructor is any class method that instantiates an instance of the class.

Class operators

A class operator is perfect to manipulate any class level data.

 

Programming paradigms

A programmer can follow different approaches, also known as programming paradigms, when writing code.  Two of the most important programming paradigms are the procedural and the Object-Oriented ones.

Usually a programming language falls under one paradigm like  Ruby , that is mainly an OOP language. But some other languages as JavaScript can be multi-paradigm and support procedural, object-oriented (prototype-based) and functional programming styles.

The main difference between them is how you organise your code:

-In a procedural approach your code is a list of instructions or steps to be carried out.

-In an Object-Oriented approach your code is organised using objects and classes.

Reading about the different paradigms I found lots of new and very complicated concepts as: modularisation as a main trait of the procedural paradigm, and data abstraction, encapsulation, polymorphism, inheritance and serialisation-marshalling as traits for the Object-Oriented paradigm.

Apart from the desire to look the other way, I decided create some short and easy to understand definitions. Maybe the next time I find these concepts I don’t feel so uncomfortable!

  • Modularisation: It is used for reducing the complexity of a system subdividing a computer program into modules  or separate software components.
  • Data abstraction: It is the reduction of data to a simplified representation of the whole.  In OOP the programmer would just keep the relevant data inside the object hiding everything else. Data hiding is a software development technique.
  • Encapsulation : It is the inclusion within an object  of all the data and methods need for the object to function.
  • Polymorphism: It is the ability to present the same interface (Shape) for differing data types (square, circle, etc).
  • Inheritance: It enables new objects (subclasses or derived classes) to take on the properties of existing objects (superclasses or base classes).
  • Serialisation and marshalling:  Both are for transforming objects into series of bits. In particular, marshalling is about getting parameters from here to there, while serialisation is about copying structured data to or from a primitive form such as a byte stream. What is byte stream? A sequence of bits. What is a bit? It is the smallest unit of data in a computer. And what am I doing right now? Yak shaving!

About me

Hi! My name is Diana and my professional background is in the Art Market.

I have a degree in Clarinet Performance, studied Art History in University and have a Masters degree in Art Market studies and management of related companies, all in Spain.

I moved to London after finishing my master’s degree and have held posts across the art world where I strengthened my knowledge of the commercial aspect of contemporary art with established artists and learned more about the avant-garde and experimental work of younger and emerging artists.

Following the birth of my daughter in 2017 I decided to change career and retrain as a web developer. This change was motivated by the possibility of using my creativity in a technical context and having an intellectually challenging job where the learning process is crucial.

Nowadays I am doing the Online Software Engineering Bootcamp at Flatiron School and I am busy creating a website using Bootstrap and writing a game registration web app for a local sports team’s website. For this last project I am learning a little bit of PHP and MySQL.

Before applying to my programming bootcamp I have been doing some readings and completing various online education courses in order to get the basics on HTML, CSS and JS. Here is a list of what I have done so far:

Online education courses:

OpenClassrooms

  1. Understanding the Web
  2. Manage your code with Git and GitHub
  3. Learn the command line in Terminal
  4. Learn to Code with JavaScript
  5. Use JavaScript in your web projects

Code Academy

  1. Introduction to HTML
  2. Learn CSS

Flatiron School

  1. Coding Bootcamp Prep

Books:

  1. Coding with JavaScript for Dummies
  2. Eloquent JavaScript
  3. Static Site Generators

Exercises:

GitHub.io where I include my work.