Module: Whatsapp::Rails::Helpers

Defined in:
lib/whatsapp/rails/helpers.rb

Overview

View helpers for displaying WhatsApp session information in Rails views.

Automatically included in ActionView when the Railtie loads.

Examples:

In a view

<%= whatsapp_qr(current_hotel.whatsapp) %>
<%= whatsapp_status_badge(current_hotel.whatsapp) %>

Instance Method Summary collapse

Instance Method Details

#whatsapp_connected?(account_or_key) ⇒ Boolean

Whether the session is connected.

Parameters:

Returns:

  • (Boolean)


40
41
42
# File 'lib/whatsapp/rails/helpers.rb', line 40

def whatsapp_connected?()
  resolve_session().connected?
end

#whatsapp_qr(account_or_key, size: 200, html_options: {}) ⇒ String?

Render a QR code as an ++ tag with a base64 data URI.

Parameters:

  • account_or_key (AccountProxy, Session, String)

    session identifier

  • size (Integer) (defaults to: 200)

    image width and height in pixels

  • html_options (Hash) (defaults to: {})

    extra HTML attributes for the ++ tag

Returns:

  • (String, nil)

    HTML img tag, or nil if no QR data available



18
19
20
21
22
23
24
25
26
# File 'lib/whatsapp/rails/helpers.rb', line 18

def whatsapp_qr(, size: 200, html_options: {})
  session = resolve_session()
  qr = session.qr_data
  return nil unless qr

  src = "data:image/png;base64,#{qr}"
  opts = {src: src, width: size, height: size, alt: "WhatsApp QR Code"}.merge(html_options)
  tag(:img, opts)
end

#whatsapp_rate_limit_info(account_or_key) ⇒ Hash

Get rate limiting statistics for a session.

Parameters:

Returns:

  • (Hash)

    rate limit stats



48
49
50
# File 'lib/whatsapp/rails/helpers.rb', line 48

def whatsapp_rate_limit_info()
  resolve_session().rate_limit_stats
end

#whatsapp_status(account_or_key) ⇒ Symbol

Get the session status as a symbol.

Parameters:

Returns:

  • (Symbol)

    +:connected+, +:qr_pending+, +:connecting+, or +:disconnected+



32
33
34
# File 'lib/whatsapp/rails/helpers.rb', line 32

def whatsapp_status()
  resolve_session().status
end

#whatsapp_status_badge(account_or_key, html_options: {}) ⇒ String

Render a status badge as a ++ with CSS classes.

Parameters:

  • account_or_key (AccountProxy, Session, String)

    session identifier

  • html_options (Hash) (defaults to: {})

    extra HTML attributes

Returns:

  • (String)

    HTML span tag with class +whatsapp-status whatsapp-status--STATUS+



57
58
59
60
61
62
# File 'lib/whatsapp/rails/helpers.rb', line 57

def whatsapp_status_badge(, html_options: {})
  status = whatsapp_status()
  css = "whatsapp-status whatsapp-status--#{status}"
  css = "#{css} #{html_options.delete(:class)}" if html_options[:class]
  (:span, status.to_s, {class: css}.merge(html_options))
end