Class: Strongspace::Client
Overview
A Ruby class to call the Strongspace REST API. You might use this if you want to manage your Strongspace apps from within a Ruby program, such as Capistrano.
Example:
require 'strongspace'
strongspace = Strongspace::Client.new('[email protected]', 'mypass')
Instance Attribute Summary collapse
-
#host ⇒ Object
Returns the value of attribute host.
-
#password ⇒ Object
Returns the value of attribute password.
-
#user ⇒ Object
Returns the value of attribute user.
Class Method Summary collapse
- .auth(user, password, host = 'https://www.strongspace.com') ⇒ Object
- .gem_version_string ⇒ Object
- .version ⇒ Object
Instance Method Summary collapse
-
#add_key(key) ⇒ Object
Add an ssh public key to the current user.
- #create_snapshot(space_name, snapshot_name) ⇒ Object
- #create_space(name, type = 'normal') ⇒ Object
-
#delete(uri, extra_headers = {}) ⇒ Object
:nodoc:.
- #delete_snapshot(space_name, snapshot_name) ⇒ Object
- #delete_space(space_name) ⇒ Object
-
#download(path) ⇒ Object
returns a tempfile to the loaded file.
-
#escape(value) ⇒ Object
:nodoc:.
- #filesystem ⇒ Object
-
#get(uri, extra_headers = {}) ⇒ Object
:nodoc:.
- #get_space(space_name) ⇒ Object
-
#initialize(user, password, host = 'https://www.strongspace.com') ⇒ Client
constructor
A new instance of Client.
-
#keys ⇒ Object
Get the list of ssh public keys for the current user.
- #login_token ⇒ Object
- #mkdir(path) ⇒ Object
-
#post(uri, payload = "", extra_headers = {}) ⇒ Object
:nodoc:.
- #process(method, uri, extra_headers = {}, payload = nil) ⇒ Object
-
#put(uri, payload, extra_headers = {}) ⇒ Object
:nodoc:.
-
#remove_all_keys ⇒ Object
Clear all keys on the current user.
-
#remove_key(key_id) ⇒ Object
Remove an existing ssh public key from the current user.
- #resource(uri) ⇒ Object
- #rm(path) ⇒ Object
- #size(path) ⇒ Object
- #snapshots(space_name) ⇒ Object
- #spaces ⇒ Object
-
#strongspace_headers ⇒ Object
:nodoc:.
- #upload(file, dest_path) ⇒ Object
- #username ⇒ Object
Constructor Details
#initialize(user, password, host = 'https://www.strongspace.com') ⇒ Client
Returns a new instance of Client.
41 42 43 44 45 |
# File 'lib/strongspace/client.rb', line 41 def initialize(user, password, host='https://www.strongspace.com') @user = user @password = password @host = host end |
Instance Attribute Details
#host ⇒ Object
Returns the value of attribute host.
18 19 20 |
# File 'lib/strongspace/client.rb', line 18 def host @host end |
#password ⇒ Object
Returns the value of attribute password.
18 19 20 |
# File 'lib/strongspace/client.rb', line 18 def password @password end |
#user ⇒ Object
Returns the value of attribute user.
18 19 20 |
# File 'lib/strongspace/client.rb', line 18 def user @user end |
Class Method Details
.auth(user, password, host = 'https://www.strongspace.com') ⇒ Object
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/strongspace/client.rb', line 20 def self.auth(user, password, host='https://www.strongspace.com') begin client = new(user, password, host) return JSON.parse client.get('/api/v1/api_token', :username => user, :password => password).to_s rescue RestClient::Request::Unauthorized => e raise Strongspace::Exceptions::InvalidCredentials rescue SocketError => e raise Strongspace::Exceptions::NoConnection end end |
.gem_version_string ⇒ Object
14 15 16 |
# File 'lib/strongspace/client.rb', line 14 def self.gem_version_string "strongspace-gem/#{version}" end |
.version ⇒ Object
10 11 12 |
# File 'lib/strongspace/client.rb', line 10 def self.version Strongspace::VERSION end |
Instance Method Details
#add_key(key) ⇒ Object
Add an ssh public key to the current user.
111 112 113 |
# File 'lib/strongspace/client.rb', line 111 def add_key(key) post("/api/v1/ssh_keys", :key => key).to_s end |
#create_snapshot(space_name, snapshot_name) ⇒ Object
95 96 97 |
# File 'lib/strongspace/client.rb', line 95 def create_snapshot(space_name, snapshot_name) doc = JSON.parse post("/api/v1/spaces/#{escape(space_name)}/snapshots", :name => snapshot_name) end |
#create_space(name, type = 'normal') ⇒ Object
78 79 80 |
# File 'lib/strongspace/client.rb', line 78 def create_space(name, type='normal') doc = JSON.parse post("/api/v1/spaces", :name => name, :type => type) end |
#delete(uri, extra_headers = {}) ⇒ Object
:nodoc:
148 149 150 |
# File 'lib/strongspace/client.rb', line 148 def delete(uri, extra_headers={}) # :nodoc: process(:delete, uri, extra_headers) end |
#delete_snapshot(space_name, snapshot_name) ⇒ Object
91 92 93 |
# File 'lib/strongspace/client.rb', line 91 def delete_snapshot(space_name, snapshot_name) doc = JSON.parse delete("/api/v1/spaces/#{escape(space_name)}/snapshots/#{escape(snapshot_name)}").to_s end |
#delete_space(space_name) ⇒ Object
74 75 76 |
# File 'lib/strongspace/client.rb', line 74 def delete_space(space_name) doc = JSON.parse delete("/api/v1/spaces/#{escape(space_name)}").to_s end |
#download(path) ⇒ Object
returns a tempfile to the loaded file
48 49 50 |
# File 'lib/strongspace/client.rb', line 48 def download(path) RestClient::Request.execute(:method => :get, :url => (@host + '/api/v1/files' + escape(path)), :user => @user, :password => @password, :raw_response => true, :headers => {:accept_encoding => ''}).file end |
#escape(value) ⇒ Object
:nodoc:
158 159 160 161 |
# File 'lib/strongspace/client.rb', line 158 def escape(value) # :nodoc: escaped = URI.escape(value.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]")) escaped.gsub('.', '%2E') # not covered by the previous URI.escape end |
#filesystem ⇒ Object
99 100 101 102 |
# File 'lib/strongspace/client.rb', line 99 def filesystem doc = JSON.parse get("/api/v1/filesystem").to_s doc["filesystem"] end |
#get(uri, extra_headers = {}) ⇒ Object
:nodoc:
136 137 138 |
# File 'lib/strongspace/client.rb', line 136 def get(uri, extra_headers={}) # :nodoc: process(:get, uri, extra_headers) end |
#get_space(space_name) ⇒ Object
82 83 84 |
# File 'lib/strongspace/client.rb', line 82 def get_space(space_name) doc = JSON.parse get("/api/v1/spaces/#{escape(space_name)}") end |
#keys ⇒ Object
Get the list of ssh public keys for the current user.
105 106 107 108 |
# File 'lib/strongspace/client.rb', line 105 def keys doc = JSON.parse get('/api/v1/ssh_keys') doc["ssh_keys"] end |
#login_token ⇒ Object
37 38 39 |
# File 'lib/strongspace/client.rb', line 37 def login_token doc = JSON.parse get('/api/v1/login_token') end |
#mkdir(path) ⇒ Object
57 58 59 |
# File 'lib/strongspace/client.rb', line 57 def mkdir(path) doc = post("/api/v1/files/#{escape(path[1..-1])}", :op => "mkdir") end |
#post(uri, payload = "", extra_headers = {}) ⇒ Object
:nodoc:
140 141 142 |
# File 'lib/strongspace/client.rb', line 140 def post(uri, payload="", extra_headers={}) # :nodoc: process(:post, uri, extra_headers, payload) end |
#process(method, uri, extra_headers = {}, payload = nil) ⇒ Object
152 153 154 155 156 |
# File 'lib/strongspace/client.rb', line 152 def process(method, uri, extra_headers={}, payload=nil) headers = strongspace_headers.merge(extra_headers) args = [method, payload, headers].compact response = resource(uri).send(*args) end |
#put(uri, payload, extra_headers = {}) ⇒ Object
:nodoc:
144 145 146 |
# File 'lib/strongspace/client.rb', line 144 def put(uri, payload, extra_headers={}) # :nodoc: process(:put, uri, extra_headers, payload) end |
#remove_all_keys ⇒ Object
Clear all keys on the current user.
121 122 123 |
# File 'lib/strongspace/client.rb', line 121 def remove_all_keys delete("/api/v1/ssh_keys").to_s end |
#remove_key(key_id) ⇒ Object
Remove an existing ssh public key from the current user.
116 117 118 |
# File 'lib/strongspace/client.rb', line 116 def remove_key(key_id) delete("/api/v1/ssh_keys/#{key_id}").to_s end |
#resource(uri) ⇒ Object
127 128 129 130 131 132 133 134 |
# File 'lib/strongspace/client.rb', line 127 def resource(uri) RestClient.proxy = ENV['HTTP_PROXY'] || ENV['http_proxy'] if uri =~ /^https?/ RestClient::Resource.new(uri, user, password) elsif host =~ /^https?/ RestClient::Resource.new(host, user, password)[uri] end end |
#rm(path) ⇒ Object
61 62 63 |
# File 'lib/strongspace/client.rb', line 61 def rm(path) doc = delete("/api/v1/files/#{escape(path[1..-1])}") end |
#size(path) ⇒ Object
65 66 67 |
# File 'lib/strongspace/client.rb', line 65 def size(path) doc = JSON.parse get("/api/v1/files/#{escape(path[1..-1])}?op=size") end |
#snapshots(space_name) ⇒ Object
86 87 88 89 |
# File 'lib/strongspace/client.rb', line 86 def snapshots(space_name) doc = JSON.parse get("/api/v1/spaces/#{escape(space_name)}/snapshots").to_s doc["snapshots"] end |
#spaces ⇒ Object
69 70 71 72 |
# File 'lib/strongspace/client.rb', line 69 def spaces doc = JSON.parse get('/api/v1/spaces') doc["spaces"] end |
#strongspace_headers ⇒ Object
:nodoc:
163 164 165 166 167 168 169 170 |
# File 'lib/strongspace/client.rb', line 163 def strongspace_headers # :nodoc: { 'X-Strongspace-API-Version' => '1', 'User-Agent' => self.class.gem_version_string, 'X-Ruby-Version' => RUBY_VERSION, 'X-Ruby-Platform' => RUBY_PLATFORM } end |
#upload(file, dest_path) ⇒ Object
52 53 54 55 |
# File 'lib/strongspace/client.rb', line 52 def upload(file, dest_path) r = resource(@host + '/api/v1/files/' + escape(dest_path[1..-1] + "/" + File.basename(file.path))) r.post(:file => file) end |
#username ⇒ Object
31 32 33 34 35 |
# File 'lib/strongspace/client.rb', line 31 def username return nil if !user self.user.split("/")[0] end |