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

- (Object) authenticate_user(area = nil, title = nil, message = nil, &authenticator)

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

Parameters:

  • area (String) (defaults to: nil)

    The name of the area.

  • title (String) (defaults to: nil)

    A title for authentication errors.

  • message (String) (defaults to: nil)

    A message for authentication errors.

  • authenticator (Proc)

    A block to verify if authentication is valid.



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/ballast/concerns/common.rb', line 98

def authenticate_user(area = nil, title = nil, message = nil, &authenticator)
  area ||= "Private Area"
  title ||= "Authentication required."
  message ||= "To view this resource you have to authenticate."
  authenticated = authenticate_with_http_basic { |username, password| authenticator.call(username, password) }

  if !authenticated then
    headers["WWW-Authenticate"] = "Basic realm=\"#{area}\""
    handle_error({status: 401, title: title, message: message})
  end
end

- (Object) format_long_date(date, separator = "•", format = "%I:%M%p %- %b %o, %Y (%:Z)")

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.



86
87
88
89
90
# File 'lib/ballast/concerns/common.rb', line 86

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.send(tz.uses_dst? && date.dst? ? :dst_name : :name)}
  date.strftime(format).gsub(/%(-|o|(:Z))/) {|r| replacements.fetch(r, r) }
end

- (String) format_short_amount(amount, suffix = "")

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.



70
71
72
73
74
75
76
77
78
# File 'lib/ballast/concerns/common.rb', line 70

def format_short_amount(amount, suffix = "")
  if amount < 1.minute then
    "#{amount.floor}s#{suffix}"
  elsif amount < 1.hour then
    "#{(amount / 60).floor}m#{suffix}"
  else
    "#{(amount / 3600).floor}h#{suffix}"
  end
end

- (String) format_short_duration(date, reference = nil, suffix = "")

Formats a relative date using abbreviation or short formats.

Parameters:

  • date (DateTime)

    The date to format.

  • reference (DateTime) (defaults to: nil)

    The reference date.

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

    The suffix to add to the formatted date.

Returns:

  • (String)

    The formatted date.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/ballast/concerns/common.rb', line 50

def format_short_duration(date, reference = nil, suffix = "")
  reference ||= Time.now
  amount = (reference.to_i - date.to_i).to_i

  if amount <= 0 then
    "now"
  elsif amount < 1.day then
    format_short_amount(amount, suffix)
  elsif amount < 1.year then
    date.strftime("%b %d")
  else
    date.strftime("%b %d %Y")
  end
end

- (Boolean) is_json?

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

Returns:

  • (Boolean)

    true if the request is JSON, false otherwise.



13
14
15
# File 'lib/ballast/concerns/common.rb', line 13

def is_json?
  ([:json, :jsonp].include?(request.format.to_sym) || params[:json].to_boolean) ? true : false
end

- (Operation) perform_operation(klass, owner = nil, **kwargs)

Performs an operation, using itself as owner by default.

Parameters:

  • klass (Class)

    The operation to perform.

  • owner (Object) (defaults to: nil)

    The owner to use. By default it uses itself.

  • kwargs (Hash)

    The arguments for performing.

Returns:



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

def perform_operation(klass, owner = nil, **kwargs)
  @operation = klass.perform(owner || self, **kwargs)
end

- (OperationChain) perform_operations_chain(klasses, owner = nil, **kwargs)

Performs an operations chain, using itself as owner by default.

Parameters:

  • klasses (Array)

    The operations to perform.

  • owner (Object) (defaults to: nil)

    The owner to use. By default it uses itself.

  • kwargs (Hash)

    The arguments for performing.

Returns:

  • (OperationChain)

    The performed operation chain.



40
41
42
# File 'lib/ballast/concerns/common.rb', line 40

def perform_operations_chain(klasses, owner = nil, **kwargs)
  @operation = Ballast::OperationsChain.perform(owner || self, klasses, **kwargs)
end

- (Boolean) sending_data?

Checks if the user is sending any data.

Returns:

  • (Boolean)

    true if the user is sending data, false otherwise.



20
21
22
# File 'lib/ballast/concerns/common.rb', line 20

def sending_data?
  request.post? || request.put?
end