Sha256: 46e060c26169802d6ae815cb757dc6353df30bb7d57e12c31ab79a6c33f465e0

Contents?: true

Size: 1.94 KB

Versions: 71

Compression:

Stored size: 1.94 KB

Contents

require 'acfs/service/middleware'

module Acfs

  # User {Acfs::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"
  #
  # @example
  #   class UserService < Acfs::Service
  #     identity :user_service_key
  #
  #     use Acfs::Middleware::MessagePackDecoder
  #   end
  #
  class Service
    attr_accessor :options

    include Service::Middleware

    # @api private
    #
    def initialize(options = {})
      @options = options
    end

    # @api private
    # @return [String]
    #
    def url_for(resource_class, options = {})
      options.reverse_merge! self.options

      url  = self.class.base_url.to_s
      url += "/#{(options[:path] || resource_class.name.pluralize.underscore).to_s}"
      url += "/#{options[:suffix].to_s}" if options[:suffix]
      url
    end

    class << self

      # @api public
      #
      # @overload identity()
      #   Return configured identity key or derive key from class name.
      #
      #   @return [Symbol] Service identity key.
      #
      # @overload identity(identity)
      #   Set identity key.
      #
      #   @param [#to_s] identity New identity key.
      #   @return [Symbol] New set identity key.
      #
      def identity(identity = nil)
        @identity = identity.to_s.to_sym unless identity.nil?
        @identity ||= name.to_sym
      end

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

        base.to_s
      end
    end
  end
end

Version data entries

71 entries across 71 versions & 1 rubygems

Version Path
acfs-0.25.0.1.b227 lib/acfs/service.rb
acfs-0.25.0.1.b226 lib/acfs/service.rb
acfs-0.25.0.1.b225 lib/acfs/service.rb
acfs-0.25.0.1.b223 lib/acfs/service.rb
acfs-0.25.0.1.b221 lib/acfs/service.rb
acfs-0.25.0.1.b217 lib/acfs/service.rb
acfs-0.25.0 lib/acfs/service.rb
acfs-0.24.0.1.b216 lib/acfs/service.rb
acfs-0.24.0.1.b214 lib/acfs/service.rb
acfs-0.24.0.1.b212 lib/acfs/service.rb
acfs-0.24.0.b210 lib/acfs/service.rb
acfs-0.24.0 lib/acfs/service.rb
acfs-0.23.2.b209 lib/acfs/service.rb
acfs-0.23.2.b208 lib/acfs/service.rb
acfs-0.23.2.b206 lib/acfs/service.rb
acfs-0.23.2.b205 lib/acfs/service.rb
acfs-0.23.2.b204 lib/acfs/service.rb
acfs-0.23.2.b203 lib/acfs/service.rb
acfs-0.23.2.b200 lib/acfs/service.rb
acfs-0.23.2.b199 lib/acfs/service.rb