Module: Ballast::Concerns::Common

Defined in:
lib/ballast/concerns/common.rb

Overview

A concern to handle common tasks in an application.

Instance Method Summary collapse

Instance Method Details

#authenticate_user(area: nil, title: nil, message: nil, &authenticator) ⇒ Object

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

Parameters:

  • area (String|NilClass) (defaults to: nil)

    The name of the area.

  • title (String|NilClass) (defaults to: nil)

    A title for authentication errors.

  • message (String|NilClass) (defaults to: nil)

    A message for authentication errors.

  • authenticator (Proc)

    A block to verify if authentication is valid.



87
88
89
90
91
92
93
94
95
96
# 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)") ⇒ Object

Formats a long date.

Parameters:

  • date (DateTime)

    The date to format.

  • separator (String) (defaults to: "•")

    The separator between date and time.

  • format (String) (defaults to: "%I:%M%p %- %b %o, %Y (%:Z)")

    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.



75
76
77
78
79
# 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 = "") ⇒ String

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

Parameters:

  • amount (Fixnum)

    The amount to format.

  • suffix (String) (defaults to: "")

    The suffix to add to the formatted amount.

Returns:

  • (String)

    The formatted amount.



59
60
61
62
63
64
65
66
67
# 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: "") ⇒ String

Formats a relative date using abbreviation or short formats.

Parameters:

  • date (DateTime)

    The date to format.

  • reference (DateTime|NilClass) (defaults to: nil)

    The reference date.

  • suffix (String) (defaults to: "")

    The suffix to add to the formatted date.

Returns:

  • (String)

    The formatted date.



40
41
42
43
44
45
46
47
48
49
50
51
52
# 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?Boolean

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

Returns:

  • (Boolean)

    true if the request is JSON(P), false otherwise.



23
24
25
# 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) ⇒ Service::Response

Executes a service.

Parameters:

  • klass (Service)

    The service to execute.

  • operation (String) (defaults to: :perform)

    The operation to invoke.

  • kwargs (Hash)

    Parameters passed to the service.

Returns:



16
17
18
# 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?Boolean

Checks if the user is sending any data.

Returns:

  • (Boolean)

    true if the user is sending data, false otherwise.



30
31
32
# File 'lib/ballast/concerns/common.rb', line 30

def request_data?
  request.post? || request.put? || request.patch?
end