module Acfs::Resource::Locatable::ClassMethods

Public Instance Methods

location(**opts) click to toggle source

Return a location object able to build the URL for this resource and given action.

@example

class Identity < ::Acfs::Resource
  service MyService, path: 'users/:user_id/identities'
end

location = Identity.location(action: :read)
location.arguments
=> [:user_id, :id]

location.raw_url
=> 'http://service/users/:user_id/identities/:id'

location = Identity.location(action: :list)
location.arguments
=> [:user_id]

location.build(user_id: 42)
=> 'http://service/users/42/identities'

@param opts [Hash] Options. @option opts [Symbol] :action Operation action,

usually `:list`, `:create`, `:read`, `:update` or`:delete`.

@return [Location] Location object.

# File lib/acfs/resource/locatable.rb, line 87
def location(**opts)
  service.location(self, **opts)
end
location_default_path(action, path) click to toggle source

@api private

# File lib/acfs/resource/locatable.rb, line 92
def location_default_path(action, path)
  case action
    when :list, :create
      path
    when :read, :update, :delete
      "#{path}/:id"
  end
end
url(suffix = nil, **opts) click to toggle source

@overload url(suffix)

@deprecated
Return URL for this class of resource. Given suffix
will be appended.

@example
  User.url    # => "http://users.srv.org/users"
  User.url(5) # => "http://users.srv.org/users/5"

@param suffix [String] Suffix to append to URL.
@return [String] Generated URL.

@overload url(opts = {})

Return URL for this class of resources. Given options
will be used to replace URL path arguments and to
determine the operation action.

@example
  User.url(id: 5, action: :read) # => "http://users.srv.org/users/5"
  User.url(action: :list) # => "http://users.srv.org/users"

@param opts [Hash] Options.
@option opts [Symbol] :action Operation action,
  usually `:list`, `:create`, `:read`, `:update` or`:delete`.
@return [String] Generated URL.
# File lib/acfs/resource/locatable.rb, line 43
def url(suffix = nil, **opts)
  if suffix.is_a? Hash
    opts = suffix
    suffix = nil
  end

  kwargs = {}
  kwargs[:path] = opts[:path] if opts.key?(:path)
  kwargs[:action] = opts.delete(:action) if opts.key?(:action)
  kwargs[:action] = :list if suffix

  url  = location(**kwargs).build(opts.stringify_keys).str
  url += "/#{suffix}" if suffix.to_s.present?
  url
end