lib/nightcrawler_swift/connection.rb in nightcrawler_swift-0.8.0 vs lib/nightcrawler_swift/connection.rb in nightcrawler_swift-0.8.1
- old
+ new
@@ -1,10 +1,27 @@
module NightcrawlerSwift
class Connection
- attr_accessor :auth_response
- attr_reader :token_id, :expires_at, :catalog, :admin_url, :upload_url, :public_url, :internal_url
+ def self.connected_attr_reader(*args)
+ args.each do |arg|
+ define_method(arg.to_sym) do
+ connect! unless connected?
+ instance_variable_get("@#{arg}")
+ end
+ end
+ end
+ private_class_method :connected_attr_reader
+
+ attr_writer :auth_response
+ attr_reader :token_id, :expires_at
+ connected_attr_reader :catalog, :admin_url, :upload_url, :public_url, :internal_url
+
+ def auth_response
+ authenticate! if @auth_response.nil?
+ @auth_response
+ end
+
def connect!
authenticate!
configure
NightcrawlerSwift.logger.debug "[NightcrawlerSwift] Connected, token_id: #{token_id}"
@@ -56,11 +73,11 @@
catalogs = auth_response.access["serviceCatalog"]
@catalog = catalogs.find {|catalog| catalog["type"] == "object-store"}
end
def select_endpoints
- raise Exceptions::ConfigurationError.new "No catalog of type 'object-store' found" if catalog.nil?
- @endpoints = catalog["endpoints"].first
+ raise Exceptions::ConfigurationError.new "No catalog of type 'object-store' found" if @catalog.nil?
+ @endpoints = @catalog["endpoints"].first
end
def configure_urls
@admin_url = opts.admin_url || @endpoints["adminURL"]
@public_url = opts.public_url || @endpoints["publicURL"]