fluke/devops-service/db/mongo/connectors/provider_account.rb

38 lines
1023 B
Ruby
Raw Normal View History

2015-10-22 12:39:22 +03:00
module Connectors
class ProviderAccount < Base
2015-10-22 12:39:22 +03:00
include Helpers::InsertCommand,
Helpers::DeleteCommand
def initialize(db)
super(db)
end
def provider_accounts provider
c = Provider::ProviderFactory.get_account_class(provider)
collection.find({provider: provider}).to_a.map{|bson| c.build_from_bson(bson)}
end
2015-11-17 12:14:02 +03:00
def provider_account provider, account
c = Provider::ProviderFactory.get_account_class(provider)
bson = collection.find({provider: provider, _id: account}).to_a.first
2015-12-15 15:49:23 +03:00
raise RecordNotFound.new("Account '#{account}' for provider '#{provider}' not found") unless bson
2015-11-17 12:14:02 +03:00
c.build_from_bson(bson)
end
2015-10-22 12:39:22 +03:00
def collection_name
'provider_accounts'
end
private
def model_from_bson(bson)
c = Provider::ProviderFactory.get_account_class(bson["provider"])
raise InvalidRecord.new "Invalid provider '#{bson["provider"]}' for account '#{bson["_id"]}'" if c.nil?
c.build_from_bson(bson)
end
end
end