module Ballast::Concerns::Common

A concern to handle common tasks in an application.

Public Instance Methods

authenticate_user(area: nil, title: nil, message: nil, &authenticator) click to toggle source

Authenticates a user via HTTP, handling the error if the authentication failed.

@param area [String|NilClass] The name of the area. @param title [String|NilClass] A title for authentication errors. @param message [String|NilClass] A message for authentication errors. @param authenticator [Proc] A block to verify if authentication is valid.

# File lib/ballast/concerns/common.rb, line 87
def authenticate_user(area: nil, title: nil, message: nil, &authenticator)
  return if authenticate_with_http_basic(&authenticator)

  area ||= "Private Area"
  title ||= "Authentication required."
  message ||= "To view this resource you have to authenticate."

  headers["WWW-Authenticate"] = "Basic realm=\"#{area}\""
  handle_error({status: 401, title: title, message: message})
end
format_long_date(date, separator: "•", format: "%I:%M%p %- %b %o, %Y (%:Z)") click to toggle source

Formats a long date.

@param date [DateTime] The date to format. @param separator [String] The separator between date and time. @param format [String] The format of the date, like in strftime. Use `%-` for the separator, `%o` for the ordinalized version of the day of the month

and `%:Z` for the zone name considering also DST.
# File lib/ballast/concerns/common.rb, line 75
def format_long_date(date, separator: "•", format: "%I:%M%p %- %b %o, %Y (%:Z)")
  tz = Time.zone
  replacements = {"%-" => separator, "%o" => date.day.ordinalize, "%:Z" => tz.current_name(tz.uses_dst? && date.dst?)}
  date.strftime(format).gsub(/%(-|o|(:Z))/) { |r| replacements.fetch(r, r) }
end
format_short_amount(amount, suffix = "") click to toggle source

Formats a short amount of time (less than one hour).

@param amount [Fixnum] The amount to format. @param suffix [String] The suffix to add to the formatted amount. @return [String] The formatted amount.

# File lib/ballast/concerns/common.rb, line 59
def format_short_amount(amount, suffix = "")
  if amount < 1.minute
    "#{amount.floor}s#{suffix}"
  elsif amount < 1.hour
    "#{(amount / 60).floor}m#{suffix}"
  else
    "#{(amount / 3600).floor}h#{suffix}"
  end
end
format_short_duration(date, reference: nil, suffix: "") click to toggle source

Formats a relative date using abbreviation or short formats.

@param date [DateTime] The date to format. @param reference [DateTime|NilClass] The reference date. @param suffix [String] The suffix to add to the formatted date. @return [String] The formatted date.

# File lib/ballast/concerns/common.rb, line 40
def format_short_duration(date, reference: nil, suffix: "")
  amount = (reference || Time.now).to_i - date.to_i

  if amount <= 0
    "now"
  elsif amount < 1.day
    format_short_amount(amount, suffix)
  elsif amount < 1.year
    date.strftime("%b %d")
  else
    date.strftime("%b %d %Y")
  end
end
json?() click to toggle source

Checks if the current request wants JSON or JSONP as response.

@return [Boolean] `true` if the request is JSON(P), `false` otherwise.

# File lib/ballast/concerns/common.rb, line 23
def json?
  [:json, :jsonp].include?(request.format.to_sym) || params[:json].to_boolean
end
perform_service(klass, operation = :perform, **kwargs) click to toggle source

Executes a service.

@param klass [Service] The service to execute. @param operation [String] The operation to invoke. @param kwargs [Hash] Parameters passed to the service. @return [Service::Response] The result of the invocation.

# File lib/ballast/concerns/common.rb, line 16
def perform_service(klass, operation = :perform, **kwargs)
  @result = klass.new(self).call(operation, params: params, **kwargs)
end
request_data?() click to toggle source

Checks if the user is sending any data.

@return [Boolean] `true` if the user is sending data, `false` otherwise.

# File lib/ballast/concerns/common.rb, line 30
def request_data?
  request.post? || request.put? || request.patch?
end