From 52595eca4232ab9f735783556d24db614586a06f Mon Sep 17 00:00:00 2001 From: Stefan Sundin Date: Thu, 2 Jul 2015 15:48:46 -0700 Subject: [PATCH] Add /go, rename Exception to Error. --- Gemfile.lock | 2 +- app.rb | 43 ++++++++++++++++++++++++++++++------------- 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 9592d82..1ffd4c4 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -61,7 +61,7 @@ GEM rack (~> 1.4) rack-protection (~> 1.4) tilt (>= 1.3, < 3) - sinatra-contrib (1.4.4) + sinatra-contrib (1.4.6) backports (>= 2.0) multi_json rack-protection diff --git a/app.rb b/app.rb index 0ea50b9..e32885e 100644 --- a/app.rb +++ b/app.rb @@ -3,9 +3,9 @@ require "erb" require "active_support/core_ext/string" -class YoutubeException < Exception; end -class FacebookException < Exception; end -class InstagramException < Exception; end +class YoutubeError < StandardError; end +class FacebookError < StandardError; end +class InstagramError < StandardError; end def httparty_error(r) "#{r.request.path.to_s}: #{r.code} #{r.message}: #{r.body}. #{r.headers.to_h.to_json}" @@ -16,6 +16,19 @@ def httparty_error(r) erb :index end +get "/go" do + return "Insufficient parameters" if params[:q].empty? + if /^https?:\/\/(www\.)?youtu(\.?be|be\.com)/ =~ params[:q] + redirect "/youtube?q=#{params[:q]}" + elsif /^https?:\/\/(www\.)?facebook\.com/ =~ params[:q] + redirect "/facebook?q=#{params[:q]}" + elsif /^https?:\/\/(www\.)?instagram\.com/ =~ params[:q] + redirect "/instagram?q=#{params[:q]}" + else + "Unknown service" + end +end + get "/youtube" do return "Insufficient parameters" if params[:q].empty? @@ -41,7 +54,7 @@ def httparty_error(r) if user response = HTTParty.get("https://www.googleapis.com/youtube/v3/channels?part=id&forUsername=#{user}&key=#{ENV["GOOGLE_API_KEY"]}") if !response.success? - raise YoutubeException, response + raise YoutubeError, response end if response.parsed_response["items"].length > 0 channel_id = response.parsed_response["items"][0]["id"] @@ -51,7 +64,7 @@ def httparty_error(r) if video_id response = HTTParty.get("https://www.googleapis.com/youtube/v3/videos?part=snippet&id=#{video_id}&key=#{ENV["GOOGLE_API_KEY"]}") if !response.success? - raise YoutubeException, response + raise YoutubeError, response end if response.parsed_response["items"].length > 0 channel_id = response.parsed_response["items"][0]["snippet"]["channelId"] @@ -76,7 +89,7 @@ def httparty_error(r) response = HTTParty.get("https://graph.facebook.com/v2.3/#{name}?access_token=#{ENV["FACEBOOK_APP_ID"]}|#{ENV["FACEBOOK_APP_SECRET"]}") return "Can't find a page with that name. Sorry." if response.code == 404 - raise FacebookException, response if !response.success? + raise FacebookError, response if !response.success? data = response.parsed_response redirect "/facebook/#{data["id"]}/#{data["username"]}" @@ -86,7 +99,7 @@ def httparty_error(r) @id = id response = HTTParty.get("https://graph.facebook.com/v2.3/#{id}/posts?access_token=#{ENV["FACEBOOK_APP_ID"]}|#{ENV["FACEBOOK_APP_SECRET"]}") - raise FacebookException, response if !response.success? + raise FacebookError, response if !response.success? @data = response.parsed_response["data"] @user = @data[0]["from"]["name"] rescue username @@ -106,7 +119,7 @@ def httparty_error(r) redirect_uri: request.base_url+request.path_info, code: params[:code] }) - raise InstagramException, httparty_error(response) if !response.success? + raise InstagramError, httparty_error(response) if !response.success? headers "Content-Type" => "text/plain" "heroku config:set INSTAGRAM_ACCESS_TOKEN=#{response.parsed_response["access_token"]}" else @@ -130,7 +143,7 @@ def httparty_error(r) if name response = HTTParty.get("https://api.instagram.com/v1/users/search?q=#{name}&access_token=#{ENV["INSTAGRAM_ACCESS_TOKEN"]}") - raise InstagramException, response if !response.success? + raise InstagramError, response if !response.success? user = response.parsed_response["data"].find { |user| user["username"] == name } end @@ -151,7 +164,7 @@ def httparty_error(r) headers "Content-Type" => "application/atom+xml;charset=utf-8" return erb :instagram_error end - raise InstagramException, response if !response.success? + raise InstagramError, response if !response.success? @data = response.parsed_response["data"] @user = @data[0]["user"]["username"] rescue username @@ -185,17 +198,21 @@ def httparty_error(r) error do + status 500 "Sorry, a nasty error occurred: #{env["sinatra.error"].message}" end -error YoutubeException do +error YoutubeError do + status 503 "There was a problem talking to YouTube." end -error FacebookException do +error FacebookError do + status 503 "There was a problem talking to Facebook." end -error InstagramException do +error InstagramError do + status 503 "There was a problem talking to Instagram." end