Sha256: 34a119be1c88971f143ed7d26a50ab5fc688258db644cd24c80cda9bc1b822c9

Contents?: true

Size: 1.36 KB

Versions: 1

Compression:

Stored size: 1.36 KB

Contents

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
Flucti-flucti-cli-0.1.16 lib/flucti/api_access.rb