class Punchtab::API

Constants

ACTIVITIES
BASE_API_URL

Attributes

access_token[R]

Public Class Methods

new(options = {}) click to toggle source
# File lib/punchtab/api.rb, line 22
def initialize(options = {})
  @client_id    = options[:client_id]     # required
  @access_key   = options[:access_key]    # required
  @secret_key   = options[:secret_key]    # required
  @domain       = options[:domain]        # required
  @user_info    = options[:user_info]     # optional

  Punchtab::API.headers 'Referer' => "http://#{@domain}"

  unless @client_id && @access_key && @secret_key && @domain
    raise Exception.new('Client Id, Access Key, Secret Key and Domain are required to authenticate, before using PunchTab services.')
  end
end

Public Instance Methods

authenticate() click to toggle source

api.punchtab.com/v1/auth/sso

# File lib/punchtab/api.rb, line 39
def authenticate
  # setup the user data structure
  user_data = {:id => @client_id}
  if @user_info
    user_data.merge!(@user_info)
  end

  # prepare authentication params
  time_stamp = Time.now.to_i
  auth_request = Base64.encode64(JSON.dump(user_data))
  string_to_sign = "#{auth_request} #{time_stamp}"
  hmac = OpenSSL::HMAC.new(@secret_key, OpenSSL::Digest::SHA1.new)
  signature = hmac.update(string_to_sign).hexdigest

  # make the POST call
  path = '/auth/sso'

  # setup the post params
  post_data = {
      :client_id    => @client_id,
      :key          => @access_key,
      :auth_request => auth_request,
      :timestamp    => time_stamp,
      :signature    => signature
  }
  raw_response = Punchtab::API.post(path, :body => post_data)
  response = Punchtab::Utils.process_response(raw_response)
  # return the access token
  @access_token = response.authResponse.accessToken
end
logout() click to toggle source

Required Parameters

None

Optional Parameters

None

Return

https://api.punchtab.com/v1/auth/logout
# File lib/punchtab/api.rb, line 76
def logout
  # make the POST call
  path = '/auth/logout'

  # setup the post params
  post_data = {
    :token  => @access_token,
    :key    => @access_key
  }
  raw_response = Punchtab::API.post(path, :body => post_data)
  Punchtab::Utils.process_response(raw_response)
end
status() click to toggle source

Required Parameters

None

Optional Parameters

None

Return api.punchtab.com/v1/auth/status

# File lib/punchtab/api.rb, line 95
def status
  # make the POST call
  path = '/auth/status'

  # setup the post params
  post_data = {
      :token  => @access_token,
      :key    => @access_key
  }
  raw_response = Punchtab::API.post(path, :body => post_data)
  Punchtab::Utils.process_response(raw_response)
end

Private Instance Methods

activity_list() click to toggle source
# File lib/punchtab/api.rb, line 240
def activity_list
  ACTIVITIES.join(',')
end
activity_valid?(activity_name) click to toggle source
# File lib/punchtab/api.rb, line 236
def activity_valid?(activity_name)
  ACTIVITIES.include?(activity_name.to_s)
end