Class: ServerDensity::Servers

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

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Servers

Returns a new instance of Servers.



57
58
59
# File 'lib/server_density.rb', line 57

def initialize(client)
  @client = client
end

Instance Method Details

#allObject



61
62
63
64
65
66
# File 'lib/server_density.rb', line 61

def all
  result = get("servers/list")
  if result.success?
    result.data['servers'].map { |data| Server.new(data) }
  end
end

#create(ip) ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'lib/server_density.rb', line 77

def create(ip)
  result = post("servers/add", {
    :name => ip,
    :ip   => ip
  })
  if result.success?
    Server.new(result.data.merge('ip' => ip))
  end
end

#find(ip) ⇒ Object



73
74
75
# File 'lib/server_density.rb', line 73

def find(ip)
  all.detect { |server| server.ip == ip }
end

#find_or_create(ip) ⇒ Object



68
69
70
71
# File 'lib/server_density.rb', line 68

def find_or_create(ip)
  server = find(ip)
  server ||= create(ip)
end

#get(command) ⇒ Object



87
88
89
90
91
92
93
94
95
# File 'lib/server_density.rb', line 87

def get(command)
  response = begin
    response = RestClient.get("https://#{@client.username}:#{@client.password}@api.serverdensity.com/1.0/?account=#{@client.}&c=#{command}")
  rescue
    puts "ERROR" # TODO polish
    exit(1)
  end
  CommandResult.new(response)
end

#post(command, params) ⇒ Object



97
98
99
100
# File 'lib/server_density.rb', line 97

def post(command, params)
  response = RestClient.post("https://#{@client.username}:#{@client.password}@api.serverdensity.com/1.0/?account=#{@client.}&c=#{command}", params)
  CommandResult.new(response)
end