Class: CFoundry::V2::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/cfoundry/v2/client.rb

Overview

The primary API entrypoint. Wraps a BaseClient to provide nicer return values. Initialize with the target and, optionally, an auth token. These are the only two internal states.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target = "http://api.cloudfoundry.com", token = nil) ⇒ Client

Create a new Client for interfacing with the given target.

A token may also be provided to skip the login step.



38
39
40
# File 'lib/cfoundry/v2/client.rb', line 38

def initialize(target = "http://api.cloudfoundry.com", token = nil)
  @base = Base.new(target, token)
end

Instance Attribute Details

#baseObject (readonly)

Internal BaseClient instance. Normally won’t be touching this.



26
27
28
# File 'lib/cfoundry/v2/client.rb', line 26

def base
  @base
end

#current_organizationObject

Organization

Currently targeted organization.



29
30
31
# File 'lib/cfoundry/v2/client.rb', line 29

def current_organization
  @current_organization
end

#current_spaceObject

Space

Currently targeted space.



32
33
34
# File 'lib/cfoundry/v2/client.rb', line 32

def current_space
  @current_space
end

Instance Method Details

#current_userObject

The currently authenticated user.



95
96
97
98
99
100
101
# File 'lib/cfoundry/v2/client.rb', line 95

def current_user
  if guid = @base.token_data[:user_id]
    user = user(guid)
    user.emails = [{ :value => @base.token_data[:email] }]
    user
  end
end

#infoObject

Cloud metadata



104
105
106
# File 'lib/cfoundry/v2/client.rb', line 104

def info
  @base.info
end

#logObject

The current log. See log=.



80
81
82
# File 'lib/cfoundry/v2/client.rb', line 80

def log
  @base.log
end

#log=(mode) ⇒ Object

Set the logging mode. Mode can be one of:

String

Name of a file to log the last 10 requests to.

Array

Array to append with log data (a Hash).

IO

An IO object to write to.

false

No logging.



90
91
92
# File 'lib/cfoundry/v2/client.rb', line 90

def log=(mode)
  @base.log = mode
end

#logged_in?Boolean

Is an authentication token set on the client?

Returns:

  • (Boolean)


146
147
148
# File 'lib/cfoundry/v2/client.rb', line 146

def logged_in?
  !!@base.token
end

#login(credentials) ⇒ Object

Authenticate with the target. Sets the client token.

Credentials is a hash, typically containing :username and :password keys.

The values in the hash should mirror the prompts given by ‘login_prompts`.



126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/cfoundry/v2/client.rb', line 126

def (credentials)
  @current_organization = nil
  @current_space = nil

  @base.token =
    if @base.uaa
      @base.uaa.authorize(credentials)
    else
      @base.create_token(
        { :password => credentials[:password] },
        credentials[:username])[:token]
    end
end

#login_promptsObject

Login prompts



109
110
111
112
113
114
115
116
117
# File 'lib/cfoundry/v2/client.rb', line 109

def 
  if @base.uaa
    @base.uaa.prompts
  else
    { :username => ["text", "Email"],
      :password => ["password", "Password"]
    }
  end
end

#logoutObject

Clear client token. No requests are made for this.



141
142
143
# File 'lib/cfoundry/v2/client.rb', line 141

def logout
  @base.token = nil
end

#proxyObject

Current proxy user. Usually nil.



58
59
60
# File 'lib/cfoundry/v2/client.rb', line 58

def proxy
  @base.proxy
end

#proxy=(email) ⇒ Object

Set the proxy user for the client. Must be authorized as an administrator for this to have any effect.



64
65
66
# File 'lib/cfoundry/v2/client.rb', line 64

def proxy=(email)
  @base.proxy = email
end

#targetObject

The current target URL of the client.



43
44
45
# File 'lib/cfoundry/v2/client.rb', line 43

def target
  @base.target
end

#tokenObject

Current authentication token.



48
49
50
# File 'lib/cfoundry/v2/client.rb', line 48

def token
  @base.token
end

#token=(token) ⇒ Object

Set the authentication token.



53
54
55
# File 'lib/cfoundry/v2/client.rb', line 53

def token=(token)
  @base.token = token
end

#traceObject

Is the client tracing API requests?



69
70
71
# File 'lib/cfoundry/v2/client.rb', line 69

def trace
  @base.trace
end

#trace=(bool) ⇒ Object

Set the tracing flag; if true, API requests and responses will be printed out.



75
76
77
# File 'lib/cfoundry/v2/client.rb', line 75

def trace=(bool)
  @base.trace = bool
end