Skip to content

Commit 8ceadb9

Browse files
committed
initial commit
0 parents  commit 8ceadb9

File tree

7 files changed

+118
-0
lines changed

7 files changed

+118
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# hide those environment variables
2+
.env

Gemfile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
source 'https://rubygems.org'
2+
ruby '2.0.0'
3+
4+
gem 'sinatra'
5+
gem 'twilio-ruby'
6+
gem 'httparty'
7+
gem 'activesupport'

Gemfile.lock

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
GEM
2+
remote: https://rubygems.org/
3+
specs:
4+
activesupport (4.1.7)
5+
i18n (~> 0.6, >= 0.6.9)
6+
json (~> 1.7, >= 1.7.7)
7+
minitest (~> 5.1)
8+
thread_safe (~> 0.1)
9+
tzinfo (~> 1.1)
10+
builder (3.2.2)
11+
httparty (0.13.3)
12+
json (~> 1.8)
13+
multi_xml (>= 0.5.2)
14+
i18n (0.6.11)
15+
json (1.8.1)
16+
jwt (1.0.0)
17+
minitest (5.4.2)
18+
multi_json (1.10.1)
19+
multi_xml (0.5.5)
20+
rack (1.5.2)
21+
rack-protection (1.5.3)
22+
rack
23+
sinatra (1.4.5)
24+
rack (~> 1.4)
25+
rack-protection (~> 1.4)
26+
tilt (~> 1.3, >= 1.3.4)
27+
thread_safe (0.3.4)
28+
tilt (1.4.1)
29+
twilio-ruby (3.13.1)
30+
builder (>= 2.1.2)
31+
jwt (~> 1.0.0)
32+
multi_json (>= 1.3.0)
33+
tzinfo (1.2.2)
34+
thread_safe (~> 0.1)
35+
36+
PLATFORMS
37+
ruby
38+
39+
DEPENDENCIES
40+
activesupport
41+
httparty
42+
sinatra
43+
twilio-ruby

Procfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
web: bundle exec rackup config.ru -p $PORT

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
gPhone
2+
=======
3+
4+
- Tinker with endpoints to help my GF out (she has an old crappy phone)
5+
6+
to do
7+
-----
8+
- ~~Procfile way of populating ENV~~
9+
- reminders with Redis or MongoDB
10+
- cron jobs to harass her

config.ru

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
require './g_phone'
2+
run Sinatra::Application

g_phone.rb

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
require 'sinatra'
2+
require 'rubygems'
3+
require 'twilio-ruby'
4+
require 'httparty'
5+
require 'active_support'
6+
require 'active_support/core_ext'
7+
8+
get '/' do
9+
@@body = params[:Body]
10+
redirect to('/g_weather')
11+
end
12+
13+
get '/g_weather' do
14+
# google geocoordinates API
15+
weather_search = @@body.gsub(" ", "+")
16+
google_api_response = HTTParty.get("https://maps.googleapis.com/maps/api/geocode/json?address=#{weather_search}")
17+
if google_api_response['status'] != "OK"
18+
@@error_msg = "Google request didn't work."
19+
redirect to('/not_right')
20+
end
21+
22+
# forecast_io API
23+
city_lat = google_api_response["results"][0]["geometry"]["location"]["lat"]
24+
city_lng = google_api_response["results"][0]["geometry"]["location"]["lng"]
25+
forecast_io_response = HTTParty.get("https://api.forecast.io/forecast/#{ENV['FORECAST_IO_KEY']}/#{city_lat},#{city_lng}")
26+
if not forecast_io_response['error'].nil?
27+
@@error_msg = "Forecast.io request didn't work."
28+
redirect to('/not_right')
29+
end
30+
31+
# all okay from APIs, get the weather info
32+
today_summary = forecast_io_response["daily"]["data"][0]["summary"]
33+
current_temp = forecast_io_response["currently"]["apparentTemperature"].round
34+
today_max = forecast_io_response["daily"]["data"][0]["apparentTemperatureMax"].round
35+
today_min = forecast_io_response["daily"]["data"][0]["apparentTemperatureMin"].round
36+
today_max_time = Time.at(forecast_io_response["daily"]["data"][0]["apparentTemperatureMaxTime"]).in_time_zone("America/Denver").strftime("%-l %p %Z")
37+
today_min_time = Time.at(forecast_io_response["daily"]["data"][0]["apparentTemperatureMinTime"]).in_time_zone("America/Denver").strftime("%-l %p %Z")
38+
39+
# send weather text back
40+
message = "\n#{@@body}: #{today_summary}\nHigh of #{today_max}˚F at #{today_max_time}.\nLow of #{today_min}˚F at #{today_min_time}.\nNow: #{current_temp}˚F."
41+
twiml = Twilio::TwiML::Response.new do |r|
42+
r.Message message
43+
end
44+
twiml.text
45+
end
46+
47+
# send error msg
48+
get '/not_right' do
49+
twiml = Twilio::TwiML::Response.new do |r|
50+
r.Message @@error_msg
51+
end
52+
twiml.text
53+
end

0 commit comments

Comments
 (0)