hmx_client 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,61 @@
1
+ require 'cli-colorize'
2
+
3
+ module HmxClient
4
+ module Helpers
5
+ include CLIColorize
6
+
7
+ def getFromUser(prompt)
8
+ content = ''
9
+ done = false
10
+ puts prompt
11
+ while(!done)
12
+ line = gets.strip
13
+ if (line[line.length-1] == '\\')
14
+ done = false
15
+ content = content + line[0 .. line.length-2]
16
+ else
17
+ content = content + line
18
+ done = true
19
+ end
20
+ end
21
+ content
22
+ end
23
+ def error(msg)
24
+ puts(colorize(msg, :red))
25
+ exit 1
26
+ end
27
+ def longest(items)
28
+ items.map { |i| i.to_s.length }.sort.last
29
+ end
30
+
31
+ def display(msg="", newline=true)
32
+ if newline
33
+ puts(msg)
34
+ else
35
+ print(msg)
36
+ STDOUT.flush
37
+ end
38
+ end
39
+
40
+ def display_table(objects, columns, headers)
41
+ lengths = []
42
+ columns.each_with_index do |column, index|
43
+ header = headers[index]
44
+ lengths << longest([header].concat(objects.map { |o| o[column].to_s }))
45
+ end
46
+ display_row headers, lengths
47
+ display_row headers.map { |header| "-" * header.length }, lengths
48
+ objects.each do |row|
49
+ display_row columns.map { |column| row[column] }, lengths
50
+ end
51
+ end
52
+
53
+ def display_row(row, lengths)
54
+ row.zip(lengths).each do |column, length|
55
+ format = column.is_a?(Fixnum) ? "%#{length}s " : "%-#{length}s "
56
+ display format % column, false
57
+ end
58
+ display
59
+ end
60
+ end
61
+ end
data/lib/hmx/hmx.rb CHANGED
@@ -3,26 +3,52 @@ require 'rest_client'
3
3
  require 'json'
4
4
  require 'digest/md5'
5
5
 
6
+ module HmxClient
7
+
8
+ include HmxClient::Helpers
9
+
10
+ class HmxException < Exception
11
+ attr :msg
12
+ def initialize(msg)
13
+ @msg = msg
14
+ end
15
+ end
16
+
6
17
  class Hmx
18
+
7
19
  def loginApi(url, urlapi)
8
20
  @urlapi = urlapi
9
21
  @context = JSON.parse (RestClient.get url).to_str
10
22
  end
11
23
  def login(config)
12
24
  puts config if $debug
25
+ @context = nil;
13
26
  @urlapi = config[:api]
14
- session = requestSession(config[:partition], config[:user])
15
- salty = session['MXSession']['salty']
16
- sendString = config[:password] + ':' + salty
17
- digestToSend = Digest::MD5.hexdigest(sendString)
18
- @context = validateUser(config[:partition], config[:user], digestToSend, session['MXSession']['sessionId'])
27
+ if config.has_key?(:context)
28
+ @context = JSON.parse(config[:context])
29
+ #Do a null check
30
+ begin
31
+ performDistRequest(:info, [])
32
+ rescue => e
33
+ @context = nil;
34
+ end
35
+ end
36
+ if (@context.nil?)
37
+ session = requestSession(config[:partition], config[:user])
38
+ salty = session['MXSession']['salty']
39
+ sendString = config[:password] + ':' + salty
40
+ digestToSend = Digest::MD5.hexdigest(sendString)
41
+ @context = validateUser(config[:partition], config[:user], digestToSend, session['MXSession']['sessionId'])
42
+ config[:context] = JSON.generate(@context)
43
+ end
19
44
  end
20
45
  def initialize
21
46
  @distMap = {
47
+ :info => [ 'user', 'INFO', 'response', []],
22
48
  :getData => [ 'user', 'GETDATA', 'data', [ { :pos => 0, :name => 'displayName' } ]],
23
49
  :getContent => [ 'user', 'GETCONTENT', 'content', [ { :pos => 0, :name => 'displayName' }]],
24
- :putData => ['user', 'PUTDATA', 'data', [ { :pos => 0, :name => 'data' }]],
25
- :putSimpleData => [ 'user', 'PUTSIMPLEDATA', 'data', [ { :pos => 0, :name => 'displayName'}, { :pos => 1, :name => 'content'}]],
50
+ :putData => ['user', 'PUTDATA', 'data', [ { :pos => 0, :name => 'data', :file => 1, :json => 1 }]],
51
+ :putSimpleData => [ 'user', 'PUTSIMPLEDATA', 'data', [ { :pos => 0, :name => 'displayName'}, { :pos => 1, :name => 'content', :file => 1}]],
26
52
  :query => [ 'user', 'QUERY', 'result', [ { :pos => 0, :name => 'index' }, { :pos => 1, :name => 'params' }]],
27
53
  :dynquery => ['user', 'DYNQUERY', 'result', [ { :pos => 0, :name => 'typeName' }, { :pos => 1, :name => 'filterFn' }, { :pos => 2, :name => 'mapFn' } ]],
28
54
  :deleteData => [ 'user', 'DELETEDATA', 'data', [ { :pos => 0, :name => 'displayName' } ]],
@@ -36,24 +62,64 @@ class Hmx
36
62
  { :pos => 1, :name => 'user' },
37
63
  { :pos => 2, :name => 'hashPassword' },
38
64
  { :pos => 3, :name => 'session' }
39
- ]]
40
-
65
+ ]],
66
+ :submitTask => [ 'user', 'SUBMITTASK', 'task', [ { :pos => 0, :name => 'task' } ]],
67
+ :runView => [ 'user', 'RUNVIEW', 'response', [ { :pos => 0, :name => 'view' }, { :pos => 1, :name => 'params' } ] ],
68
+ :getTypes => [ 'admin', 'GETTYPES', 'types', []],
69
+ :getType => [ 'admin', 'GETTYPE', 'type', [ { :pos => 0, :name => 'typeName' } ]],
70
+ :updateType => [ 'admin', 'UPDATETYPE', 'type', [ { :pos => 0, :name => 'type' }]],
71
+ :getFn => [ 'user', 'GETFN', 'fnContent', [ { :pos =>0, :name => 'fnName' } ]],
72
+ :putFn => [ 'user', 'PUTFN', 'response', [ { :pos => 0, :name => 'fnName' }, { :pos => 1, :name => 'fnContent', :file => 1 } ]],
73
+ :expireSessions => [ 'admin', 'EXPIRESESSIONS', 'sessions', []],
74
+ :getAllTasks => [ 'task', 'GETALLTASKS', 'tasks', []],
75
+ :getTask => [ 'task', 'GETTASK', 'task', [ { :pos=> 0, :name => 'taskId' } ]],
76
+ :purgeTasks => [ 'task', 'PURGETASKS', 'response', []],
77
+ :getFountainState => [ 'admin', 'GETFOUNTAINSTATE', 'state', []],
78
+ :setFountainId => [ 'admin', 'SETFOUNTAINID', 'dummy', [ { :pos => 0, :name => 'fountain' }, { :pos => 1, :name=> 'value' }]],
79
+ :getFountain => [ 'user', 'AUTOID', 'id', [ { :pos => 0, :name => 'fountain' } ]],
80
+ :bootstrap => ['special', 'BOOTSTRAP', 'response', []]
41
81
  }
42
82
  end
43
83
  def performDistRequest(cmd, args)
44
84
  spec = @distMap[cmd]
45
85
  hash = getSenderHash()
46
- spec[3].each { | param | hash[param[:name]] = args[param[:pos]] }
86
+ spec[3].each { | param |
87
+ # if the args has enough , use that
88
+ # otherwise, prompt for the parameter
89
+ if (args.length > param[:pos])
90
+ hash[param[:name]] = args[param[:pos]]
91
+ elsif (param.has_key?(:file) && !HmxClient::Command.fileIn.nil?)
92
+ # Content comes from file
93
+ realFile = File.expand_path(HmxClient::Command.fileIn)
94
+ puts "Reading #{ realFile } to get parameter #{ param[:name] }"
95
+ hash[param[:name]] = File.open(realFile) { | f |
96
+ c = ''
97
+ while(line = f.gets)
98
+ c = c + line
99
+ end
100
+ c
101
+ }
102
+ else
103
+ hash[param[:name]] = getFromUser("Please enter the value of #{ param[:name] }")
104
+ end
105
+ if (param.has_key?(:json))
106
+ hash[param[:name]] = JSON.parse(hash[param[:name]])
107
+ end
108
+ }
47
109
  standardRequest(spec[0], spec[1], hash, spec[2])
48
110
  end
49
111
  def performRequest(prefix, function, command)
50
- puts "Send data #{ JSON.generate(command) }" if $debug
112
+ puts "Send data #{ JSON.generate(command) }" if HmxClient::Command.debug?
51
113
  response = RestClient.post @urlapi+'/'+prefix, :function=>function, :params=>JSON.generate(command), :multipart=>true
52
- puts "Raw response is #{ response.to_str }" if $debug
114
+ puts "Raw response is #{ response.to_str }" if HmxClient::Command.debug?
53
115
  return JSON.parse(response.to_str)
54
116
  end
55
117
  def standardRequest(prefix, commandName, params, returnPart)
56
118
  response = performRequest(prefix, commandName, params)
119
+ puts "JSON response is #{ response }" if HmxClient::Command.debug?
120
+ if response['inError']
121
+ raise HmxException.new(response['exception']), "Error returned from server"
122
+ end
57
123
  return response[returnPart]
58
124
  end
59
125
  def getSenderHash()
@@ -61,16 +127,16 @@ class Hmx
61
127
  hash['context'] = @context if @context
62
128
  return hash
63
129
  end
64
- def getData(*args)
130
+ def getData(args)
65
131
  performDistRequest(:getData, args)
66
132
  end
67
- def getContent(*args)
133
+ def getContent(args)
68
134
  performDistRequest(:getContent, args)
69
135
  end
70
- def putData(*args)
136
+ def putData(args)
71
137
  performDistRequest(:putData, args)
72
138
  end
73
- def putSimpleData(*args)
139
+ def putSimpleData(args)
74
140
  performDistRequest(:putSimpleData, args)
75
141
  end
76
142
  def query(*args)
@@ -94,4 +160,47 @@ class Hmx
94
160
  def validateUser(*args)
95
161
  performDistRequest(:validateUser, args)
96
162
  end
163
+ def runView(*args)
164
+ performDistRequest(:runView, args)
165
+ end
166
+ def getTypes(*args)
167
+ performDistRequest(:getTypes, args)
168
+ end
169
+ def updateType(*args)
170
+ performDistRequest(:updateType, args)
171
+ end
172
+ def getType(*args)
173
+ performDistRequest(:getType, args)
174
+ end
175
+ def getFn(args)
176
+ performDistRequest(:getFn, args)
177
+ end
178
+ def putFn(args)
179
+ performDistRequest(:putFn, args)
180
+ end
181
+ def expireSessions(args)
182
+ performDistRequest(:expireSessions, args)
183
+ end
184
+ def getAllTasks(args)
185
+ performDistRequest(:getAllTasks, args)
186
+ end
187
+ def getTask(args)
188
+ performDistRequest(:getTask, args)
189
+ end
190
+ def purgeTasks(args)
191
+ performDistRequest(:purgeTasks, args)
192
+ end
193
+ def getFountainState(args)
194
+ performDistRequest(:getFountainState, args)
195
+ end
196
+ def setFountainId(args)
197
+ performDistRequest(:setFountainId, args)
198
+ end
199
+ def getFountain(args)
200
+ performDistRequest(:getFountain, args)
201
+ end
202
+ def bootstrap
203
+ performDistRequest(:bootstrap, [])
204
+ end
205
+ end
97
206
  end
data/lib/hmx/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module HmxClient
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
data/lib/hmx_client.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  base_path = File.dirname(__FILE__)
2
- %w{client hmx}.each do |lib|
2
+ %w{command client hmx helpers}.each do |lib|
3
3
  require "#{base_path}/hmx/#{lib}"
4
4
  end
5
5
  module HmxClient
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hmx_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-10-25 00:00:00.000000000Z
12
+ date: 2011-11-07 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rest-client
16
- requirement: &70272876666120 !ruby/object:Gem::Requirement
16
+ requirement: &70187035831060 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70272876666120
24
+ version_requirements: *70187035831060
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: json
27
- requirement: &70272876665520 !ruby/object:Gem::Requirement
27
+ requirement: &70187035830640 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70272876665520
35
+ version_requirements: *70187035830640
36
36
  description: Gives a client the ability to interact fully with the API for HMX
37
37
  email:
38
38
  - ukmoore@gmail.com
@@ -47,6 +47,24 @@ files:
47
47
  - bin/hmx
48
48
  - hmx_client.gemspec
49
49
  - lib/hmx/client.rb
50
+ - lib/hmx/command.rb
51
+ - lib/hmx/command/base.rb
52
+ - lib/hmx/command/bootstrap.rb
53
+ - lib/hmx/command/clone.rb
54
+ - lib/hmx/command/config.rb
55
+ - lib/hmx/command/dump.rb
56
+ - lib/hmx/command/fn.rb
57
+ - lib/hmx/command/fountain.rb
58
+ - lib/hmx/command/get.rb
59
+ - lib/hmx/command/getContent.rb
60
+ - lib/hmx/command/help.rb
61
+ - lib/hmx/command/query.rb
62
+ - lib/hmx/command/session.rb
63
+ - lib/hmx/command/task.rb
64
+ - lib/hmx/command/type.rb
65
+ - lib/hmx/command/user.rb
66
+ - lib/hmx/command/view.rb
67
+ - lib/hmx/helpers.rb
50
68
  - lib/hmx/hmx.rb
51
69
  - lib/hmx/version.rb
52
70
  - lib/hmx_client.rb