Class: WWTClient

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

Constant Summary collapse

Default_host =
"http://0.0.0.0"
Default_port =
"5050"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url = nil, port = nil) ⇒ WWTClient

Returns a new instance of WWTClient.



12
13
14
15
# File 'lib/WWT/WWTClient.rb', line 12

def initialize(url=nil,port=nil)
  @url  ||= Default_host
  @port ||= Default_port
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/WWT/WWTClient.rb', line 17

def  method_missing(method_name,*args)
  args ||= []
  data = args.first.delete(:data) || ""
  data = data.join(",") if data.class==Array
  args_list = make_arg_list(args.first.merge({:cmd=>method_name}))
  call_command args_list,data
end

Instance Attribute Details

#portObject

Returns the value of attribute port.



7
8
9
# File 'lib/WWT/WWTClient.rb', line 7

def port
  @port
end

#urlObject

Returns the value of attribute url.



7
8
9
# File 'lib/WWT/WWTClient.rb', line 7

def url
  @url
end

Instance Method Details

#call_command(args_list, data = "") ⇒ Object



30
31
32
33
# File 'lib/WWT/WWTClient.rb', line 30

def call_command(args_list,data="")
  parse_responce(RestClient.post "#{@url}:#{@port}/layerApi.aspx?#{args_list}", data)
  
end

#layersObject

not working just yet



48
49
50
# File 'lib/WWT/WWTClient.rb', line 48

def layers 
  self.layerlist :layersonly=>true
end

#look_at(subject) ⇒ Object



39
40
41
# File 'lib/WWT/WWTClient.rb', line 39

def look_at(subject)
  self.mode :lookat=>subject
end

#make_arg_list(args) ⇒ Object



35
36
37
# File 'lib/WWT/WWTClient.rb', line 35

def make_arg_list (args)
  args.to_a.inject(""){|str, arg| str+="&#{arg[0].to_s}=#{arg[1].to_s}"}
end

#move(command) ⇒ Object



43
44
45
# File 'lib/WWT/WWTClient.rb', line 43

def move(command)
  self.call_command( make_arg_list({:cmd=>:move, :move=>command}))
end

#new_layer(*args) ⇒ Object



25
26
27
28
# File 'lib/WWT/WWTClient.rb', line 25

def new_layer (*args)
  result = self.new args.first 
  return WWT_layer.new(self,result.first["newlayerid"][:content])
end

#parse_responce(responce) ⇒ Object



52
53
54
55
56
57
# File 'lib/WWT/WWTClient.rb', line 52

def parse_responce(responce)
    
    parse = Hpricot(responce)
    raise "WWT error #{parse.search('h2').inner_html}"if responce.match(/Error/)
    parse.search("layerapi").collect{|l| l.children.map{|c| {c.name =>{:content=>c.inner_html, :properties=>c.attributes}}}}.first
end