Skip to content

Instantly share code, notes, and snippets.

@giridhar
Created November 25, 2011 07:02
Show Gist options
  • Save giridhar/1392971 to your computer and use it in GitHub Desktop.
Save giridhar/1392971 to your computer and use it in GitHub Desktop.
Creating a Clothes Recommender
#Prerequisites
#Basic Ruby/Rails knowledge, including an installed version of Ruby 1.9.2, Rubygems, Bundler, and Rails 3.
#Basic Git knowledge, including an installed version of Git.
#Create a Heroku account http://www.heroku.com/
#All the commands work on linux
#Install the Heroku client:
$ gem install heroku
Write Your App
You may be starting from an existing app from <dropbox link>. If not you can create your own
$ rails new myapp
$ cd myapp
Edit your Gemfile and change the line:
group :production do
gem 'pg'
end
group :development, :test do
gem 'sqlite3'
end
gem 'httparty'
Install Gems
$ bundle install --without production
Store Your App in Git
$ git init
$ git add .
$ git commit -m "init"
Creating the recommender:
* create the models
create file app/models/google_weather.rb
class GoogleWeather
include HTTParty
base_uri "www.google.com"
attr_reader :long , :lat
def initialize(options)
@long = options[:long].to_f*1000000
@lat = options[:lat].to_f*1000000
end
def weather
@weather ||= self.class.get("/ig/api", :query => {:weather => ",,,#{@lat.to_i},#{@long.to_i}"}, :format => :xml)
end
def current_condition
@current_condition ||=@weather['xml_api_reply']['weather']['current_conditions']["condition"]["data"]
end
def current_temp
@current_temp ||= @weather['xml_api_reply']['weather']['current_conditions']["temp_f"]["data"]
end
def current_icon
@current_icon ||= "http://www.google.com/#{@weather['xml_api_reply']['weather']['current_conditions']["icon"]["data"]}"
end
def tomorrow_condition
@tomorrow_condition||=@weather['xml_api_reply']['weather']['forecast_conditions'][1]["condition"]["data"]
end
def tomorrow_high
@tomorrow_high||=@weather['xml_api_reply']['weather']['forecast_conditions'][1]["high"]["data"]
end
def tomorrow_low
@tomorrow_low||=@weather['xml_api_reply']['weather']['forecast_conditions'][1]["low"]["data"]
end
def tomorrow_avgtemp
@tomorrow_avgtemp||=(tomorrow_low.to_i+tomorrow_high.to_i)/2
end
end
create file app/models/clothes_recommender.rb
* Create controller
create file app/controller/recommender_controller.rb
class RecommenderController < ApplicationController
def index
@recommendations= ClothesRecommender.get_recommendation(params[:latitude].to_f,params[:longitude].to_f)
@recommendations[:details_url]="http://#{request.host_with_port}/recommender/details?latitude=#{params[:latitude]},longitude=#{params[:longitude]}"
end
def details
options={:lat=>params[:latitude],:long => params[:longitude]}
@gw=GoogleWeather.new(options)
@gw.weather
@today="Today's weather is #{@gw.current_condition} at #{@gw.current_temp}F, we recommend #{ClothesRecommender.lookup(@gw.current_condition,@gw.current_temp)}"
@tomorrow="Tomorrow's forecast #{@gw.tomorrow_condition} with High at #{@gw.tomorrow_high}F and Low at #{@gw.tomorrow_low}F, we recommend #{ClothesRecommender.lookup(@gw.tomorrow_condition,@gw.tomorrow_avgtemp)}"
end
end
create the views
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width; height=device-height; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;" />
<title>Action Details</title>
<style>
body { font-family: "Arial Black",Helvetica,Arial,sans-serif;}
div.list_container { width:100%; position:relative;margin-bottom: -35px; min-height: 100%;}
div.list_container div.day_title{}
</style>
</head>
<body marginheight="0" marginwidth="0">
<div class="list_container">
<div>
<div class='day_title'>Today:</div><div><%=@today%></div>
</div>
<div>
<div class='day_title'>Tomorrow:</div><div><%=@tomorrow%></div>
</div>
</div>
</body>
</html>
create xml builder
xml.instruct!
xml.dev_expert do
xml.display_text "#{@recommendations[:display_text]}"
xml.icon_hdpi_url "#{@recommendations[:icon]}"
xml.icon_mdpi_url "#{@recommendations[:icon]}"
xml.icon_ldpi_url "#{@recommendations[:icon]}"
xml.details_view_url "#{@recommendations[:details_url]}"
end
Deploy your code:
$ git push heroku master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment