Created
April 8, 2011 16:57
-
-
Save mckelvey/910273 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# GET /entries/:hashtag/receiver | |
# POST /entries/:hashtag/receiver.json | |
def receiver | |
if request.method == 'GET' and !params['hub.challenge'].blank? and params['hub.mode'] == 'subscribe' | |
if !params['hub.verify_token'].blank? | |
game = Game.find_by_token(params['hub.verify_token']) | |
if !game.nil? and game.hashtag == params[:hashtag] | |
render :status => 200, :text => params['hub.challenge'] | |
else | |
InstagaMe::Log.rails "bad token match for #{params['hub.verify_token']}; returning status 406" | |
render :status => 406, :text => "unauthorized token" | |
end | |
else | |
render :status => 200, :text => params['hub.challenge'] | |
end | |
return | |
elsif request.method == 'POST' and params[:hashtag] == params[:_json][0][:object_id] | |
# TODO: Need hexdigest match code here > help requested | |
# logger.info Instagram.client_secret.inspect | |
# logger.info params[:_json].inspect | |
# logger.info request.env['HTTP_X_HUB_SIGNATURE'].inspect | |
# logger.info HMAC::SHA1.hexdigest(Instagram.client_secret, params[:_json].to_s) | |
game = Game.find_by_hashtag(params[:hashtag]) | |
raise InstagaMe::Errors::GameNilError if game.nil? | |
if game.running? | |
client = Instagram.client(:access_token => game.instagator.access_token) | |
media = client.tag_recent_media(game.hashtag, {:count => 1}) | |
InstagaMe::Media.create_or_update(game, media[0]) | |
Delayed::Job.enqueue(GameRankJob.new(game.id), :priority => -1) | |
Delayed::Job.enqueue(CacheClearJob.new("games/#{game.hashtag}/entries"), :priority => -1) | |
Delayed::Job.enqueue(CacheClearJob.new("games#index"), :priority => -1) | |
tag = client.tag(game.hashtag) | |
if !tag.nil? and !tag.media_count.blank? and tag.media_count.to_i > game.entries.size | |
Delayed::Job.enqueue(CollectMissingEntriesJob.new(game.id), :priority => -10) | |
end | |
end | |
end | |
render :status => 200, :nothing => true | |
return | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment