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)
-
- (Object) authenticate_user(area: nil, title: nil, message: nil, &authenticator)
Authenticates a user via HTTP, handling the error if the authentication failed.
-
- (Object) format_long_date(date, separator: "•", format: "%I:%M%p %- %b %o, %Y (%:Z)")
Formats a long date.
-
- (String) format_short_amount(amount, suffix = "")
Formats a short amount of time (less than one hour).
-
- (String) format_short_duration(date, reference: nil, suffix: "")
Formats a relative date using abbreviation or short formats.
-
- (Boolean) json?
Checks if the current request wants JSON or JSONP as response.
-
- (Boolean) request_data?
Checks if the user is sending any data.
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.
77 78 79 80 81 82 83 84 85 86 |
# File 'lib/ballast/concerns/common.rb', line 77 def authenticate_user(area: nil, title: nil, message: nil, &authenticator) return if authenticate_with_http_basic { |username, password| authenticator.call(username, password) } area ||= "Private Area" title ||= "Authentication required." ||= "To view this resource you have to authenticate." headers["WWW-Authenticate"] = "Basic realm=\"#{area}\"" handle_error({status: 401, title: title, message: }) end |
- (Object) format_long_date(date, separator: "•", format: "%I:%M%p %- %b %o, %Y (%:Z)")
Formats a long date.
65 66 67 68 69 |
# File 'lib/ballast/concerns/common.rb', line 65 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 |
- (String) format_short_amount(amount, suffix = "")
Formats a short amount of time (less than one hour).
49 50 51 52 53 54 55 56 57 |
# File 'lib/ballast/concerns/common.rb', line 49 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 |
- (String) format_short_duration(date, reference: nil, suffix: "")
Formats a relative date using abbreviation or short formats.
30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/ballast/concerns/common.rb', line 30 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 |
- (Boolean) json?
Checks if the current request wants JSON or JSONP as response.
13 14 15 |
# File 'lib/ballast/concerns/common.rb', line 13 def json? [:json, :jsonp].include?(request.format.to_sym) || params[:json].to_boolean end |
- (Boolean) request_data?
Checks if the user is sending any data.
20 21 22 |
# File 'lib/ballast/concerns/common.rb', line 20 def request_data? request.post? || request.put? || request.patch? end |