Why should you use CocoaPods?

This blog post has the intention to show to beginners the tool called CocoaPods, if you’re already familiar with it you may find it boring because I’ll probably show you something you already now.

What’s CocoaPods?

“CocoaPods is a dependency manager for Swift and Objective-C Cocoa projects. It has over 37 thousand libraries and is used over 2.6 million apps. CocoaPods can help you scale your projects elegantly.”(CocoaPods)

If you’re familiar with Rails, you may remember the Gemfile system, and yes, it works just like that. In fact CocoaPods is a gem for Ruby. You can contribute for the project by sending a pull request on the official repository.

 

The advantages of using CocoaPods

You are ready to develop your first iOS project, you’re full of ideias, at first having a dependency manager seems to be pretty not necessary, but remember, you app won’t be the same after every update, in fact, if you’re planning to build the next successful app, you may need starting worrying about how to organize it, this where CocoaPods comes in.

Managing a project dependencies by hand it is not the best ideia, actually, it is a terrible idea, imagine having a project with 50 dependencies and having to update all of them ? Scary huh ? That’s the reason you need a project manager, with CocoaPods if you want to update a pod(That’s how a dependency is called in CocoaPods, like a gem in Ruby) you just need to type:

pod update the_name_of_the_pod

and it will do all the work for you, after that you just need to recompile your project on XCode, and that’s all.

Another reason to use CocoaPods is: a lot of big companies are using it, like Facebook , Google and many others.

You have a lot of support, there are thousands of apps using it, and Stackoverflow if full of questions about it.

Acknowledgment

Thanks for reading this post, it is always a pleasure writing for you guys. If you like what you just read and somehow you want to support me, you can become a Patreon . Being my Patreon makes you get my private posts about programming with iOS and Ruby.

If you’d like to know more about me or what I do, you can follow me on Instagram or Twitter .

See you next time!

Advertisements

Why did I choose Ruby as my first programming language ?

Introduction

Back in the time when I was starting learning how to program, I remember a bunch of guys talking about Java and many other cool stuff and I was really excited about it, but there was also a guy, a shy one, which said me he was learning a different programming language, the one you won’t get scared at first glance, the one with an easy syntax and also a a cool framework called Rails. The programming language he was talking about was Ruby.

At first I thought “Man, this guy is insane, Java is the future, this programming language is not even popular”, but I was wrong, I live in Brazil and that time (January 2010) Ruby wasn’t a known programming language here, but after some research I realized it was popular in other countries like the USA, then I’ve decided to take a look at it.

The First Book

I remember the first book I got for learning Ruby, it was Programming Ruby by The Pragmatic Programmer’s Guide  , at first I was amazed by the way the book was organized, they used a non convencional way of teaching a new programming language, which sounded pretty amazing.

After I finished that book I decided to read “The Agile Development with Rails ” also from The Pragmatic Programmer’s Guide, and once again, another cool way of teaching a framework, it was like, reading some basic stuff and then jump into a real application.

The First Job

After studying lots of things about Ruby/Rails I realized I needed a job, but then I thought: How to get a job in a country where Ruby is not even known ? Lucky, I met a guy that was working for a company which I don’t remember the name right now, but he was using Rails and that fitted perfectly for me, so I asked him for an opportunity to work with him, and surprisingly he said yes.

We started working from his garage (I know this sounds like a cliche, but it was like that), the first projected I did was a simple web site with a couple of pages like: Contact, Bio, Fotos. I was really excited when I finished it, you know, the feeling you have when you get something done.

After a couple of months working with him, I realized I learnt how to program faster as I’d be if I’d studied Java. I’m not saying here Java is a bad language, but in that time I was a beginner and making a lot of things with less code and a more descriptive programming language sounded like a dream to me.

 

Nowadays

I’m still using Ruby on Rails on some projects, but not as much as before, but why ? Because now I’m working as a mobile developer and there are many things a I need to deal when making apps, one of them is: Scalability. Ruby is not scalable which is pretty bad for today’s applications. You may wondering, but there are a lot of solutions for that with Rails, I know, the fact is: I need something made for it, this is how I met Elixir , but this is a subject for another Post (which I promise I will do it soon).

 

Advices

If you’re new to the programming world, you should learn Ruby. Why ? Because it is still popular, and you can understand how the programming world works faster. There are still a lot of jobs for Ruby developers, of course, with a lot of skills needed, but still worth it.

Acknowledgment

Thanks for reading this post, it is always a pleasure writing for you guys. If you like what you just read and somehow you want to support me, you can become a Patreon . Being my Patreon makes you get my private posts about programming with iOS and Ruby.

If you’d like to know more about me or what I do, you can follow me on Instagram or Twitter .

See you next time!

How to build an API using Rails – Part 1

The Rails 5 is out, and of course it came with a lot of new features and improvements, on this tutorial I’m going to cover the Rails API feature.

With all the Javascript stuff coming out, we have a lot of tools for building APIs these days, we have Express, Sails Js and many others. How about Ruby Developers? The most cool feature of Rails 5 is Rails API.

Rails::API is a subset of a normal Rails application, created for applications that don’t require all functionality that a complete Rails application provides. It is a bit more lightweight, and consequently a bit faster than a normal Rails application. The main example for its usage is in API applications only, where you usually don’t need the entire Rails middleware stack nor template generation.

Before you starting doing something you should download the most recent version of Ruby and Rails. I’d recommend using RVM for managing many versions of Ruby.

With all set the first command you should run is:

rails new rails_api_tutorial –api

This command will generate only the basic structure for building an API. It will make the ApplicationController inherit from ActionController::API instead of ActionController::Base. It will also configure the generators to skip generating views, helpers and assets when you generate a new resource.

The next step is to setup the testing stuff like: Rspec, FactoryGirl, Shoulda-Matchers and Rails Controller Testing.

After you adding all those gems your Gemfile should looks like this:

bildschirmfoto-2017-02-02-um-08-22-34

Hit bundle install to install those gems, and check the documentation of each one for more information about putting them to work.

After that you should create a folder called models inside of the spec/ folder, which will contain all the model specs of the API.

With all set, now we will create the first model spec of the application. Inside the spec/models folder create a Ruby file called post_spec.rb

The things the should be tested on model are: Table Fields, Validations, Relationships and any method you’d create. 

We are going to create a simple table for handling our posts, but first we need to create the spec for it:

spec/models/post_spec.rb

bildschirmfoto-2017-02-02-um-09-00-14

If we run this spec it will shows an error, because we haven’t implemented the Post model yet. Let’s do it:

bildschirmfoto-2017-02-02-um-09-13-08

We also need to create a migration with all the fields. For creating a migration run:

rails g migration post

After that, open the migration and add the fields:

bildschirmfoto-2017-02-02-um-09-39-24

Now you can migrate:

rails db:migrate

And prepare the migration for the tests:

rails db:test:prepare

After all you can run the command:

rspec

And all the tests should  pass:

bildschirmfoto-2017-02-02-um-09-39-53

The first part of this tutorial we covered:

  • The creation of an API only Rails Application
  • The setup of all the testing stuff
  • The creating of the Post model

On the next part of this tutorial I’m going to cover about controllers, how to test them and so on.

The source code of this tutorial is available on my Github: https://goo.gl/7Gr8WC

Do you like this post ? Would you mind to follow it ?

See you guys on the next part.

How to solve: Sorry, you can’t use byebug without Readline (Mac OS)

Byebug is a nice gem for debugging Ruby 2.

The most common problem to happen using this gem is the error: ‘Sorry, you can’t use byebug without Readline‘ , lucky for those running Mac OS, this issue can be simply solved by using this command on terminal:

ln -s /usr/local/opt/readline/lib/libreadline.dylib /usr/local/opt/readline/lib/libreadline.6.dylib

How to get the full path image url with carrierwave gem

In this post I’m going to cover how to get the full path url of an uploaded image using carrierwave gem.

By default carrierwave stores the uploaded images at /uploads/model_name/id/image.png

If you request the image url from the database it will only return the /uploads/model_name/id/image.jpg directory.

What if you need the full path ? Like: http://www.mysite.com/uploads/model_name/id/image.jpg

The only thing you need to do is to to set the asset_host in your development/production environment:Captura de Tela 2016-06-05 às 07.56.20.png

After that you need to restart your server and now you can get the full path of the image.

Feel free to ask questions and make suggestions 🙂

Acknowledgment

Thanks for reading this post, it is always a pleasure writing for you guys. If you like what you just read and somehow you want to support me, you can become a Patreon . Being my Patreon makes you get my private posts about programming with iOS and Ruby.

If you’d like to know more about me or what I do, you can follow me on Instagram or Twitter .

See you next time!

Cpf_Validation Gem

Some days ago I released my first gem for CPF validation.

I’ve seen many gems which do the same but the authors seem to no update them.

You can help me to improve this gem by sending a pull request on github: https://github.com/GabrielLidenor/cpf_validation

Installation

As gem:

# in Gemfile
gem 'cpf_validation'

# Run bundler
$ bundle install

Usage

Validating a CPF attribute:

class Patient < ActiveRecord::Base
  validates_cpf :cpf_attr
  # or
  validates :cpf_attr, :cpf => true
end

Validating an attribute that can store CPF:

class Customer < ActiveRecord::Base
  validates_cpf :cpf_attr
  # or
  validates :cpf_attr, :cpf => true
end

Regular validation options:

:allow_nil   - Allows a nil value to be valid
:allow_blank - Allows a nil or empty string value to be valid
:if          - Executes validation when :if evaluates true
:unless      - Executes validation when :unless evaluates false
:on          - Specifies validation context (e.g :save, :create or :update). Default is :save

Working with Sinatra – Part 2

In the last post I showed how to install Sinatra and how to write a simple code. In these post I’m going to cover Views. View is the V of MVC pattern.

First of all you need to create a folder called views

mkdir views

In this folder you’ll put all your views files .erb or .haml.

To render a view you need to define the format of the view (.erb or .haml) and the name of your view.

Ex:

get '/' do
  erd :index
end

This code will render a view called index in the views folder.

Obs: You need to create a view called index.rb in your views folder.

You can also define a template in your views folder and use it in some of your views

Ex:

get '/' do
  erb :index , layout: :post
end

This code will render the view index with the post layout (views/post.rb)

There are  many availble options in the options hash.

Here all of them:

locals
List of locals passed to the document. Handy with partials. Example: erb "<%= foo %>", :locals => {:foo => "bar"}

default_encodingString 
encoding to use if uncertain. Defaults to settings.default_encoding.

views
Views folder to load templates from. Defaults to settings.views.

layout
Whether to use a layout 
(true or f   alse). If it's a Symbol, specifies what template to use. Example: erb :index, :layout => !request.xhr?

content_type
Content-Type the template produces. Default depends on template language.

scope
Scope to render template under. Defaults to the application instance. If you change this, instance variables and helper methods will not be available.

layout_engine
Template engine to use for rendering the layout. Useful for languages that do not support layouts otherwise. Defaults to the engine used for the template. Example: set :rdoc, :layout_engine => :erb

layout_options
Special options only used for rendering the layout. Example: set :rdoc, :layout_options => { :views => 'views/layouts' }

In the third part of this post we will start coding a simple blog application.

See ya!