require 'erb' require 'rest-client' class OEHClient::Config::Space ### ### ------------- Constants ### ### ### ------------- Attributes ### attr_accessor :site_key, # The ONE Site KEY Value :host, # The name of the HOST machine on thunderhead.com (ONEDEMO, EU2, NA4, etc..) :api_key, # The API key provided in the ONE SETTINGS interface :shared_secret, # The shared secret value provided in the ONE SETTINGS interface :username, # The fully-qualified username configured for access to the space :meta_password, # The password needed to access meta-data from the OEH server :cookies # The cookies returned from an OEH API request ### ### ------------- Class Methods ### # create builds a new instance of the Site class, populats the attributes using the properties HASH provided # and returns an instance of the class. def self.create(properties={}) # create a new instance of the OHEClient::Space class space_instance = OEHClient::Config::Space.new() # assign all attributes from the passed properties hash space_instance.site_key = properties[:site_key] if (properties.has_key?(:site_key)) space_instance.host = properties[:host] if (properties.has_key?(:host)) space_instance.api_key = properties[:api_key] if (properties.has_key?(:api_key)) space_instance.shared_secret = properties[:shared_secret] if (properties.has_key?(:shared_secret)) space_instance.username = properties[:username] if (properties.has_key?(:username)) space_instance.meta_password = properties[:meta_password] if (properties.has_key?(:meta_password)) # return the instance of the OEHClient::Site Class space_instance end ### ### ------------- Instance Methods ### # token is the method used to generate a new OAuth::AccessToken object based on the configuration of # the related space def token() # raise the OEHClient::InvalidSpaceException the current instance is not valid raise OEHClient::Exception::InvalidSpaceConfigException unless is_valid? # Create the consumer and access token oauth_consumer = OAuth::Consumer.new("#{@api_key}!#{@username}", @shared_secret, {:site => "#{OEHClient::Helper::Request::ONE_PROTOCOL}#{@host}#{OEHClient::Helper::Request::THUNDERHEAD_DOMAIN}", :scheme => :header}) # return the access token return(OAuth::AccessToken.new(oauth_consumer)) end # return a new instance of the OAuth::Consumer using the details of the space def oauth_consumer() OAuth::Consumer.new("#{@api_key}!#{@username}", @shared_secret, {:site => "#{OEHClient::Helper::Request::ONE_PROTOCOL}#{@host}#{OEHClient::Helper::Request::THUNDERHEAD_DOMAIN}", :scheme => :header}) end # is_valid determines if the current class has values for all the attributes. Each attribute is # required with a non-nul / non-blank value to be considered valid def is_valid?() ((!@site_key.nil? && !@site_key.empty?) && (!@host.nil? && !@host.empty?) && (!@api_key.nil? && !@api_key.empty?) && (!@shared_secret.nil? && !@shared_secret.empty?) && (!@username.nil? && !@username.empty?)) end # determines if the space configuration appears to be valid for access to the raw meta-data entities. # The method expects the standard valid attributes from is_valid? as well as the meta_password # attribute def meta_access?() self.is_valid? && (!@meta_password.blank?) end # return the hash that def meta_credentials() {:username => @username, :password => @meta_password, :rememberMe => "false"} end # def meta_credentials # return the URL for the posting a login request def login_url() "#{OEHClient::Helper::Request::ONE_PROTOCOL}#{@host}#{OEHClient::Helper::Request::THUNDERHEAD_DOMAIN}/one/idm_login" end # def login_url # return the URL for posting a logout request def logout_url() "#{OEHClient::Helper::Request::ONE_PROTOCOL}#{@host}#{OEHClient::Helper::Request::THUNDERHEAD_DOMAIN}/one/logout" end # logon to the OEH thinstance with the existing credentials def login() # post the login request response = OEHClient.post(login_url, nil, :payload => meta_credentials) # store the cookies if they are returned in the response @cookies = response[:cookies] if (response.has_key?(:cookies)) end # logout of the OEH thinstance and remove the current cookie references def logout() # construct a header object, merging cookies (if present) wit the default JSON header header = Hash.new header.merge!(:cookies => @cookies) unless (@cookies.blank?) header.merge!(OEHClient::Helper::Request.default_JSON_header) # post the logout request OEHClient.post(logout_url, nil, :header => header) # remove the cookies if the logout is posted successfully @cookies = nil end # retrieve the workspace meta-data object from the thinstance in realtime def workspace() # if the cookies are currently NIL then login using the existing credentials login() if (@cookies.nil?) # get the workspace object using the site key, the host value, and the exisitng cookies workspace_instance = OEHClient::Meta::Workspace.fetch(@site_key, @host, :cookies => @cookies) # assign the current space to the workspace instance workspace_instance.space = self # return the workspace instance object workspace_instance end end