lib/vidibus/service/mongoid.rb in vidibus-service-0.1.3 vs lib/vidibus/service/mongoid.rb in vidibus-service-0.2.0
- old
+ new
@@ -10,11 +10,11 @@
included do
field :url
field :uuid
field :function
field :realm_uuid
- field :this, :type => Boolean
+ field :this, :type => ::Mongoid::Boolean
attr_accessor :nonce
attr_encrypted :secret
validates :url, :uri => {:protocol => [:http, :https], :accessible => false}
@@ -35,40 +35,25 @@
# Returns true if this service is a connector
def connector?
@is_connector ||= function == "connector"
end
- # Sends a GET request to given path.
- def get(path, options = {})
- client.get(path, options)
- end
-
- # Sends a POST request to given path.
- def post(path, options = {})
- client.post(path, options)
- end
-
- # Sends a PUT request to given path.
- def put(path, options = {})
- client.put(path, options)
- end
-
- # Sends a DELETE request to given path.
- def delete(path, options = {})
- client.delete(path, options)
- end
-
# Returns publicly requestable data.
def public_data
attributes.only(%w[uuid function url])
end
# Returns url without protocol.
def domain
url.gsub(/https?:\/\//, "") if url
end
+ # Returns a Client for current service.
+ def client
+ @client ||= Client.new(self)
+ end
+
# Returns true if given client_secret matches signature.
# This method is called from Vidibus' OauthServer when issuing an OAuth token.
# To prevent sending the service's secret over the network, the ConnectorClient
# sends a signature instead.
# TODO: Where to put this method? Wanted to extend this gem with vidius-user gem,
@@ -77,18 +62,15 @@
client_secret == Vidibus::Secure.sign(uuid, secret)
end
protected
- # Returns a Client for current service.
- def client
- @client ||= Client.new(self)
- end
-
# Sets an error if secret is given for Connector service.
def dont_allow_secret_for_connector
- errors.add(:secret, :secret_not_allowed_for_connector) if connector? and secret
+ if connector? && secret
+ errors.add(:secret, :secret_not_allowed_for_connector)
+ end
end
end
module ClassMethods
@@ -124,10 +106,10 @@
# This method should not be called directly. Use #discover to avoid unneccessary lookups.
def remote(wanted, realm)
unless realm
raise ArgumentError.new("Please provide a valid realm to discover an appropriate service.")
end
- if response = connector.get("/services/#{wanted}", :query => {:realm => realm})
+ if response = connector.client.get("/services/#{wanted}", :query => {:realm => realm})
secret = response["secret"] || raise(ConnectorError.new("The Connector did not return a secret for #{wanted}. Response was: #{response.parsed_response.inspect}"))
secret = Vidibus::Secure.decrypt(secret, this.secret)
attributes = response.only(%w[uuid function url]).merge(:realm_uuid => realm, :secret => secret)
create!(attributes)
else