Skip to content

Commit

Permalink
Add /go, rename Exception to Error.
Browse files Browse the repository at this point in the history
  • Loading branch information
stefansundin committed Jul 2, 2015
1 parent e1e28cd commit 52595ec
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
43 changes: 30 additions & 13 deletions app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand All @@ -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?

Expand All @@ -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"]
Expand All @@ -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"]
Expand All @@ -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"]}"
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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

0 comments on commit 52595ec

Please sign in to comment.