Skip to content

Instantly share code, notes, and snippets.

@benhawker
benhawker / presenter.rb
Created February 2, 2016 08:37
Rails Presenters example
# Assume you also have the following "general case" presenters
# app/presenters/cancellation_policy_presenter.rb
# app/presenters/daily_price_presenter.rb
# app/presenters/weekly_price_presenter.rb
# Now for some presenter fun..
# app/presenters/room_presenter.rb
class RoomPresenter
# gives you access to RoomPresenter#cancellation_policy (see below)
@benhawker
benhawker / title_generator.rb
Last active April 8, 2016 07:06
Automatic Description Generator - Rails Service Class for a Property Rental Web App
module Rooms
class TitleGenerator
attr_reader :room, :locale
def initialize(room, locale="en")
@room = room
@locale = locale
end
def generate
@benhawker
benhawker / description_generator.rb
Created April 8, 2016 07:07
Rails Service Class to Auto Generate Descriptions in various locales
module Rooms
class DescriptionGenerator
attr_reader :room, :locale
def initialize(room, locale="en")
@room = room
@locale = locale
end
def generate
RedDotRubyConf 2016 links & resources
===
📆 Jun 23-24, 2016
🌏 Web site: http://reddotrubyconf.com/ Twitter: http://twitter.com/reddotrubyconf
Credits to [@cheeaun on Twitter](https://twitter.com/cheeaun)
🕙 Previously, on RedDotRubyConf...
@benhawker
benhawker / room_stats.rb
Last active August 12, 2016 08:23
Pulls values on views to inquiry to booking conversion into a CSV for analysis
require 'csv'
class RoomStats
VALUES = %w(num_views num_inquiries num_bookings
views_to_inquiry_conversion inquiry_to_booking_conversion
views_to_booking_conversion)
attr_reader :counts, :averages
def initialize
@benhawker
benhawker / csv_to_json.rb
Created April 28, 2017 14:59
Takes a CSV and outputs a required format in json.
require 'csv'
require 'json'
file = 'data.csv'
# TODO: Check file encoding - some destinations airports are missing a few chars.
file_contents = CSV.read(file, col_sep: ",", encoding: "ISO8859-1")
grouped_hash = file_contents[1..-1].group_by { |x| x[0] }
master_arr = []
id_iterator = 1
@benhawker
benhawker / codes.rb
Created May 4, 2017 14:20
Unique codes
def generate_coupon_code(user_id)
characters = %w(A B C D E F G H J K L M P Q R T W X Y Z 1 2 3 4 5 6 7 8 9)
code = ''
4.times { code << characters.sample }
code << user_id.to_s
code
end
require 'csv'
require 'json'
file = 'data2.csv'
# TODO: Check file encoding - some destinations airports are missing a few chars.
file_contents = CSV.read(file, col_sep: ",", encoding: "ISO8859-1")
grouped_hash = file_contents[1..-1].group_by { |x| x[0] }
master_arr = []
id_iterator = 1
@benhawker
benhawker / script.rb
Created June 7, 2017 16:36
Combining some xml files.
require 'nokogiri'
require 'pry'
# Layout - There remains an issue with the XML formatting Layout with i need to resolve.
files = %w(BookingPhrases CommonTerms ClientSide Validation)
# files = ["Layout"]
languages = %w(en-US it pt fr es de nl zh-CN)
files.each do |filename|
@benhawker
benhawker / README.md
Created July 19, 2017 09:01 — forked from lopezjurip/README.md
Ruby on Rails - Send (batch) emails using mailgun-ruby (HTTP, not SMTP)

Requisites:

  • Create a free account at https://www.mailgun.com/
  • Save your API Key as a environment variables: export MAILGUN_API_KEY=key-1111111111111111111111111

As example, I will use a model named Request.