require "forwardable" module JFoundry::V2 # 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. class Client include ClientMethods extend Forwardable # Internal BaseClient instance. Normally won't be touching this. attr_reader :base # [Organization] Currently targeted organization. attr_accessor :current_organization # [Space] Currently targeted space. attr_accessor :current_space def_delegators :@base, :target, :target=, :access_key, :access_key=, :secret_key, :secret_key=, :version, :version=, :from, :from=, :http_proxy, :http_proxy=, :https_proxy, :https_proxy=, :trace, :trace=, :log, :log=, :info # Create a new Client for interfacing with the given target. # # A token may also be provided to skip the login step. def initialize(target, access_key, secret_key, version, from = "jdc") @base = Base.new(target, access_key, secret_key, version, from) end def version 2 end # The currently authenticated user. def current_user user = user(@base.access_key) user_info = @base.user_info if user_info && user_info.key?(:user) user.emails = [{ :value => user_info[:user]}] end user end def query_target(klass) if klass.scoped_space && space = current_space space elsif klass.scoped_organization && org = current_organization org else self end end def stream_url(url, &blk) @base.stream_url(url, &blk) end def service_instances(opts={}) opts[:user_provided] = true super(opts) end def service_instances_from(path, *args) opts = args.first || {} opts[:user_provided] = true super(path, opts, *args) end def make_service_instance(json) klass = "JFoundry::V2::#{json[:entity][:type].camelize}".constantize klass.new( json[:metadata][:guid], self, json) end end end