class Vidibus::Service::Client

Attributes

base_uri[RW]
service[RW]
this[RW]

Public Class Methods

new(service) click to toggle source

Initializes a new client for given service.

# File lib/vidibus/service/client.rb, line 15
def initialize(service)
  unless service && service.is_a?(::Service)
    raise(ServiceError, 'Service required')
  end
  unless service.url
    raise(ServiceError, 'URL of service required')
  end
  self.service = service
  self.this = ::Service.this
  self.base_uri = service.url
end

Public Instance Methods

delete(path, options = {}) click to toggle source

Sends a DELETE request to given path.

# File lib/vidibus/service/client.rb, line 43
def delete(path, options = {})
  request(:delete, path, options)
end
get(path, options = {}) click to toggle source

Sends a GET request to given path.

# File lib/vidibus/service/client.rb, line 28
def get(path, options = {})
  request(:get, path, options)
end
post(path, options = {}) click to toggle source

Sends a POST request to given path.

# File lib/vidibus/service/client.rb, line 33
def post(path, options = {})
  request(:post, path, options)
end
put(path, options = {}) click to toggle source

Sends a PUT request to given path.

# File lib/vidibus/service/client.rb, line 38
def put(path, options = {})
  request(:put, path, options)
end

Protected Instance Methods

build_uri(path) click to toggle source

Builds URI from base URI of service and given path.

# File lib/vidibus/service/client.rb, line 63
def build_uri(path)
  path = path.to_s
  unless path.match(/^\//)
    path = "/#{path}"
  end
  base_uri + path
end
request(verb, path, options) click to toggle source

Extends given query options and sends request with given verb.

# File lib/vidibus/service/client.rb, line 50
def request(verb, path, options)
  options_type = %w[post put].include?(verb.to_s) ? :body : :query
  options[options_type] = {:realm => service.realm_uuid, :service => this.uuid}.merge(options[options_type] || {})
  uri = build_uri(path)
  Vidibus::Secure.sign_request(verb, uri, options[options_type], secret)
  begin
    self.class.send(verb, uri, options)
  rescue StandardError, Exception => e
    raise(RequestError, e.message, e.backtrace)
  end
end
secret() click to toggle source

Returns secret to use depending on given service. If a Connector is about to be contacted, the secret of this service will be used, otherwise the secret of the contacted service.

# File lib/vidibus/service/client.rb, line 73
def secret
  (@service.connector? and @service.secret == nil) ? @this.secret : @service.secret
end