Class: KynetxAmApi::User

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

Overview

Simple wrapper to allow access to the OAuth user information. This also hold some basic user data like username, name and user id.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ User

Accepts a hash that has the following entries.

  • :request_token

  • :request_secret

  • :oauth_verifier

  • :access_token

  • :access_secret

  • :username

  • :userid

  • :name



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/kynetx_am_api/user.rb', line 39

def initialize(attributes)
  @request_token = attributes[:request_token]
  @request_secret = attributes[:request_secret]
  @oauth_verifier = attributes[:oauth_verifier]
  @access_token = attributes[:access_token]
  @access_secret = attributes[:access_secret]
  @username = attributes[:username]
  @userid = attributes[:userid]
  @name = attributes[:name]
  @current_applicaion = nil
end

Instance Attribute Details

#access_secretObject

OAuth Access Secret



16
17
18
# File 'lib/kynetx_am_api/user.rb', line 16

def access_secret
  @access_secret
end

#access_tokenObject

OAuth Access Token



14
15
16
# File 'lib/kynetx_am_api/user.rb', line 14

def access_token
  @access_token
end

#current_applicationObject (readonly)

Current Application context.



24
25
26
# File 'lib/kynetx_am_api/user.rb', line 24

def current_application
  @current_application
end

#nameObject

Full name of user



22
23
24
# File 'lib/kynetx_am_api/user.rb', line 22

def name
  @name
end

#oauth_verifierObject

OAuth Verifieer



12
13
14
# File 'lib/kynetx_am_api/user.rb', line 12

def oauth_verifier
  @oauth_verifier
end

#request_secretObject

OAuth Secret Token



10
11
12
# File 'lib/kynetx_am_api/user.rb', line 10

def request_secret
  @request_secret
end

#request_tokenObject

OAuth Request Token



8
9
10
# File 'lib/kynetx_am_api/user.rb', line 8

def request_token
  @request_token
end

#useridObject

Kynetx User ID



20
21
22
# File 'lib/kynetx_am_api/user.rb', line 20

def userid
  @userid
end

#usernameObject

Kynetx User name



18
19
20
# File 'lib/kynetx_am_api/user.rb', line 18

def username
  @username
end

Instance Method Details

#apiObject

Returns the direct api to the Kynetx Application Manager.



54
55
56
57
# File 'lib/kynetx_am_api/user.rb', line 54

def api
  @api ||= KynetxAmApi::DirectApi.new({:access_token => @access_token, :access_secret => @access_secret})
  return @api
end

#applications(options = {}) ⇒ Object

Read applications list

  • :offset => Start in list (not implemented)

  • :size => Number of application to list (not implemented)

Returns a Hash with two keys

  • “apps” => Array Off Hashes with :appid , :role, :name, :created

  • “valid” => true



69
70
71
72
# File 'lib/kynetx_am_api/user.rb', line 69

def applications(options = {})
  @applications = api.get_applist if !@applications
  @applications
end

#create_application(name, description = "") ⇒ Object



89
90
91
92
93
94
95
96
97
98
# File 'lib/kynetx_am_api/user.rb', line 89

def create_application(name, description="")
  response = api.get_appcreate

  raise "Error from API: #{response["error"]}" if not response["valid"]

  appid = response["appid"]
  @current_application = KynetxAmApi::Application.new(self, appid).create_initial_app(name, description)
  
  return @current_application
end

#duplicate_application(application_id) ⇒ Object



100
101
102
103
104
105
# File 'lib/kynetx_am_api/user.rb', line 100

def duplicate_application(application_id)
  old_app = KynetxAmApi::Application.new(self, application_id)
  new_app = create_application(old_app.name || "", "")
  new_app.krl = old_app.krl
  return new_app
end

#find_application(options = {}) ⇒ Object

  • :application_id => application_id



77
78
79
80
81
82
83
84
85
86
# File 'lib/kynetx_am_api/user.rb', line 77

def find_application(options = {})
  raise "Expecting :application_id" unless options[:application_id]
  
  if @current_application && @current_application.application_id != options[:application_id]
    @current_application = KynetxAmApi::Application.new(self, options[:application_id])
  else
    @current_application ||= KynetxAmApi::Application.new(self, options[:application_id])
  end
  return @current_application
end

#kpis(rulesets = [], range = nil) ⇒ Object



126
127
128
129
130
131
132
# File 'lib/kynetx_am_api/user.rb', line 126

def kpis(rulesets=[], range=nil)
  conditions = rulesets.empty? ? nil : []
  rulesets.each do |ruleset|
    conditions.push << {:field => "ruleset", :value => ruleset}
  end
  return api.get_stats_query("rse,brse,rules,rules_fired,actions,callbacks", 'ruleset,day', conditions, range)
end

#owns_current?Boolean

Returns:

  • (Boolean)


107
108
109
110
111
# File 'lib/kynetx_am_api/user.rb', line 107

def owns_current?
  return false unless @current_application
  return false unless @current_application.owner
  return @current_application.owner["kynetxuserid"].to_i == self.userid.to_i
end

#stats_interfaceObject



134
135
136
# File 'lib/kynetx_am_api/user.rb', line 134

def stats_interface
  return api.get_stats_interface
end

#to_hObject



113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/kynetx_am_api/user.rb', line 113

def to_h
  return {
    :access_secret => @access_secret,
    :access_token => @access_token,
    :request_token => @request_token,
    :request_secret => @request_secret,
    :oauth_verifier => @oauth_verifier,
    :name => @name,
    :userid => @userid,
    :username => @username
  }
end