Module: Vidibus::Service::Mongoid::ClassMethods

Defined in:
lib/vidibus/service/mongoid.rb

Instance Method Summary collapse

Instance Method Details

#connectorObject

Returns Connector service, if it has been configured, or raises an ConfigurationError.



85
86
87
88
# File 'lib/vidibus/service/mongoid.rb', line 85

def connector
  where(:function => "connector").and(:realm_uuid => nil).first or
    raise ConfigurationError.new("No Connector has been assigned to this service yet. Use your Connector to perform the assignment.")
end

#discover(wanted, realm = nil) ⇒ Object

Returns best service by function or UUID within given realm. If a service can be found in stored, it will be fetched from Connector.



92
93
94
# File 'lib/vidibus/service/mongoid.rb', line 92

def discover(wanted, realm = nil)
  local(wanted, realm) || remote(wanted, realm)
end

#local(wanted, realm = nil) ⇒ Object

Returns stored service by function or UUID within given realm.



97
98
99
100
# File 'lib/vidibus/service/mongoid.rb', line 97

def local(wanted, realm = nil)
  key = Vidibus::Uuid.validate(wanted) ? :uuid : :function
  where(key => wanted).and(:realm_uuid => realm).first
end

#remote(wanted, realm) ⇒ Object

Requests service from Connector and stores it. Wanted may be a function or an UUID. This method should not be called directly. Use #discover to avoid unneccessary lookups.



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/vidibus/service/mongoid.rb', line 105

def remote(wanted, realm)
  unless realm
    fail(ArgumentError, 'Please provide a valid realm to discover an appropriate service.')
  end
  response = connector.client.
    get("/services/#{wanted}", :query => {:realm => realm})
  if response
    secret = response["secret"]
    unless secret
      fail(ConnectorError, "The Connector did not return a secret for #{wanted}. Response was: #{response.parsed_response.inspect}")
    end
    secret = Vidibus::Secure.decrypt(secret, this.secret)
    attributes = response.only(%w[uuid function url]).
      merge(:realm_uuid => realm, :secret => secret)
    create!(attributes)
  else
    fail('no service found')
  end
end

#thisObject

Returns this service, if it has been configured, or raises an ConfigurationError.



79
80
81
82
# File 'lib/vidibus/service/mongoid.rb', line 79

def this
  where(:this => true).and(:realm_uuid => nil).first or
    raise ConfigurationError.new("This service has not been configured yet. Use your Connector to set it up.")
end