Understanding Accessors in Ruby

One thing that puzzled me as a newbie (disclaimer: I still am) are accessors in Ruby, more commonly known as setters and getters or explicitly described as attr_reader, attr_writer and attr_accessor. Now let’s dive into the code first and describe the concepts of accessors after we’re done with coding. Initializing a Class Let’s say we want to create a class to resemble a Person with a name, and finally let’s try to access that name outside the class:...

February 25, 2018 · 5 min · Franccesco Orozco

Initialize Rails and Deploy to Heroku

Install Rails Install the gem: gem install rails Create New Rails Project Create a new project and cd into it: rails new ProjectTest cd ProjectTest Change Gemfile to add PostgreSQL Heroku works with PostgreSQL as backend database as it doesn’t support SQLite3, so you’ll have to add the pg gem in the Gemfile in a production group: group :production do gem 'pg' end IMPORTANT: After adding PostgreSQL to the production group in the Gemfile you’ll have to move the SQLite3 gem to a development group or delete it, if you work with PostgreSQL just delete it entirely but if you would like to keep SQLite3 for local development then move the gem to a dev group like this:...

February 23, 2018 · 3 min · Franccesco Orozco

Calculate Filename SHA1 with Ruby

Basic usage Where FILENAME is the filename that you want to calculate require 'digest/sha1' Digest::SHA1.hexdigest(FILENAME) More advanced usage Save this code as checkhash.rb, usage: checkhash.rb <filename>. require 'digest/sha1' # Usage: checkhash.rb <filename> filename = ARGV.pop if filename.nil? # if no filename specified then prints help puts 'Please specify the filename to calculate the hash' puts "Usage: #{File.basename($PROGRAM_NAME)} FILENAME" exit end # calculating SHA1 hash def calculate_hash(file) Digest::SHA1.hexdigest(file) end file_hash = calculate_hash(filename) puts "#{filename}: #{file_hash}"

February 22, 2018 · 1 min · Franccesco Orozco

HTTP Requests in Python

Before starting lets try HTTP requests with httpbin.org to test multiple HTTP methods with requests and JSON data. We’ll see how to extract data from a JSON-encoded response (e.g. {‘key’: ‘value’}) Install requests library With pipenv (recommended): pipenv install requests With pip: pip install requests GET request Check our IP address: import requests # get request, response is JSON-encoded myIP = requests.get('https://httpbin.org/ip') # extract value from JSON key 'origin' # {'origin': '38....

February 20, 2018 · 2 min · Franccesco Orozco

How to Get Started With Pipenv

What is pipenv Essentially Pipenv is pip + virtualenv and it’s a match made in heaven. It manages dependencies, required python versions (if pyenv is available), generates pipfiles which is more reliable than a requirements.txt file and it generates a virtual environment so you don’t screw other environments and its requirements. It automatically creates and manages a virtualenv for your projects, as well as adds/removes packages from your Pipfile as you install/uninstall packages....

February 20, 2018 · 2 min · Franccesco Orozco