lib/cfoundry/v2/base.rb in cfoundry-0.3.61 vs lib/cfoundry/v2/base.rb in cfoundry-0.4.0
- old
+ new
@@ -1,15 +1,16 @@
require "multi_json"
-require "base64"
require "cfoundry/baseclient"
require "cfoundry/uaaclient"
require "cfoundry/errors"
module CFoundry::V2
class Base < CFoundry::BaseClient
+ include BaseClientMethods
+
attr_accessor :target, :token, :proxy, :trace, :backtrace, :log
def initialize(
target = "https://api.cloudfoundry.com",
token = nil)
@@ -36,45 +37,10 @@
# Cloud metadata
def info
get("info", :accept => :json)
end
-
- [ :app, :organization, :space, :user, :runtime, :framework, :service,
- :domain, :route, :service_plan, :service_binding, :service_instance,
- :service_auth_token
- ].each do |obj|
- plural = "#{obj}s"
-
- define_method(obj) do |guid, *args|
- depth, _ = args
- depth ||= 1
-
- params = { :"inline-relations-depth" => depth }
-
- get("v2", plural, guid, :accept => :json, :params => params)
- end
-
- define_method(:"create_#{obj}") do |payload|
- post(payload, "v2", plural, :content => :json, :accept => :json)
- end
-
- define_method(:"delete_#{obj}") do |guid|
- delete("v2", plural, guid)
- true
- end
-
- define_method(:"update_#{obj}") do |guid, payload|
- put(payload, "v2", plural, guid, :content => :json, :accept => :json)
- end
-
- define_method(plural) do |*args|
- all_pages(
- get("v2", plural, :accept => :json, :params => params_from(args)))
- end
- end
-
def resource_match(fingerprints)
put(fingerprints, "v2", "resource_match",
:content => :json, :accept => :json)
end
@@ -137,23 +103,10 @@
def stats(guid)
get("v2", "apps", guid, "stats", :accept => :json)
end
- def params_from(args)
- depth, query = args
- depth ||= 1
-
- params = { :"inline-relations-depth" => depth }
-
- if query
- params[:q] = "#{query.keys.first}:#{query.values.first}"
- end
-
- params
- end
-
def all_pages(paginated)
payload = paginated[:resources]
while next_page = paginated[:next_url]
paginated = request_path(Net::HTTP::Get, next_page, :accept => :json)
@@ -188,10 +141,10 @@
Net::HTTPNotImplemented, Net::HTTPServiceUnavailable
begin
info = parse_json(response.body)
return super unless info[:code]
- cls = CFoundry::APIError.v2_classes[info[:code]]
+ cls = CFoundry::APIError.error_classes[info[:code]]
raise (cls || CFoundry::APIError).new(info[:code], info[:description])
rescue MultiJson::DecodeError
super
end