Class: Planga

Inherits:
Object
  • Object
show all
Defined in:
lib/planga.rb

Overview

This class allows you to create a snippet of HTML/JS that, if included in a webpage, lets the visitor of that webpage connect with the Planga Chat Server and start chatting.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**conf) ⇒ Planga

The following configuration options are available:

Required are:

  • public_api_id: The public API ID that can be found in the Planga Dashboard.

  • private_api_key: The private API key that can be found in the Planga Dashboard.

  • conversation_id: The identifier that uniquely identifies the single conversation

that the user can connect with in this chat. Examples would be “general”, “product/1234” or “private/123/543”.

  • current_user_id: The internal ID your application uses, which uniquely identifies

the user currently using your application.

  • current_user_name: The name of this user. This name is shown in the chat interface

next to the typed messages

Optional are:

  • remote_host: This can point to another host, if you are hosting your own instance of Planga.

It defaults to the URL of Planga’s main chat server. (‘//chat.planga.io`)

  • container_id: If you want a custom HTML ID attribute specified to the created HTML element,

you can enter it here.

  • include_style: Can be the location of a custom stylesheet to include,

or ‘false` if you do not want to include your own style (and do this manually somewhere else in the page). (Defaults to “##remote_host/css/chat-style-basic.css”)

  • debug: (defaults to ‘false`).



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/planga.rb', line 35

def initialize(**conf)
    @public_api_id = conf[:public_api_id]
    @private_api_key = conf[:private_api_key]
    @conversation_id = conf[:conversation_id]
    @current_user_id = conf[:current_user_id]
    @current_user_name = conf[:current_user_name]
    @current_user_role = conf[:current_user_role]

    @remote_host = conf[:remote_host]
    @remote_host ||= "//chat.planga.io"

    @container_id = conf[:container_id]
    @container_id ||= "planga-chat-" + SecureRandom.hex

    @include_style = conf[:include_style]
    if @include_style == nil
      @include_style = "#{remote_host}/css/chat-style-basic.css"
    end
   @debug = conf[:debug] || false
end

Instance Attribute Details

#container_idObject (readonly)

Returns the value of attribute container_id.



9
10
11
# File 'lib/planga.rb', line 9

def container_id
  @container_id
end

#conversation_idObject (readonly)

Returns the value of attribute conversation_id.



9
10
11
# File 'lib/planga.rb', line 9

def conversation_id
  @conversation_id
end

#current_user_idObject (readonly)

Returns the value of attribute current_user_id.



9
10
11
# File 'lib/planga.rb', line 9

def current_user_id
  @current_user_id
end

#current_user_nameObject (readonly)

Returns the value of attribute current_user_name.



9
10
11
# File 'lib/planga.rb', line 9

def current_user_name
  @current_user_name
end

#debugObject (readonly)

Returns the value of attribute debug.



9
10
11
# File 'lib/planga.rb', line 9

def debug
  @debug
end

#include_styleObject (readonly)

Returns the value of attribute include_style.



9
10
11
# File 'lib/planga.rb', line 9

def include_style
  @include_style
end

#public_api_idObject (readonly)

Returns the value of attribute public_api_id.



9
10
11
# File 'lib/planga.rb', line 9

def public_api_id
  @public_api_id
end

#remote_hostObject (readonly)

Returns the value of attribute remote_host.



9
10
11
# File 'lib/planga.rb', line 9

def remote_host
  @remote_host
end

Instance Method Details

#chat_snippetObject

Creates a full-fledged HTML snippet that includes Planga in your page.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/planga.rb', line 58

def chat_snippet
    snippet = <<-SNIPPET
#{style_tag()}
<script type="text/javascript" src="#{@remote_host}/js/js_snippet.js"></script>
<div id="#{@container_id}"></div>
<script type="text/javascript">
new Planga(document.getElementById("#{@container_id}"), \{
    public_api_id: "#{@public_api_id}",
    encrypted_options: "#{encrypted_options()}",
    socket_location: "#{@remote_host}/socket",
    debug: #{@debug},
\});
</script>
    SNIPPET

    snippet.strip!
end

#encrypted_optionsObject

Returns the encrypted configuration.

This function is useful if (and only if) you do not want to use the normal chat snippet, but want to completely customize how Planga loads (so you want to create it manually).



89
90
91
# File 'lib/planga.rb', line 89

def encrypted_options
    encrypt(construct_encrypted_options())
end

#style_tagObject



76
77
78
79
80
81
82
# File 'lib/planga.rb', line 76

def style_tag()
 return "" unless @include_style

 <<-SNIPPET
 <link rel="#{include_style}" />
 SNIPPET
end