Class: Ubalo::Account
- Inherits:
-
Object
- Object
- Ubalo::Account
- Defined in:
- lib/ubalo/account.rb
Instance Attribute Summary collapse
-
#authorization ⇒ Object
Returns the value of attribute authorization.
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
-
#username ⇒ Object
Returns the value of attribute username.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #archive_from_json(archive_json) ⇒ Object
- #authorize(login, password) ⇒ Object
- #authorized? ⇒ Boolean
- #container_process_from_json(container_process_json) ⇒ Object
-
#initialize(base_url) ⇒ Account
constructor
A new instance of Account.
- #inspect ⇒ Object
- #pod_by_name(name) ⇒ Object
- #pod_by_name!(name) ⇒ Object
- #pod_from_json(pod_json) ⇒ Object
- #pods ⇒ Object
- #refresh! ⇒ Object
- #request(method, path = nil, options = {}) ⇒ Object
- #request_tasks(path, options = {}) ⇒ Object
- #running_tasks ⇒ Object
- #task_by_label!(label) ⇒ Object
- #task_from_json(task_json) ⇒ Object
- #tasks ⇒ Object
- #url_for(path) ⇒ Object
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
#authorization ⇒ Object
Returns the value of attribute authorization.
3 4 5 |
# File 'lib/ubalo/account.rb', line 3 def @authorization end |
#base_url ⇒ Object (readonly)
Returns the value of attribute base_url.
4 5 6 |
# File 'lib/ubalo/account.rb', line 4 def base_url @base_url end |
#username ⇒ Object
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, ] == [base_url, other.username, other.] 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 (login, password) response = request(:post, "/user/authorization", :params => {:user => {:login => 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
10 11 12 |
# File 'lib/ubalo/account.rb', line 10 def !!@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 |
#inspect ⇒ Object
135 136 137 |
# File 'lib/ubalo/account.rb', line 135 def inspect "#<Account #{username} #{base_url} #{'authorized' if }>" 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 |
#pods ⇒ Object
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.login_suggestion}" 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, = {} url = url_for(path) headers = ubalo_headers.merge({:accept => :json}) if and ![:skip_authorization] headers[:authorization] = end params = .delete(:params) || {} if Util.debug_mode? $stderr.puts "request method=#{method.inspect} url=#{url.inspect} 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 .delete(:parse) == false response else JSON.load(response) end rescue RestClient::Request::Unauthorized raise Ubalo::Error, "Your credentials are invalid. #{Util.login_suggestion}" rescue RestClient::BadRequest => e if = JSON.load(e.response.to_s)["upgrade_message"] raise Ubalo::Error, 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, = {}) request(:get, path, ).map do |task_json| task_from_json(task_json) end end |
#running_tasks ⇒ Object
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 |
#tasks ⇒ Object
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 |