Class: CloudFoundry::Environment

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudfoundry/environment.rb

Class Method Summary collapse

Class Method Details

.app_infoObject



23
24
25
26
# File 'lib/cloudfoundry/environment.rb', line 23

def app_info
  raw_vcap_app = ENV["VCAP_APPLICATION"]
  Hashie::Mash.new(JSON.parse(raw_vcap_app)) if raw_vcap_app
end

.first_urlObject



28
29
30
# File 'lib/cloudfoundry/environment.rb', line 28

def first_url
  raw_uris.first if raw_uris
end

.hostObject



14
15
16
# File 'lib/cloudfoundry/environment.rb', line 14

def host
  ENV["VCAP_APP_HOST"]
end

.instance_indexObject



18
19
20
# File 'lib/cloudfoundry/environment.rb', line 18

def instance_index
  return app_info.instance_index if app_info
end

.is_prod_app?(base = "www") ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
80
# File 'lib/cloudfoundry/environment.rb', line 77

def is_prod_app?(base = "www")
  staging_regex = /^#{base}[0-9]?([^\.]?)(\..*)$/
  !raw_uris.find{|uri| uri =~ staging_regex}.nil? if raw_uris
end

.method_missing(id, *args) ⇒ Object

Raises:

  • (NoMethodError)


41
42
43
44
45
46
47
48
49
# File 'lib/cloudfoundry/environment.rb', line 41

def method_missing(id, *args)
  index = args[0] || 0
  if id =~ /([^_]+)_cnx$/
    return service_cnx(Regexp.last_match(1), index)
  elsif id =~ /([^_]+)_info$/
    return service_info(Regexp.last_match(1), index)
  end
  raise NoMethodError
end

.portObject



9
10
11
12
# File 'lib/cloudfoundry/environment.rb', line 9

def port
  raw_port = ENV["VCAP_APP_PORT"]
  raw_port.to_i if raw_port
end

.raw_app_versionObject



73
74
75
# File 'lib/cloudfoundry/environment.rb', line 73

def raw_app_version
  app_info.version if app_info
end

.raw_urisObject



69
70
71
# File 'lib/cloudfoundry/environment.rb', line 69

def raw_uris
  app_info.uris if app_info
end

.running_local?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/cloudfoundry/environment.rb', line 32

def running_local?
  app_info.nil?
end

.service_cnx(service_regexp, index = 0) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/cloudfoundry/environment.rb', line 51

def service_cnx(service_regexp, index = 0)
  if services and services.count > 0
    svc_key = services.keys.find {|svc| svc =~ /#{service_regexp}/i }
    if svc_key and index < services[svc_key].length
      return Hashie::Mash.new(services[svc_key][index][:credentials])
    end
  end
end

.service_info(service_regexp, index = 0) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/cloudfoundry/environment.rb', line 60

def service_info(service_regexp, index = 0)
  if services and services.count > 0
    svc_key = services.keys.find {|svc| svc =~ /#{service_regexp}/i }
    if svc_key and index < services[svc_key].length
      return Hashie::Mash.new(services[svc_key][index])
    end
  end
end

.servicesObject



36
37
38
39
# File 'lib/cloudfoundry/environment.rb', line 36

def services
  svcs_raw = ENV["VCAP_SERVICES"]
  Hashie::Mash.new(JSON.parse(svcs_raw)) if svcs_raw
end