Last active
June 21, 2019 22:30
-
-
Save lambtron/5898183 to your computer and use it in GitHub Desktop.
Parsing inbound email from Mandrill for the SF apartment hunt.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'mechanize' | |
class InboxController < ApplicationController | |
include Mandrill::Rails::WebHookProcessor | |
# For the apartment hunt. | |
def parse_inbound_sf_apartment_email | |
# Mandrill needs an ok in order to proceed to POST Mandrill events to this endpoint. | |
if request.head? | |
head :ok | |
else | |
# When receive new IFTTT email, use Mechanize to go to the URL of the listing. | |
# Then get two things: | |
# - anything that resembles a phone number | |
# - a response email address | |
# Afterwards, dial order for all roommates and connect w the broker | |
# Then send a templatized email. | |
if params['mandrill_events'] | |
text_body = '' | |
JSON.parse(params['mandrill_events']).each do |raw_event| | |
event = Mandrill::WebHook::EventDecorator[raw_event] | |
text_body = event['msg']['text'].to_s | |
end | |
# Get the URL of the craigslist listing. | |
url = text_body[/http\:(.*?)\.html/m] | |
# Mechanize to get email address and phone number. | |
a = Mechanize.new | |
begin | |
craigslist_listing = a.get(url.to_s) | |
rescue ArgumentError | |
# URL is not valid | |
puts "\n\n\n\nURL IS NOT VALID\n\n\n\n" | |
return "error" | |
else | |
# Regex to get email and phone number | |
email_addresses = craigslist_listing.content.to_s.scan(/[\w.!#\$%+-]+@[\w-]+(?:\.[\w-]+)+/).uniq! | |
phone_numbers = craigslist_listing.content.to_s.scan(/\W(\d{3}.?\d{3}.?\d{4})\W/m).uniq! - craigslist_listing.content.to_s.scan(/postingID=(.*?)\W/mi).uniq! | |
# 'Click-to-call'. | |
phone_numbers.each do |phone_number| | |
# puts phone_number | |
# Make outbound call to 314 (Andy, Jeff, whoever else). | |
# Then, make outbound call to phone number. | |
click_to_call(phone_number[0]) | |
end | |
# Send templatized email to email_address. | |
email_addresses.each do |email_address| | |
Mailer.email_apartment_listing(email_address, 'Apartment listing').deliver | |
end | |
end | |
end | |
render 'app/views/inbox/sfapartments.html', :formats => [:html] | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment