The vLineRails Example shows you how to create a Rails app that can be used with the low-level vLine API and also used to launch the vLine Web Client that uses the users you provide.
- Sign up for a [vLine developer account] (https://vline.com/developer/).
- Sign up for Forward and install the gem:
gem install forward. - Install ruby version 1.9.3 or newer.
- Clone this repository.
- Optional: Install rvm.
Create a service in the vLine Developer Console.
-
Change into the directory where you cloned this repository.
-
Install all dependencies:
bundle install -
Create an app directory and change into it:
mkdir ../vline cd ../vlineThe following instructions walk you through creating the Rails app from scratch. If you'd rather use the pre-generated one, you can skip to the Configure your app step. Make sure you have configured the values in
config/initializers/vline.rb. -
Create the new app:
rails new vline-demo-app -m https://raw.github.com/RailsApps/rails-composer/f9d885977ad345dd90f07e6dcdac17407cf9b3a7/composer.rb -T -
You will get several prompts. Choose the following:
4) rails-3-bootstrap-devise-cancan1) WEBrick (default)1) Same as development1) ERB1) NoneSet a robots.txt file to ban spiders:: You chooseCreate a GitHub repository?: You chooseUse or create a project-specific rvm gemset?: y
-
At the end of the setup, you should see
Your bundle is complete!. If you get any error messages during this process, take a look at this page. -
Change into the app directory:
cd vline-demo-app -
The email and password for the admin account is in
config/application.yml. It is probably[email protected]andchangeme. -
Create some extra users by adding the following to the
db/seeds.rbfile:for i in 2..5 user = User.find_or_create_by_email :name => "User #{i}", :email => "user#{i}@example.com", :password => 'changeme', :password_confirmation => 'changeme' puts 'user: ' << user.name user.add_role :user end
-
Add
gem "vline-rails"to yourGemfileand runbundle installto install it. -
Find your service in the vLine Developer console and make note of the
Service IDandAPI Secret. -
Use the values from the last step to generate the vLine provider by running the following command:
rails generate vline_provider --service-id=Your-Service-Id --provider-secret=Your-Service-API-SecretMake note of the
Client IDandClient Secretoutput by the command. -
Check your
config.rufile and verify that it has:require 'rack/jsonp' use Rack::JSONP
-
At the top of
app/views/home/index.html.erb, add the following, which includes vline.js:<% content_for :head do %> <script src="https://static.vline.com/vline.js" type="text/javascript"></script> <% end %> -
In the same file, replace:
<p>User: <%=link_to user.name, user %></p>with
<div> User: <%= user.name %>, Presence: <span id="<%= user.id %>">offline</span>, <%= vline_launch "Launch Web Client", user %> <span id="call-<%= user.id %>" style="display:none"> , <a onclick="call('<%= user.id %>')" href="javascript:void(0)">Call</a> </span> </div> -
In the same file, add the following JavaScript at the bottom of the file:
<script type="text/javascript"> var loginToken = "<%= Vline.create_login_token(current_user.id) %>"; var serviceId = "<%= Vline.service_id %>"; var authId = "<%= Vline.provider_id %>"; var profile = {"displayName" : "<%= current_user.name %>"}; var people = [ <% @users.each do |user| %> "<%= user.id %>", <% end %> ]; var client = vline.Client.create({"serviceId": serviceId, "ui": true}); var session; function call(userId) { if (session) { session.getPerson(userId) .done(function(person) { person.startMedia(); }); } } client.on('login', function(e) { session = e.target; for (var i=0; i < people.length; i++) { session.getPerson(people[i]) .done(function(person) { var updatePresence = function(e) { var person = e.target; var presenceState = person.getPresenceState(); var shortId = person.getShortId(); var elem = document.getElementById(shortId); elem.innerHTML = presenceState; // Show/hide the call link based on presence elem = document.getElementById('call-' + shortId); if (presenceState === "online" && shortId !== "<%= current_user.id %>") { elem.style.display = ""; } else { elem.style.display = "none"; } }; updatePresence({target: person}); person.on('change:presenceState', updatePresence); }); } }); client.login(authId, profile, loginToken); </script> -
In the home controller (
app/controllers/home_controller.rb), force the user to login to see the page with:before_filter :authenticate_user!at the beginning of the class (
class HomeController < ApplicationController)Also, add:
require 'vline'to the top of the file.
-
Make sure all dependencies are installed:
bundle install -
Prepare the database:
rake db:migrate rake db:seed -
Generate a secret token:
rake secret -
Add the generated token to
config/initializers/secret_token.rb. -
Start Forward by running the following command in your app directory:
forward 3000Make a note of the URL that it prints so that you can use it in the next steps.
-
Open up the vLine Developer Console and choose
Service Settings. -
Click the
Editbutton and selectCustom OAuthin theAuthorizationdropdown:- Add the
Client IDandClient Secretfrom therails generatecommand you previously ran. - Set the
Provider URLto :https://your-forward-url/_vline/api/v1/ - Set the
OAuth URLto:https://your-forward-url/_vline/api/v1/oauth/
- Add the
-
Further customization of the Web Client can be done by providing custom images in the
Web Client Settings.
-
Run the rails server in your app directory (make sure Forward is still running):
rails server -
Go to http://localhost:3000 in your browser.
-
Log in as
[email protected]. -
Open up a Chrome incognito window.
-
Log in as
[email protected]. -
You should see both "First User" and "User 2" online.
-
Click
Launch Web Clientto launch the Web Client for a specific user. -
A
Calllink will show up next to online users and will initiate a call directly from the page.