Class: CFoundry::V2::Client

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
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, token = nil) ⇒ Client

Create a new Client for interfacing with the given target.

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



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

def initialize(target, token = nil)
  @base = Base.new(target, token)
end

Instance Attribute Details

#baseObject (readonly)

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



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

def base
  @base
end

#current_organizationObject

Organization

Currently targeted organization.



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

def current_organization
  @current_organization
end

#current_spaceObject

Space

Currently targeted space.



19
20
21
# File 'lib/cfoundry/v2/client.rb', line 19

def current_space
  @current_space
end

Instance Method Details

#current_userObject

The currently authenticated user.



36
37
38
39
40
41
42
43
44
45
# File 'lib/cfoundry/v2/client.rb', line 36

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

#logged_in?Boolean

Is an authentication token set on the client?

Returns:

  • (Boolean)


67
68
69
# File 'lib/cfoundry/v2/client.rb', line 67

def logged_in?
  !!@base.token
end

#login(credentials) ⇒ Object



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

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

#logoutObject

Clear client token. No requests are made for this.



62
63
64
# File 'lib/cfoundry/v2/client.rb', line 62

def logout
  @base.token = nil
end

#make_service_instance(json) ⇒ Object



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

def make_service_instance(json)
  klass = "CFoundry::V2::#{json[:entity][:type].camelize}".constantize
  klass.new(
    json[:metadata][:guid],
    self,
    json)
end

#query_target(klass) ⇒ Object



71
72
73
74
75
76
77
78
79
# File 'lib/cfoundry/v2/client.rb', line 71

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, options = {}) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/cfoundry/v2/client.rb', line 53

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

#service_instances(opts = {}) ⇒ Object



85
86
87
88
# File 'lib/cfoundry/v2/client.rb', line 85

def service_instances(opts={})
  opts[:user_provided] = true
  super(opts)
end

#service_instances_from(path, *args) ⇒ Object



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

def service_instances_from(path, *args)
  opts = args.first || {}
  opts[:user_provided] = true
  super(path, opts, *args)
end

#stream_url(url, &blk) ⇒ Object



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

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

#versionObject



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

def version
  2
end