Class: CFoundry::V2::Client

Inherits:
Object
  • Object
show all
Includes:
LoginHelpers, 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

Instance Method Summary collapse

Methods included from LoginHelpers

#login_prompts

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.



23
24
25
# File 'lib/cfoundry/v2/client.rb', line 23

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.



11
12
13
# File 'lib/cfoundry/v2/client.rb', line 11

def base
  @base
end

#current_organizationObject

Organization

Currently targeted organization.



14
15
16
# File 'lib/cfoundry/v2/client.rb', line 14

def current_organization
  @current_organization
end

#current_spaceObject

Space

Currently targeted space.



17
18
19
# File 'lib/cfoundry/v2/client.rb', line 17

def current_space
  @current_space
end

Instance Method Details

#current_userObject

The currently authenticated user.



84
85
86
87
88
89
90
91
92
93
# File 'lib/cfoundry/v2/client.rb', line 84

def current_user
  return unless token

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

#infoObject

Cloud metadata



96
97
98
# File 'lib/cfoundry/v2/client.rb', line 96

def info
  @base.info
end

#logObject

The current log. See log=.



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

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.



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

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

#logged_in?Boolean

Is an authentication token set on the client?

Returns:

  • (Boolean)


120
121
122
# File 'lib/cfoundry/v2/client.rb', line 120

def logged_in?
  !!@base.token
end

#login(username, password) ⇒ Object



100
101
102
103
104
# File 'lib/cfoundry/v2/client.rb', line 100

def (username, password)
  @current_organization = nil
  @current_space = nil
  super
end

#logoutObject

Clear client token. No requests are made for this.



115
116
117
# File 'lib/cfoundry/v2/client.rb', line 115

def logout
  @base.token = nil
end

#proxyObject

Current proxy user. Usually nil.



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

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.



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

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

#query_target(klass) ⇒ Object



124
125
126
127
128
129
130
131
132
# File 'lib/cfoundry/v2/client.rb', line 124

def query_target(klass)
  if klass.scoped_space && space = current_space
    space
  elsif klass.scoped_organization && org = current_organization
    org
  else
    self
  end
end

#register(email, password) ⇒ Object



106
107
108
109
110
111
112
# File 'lib/cfoundry/v2/client.rb', line 106

def register(email, password)
  uaa_user = @base.uaa.add_user(email, password)
  cc_user = user
  cc_user.guid = uaa_user['id']
  cc_user.create!
  cc_user
end

#stream_url(url, &blk) ⇒ Object



134
135
136
# File 'lib/cfoundry/v2/client.rb', line 134

def stream_url(url, &blk)
  @base.stream_url(url, &blk)
end

#targetObject

The current target URL of the client.



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

def target
  @base.target
end

#tokenObject

Current authentication token.



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

def token
  @base.token
end

#token=(token) ⇒ Object

Set the authentication token.



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

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

#traceObject

Is the client tracing API requests?



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

def trace
  @base.trace
end

#trace=(bool) ⇒ Object

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



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

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

#versionObject



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

def version
  2
end