Class: CFoundry::BaseClient

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/cfoundry/baseclient.rb

Overview

:nodoc:

Direct Known Subclasses

Client, V1::Base, V2::Base

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target = "https://api.cloulu.com", token = nil) ⇒ BaseClient

Returns a new instance of BaseClient.



18
19
20
21
22
23
# File 'lib/cfoundry/baseclient.rb', line 18

def initialize(target = "https://api.cloulu.com", token = nil)
  @rest_client = CFoundry::RestClient.new(target, token)
  self.trace = false
  self.backtrace = false
  self.log = false
end

Instance Attribute Details

#rest_clientObject (readonly)

Returns the value of attribute rest_client.



12
13
14
# File 'lib/cfoundry/baseclient.rb', line 12

def rest_client
  @rest_client
end

Instance Method Details

#delete(*args) ⇒ Object



65
66
67
# File 'lib/cfoundry/baseclient.rb', line 65

def delete(*args)
  request("DELETE", *args)
end

#get(*args) ⇒ Object



61
62
63
# File 'lib/cfoundry/baseclient.rb', line 61

def get(*args)
  request("GET", *args)
end

#infoObject

Cloud metadata



57
58
59
# File 'lib/cfoundry/baseclient.rb', line 57

def info
  get("info", :accept => :json)
end

#password_score(password) ⇒ Object



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

def password_score(password)
  uaa ? uaa.password_score(password) : :unknown
end

#post(*args) ⇒ Object



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

def post(*args)
  request("POST", *args)
end

#put(*args) ⇒ Object



73
74
75
# File 'lib/cfoundry/baseclient.rb', line 73

def put(*args)
  request("PUT", *args)
end

#refresh_token!Object



92
93
94
# File 'lib/cfoundry/baseclient.rb', line 92

def refresh_token!
  self.token = uaa.try_to_refresh_token!
end

#request(method, *args) ⇒ Object



77
78
79
80
81
82
83
84
85
86
# File 'lib/cfoundry/baseclient.rb', line 77

def request(method, *args)
  if needs_token_refresh?
    token.auth_header = nil
    refresh_token!
  end

  path, options = normalize_arguments(args)
  request, response = request_raw(method, path, options)
  handle_response(response, options, request)
end

#request_raw(method, path, options) ⇒ Object



88
89
90
# File 'lib/cfoundry/baseclient.rb', line 88

def request_raw(method, path, options)
  @rest_client.request(method, path, options)
end

#stream_url(url, &blk) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/cfoundry/baseclient.rb', line 96

def stream_url(url, &blk)
  uri = URI.parse(url)

  Net::HTTP.start(uri.host, uri.port) do |http|
    http.read_timeout = 10

    req = Net::HTTP::Get.new(uri.request_uri)
    req["Authorization"] = token.auth_header if token

    http.request(req) do |response|
      case response
      when Net::HTTPOK
        response.read_body(&blk)
      when Net::HTTPNotFound
        raise CFoundry::NotFound.new(response.body, 404)
      when Net::HTTPForbidden
        raise CFoundry::Denied.new(response.body, 403)
      else
        raise CFoundry::BadResponse.new(response.body, response.code)
      end
    end
  end
end

#token=(token) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/cfoundry/baseclient.rb', line 42

def token=(token)
  if token.is_a?(String)
    token = CFoundry::AuthToken.new(token)
  end

  @rest_client.token = token
  @uaa.token = token if @uaa
end

#trace=(trace) ⇒ Object



51
52
53
54
# File 'lib/cfoundry/baseclient.rb', line 51

def trace=(trace)
  @rest_client.trace = trace
  @uaa.trace = trace if @uaa
end

#uaaObject



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/cfoundry/baseclient.rb', line 25

def uaa
  @uaa ||= begin
    if(endpoint = info[:authorization_endpoint])
      uaa = CFoundry::UAAClient.new(endpoint)
      uaa.trace = trace
      uaa.token = token
      uaa
    else
      nil
    end
  end
end