Class: Ubalo::Account

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url) ⇒ Account

Returns a new instance of Account.



6
7
8
# File 'lib/ubalo/account.rb', line 6

def initialize(base_url)
  @base_url = base_url
end

Instance Attribute Details

#authorizationObject

Returns the value of attribute authorization.



3
4
5
# File 'lib/ubalo/account.rb', line 3

def authorization
  @authorization
end

#base_urlObject (readonly)

Returns the value of attribute base_url.



4
5
6
# File 'lib/ubalo/account.rb', line 4

def base_url
  @base_url
end

#usernameObject

Returns the value of attribute username.



3
4
5
# File 'lib/ubalo/account.rb', line 3

def username
  @username
end

Instance Method Details

#==(other) ⇒ Object



139
140
141
# File 'lib/ubalo/account.rb', line 139

def ==(other)
  [base_url, username, authorization] == [base_url, other.username, other.authorization]
end

#archive_from_json(archive_json) ⇒ Object



87
88
89
# File 'lib/ubalo/account.rb', line 87

def archive_from_json(archive_json)
  PodArchive.create_with_json(self, archive_json)
end

#authorize(login, password) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/ubalo/account.rb', line 65

def authorize(, password)
  response = request(:post, "/user/authorization", :params => {:user => {:login => , :password => password}}, :skip_authorization => true)
  @username = response.fetch("username")
  @authorization = response.fetch("authorization")
rescue RestClient::BadRequest
  raise Ubalo::Error, "Incorrect login or password"
end

#authorized?Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/ubalo/account.rb', line 10

def authorized?
  !!@authorization
end

#container_process_from_json(container_process_json) ⇒ Object



91
92
93
# File 'lib/ubalo/account.rb', line 91

def container_process_from_json(container_process_json)
  ContainerProcess.create_with_json(self, container_process_json)
end

#inspectObject



135
136
137
# File 'lib/ubalo/account.rb', line 135

def inspect
  "#<Account #{username} #{base_url} #{'authorized' if authorization}>"
end

#pod_by_name(name) ⇒ Object



101
102
103
104
105
106
107
108
109
# File 'lib/ubalo/account.rb', line 101

def pod_by_name(name)
  pod_username, pod_name = Util.normalize_pod_name(username, name)

  unless pod_name =~ /\A[\w-]+\Z/
    raise Error, "Pod name is invalid"
  end

  Pod.new(self, pod_username, pod_name, {})
end

#pod_by_name!(name) ⇒ Object



111
112
113
114
# File 'lib/ubalo/account.rb', line 111

def pod_by_name!(name)
  pod = pod_by_name(name)
  pod.refresh!
end

#pod_from_json(pod_json) ⇒ Object



79
80
81
# File 'lib/ubalo/account.rb', line 79

def pod_from_json(pod_json)
  Pod.create_with_json(self, pod_json)
end

#podsObject



95
96
97
98
99
# File 'lib/ubalo/account.rb', line 95

def pods
  request(:get, "/pods").map do |pod_json|
    pod_from_json(pod_json)
  end
end

#refresh!Object



73
74
75
76
77
# File 'lib/ubalo/account.rb', line 73

def refresh!
  update_attributes(request(:get, "/users/#{username}"))
rescue RestClient::ResourceNotFound
  raise Ubalo::Error, "Your credentials for #{username} are invalid. #{Util.}"
end

#request(method, path = nil, options = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ubalo/account.rb', line 14

def request method, path = nil, options = {}
  url = url_for(path)
  headers = ubalo_headers.merge({:accept => :json})

  if authorization and !options[:skip_authorization]
    headers[:authorization] = authorization
  end

  params = options.delete(:params) || {}

  if Util.debug_mode?
    $stderr.puts "request method=#{method.inspect} url=#{url.inspect} options=#{options.inspect} headers=#{headers.inspect} params=#{params.inspect}"
  end

  resource = RestClient::Resource.new url, :timeout => 60
  case method
  when :get
    response = resource.get headers.merge(:params => params)
  when :post
    response = resource.post params, headers
  when :put
    response = resource.put params, headers
  when :delete
    response = resource.delete headers
  else
    raise "don't understand request method #{method.inspect}"
  end

  if response.code == 204
    nil
  elsif options.delete(:parse) == false
    response
  else
    JSON.load(response)
  end
rescue RestClient::Request::Unauthorized
  raise Ubalo::Error, "Your credentials are invalid. #{Util.}"
rescue RestClient::BadRequest => e
  if message = JSON.load(e.response.to_s)["upgrade_message"]
    raise Ubalo::Error, message
  else
    raise e
  end
rescue RestClient::BadGateway, RestClient::ServiceUnavailable => e
  raise Ubalo::Error, "Ubalo is unable to handle your request at this time"
end

#request_tasks(path, options = {}) ⇒ Object



129
130
131
132
133
# File 'lib/ubalo/account.rb', line 129

def request_tasks(path, options = {})
  request(:get, path, options).map do |task_json|
    task_from_json(task_json)
  end
end

#running_tasksObject



121
122
123
# File 'lib/ubalo/account.rb', line 121

def running_tasks
  request_tasks("/users/#{username}/tasks", :params => {:state => :running})
end

#task_by_label!(label) ⇒ Object



116
117
118
119
# File 'lib/ubalo/account.rb', line 116

def task_by_label!(label)
  task = Task.new(self, label, {})
  task.refresh!
end

#task_from_json(task_json) ⇒ Object



83
84
85
# File 'lib/ubalo/account.rb', line 83

def task_from_json(task_json)
  Task.create_with_json(self, task_json)
end

#tasksObject



125
126
127
# File 'lib/ubalo/account.rb', line 125

def tasks
  request_tasks("/tasks")
end

#url_for(path) ⇒ Object



61
62
63
# File 'lib/ubalo/account.rb', line 61

def url_for(path)
  "#{@base_url}#{path}"
end