Class: Acfs::Service

Inherits:
Object
  • Object
show all
Includes:
Middleware
Defined in:
lib/acfs/service.rb,
lib/acfs/service/middleware.rb,
lib/acfs/service/middleware/stack.rb

Overview

User Service to define your services. That includes an identity used to identify the service in configuration files and middlewares the service uses.

Configure your service URLs in a YAML file loaded in an initializer using the identity as a key:

production:
  services:
    user_service_key: "http://users.service.org/base/path"

Examples:

class UserService < Acfs::Service
  identity :user_service_key

  use Acfs::Middleware::MessagePackDecoder
end

Defined Under Namespace

Modules: Middleware

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Middleware

#prepare

Constructor Details

#initialize(options = {}) ⇒ Service

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Service



29
30
31
# File 'lib/acfs/service.rb', line 29

def initialize(options = {})
  @options = options
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options



23
24
25
# File 'lib/acfs/service.rb', line 23

def options
  @options
end

Class Method Details

.base_urlString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (String)


82
83
84
85
86
87
88
# File 'lib/acfs/service.rb', line 82

def base_url
  unless (base = Acfs::Configuration.current.locate identity)
    raise ArgumentError.new "#{identity} not configured. Add `locate '#{identity.to_s.underscore}', 'http://service.url/'` to your configuration."
  end

  base.to_s
end

.identitySymbol .identity(identity) ⇒ Symbol

Overloads:

  • .identitySymbol

    Return configured identity key or derive key from class name.

    Returns:

    • (Symbol)

      Service identity key.

  • .identity(identity) ⇒ Symbol

    Set identity key.

    Parameters:

    • identity (#to_s)

      New identity key.

    Returns:

    • (Symbol)

      New set identity key.



74
75
76
77
# File 'lib/acfs/service.rb', line 74

def identity(identity = nil)
  @identity = identity.to_s.to_sym unless identity.nil?
  @identity ||= name.to_sym
end

Instance Method Details

#location(resource_class, opts = {}) ⇒ Location

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/acfs/service.rb', line 36

def location(resource_class, opts = {})
  opts.reverse_merge! options

  action = opts[:action] || :list

  path = if opts[:path].is_a?(Hash) && opts[:path].key?(action)
           opts[:path].fetch(action)
         else
           path = if opts[:path].is_a?(Hash)
                    opts[:path][:all].to_s
                  else
                    opts[:path].to_s
                  end

           path = (resource_class.name || 'class').pluralize.underscore if path.blank?

           resource_class.location_default_path(action, path.strip)
         end

  raise ArgumentError.new "Location for `#{action}' explicit disabled by set to nil." if path.nil?

  Location.new [self.class.base_url.to_s, path.to_s].join('/')
end