Class: UnitHosting::Agent

Inherits:
Mechanize
  • Object
show all
Defined in:
lib/unit-hosting/agent.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAgent

Returns a new instance of Agent.



8
9
10
11
12
# File 'lib/unit-hosting/agent.rb', line 8

def initialize
  super
  @endpoint = "https://cloud.unit-hosting.com"
  max_history = 0
end

Instance Attribute Details

#endpointObject

Returns the value of attribute endpoint.



7
8
9
# File 'lib/unit-hosting/agent.rb', line 7

def endpoint
  @endpoint
end

Instance Method Details

#getr(path) ⇒ Object



14
15
16
# File 'lib/unit-hosting/agent.rb', line 14

def getr path
  get(@endpoint + path)
end

#group(id) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/unit-hosting/agent.rb', line 41

def group(id)
  getr("/my/group/#{id}/info")
  group = Group.new(id)
  group.key  = page.at("span.api-key").text.to_s
  group.name = page.at("span.group-name").text.to_s
  group
end

#groupsObject



49
50
51
52
53
54
# File 'lib/unit-hosting/agent.rb', line 49

def groups
  getr("/my/group")
  page.search("#server-groups .instance_id a").collect { |a|
    group(a.text.to_s)
  }.extend(Groups)
end

#login(id, password) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/unit-hosting/agent.rb', line 18

def  id,password
  getr("/login")
  form = page.forms[0]
  form.fields.find{|f| f.name == "username" }.value = id
  form.fields.find{|f| f.name == "password" }.value = password
  submit(form)
end

#login?Boolean

Returns:

  • (Boolean)


32
33
34
35
# File 'lib/unit-hosting/agent.rb', line 32

def login?
  getr("/dashboard")
  /ログアウト/ =~ page_body # => OK
end

#logoutObject



37
38
39
# File 'lib/unit-hosting/agent.rb', line 37

def logout
  getr("/logout")
end

#page_body(enc = 'utf-8') ⇒ Object



26
27
28
29
30
# File 'lib/unit-hosting/agent.rb', line 26

def page_body enc='utf-8'
  body = page.body
  body.force_encoding(enc) if body.respond_to?(:force_encoding)
  body
end