Class: CFoundry::V2::Client
- Inherits:
-
Object
- Object
- CFoundry::V2::Client
- Includes:
- ClientMethods
- 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
-
#base ⇒ Object
readonly
Internal BaseClient instance.
-
#current_organization ⇒ Object
- Organization
-
Currently targeted organization.
-
#current_space ⇒ Object
- Space
-
Currently targeted space.
Instance Method Summary collapse
-
#current_user ⇒ Object
The currently authenticated user.
-
#info ⇒ Object
Cloud metadata.
-
#initialize(target = "http://api.cloudfoundry.com", token = nil) ⇒ Client
constructor
Create a new Client for interfacing with the given target.
-
#log ⇒ Object
The current log.
-
#log=(mode) ⇒ Object
Set the logging mode.
-
#logged_in? ⇒ Boolean
Is an authentication token set on the client?.
-
#login(credentials) ⇒ Object
Authenticate with the target.
-
#login_prompts ⇒ Object
Login prompts.
-
#logout ⇒ Object
Clear client token.
-
#proxy ⇒ Object
Current proxy user.
-
#proxy=(email) ⇒ Object
Set the proxy user for the client.
- #query_target(klass) ⇒ Object
-
#target ⇒ Object
The current target URL of the client.
-
#token ⇒ Object
Current authentication token.
-
#token=(token) ⇒ Object
Set the authentication token.
-
#trace ⇒ Object
Is the client tracing API requests?.
-
#trace=(bool) ⇒ Object
Set the tracing flag; if true, API requests and responses will be printed out.
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.
21 22 23 |
# File 'lib/cfoundry/v2/client.rb', line 21 def initialize(target = "http://api.cloudfoundry.com", token = nil) @base = Base.new(target, token) end |
Instance Attribute Details
#base ⇒ Object (readonly)
Internal BaseClient instance. Normally won’t be touching this.
9 10 11 |
# File 'lib/cfoundry/v2/client.rb', line 9 def base @base end |
#current_organization ⇒ Object
- Organization
-
Currently targeted organization.
12 13 14 |
# File 'lib/cfoundry/v2/client.rb', line 12 def current_organization @current_organization end |
#current_space ⇒ Object
- Space
-
Currently targeted space.
15 16 17 |
# File 'lib/cfoundry/v2/client.rb', line 15 def current_space @current_space end |
Instance Method Details
#current_user ⇒ Object
The currently authenticated user.
78 79 80 81 82 83 84 |
# File 'lib/cfoundry/v2/client.rb', line 78 def current_user if guid = @base.token_data[:user_id] user = user(guid) user.emails = [{ :value => @base.token_data[:email] }] user end end |
#info ⇒ Object
Cloud metadata
87 88 89 |
# File 'lib/cfoundry/v2/client.rb', line 87 def info @base.info end |
#log ⇒ Object
The current log. See log=
.
63 64 65 |
# File 'lib/cfoundry/v2/client.rb', line 63 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.
73 74 75 |
# File 'lib/cfoundry/v2/client.rb', line 73 def log=(mode) @base.log = mode end |
#logged_in? ⇒ Boolean
Is an authentication token set on the client?
129 130 131 |
# File 'lib/cfoundry/v2/client.rb', line 129 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`.
109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/cfoundry/v2/client.rb', line 109 def login(credentials) @current_organization = nil @current_space = nil @base.token = if @base.uaa @base.uaa.(credentials) else @base.create_token( { :password => credentials[:password] }, credentials[:username])[:token] end end |
#login_prompts ⇒ Object
Login prompts
92 93 94 95 96 97 98 99 100 |
# File 'lib/cfoundry/v2/client.rb', line 92 def login_prompts if @base.uaa @base.uaa.prompts else { :username => ["text", "Email"], :password => ["password", "Password"] } end end |
#logout ⇒ Object
Clear client token. No requests are made for this.
124 125 126 |
# File 'lib/cfoundry/v2/client.rb', line 124 def logout @base.token = nil end |
#proxy ⇒ Object
Current proxy user. Usually nil.
41 42 43 |
# File 'lib/cfoundry/v2/client.rb', line 41 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.
47 48 49 |
# File 'lib/cfoundry/v2/client.rb', line 47 def proxy=(email) @base.proxy = email end |
#query_target(klass) ⇒ Object
133 134 135 136 137 138 139 140 141 |
# File 'lib/cfoundry/v2/client.rb', line 133 def query_target(klass) if klass.scoped_space && space = current_space space elsif klass.scoped_organization && org = current_org org else self end end |
#target ⇒ Object
The current target URL of the client.
26 27 28 |
# File 'lib/cfoundry/v2/client.rb', line 26 def target @base.target end |
#token ⇒ Object
Current authentication token.
31 32 33 |
# File 'lib/cfoundry/v2/client.rb', line 31 def token @base.token end |
#token=(token) ⇒ Object
Set the authentication token.
36 37 38 |
# File 'lib/cfoundry/v2/client.rb', line 36 def token=(token) @base.token = token end |
#trace ⇒ Object
Is the client tracing API requests?
52 53 54 |
# File 'lib/cfoundry/v2/client.rb', line 52 def trace @base.trace end |
#trace=(bool) ⇒ Object
Set the tracing flag; if true, API requests and responses will be printed out.
58 59 60 |
# File 'lib/cfoundry/v2/client.rb', line 58 def trace=(bool) @base.trace = bool end |