Class: HmxClient::Hmx

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

Instance Method Summary collapse

Constructor Details

#initializeHmx

Returns a new instance of Hmx.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/hmx/hmx.rb', line 45

def initialize
  @distMap = {
        :info => [ 'user', 'INFO', 'response', []],
        :getData => [ 'user', 'GETDATA', 'data', [ { :pos => 0, :name => 'displayName' } ]],
        :getContent  => [ 'user', 'GETCONTENT', 'content', [ { :pos  => 0, :name => 'displayName' }]],
 :putData => ['user', 'PUTDATA', 'data', [ { :pos => 0, :name => 'data', :file => 1, :json => 1 }]],
        :putSimpleData => [ 'user', 'PUTSIMPLEDATA', 'data', [ { :pos => 0, :name => 'displayName'}, { :pos => 1, :name => 'content', :file => 1}]],
        :query => [ 'user', 'QUERY', 'result', [ { :pos => 0, :name => 'index' }, { :pos => 1, :name => 'params' }]],
        :dynquery => ['user', 'DYNQUERY', 'result', [ { :pos => 0, :name => 'typeName' }, { :pos => 1, :name => 'filterFn' }, { :pos => 2, :name => 'mapFn' } ]],
        :deleteData => [ 'user', 'DELETEDATA', 'data', [ { :pos => 0, :name => 'displayName' } ]],
        :getSequenceSize => [ 'user', 'GETSEQUENCESIZE', 'size', [ { :pos => 0, :name => 'typeName' }]],
        :execPartSeq => [ 'user', 'EXECPARTIALSEQUENCE', 'response', [ { :pos => 0, :name => 'typeName' }, 
                                                 { :pos => 1, :name => 'fn' },
						   { :pos => 2, :name => 'start' },
				   { :pos => 3, :name => 'size' } ] ] ,
        :requestSession => [ 'user', 'REQUESTSESSION', 'session', [ { :pos => 0, :name => 'partition' }, { :pos => 1, :name => 'user'}]],
        :validateUser => [ 'user', 'VALIDATEUSER', 'context', [ { :pos => 0, :name => 'partition' },
						{ :pos => 1, :name => 'user' },
						{ :pos => 2, :name => 'hashPassword' },
						{ :pos => 3, :name => 'session' }
			]],
        :submitTask => [ 'user', 'SUBMITTASK', 'task', [ { :pos => 0, :name => 'task' } ]],
        :runView => [ 'user', 'RUNVIEW', 'response', [ { :pos => 0, :name => 'view' }, { :pos => 1, :name => 'params' } ] ],
 :getTypes => [ 'admin', 'GETTYPES', 'types', []],
        :getType => [ 'admin', 'GETTYPE', 'type', [ { :pos => 0, :name => 'typeName' } ]],
        :updateType => [ 'admin', 'UPDATETYPE', 'type', [ { :pos => 0, :name => 'type' }]],
 :getFn => [ 'user', 'GETFN', 'fnContent', [ { :pos =>0, :name => 'fnName' } ]],
        :putFn => [ 'user', 'PUTFN', 'response', [ { :pos => 0, :name => 'fnName' }, { :pos => 1, :name => 'fnContent', :file => 1 } ]],
 :expireSessions => [ 'admin', 'EXPIRESESSIONS', 'sessions', []],
        :getAllTasks => [ 'task', 'GETALLTASKS', 'tasks', []],
        :getTask => [ 'task', 'GETTASK', 'task', [ { :pos=> 0, :name => 'taskId' } ]],
        :purgeTasks => [ 'task', 'PURGETASKS', 'response', []],
        :getFountainState => [ 'admin', 'GETFOUNTAINSTATE', 'state', []],
        :setFountainId => [ 'admin', 'SETFOUNTAINID', 'dummy', [ { :pos => 0, :name => 'fountain' }, { :pos => 1, :name=> 'value' }]],
        :getFountain => [ 'user', 'AUTOID', 'id', [ { :pos => 0, :name => 'fountain' } ]],
        :bootstrap => ['special', 'BOOTSTRAP', 'response', []]
  }
end

Instance Method Details

#bootstrapObject



202
203
204
# File 'lib/hmx/hmx.rb', line 202

def bootstrap
  performDistRequest(:bootstrap, [])
end

#deleteData(*args) ⇒ Object



148
149
150
# File 'lib/hmx/hmx.rb', line 148

def deleteData(*args)
  performDistRequest(:deleteData, args)
end

#dynquery(*args) ⇒ Object



145
146
147
# File 'lib/hmx/hmx.rb', line 145

def dynquery(*args)
  performDistRequest(:dynquery, args)
end

#execPartialSequence(*args) ⇒ Object



154
155
156
# File 'lib/hmx/hmx.rb', line 154

def execPartialSequence(*args)
  performDistRequest(:execPartSeq, args)
end

#expireSessions(args) ⇒ Object



181
182
183
# File 'lib/hmx/hmx.rb', line 181

def expireSessions(args)
  performDistRequest(:expireSessions, args)
end

#getAllTasks(args) ⇒ Object



184
185
186
# File 'lib/hmx/hmx.rb', line 184

def getAllTasks(args)
  performDistRequest(:getAllTasks, args)
end

#getContent(args) ⇒ Object



133
134
135
# File 'lib/hmx/hmx.rb', line 133

def getContent(args)
  performDistRequest(:getContent, args)
end

#getData(args) ⇒ Object



130
131
132
# File 'lib/hmx/hmx.rb', line 130

def getData(args)
  performDistRequest(:getData, args)
end

#getFn(args) ⇒ Object



175
176
177
# File 'lib/hmx/hmx.rb', line 175

def getFn(args)
  performDistRequest(:getFn, args)
end

#getFountain(args) ⇒ Object



199
200
201
# File 'lib/hmx/hmx.rb', line 199

def getFountain(args)
  performDistRequest(:getFountain, args)
end

#getFountainState(args) ⇒ Object



193
194
195
# File 'lib/hmx/hmx.rb', line 193

def getFountainState(args)
  performDistRequest(:getFountainState, args)
end

#getSenderHashObject



125
126
127
128
129
# File 'lib/hmx/hmx.rb', line 125

def getSenderHash()
  hash = Hash.new
  hash['context'] = @context if @context
  return hash
end

#getSequenceSize(*args) ⇒ Object



151
152
153
# File 'lib/hmx/hmx.rb', line 151

def getSequenceSize(*args)
  performDistRequest(:getSequenceSize, args)
end

#getTask(args) ⇒ Object



187
188
189
# File 'lib/hmx/hmx.rb', line 187

def getTask(args)
  performDistRequest(:getTask, args)
end

#getType(*args) ⇒ Object



172
173
174
# File 'lib/hmx/hmx.rb', line 172

def getType(*args)
  performDistRequest(:getType, args)
end

#getTypes(*args) ⇒ Object



166
167
168
# File 'lib/hmx/hmx.rb', line 166

def getTypes(*args)
  performDistRequest(:getTypes, args)
end

#login(config) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/hmx/hmx.rb', line 23

def (config) 
  puts config if $debug
  @context = nil;
  @urlapi = config[:api]
  if config.has_key?(:context)
    @context = JSON.parse(config[:context])
    #Do a null check
    begin
      performDistRequest(:info, []) 
    rescue => e
      @context = nil;
    end  
  end   
  if (@context.nil?)
    session = requestSession(config[:partition], config[:user])
    salty = session['MXSession']['salty']
    sendString = config[:password] + ':' + salty
    digestToSend = Digest::MD5.hexdigest(sendString)
    @context = validateUser(config[:partition], config[:user], digestToSend, session['MXSession']['sessionId'])
    config[:context] = JSON.generate(@context)
  end
end

#loginApi(url, urlapi) ⇒ Object



19
20
21
22
# File 'lib/hmx/hmx.rb', line 19

def loginApi(url, urlapi)
  @urlapi = urlapi
  @context = JSON.parse (RestClient.get url).to_str
end

#performDistRequest(cmd, args) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/hmx/hmx.rb', line 83

def performDistRequest(cmd, args)
   spec = @distMap[cmd]
   hash = getSenderHash()
   spec[3].each { | param |
	# if the args has enough , use that
                      # otherwise, prompt for the parameter
                      if (args.length > param[:pos])
	  hash[param[:name]] = args[param[:pos]] 
	elsif (param.has_key?(:file) && !HmxClient::Command.fileIn.nil?)
	  # Content comes from file
			  realFile = File.expand_path(HmxClient::Command.fileIn)
                        puts "Reading #{ realFile } to get parameter #{ param[:name] }"
                        hash[param[:name]] = File.open(realFile) { | f | 
					c = ''
                                                      while(line = f.gets)
                                                          c = c + line
                                                      end
					c
                                                }
                      else
	  hash[param[:name]] = getFromUser("Please enter the value of #{ param[:name] }")
                      end
                      if (param.has_key?(:json))
                        hash[param[:name]] = JSON.parse(hash[param[:name]])
                      end
                 }
   standardRequest(spec[0], spec[1], hash, spec[2])
end

#performRequest(prefix, function, command) ⇒ Object



111
112
113
114
115
116
# File 'lib/hmx/hmx.rb', line 111

def performRequest(prefix, function, command)
  puts "Send data #{ JSON.generate(command) }" if HmxClient::Command.debug?
  response = RestClient.post @urlapi+'/'+prefix, :function=>function, :params=>JSON.generate(command), :multipart=>true
  puts "Raw response is #{ response.to_str }" if HmxClient::Command.debug?
  return JSON.parse(response.to_str)
end

#purgeTasks(args) ⇒ Object



190
191
192
# File 'lib/hmx/hmx.rb', line 190

def purgeTasks(args)
  performDistRequest(:purgeTasks, args)
end

#putData(args) ⇒ Object



136
137
138
# File 'lib/hmx/hmx.rb', line 136

def putData(args)
  performDistRequest(:putData, args)
end

#putFn(args) ⇒ Object



178
179
180
# File 'lib/hmx/hmx.rb', line 178

def putFn(args)
  performDistRequest(:putFn, args)
end

#putSimpleData(args) ⇒ Object



139
140
141
# File 'lib/hmx/hmx.rb', line 139

def putSimpleData(args)
  performDistRequest(:putSimpleData, args)
end

#query(*args) ⇒ Object



142
143
144
# File 'lib/hmx/hmx.rb', line 142

def query(*args)
  performDistRequest(:query, args)
end

#requestSession(*args) ⇒ Object



157
158
159
# File 'lib/hmx/hmx.rb', line 157

def requestSession(*args)
  performDistRequest(:requestSession, args)
end

#runView(*args) ⇒ Object



163
164
165
# File 'lib/hmx/hmx.rb', line 163

def runView(*args)
  performDistRequest(:runView, args)
end

#setFountainId(args) ⇒ Object



196
197
198
# File 'lib/hmx/hmx.rb', line 196

def setFountainId(args)
  performDistRequest(:setFountainId, args)
end

#standardRequest(prefix, commandName, params, returnPart) ⇒ Object



117
118
119
120
121
122
123
124
# File 'lib/hmx/hmx.rb', line 117

def standardRequest(prefix, commandName, params, returnPart)
  response = performRequest(prefix, commandName, params)
  puts "JSON response is #{ response }" if HmxClient::Command.debug?
  if response['inError']
     raise HmxException.new(response['exception']), "Error returned from server" 
  end
  return response[returnPart]
end

#updateType(*args) ⇒ Object



169
170
171
# File 'lib/hmx/hmx.rb', line 169

def updateType(*args)
  performDistRequest(:updateType, args)
end

#validateUser(*args) ⇒ Object



160
161
162
# File 'lib/hmx/hmx.rb', line 160

def validateUser(*args)
  performDistRequest(:validateUser, args)
end