Skip to content

Instantly share code, notes, and snippets.

View gusrub's full-sized avatar
🏠
Working from home

Gustavo Rubio gusrub

🏠
Working from home
View GitHub Profile
// RUN THIS IN A NODE CONSOLE
let callTimes = 0;
function fibonacci(n) {
callTimes++;
switch(n) {
case 0:
return 0;
case 1:
return 1;
default:
@gusrub
gusrub / index.php
Created April 26, 2018 04:28
Keyword arguments for translation PHP
<?php
putenv("APP_LANG=es_MX.utf8");
require_once('i18n.php');
class Person
{
const VERSION = '1.0.0';
public $firstName;
@gusrub
gusrub / i18n.php
Created April 26, 2018 04:23
i18n translation class
<?php
use \InvalidArgumentException as InvalidArgumentException;
/**
* This class contain helper methods to localize and translate the application.
*/
class I18n
{
/**
@gusrub
gusrub / index.php
Created April 26, 2018 04:00
Encapsulating translation
<?php
require_once('i18n.php');
use function Helpers\I18n\translate;
class Person
{
const VERSION = '1.0.0';
public $firstName;
@gusrub
gusrub / i18n.php
Created April 26, 2018 03:56
I18n helper
<?php
namespace Helpers\I18n {
// I18N support information here
$language = 'es_MX.utf8';
putenv("LC_ALL=$language");
putenv("LC_LANG=$language");
setlocale(LC_ALL, $language);
// // Set the text domain as 'messages'
@gusrub
gusrub / index.php
Last active April 16, 2018 07:21
Internationalization PHP 1
<?php
class Person
{
const VERSION = '1.0.0';
public $firstName;
public $lastName;
public $dob;
class UsersController < ApplicationController
def create
service = CreateUserService.new(@user, params[:send_email])
if service.perform
render json: service.output, status: 200
else
render json service.errors, status: 400
end
end
class UsersController < ApplicationController
def create
service = CreateUserService.new(@user, params[:send_email])
if service.perform
render json: service.output, status: 200
else
render json service.errors, status: 400
end
end
# service name hints that it does
class CreateUserService
# default attributes that all services should have
attr_reader :errors, :messages, :output
# custom attributes for what this service does
attr_reader :user, :send_email
# we receive all parameters in the initialization
def initialize(user:, send_email: true)
class TokenProvider
def self.generate(email)
uri = URI("https://example.com/#{email}")
response = Net::HTTP.get_response(uri)
if response.code == '200'
return JSON.parse(response.body)[:token]
end
end
end