module Flucti module APIAccess extend self extend Utilities HOST = "api.flucti.com".freeze def site site_with_api_key(Parameters.fetch(:api_key) || (api_key_tasks.switch; Parameters.fetch(:api_key))) end def site_with_api_key(key) WebService::Site.new("http://#{key}@#{HOST}") end def configure! if (init = Parameters.store_dir / "init.rb").file? load(init) end Resources::BasicResource.site = site Resources::BasicResource.extend DeniedAccessRescue end def solve_denied_access! puts_title "Access denied" puts_long <<-MSG You API key seems invalid. If you reset it recently, please enter the new one at the prompt below. Otherwise, please contact us at #{SUPPORT_EMAIL_ADDR} so that we can sort it out. MSG api_key_tasks.prompt_and_store_key configure! end private def api_key_tasks @api_key_tasks ||= begin require "capistrano/configuration" tasks = Capistrano::Configuration.new tasks.extend Utilities tasks.load File.dirname(__FILE__) + '/tasks/apikey_tasks.rb' tasks.apikey end end module DeniedAccessRescue def request(*args) super rescue WebService::UnauthorizedAccess APIAccess.solve_denied_access! retry end end end end