Skip to content

Instantly share code, notes, and snippets.

@theinventor
Created April 1, 2014 17:30
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theinventor/9918950 to your computer and use it in GitHub Desktop.
Save theinventor/9918950 to your computer and use it in GitHub Desktop.
A quick ruby api wrapper for the teamviewer api
module Teamviewer
class Connector
attr_reader :client
def initialize
@client = connection(credentials)
end
def credentials
# should be loaded from yml or something else
hash = {}
hash[:base_url] = 'https://webapi.teamviewer.com'
hash[:client_id] = TV_KEY
hash[:client_secret] = TV_SECRET
hash
end
def connection(credentials)
OAuth2::Client.new(
credentials[:client_id],
credentials[:client_secret],
:site => credentials[:base_url],
:authorize_url => '/api/v1/oauth2/authorize',
:token_url => '/api/v1/oauth2/token'
)
end
end
class Client
attr_reader :access_token
def initialize(client, token_str)
@client = client
@access_token = token(token_str)
end
def token(str)
OAuth2::AccessToken.new(@client, str)
end
#------------#
#--- Ping ---#
#------------#
def ping
get 'ping'
end
#-------------#
#--- Users ---#
#-------------#
def users(params = nil)
get 'users', params
end
def user_add(data)
post 'users', data
end
def user_get(id)
get "users/#{id}"
end
def user_update(id, data)
put "users/#{id}", data
end
#-------------#
#--- Groups ---#
#-------------#
def groups(params = nil)
get 'groups', params
end
#----------------#
#--- Sessions ---#
#----------------#
def sessions(params = nil)
get 'sessions', params
end
def session_add(data)
post 'sessions', data
end
def session_get(code)
get "sessions/#{code}"
end
def session_update(code)
put "sessions/#{code}", data
end
#--------------------------#
#--- Connection Reports ---#
#--------------------------#
def reports(params = nil)
get 'reports/connections', params
end
def report_update(id, data)
put "reports/connections/#{id}", data
end
def report_delete(id)
delete "reports/connections/#{id}"
end
#----------------#
#--- Meetings ---#
#----------------#
def meetings(params = nil)
get 'meetings', params
end
def meeting_get(id)
get "meetings/#{id}"
end
def meeting_invitation(id, params)
get "meetings/#{id}/invitation", params
end
def meeting_add(data)
post 'meetings', data
end
def meeting_update(id, data)
put "meetings/#{id}", data
end
def meeting_delete(id)
delete "meetings/#{id}"
end
private
def full_path(resource)
"api/v1/#{resource}"
end
def get(resource, params = nil)
response = @access_token.get( full_path(resource), :params => params )
response.parsed
end
def post(resource, data)
response = @access_token.post( full_path(resource), { body: data } )
response.status == 200 ? response.parsed : response.status
end
def put(resource, data)
response = @access_token.put( full_path(resource), { body: data } )
response.status
end
def delete(resource)
response = @access_token.delete( full_path(resource) )
response.status
end
end
end
@theinventor
Copy link
Author

Here is a potential controller to hook up to a rails app;

class TeamviewerController < ApplicationController

  require 'teamviewer'

  def index
  end

  def auth
    redirect_to client.auth_code.authorize_url(:redirect_uri => auth_teamviewer_callback_url)
  end

  def callback
    @access_token = client.auth_code.get_token(
        params[:code],
        :redirect_uri => auth_teamviewer_callback_url
    )

    #
    # Here we need to save token with needed info somewhere,
    # and check when it expires.
    #
    session[:access_token] = @access_token.token
    session[:expires_at] = @access_token.expires_at
    @account.settings.teamviewer_access_token = @access_token.token
    @account.settings.teamviewer_expires_at = @access_token.expires_at

    redirect_to teamviewer_index_path
  end


  #
  # Just using a simple Teamviewer's internal API method for testing - 'ping'
  #
  def test_ping
    teamviewer = Teamviewer::Client.new(client, session[:access_token])
    res = teamviewer.ping

    render json: res.inspect.to_json
  end

  def disconnect
    @account.settings.teamviewer_access_token = false
    @account.settings.teamviewer_expires_at = false
    redirect_to '/teamviewer', notice: 'We disconnected your authorization!'
  end

  private

  def client
    conn = Teamviewer::Connector.new
    conn.client
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment