-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
32 lines (28 loc) · 1.05 KB
/
app.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
require 'sinatra'
require 'faraday'
require 'nokogiri'
require 'json'
#index page
get '/' do
headers "Content-Type" => "text/html; charset=utf-8"
erb :index
end
#get city location
get '/find/city/:city' do
yahoo_appid = 'pmQ_VnzV34FddFT6do_XVxcjzkrjmeKzNpJjLP1MqfPSEN6yCN0vunwBt8QbZYWEc65EPzD6o8VVmDYXTQZbPY0DkXSGUO4-'
city_url = "http://where.yahooapis.com/v1/places.q('#{params[:city]}')?appid=#{yahoo_appid}"
connection = Faraday.new
response = connection.get(city_url)
document = Nokogiri::HTML(response.body)
latitude = document.xpath('//places/place/centroid/latitude').text
longitude = document.xpath('//places/place/centroid/longitude').text
city = document.xpath('//places/place/name').text
{:city => city, :latitude => latitude, :longitude => longitude}.to_json
end
#get weather data
get '/forecast/:latitude/:longitude' do
forecast_url = "https://api.forecast.io/forecast/0fe656d926f844bc4c0745ac4ea9814f/#{params[:latitude]},#{params[:longitude]}"
connection = Faraday.new
forecast = connection.get(forecast_url)
forecast.body
end