require 'oauth' require 'rest-client' require 'csv' require 'uuidtools' require 'json' require 'active_support' require 'openssl' require File.dirname(__FILE__) + '/oehclient/helper' require File.dirname(__FILE__) + '/oehclient/exception' require File.dirname(__FILE__) + '/oehclient/config' require File.dirname(__FILE__) + '/oehclient/realtime' require File.dirname(__FILE__) + '/oehclient/data' require File.dirname(__FILE__) + '/oehclient/meta' module OEHClient class << self attr_accessor :spaces, :space_mgr # configure provides the basic interface for passing tenancy configuration to the gem def configure_space # if space_mgr attribute is NIL then return a new instance of the OEHClient::Config::SpaceManager # singleton class @space_mgr ||= OEHClient::Config::SpaceManager.instance # yield control to the block passed in the configuration yield(@space_mgr) end # wraps the GET method of the RestClient::Resource and manages the response that is returned def get(url, oauth_consumer=nil, *args) # retrieved the passed options from the SPLAT options = (args.length > 0 ? args[0] : Hash.new) # if the oauth_consumer is not a proper OAuth::Consumer, then raise the Oeh::Exception::InvalidTokenException # exception to the calling method raise OEHClient::Exception::InvalidConsumerException if (!oauth_consumer.nil? && !oauth_consumer.kind_of?(OAuth::Consumer)) # reset execution processes RestClient.reset_before_execution_procs # if this request has an oauth token, sign the RestClient object unless (oauth_consumer.nil?) # add the oauth signature RestClient.add_before_execution_proc { |req, params| oauth_consumer.sign! req } end # force the new hash for the header and params if they are NIL header = (options.has_key?(:header) ? options[:header] : OEHClient::Helper::Request.default_JSON_header()) # merge parameters into the header if they are passed header.merge!(:params => options[:params] ) if (options.has_key?(:params)) puts("URL: #{url} ; Header: #{header}") # send the GET request, manage the returned response, and return the body of the response to the # calling method RestClient::Request.execute(method: :get, url: url, headers: header, verify_ssl: OpenSSL::SSL::VERIFY_NONE) { | response, request, result | OEHClient::Helper::Response.handle(response)} end # post wraps the POST method of the access token and manages the response that is returned def post(url, oauth_consumer=nil, *args) # retrieved the passed options from the SPLAT options = (args.length > 0 ? args[0] : Hash.new) # if the oauth_consumer is not a proper OAuth::Consumer, then raise the Oeh::Exception::InvalidTokenException # exception to the calling method raise OEHClient::Exception::InvalidConsumerException if (!oauth_consumer.nil? && !oauth_consumer.kind_of?(OAuth::Consumer)) # reset execution processes RestClient.reset_before_execution_procs # if this request has an oauth token, sign the RestClient object unless (oauth_consumer.nil?) # add the oauth signature RestClient.add_before_execution_proc { |req, params| oauth_consumer.sign! req } end # force the new hash for the header and params if they are NIL header = (options.has_key?(:header) ? options[:header] : OEHClient::Helper::Request.default_JSON_header()) # merge parameters into the header if they are passed header.merge!(:params => options[:params] ) if (options.has_key?(:params)) # send the POST request, manage the returned response, and return the body of the response to the # calling method RestClient.post(url, options[:payload], header) { | response, request, result | OEHClient::Helper::Response.handle(response)} end # put wraps the PUT method of the access token and manages the response that is returned def put(url, oauth_consumer=nil, *args) # retrieved the passed options from the SPLAT options = (args.length > 0 ? args[0] : Hash.new) # if the oauth_consumer is not a proper OAuth::Consumer, then raise the Oeh::Exception::InvalidTokenException # exception to the calling method raise OEHClient::Exception::InvalidConsumerException if (!oauth_consumer.nil? && !oauth_consumer.kind_of?(OAuth::Consumer)) # reset execution processes RestClient.reset_before_execution_procs # if this request has an oauth token, sign the RestClient object unless (oauth_consumer.nil?) # add the oauth signature RestClient.add_before_execution_proc { |req, params| oauth_consumer.sign! req } end # force the new hash for the header and params if they are NIL header = (options.has_key?(:header) ? options[:header] : OEHClient::Helper::Request.default_JSON_header()) # merge parameters into the header if they are passed header.merge!(:params => options[:params] ) if (options.has_key?(:params)) # send the POST request, manage the returned response, and return the body of the response to the # calling method RestClient.put(url, options[:payload], header) { | response, request, result | OEHClient::Helper::Response.handle(response)} end end end