Sha256: 69a7bdd21b707af76f8a987680bd91a48166b6437302f6f78f6c2fb13d0b7f54

Contents?: true

Size: 1.28 KB

Versions: 65

Compression:

Stored size: 1.28 KB

Contents

class Shelly::Client
  def authorize_with_email_and_password(email, password)
    forget_authorization
    @email = email; @password = password
    api_key = get_token
    store_api_key_in_netrc(email, api_key)
  end

  def user_email
    @email || email_from_netrc
  end

  def authorize!
    get_token
    true
  end

  def forget_authorization
    remove_api_key_from_netrc
  end

  def get_token
    get("/token")["token"]
  end

  def basic_auth_from_netrc
    if netrc
      user, password = netrc[api_host]
      {:user => user, :password => password}
    else
      {}
    end
  end

  def store_api_key_in_netrc(email, api_key)
    FileUtils.mkdir_p(File.dirname(netrc_path))
    FileUtils.touch(netrc_path)
    FileUtils.chmod(0600, netrc_path)

    netrc[api_host] = [email, api_key]
    netrc.save
  end

  def remove_api_key_from_netrc
    if netrc
      netrc.delete(api_host)
      netrc.save
      true # must return a truthy value to print logout confirmation
    end
  end

  def email_from_netrc
    netrc[api_host].first if netrc
  end

  def netrc
    @netrc ||= File.exists?(netrc_path) && Netrc.read(netrc_path)
  end

  def netrc_path
    default = Netrc.default_path
    encrypted = default + ".gpg"
    if File.exists?(encrypted)
      encrypted
    else
      default
    end
  end
end

Version data entries

65 entries across 65 versions & 1 rubygems

Version Path
shelly-0.5.7 lib/shelly/client/auth.rb
shelly-0.5.6 lib/shelly/client/auth.rb
shelly-0.5.5 lib/shelly/client/auth.rb
shelly-0.5.4 lib/shelly/client/auth.rb
shelly-0.5.3 lib/shelly/client/auth.rb
shelly-0.5.2 lib/shelly/client/auth.rb
shelly-0.5.1 lib/shelly/client/auth.rb
shelly-0.5.0 lib/shelly/client/auth.rb
shelly-0.4.42 lib/shelly/client/auth.rb
shelly-0.4.41 lib/shelly/client/auth.rb
shelly-0.4.40 lib/shelly/client/auth.rb
shelly-0.4.39 lib/shelly/client/auth.rb
shelly-0.4.38 lib/shelly/client/auth.rb
shelly-0.4.37 lib/shelly/client/auth.rb
shelly-0.4.36 lib/shelly/client/auth.rb
shelly-0.4.35 lib/shelly/client/auth.rb
shelly-0.4.34 lib/shelly/client/auth.rb
shelly-0.4.33 lib/shelly/client/auth.rb
shelly-0.4.32 lib/shelly/client/auth.rb
shelly-0.4.31 lib/shelly/client/auth.rb