Class: HmxClient::Client

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

Defined Under Namespace

Classes: CommandInvalid

Constant Summary collapse

FILE =
File.expand_path("~/.hmxConfig")

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Client

Returns a new instance of Client.



7
8
9
10
# File 'lib/hmx/client.rb', line 7

def initialize(args)
  @args = args
  loadConfig!
end

Instance Method Details

#configObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/hmx/client.rb', line 38

def config
  case prop = @args.shift
   when "user"
     storeConfig(:user, @args.shift)
   when "password"
     storeConfig(:password, Digest::MD5.hexdigest(@args.shift)) # Really ask on the command line later
   when "url"
     storeConfig(:url, @args.shift)
   when "apiUrl"
     storeConifg(:apiKey, @args.shift)
   when "api"
     storeConfig(:api, @args.shift)
   when "partition"
     storeConfig(:partition, @args.shift)
   when "proxy"
	 proxy = @args.shift
     storeConfig(:proxy, proxy)
     RestClient.proxy =  proxy
   else
     abort "Unknown config command"
  end
end

#deleteDataObject



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

def deleteData
  h = getapi
  h.deleteData(@args.shift)
end

#dumpTypeObject

Take all of the documents for a type and put it into the folder passed, one per file



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/hmx/client.rb', line 96

def dumpType
  h = getapi
  displayNames = h.query(@args.shift, nil)
  puts displayNames
  displayNames.each { | displayName | 
                            fileName = $folder + "/" + displayName
                            puts "FileName will be #{ fileName } "
                            Dir.mkdir(File.dirname(fileName)) if !Dir.exist?(File.dirname(fileName))
                            puts "DisplayName is #{ displayName } "
                            File.open(fileName, 'w') { |f| f.write(h.getData([displayName])) } }
end

#genout(content) ⇒ Object



60
61
62
63
64
# File 'lib/hmx/client.rb', line 60

def genout(content)
  puts content
  File.open($outFile, 'w') { | f | puts "And writing to #{$outFile} "
                                f.write(content) } unless $outFile.nil?
end

#getObject



65
66
67
68
# File 'lib/hmx/client.rb', line 65

def get
  h = getapi
  genout JSON.pretty_generate(h.getData(@args))
end

#getapiObject



33
34
35
36
37
# File 'lib/hmx/client.rb', line 33

def getapi
  hmx = Hmx.new
  hmx.(@config)
  hmx
end

#getDataObject



69
70
71
72
# File 'lib/hmx/client.rb', line 69

def getData
  h = getapi
  genout h.getContent(@args)
end

#getFnObject



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

def getFn
  h = getapi
  genout h.getFn(@args)
end

#getTypeObject



137
138
139
140
# File 'lib/hmx/client.rb', line 137

def getType
  h = getapi
  genout JSON.pretty_generate(h.getType(@args.shift))
end

#getTypesObject



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

def getTypes
  h = getapi
  genout JSON.pretty_generate(h.getTypes) 
end

#loadConfig!Object



14
15
16
17
18
19
20
21
22
# File 'lib/hmx/client.rb', line 14

def loadConfig!
	# Load the config from the save file
    @config = if File.exist?(FILE)
         File.open(FILE) { |file| Marshal.load(file) }
else
   {}
end
   RestClient.proxy = @config[:proxy] if @config.has_key?(:proxy)
end

#loadTypeObject

Take all of the documents in a folder for a type (the opposite of dumpType above) and simply put them back in



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/hmx/client.rb', line 109

def loadType
  h = getapi
  typeName = @args.shift
  folderName = $folder + "/" + typeName
  Dir.foreach(folderName) { | f | 
   puts "Working with #{ f }"
                               fullFile = folderName + "/" + f
                               if (File.file?(fullFile))
                                  content = File.open(fullFile) { | h | 
		c = '';
		while(line = h.gets) 
			c = c + line
		end
	        c
                                            }
                                  puts "Content is #{ content } "
                                  h.putData(JSON.parse(content))	
   end
}
end

#putObject



77
78
79
80
# File 'lib/hmx/client.rb', line 77

def put
  h = getapi
  genout JSON.pretty_generate(h.putData(@args))
end

#putFnObject



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

def putFn
  h = getapi
  genout h.putFn(@args)
end

#putSimpleDataObject



73
74
75
76
# File 'lib/hmx/client.rb', line 73

def putSimpleData
  h = getapi
  genout JSON.pretty_generate(h.putSimpleData(@args))
end

#queryObject



81
82
83
84
# File 'lib/hmx/client.rb', line 81

def query
  h = getapi
  genout h.query(@args.shift, nil)
end

#run!Object

Raises:



28
29
30
31
32
# File 'lib/hmx/client.rb', line 28

def run!
  command = @args.shift || @options[:command]
  raise CommandInvalid unless command && respond_to?(command)
  send(command)
end

#storeConfig(keyName, keyValue) ⇒ Object



23
24
25
26
27
# File 'lib/hmx/client.rb', line 23

def storeConfig(keyName, keyValue)
   # Update the config hashMap and persist it
   @config[keyName] = keyValue
   File.open(FILE, 'w+') { |f| Marshal.dump(@config, f) }
end

#userObject

Add a user, given a name (more can be added through a low level api)



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/hmx/client.rb', line 150

def user
  h = getapi
  case command = @args.shift
    when "add" 
  	   username = @args.shift
       password = Digest::MD5::hexdigest(@args.shift)

       user = { "MXUser" => { "hashPassword" => password , "apiKey" => false, "userName" => username, "fullName" => username }}
       genout JSON.pretty_generate(h.putSimpleData([ "sys.user/#{ username}", JSON.generate(user)]))
   when "delete"
       username = @args.shift
       h.deleteData("sys.user/#{username}")
   when "list"
       puts h.query("sys.user", nil)
   end
end

#viewObject



85
86
87
88
89
90
91
92
93
94
# File 'lib/hmx/client.rb', line 85

def view
  h = getapi
  viewData = h.runView(@args.shift, JSON.parse(@args.shift))
  resp = ''
  viewData.each { | line | 
                     line.each { | cell | resp = resp + "%20.20s\t" % cell }
                     resp = resp + "\n"
                }
  genout resp
end