Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

183 lines (142 sloc) 4.976 kb
require 'bundler/setup'
Bundler.require
require 'json'
Bundler.require(:development) if development?
require 'cgi'
require 'csv'
FuzzyMatch.engine = :amatch if defined?(Amatch)
configure do
enable :sessions
if File.exists?('./config.yml')
$config = YAML.load_file('./config.yml')
else
$config = {'foursquare_id' => ENV['FOURSQUARE_ID'], 'foursquare_secret' => ENV['FOURSQUARE_SECRET']}
end
use OmniAuth::Builder do
provider :foursquare, $config['foursquare_id'], $config['foursquare_secret']
end
DB = Sequel.connect $config['mysql']
if DB.table_exists?(:users) == false
DB.create_table :users do
String :uid, primary_key: true, index: true
String :token, index: true
String :username
end
end
if DB.table_exists?(:checkins) == false
DB.create_table :checkins do
primary_key :id
String :uid
String :checkin_id
DateTime :date
String :venue_name
String :business_name
String :business_symbol
String :change_price
String :change_percent
Text :message
Text :payload
end
end
COMPANIES = []
filter_criteria = /^The |, Inc\.?| Corporation| Incorporated| Ltd\.|, Ltd| Inc\.| Inc| Corp.| Corp| Company \(The\)| Company| Bancorp| Enterprises/i
first_row = false
CSV.foreach('./companylist.csv') do |row|
first_row = true and next if first_row
filtered_name = CGI.unescapeHTML row[1]
filtered_name.gsub!(filter_criteria, '')
filtered_name.strip!
COMPANIES << { name: row[1], symbol: row[0], filtered_name: filtered_name }
end
FUZZ = FuzzyMatch.new( COMPANIES, :read => :filtered_name )
end
def get_quote_for_name(name)
fuzz = FUZZ.dup
# Start with first word, require it to have a high percentage
first_word_result = fuzz.find name.split(' ').first, :gather_last_result => true
return nil if first_word_result.nil? || fuzz.last_result.score < 0.7
all_words_result = fuzz.find(name, :gather_last_result => true)
return nil if all_words_result.nil? || fuzz.last_result.score < 0.4 || first_word_result != all_words_result
quote = get_quote all_words_result[:symbol]
quote[:name] = all_words_result[:filtered_name]
quote
end
def get_quote(symbol)
result = RestClient.get "http://download.finance.yahoo.com/d/quotes.csv?s=#{symbol}&f=csv"
quote = CSV.parse(result).first
change_price, change_percent = quote[0].split(' - ')
{change_price: change_price, change_percent: change_percent, symbol: quote[1], market_cap: quote[2].to_i}
end
get '/' do
erb :index
end
get '/auth/foursquare' do
redirect '/auth/foursquare' if session[:foursquare_key].nil? || session[:foursquare_key].empty?
end
get '/auth/foursquare/test' do
erb :confirmed
end
get '/auth/:name/callback' do
token, uid = request.env['omniauth.auth'].credentials.token, request.env['omniauth.auth'].uid
user = DB[:users].filter(uid: uid).first
if user.nil?
DB[:users].insert uid: uid, token: token
end
erb :confirmed
end
# When a user checks in, Foursquare will send a post request here
post '/foursquare/push' do
puts "=============================="
# puts params['secret']
checkin = JSON.parse params['checkin'], symbolize_names: true
# puts checkin.inspect
# User ID is at: checkin[:user][:id]
# Venue name is at: checkin[:venue][:name]
# See https://developer.foursquare.com/overview/realtime for more docs
user = DB[:users].filter(uid: checkin[:user][:id]).first
return 'no user' if user.nil?
# Look up the stock price for the symbol
quote = get_quote_for_name checkin[:venue][:name]
if quote.nil?
DB[:checkins].insert(
date: Time.at(checkin[:createdAt]),
uid: checkin[:user][:id],
checkin_id: checkin[:id],
venue_name: checkin[:venue][:name]
)
return 'no stock symbol'
end
# Now send a message back to the checkin so Foursquare shows it in the app
message = "#{quote[:name]} (#{quote[:symbol]}) is a publicly traded company, stock price changed #{quote[:change_price]} (#{quote[:change_percent]}) Tap to read more about #{quote[:name]}"
url = "http://www.reuters.com/finance/stocks/companyProfile?symbol=#{quote[:symbol]}.O"
puts "Posting message to user:"
puts message
puts url
# https://developer.foursquare.com/docs/checkins/reply
# Params to send:
# CHECKIN_ID from checkin[:id]
# text
# url
# oauth_token
begin
RestClient.post "https://api.foursquare.com/v2/checkins/#{checkin[:id]}/reply", {
oauth_token: user[:token],
text: message,
url: url
}
rescue
end
DB[:checkins].insert(
date: Time.at(checkin[:createdAt]),
uid: checkin[:user][:id],
checkin_id: checkin[:id],
venue_name: checkin[:venue][:name],
business_name: quote[:name],
business_symbol: quote[:symbol],
change_price: quote[:change_price],
change_percent: quote[:change_percent],
message: message,
payload: params['checkin']
)
'ok'
end
Jump to Line
Something went wrong with that request. Please try again.