Class: UtopiaRuby::Client

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

Constant Summary collapse

@@protocol =
"http"
@@api_v =
"1.0"

Instance Method Summary collapse

Constructor Details

#initialize(host = "127.0.0.1", token = "", port = 22820) ⇒ Client

Returns a new instance of Client.



12
13
14
15
16
# File 'lib/utopialib.rb', line 12

def initialize(host = "127.0.0.1", token = "", port = 22820)
  @host  = host
  @token = token
  @port  = port
end

Instance Method Details

#ApiQuery(query_method = "GetSystemInfo") ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/utopialib.rb', line 26

def ApiQuery(query_method = "GetSystemInfo")
  query_array = {method: query_method, token: @token, params: nil}
  
  #puts @host #debug

  #return "" #debug

  endpoint = @@protocol + "://" + @host + ":" + @port.to_s + "/api/" + @@api_v + "/"
  #puts endpoint #debug


  begin
    uri = URI.parse(endpoint)
    header = {"Content-Type": "text/json"}
    http = Net::HTTP.new(uri.host, uri.port)
    request = Net::HTTP::Post.new(uri.request_uri, header)
    request.body = query_array.to_json
    response = http.request(request)
  rescue SocketError
    #puts "SocketError"

    return ""
  end

  return response.body
end

#GetSystemInfoObject



49
50
51
52
53
54
55
# File 'lib/utopialib.rb', line 49

def GetSystemInfo
  result = ApiQuery()
  if result == ""
    return {}
  end
  return JSON.parse(result)
end

#SetClientAPIVersion(api_v = "1.0") ⇒ Object



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

def SetClientAPIVersion(api_v = "1.0")
  @@api_v = api_v
end

#SetClientProtocol(protocol = "http") ⇒ Object



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

def SetClientProtocol(protocol = "http")
  @@protocol = protocol
end