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.



31
32
33
# File 'lib/acfs/service.rb', line 31

def initialize(**options)
  @options = options
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



25
26
27
# File 'lib/acfs/service.rb', line 25

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
89
90
91
# 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, path: nil, action: :list) ⇒ 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:



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 38

def location(resource_class, path: nil, action: :list, **)
  path ||= options[:path]

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

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

    path = resource_class.location_default_path(action, path.strip)
  end

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

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