Class: HerokuGarden::Client
- Inherits:
-
Object
- Object
- HerokuGarden::Client
- Defined in:
- lib/herokugarden/client.rb
Overview
A Ruby class to call the Heroku REST API. You might use this if you want to manage your Heroku apps from within a Ruby program, such as Capistrano.
Example:
require 'heroku'
heroku = HerokuGarden::Client.new('[email protected]', 'mypass')
heroku.create('myapp')
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
Instance Method Summary collapse
-
#add_collaborator(app_name, email, access = 'view') ⇒ Object
Invite a person by email address to collaborate on the app.
-
#add_key(key) ⇒ Object
Add an ssh public key to the current user.
-
#create(name = nil, options = {}) ⇒ Object
Create a new app, with an optional name.
-
#delete(uri) ⇒ Object
:nodoc:.
-
#destroy(name) ⇒ Object
Destroy the app permanently.
-
#escape(value) ⇒ Object
:nodoc:.
-
#get(uri, extra_headers = {}) ⇒ Object
:nodoc:.
-
#heroku_headers ⇒ Object
:nodoc:.
-
#info(name, saferoute = false) ⇒ Object
Show info such as mode, custom domain, and collaborators on an app.
-
#initialize(user, password, host = 'herokugarden.com') ⇒ Client
constructor
A new instance of Client.
-
#keys ⇒ Object
Get the list of ssh public keys for the current user.
-
#list ⇒ Object
Show a list of apps which you are a collaborator on.
-
#list_collaborators(app_name) ⇒ Object
Get a list of collaborators on the app, returns an array of hashes each of which contain :email and :access (=> edit | view) elements.
-
#post(uri, payload = "", extra_headers = {}) ⇒ Object
:nodoc:.
-
#put(uri, payload, extra_headers = {}) ⇒ Object
:nodoc:.
-
#rake(app_name, cmd) ⇒ Object
Run a rake command on the Heroku app.
-
#remove_all_keys ⇒ Object
Clear all keys on the current user.
-
#remove_collaborator(app_name, email) ⇒ Object
Remove a collaborator.
-
#remove_key(key) ⇒ Object
Remove an existing ssh public key from the current user.
- #resource(uri) ⇒ Object
-
#update(name, attributes) ⇒ Object
Update an app.
-
#update_collaborator(app_name, email, access) ⇒ Object
Change an existing collaborator.
-
#xml(raw) ⇒ Object
:nodoc:.
Constructor Details
#initialize(user, password, host = 'herokugarden.com') ⇒ Client
Returns a new instance of Client.
22 23 24 25 26 |
# File 'lib/herokugarden/client.rb', line 22 def initialize(user, password, host='herokugarden.com') @user = user @password = password @host = host end |
Instance Attribute Details
#host ⇒ Object
Returns the value of attribute host.
20 21 22 |
# File 'lib/herokugarden/client.rb', line 20 def host @host end |
#password ⇒ Object
Returns the value of attribute password.
20 21 22 |
# File 'lib/herokugarden/client.rb', line 20 def password @password end |
#user ⇒ Object
Returns the value of attribute user.
20 21 22 |
# File 'lib/herokugarden/client.rb', line 20 def user @user end |
Class Method Details
.version ⇒ Object
16 17 18 |
# File 'lib/herokugarden/client.rb', line 16 def self.version '0.4.2' end |
Instance Method Details
#add_collaborator(app_name, email, access = 'view') ⇒ Object
Invite a person by email address to collaborate on the app. Optional third parameter can be edit or view.
74 75 76 |
# File 'lib/herokugarden/client.rb', line 74 def add_collaborator(app_name, email, access='view') xml(post("/apps/#{app_name}/collaborators", { 'collaborator[email]' => email, 'collaborator[access]' => access })) end |
#add_key(key) ⇒ Object
Add an ssh public key to the current user.
97 98 99 |
# File 'lib/herokugarden/client.rb', line 97 def add_key(key) post("/user/keys", key, { 'Content-Type' => 'text/ssh-authkey' }) end |
#create(name = nil, options = {}) ⇒ Object
Create a new app, with an optional name.
44 45 46 47 48 |
# File 'lib/herokugarden/client.rb', line 44 def create(name=nil, ={}) params = {} params['app[name]'] = name if name xml(post('/apps', params)).elements["//app/name"].text end |
#delete(uri) ⇒ Object
:nodoc:
134 135 136 |
# File 'lib/herokugarden/client.rb', line 134 def delete(uri) # :nodoc: resource(uri).delete end |
#destroy(name) ⇒ Object
Destroy the app permanently.
59 60 61 |
# File 'lib/herokugarden/client.rb', line 59 def destroy(name) delete("/apps/#{name}") end |
#escape(value) ⇒ Object
:nodoc:
149 150 151 152 |
# File 'lib/herokugarden/client.rb', line 149 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 |
#get(uri, extra_headers = {}) ⇒ Object
:nodoc:
122 123 124 |
# File 'lib/herokugarden/client.rb', line 122 def get(uri, extra_headers={}) # :nodoc: resource(uri).get(heroku_headers.merge(extra_headers)) end |
#heroku_headers ⇒ Object
:nodoc:
138 139 140 141 142 143 |
# File 'lib/herokugarden/client.rb', line 138 def heroku_headers # :nodoc: { 'X-Heroku-API-Version' => '2', 'User-Agent' => "herokugarden-gem/#{self.class.version}", } end |
#info(name, saferoute = false) ⇒ Object
Show info such as mode, custom domain, and collaborators on an app.
35 36 37 38 39 40 41 |
# File 'lib/herokugarden/client.rb', line 35 def info(name, saferoute=false) doc = xml(get("/#{'safe' if saferoute}apps/#{name}")) attrs = { :collaborators => list_collaborators(name) } doc.elements.to_a('//app/*').inject(attrs) do |hash, element| hash[element.name.to_sym] = element.text; hash end end |
#keys ⇒ Object
Get the list of ssh public keys for the current user.
89 90 91 92 93 94 |
# File 'lib/herokugarden/client.rb', line 89 def keys doc = xml get('/user/keys') doc.elements.to_a('//keys/key').map do |key| key.elements['contents'].text end end |
#list ⇒ Object
Show a list of apps which you are a collaborator on.
29 30 31 32 |
# File 'lib/herokugarden/client.rb', line 29 def list doc = xml(get('/apps')) doc.elements.to_a("//apps/app/name").map { |a| a.text } end |
#list_collaborators(app_name) ⇒ Object
Get a list of collaborators on the app, returns an array of hashes each of which contain :email and :access (=> edit | view) elements.
65 66 67 68 69 70 |
# File 'lib/herokugarden/client.rb', line 65 def list_collaborators(app_name) doc = xml(get("/apps/#{app_name}/collaborators")) doc.elements.to_a("//collaborators/collaborator").map do |a| { :email => a.elements['email'].text, :access => a.elements['access'].text } end end |
#post(uri, payload = "", extra_headers = {}) ⇒ Object
:nodoc:
126 127 128 |
# File 'lib/herokugarden/client.rb', line 126 def post(uri, payload="", extra_headers={}) # :nodoc: resource(uri).post(payload, heroku_headers.merge(extra_headers)) end |
#put(uri, payload, extra_headers = {}) ⇒ Object
:nodoc:
130 131 132 |
# File 'lib/herokugarden/client.rb', line 130 def put(uri, payload, extra_headers={}) # :nodoc: resource(uri).put(payload, heroku_headers.merge(extra_headers)) end |
#rake(app_name, cmd) ⇒ Object
Run a rake command on the Heroku app.
112 113 114 |
# File 'lib/herokugarden/client.rb', line 112 def rake(app_name, cmd) post("/apps/#{app_name}/rake", cmd) end |
#remove_all_keys ⇒ Object
Clear all keys on the current user.
107 108 109 |
# File 'lib/herokugarden/client.rb', line 107 def remove_all_keys delete("/user/keys") end |
#remove_collaborator(app_name, email) ⇒ Object
Remove a collaborator.
84 85 86 |
# File 'lib/herokugarden/client.rb', line 84 def remove_collaborator(app_name, email) delete("/apps/#{app_name}/collaborators/#{escape(email)}") end |
#remove_key(key) ⇒ Object
Remove an existing ssh public key from the current user.
102 103 104 |
# File 'lib/herokugarden/client.rb', line 102 def remove_key(key) delete("/user/keys/#{escape(key)}") end |
#resource(uri) ⇒ Object
118 119 120 |
# File 'lib/herokugarden/client.rb', line 118 def resource(uri) RestClient::Resource.new("http://#{host}", user, password)[uri] end |
#update(name, attributes) ⇒ Object
Update an app. Available attributes:
:name => rename the app (changes http and git urls)
:public => true | false
:mode => production | development
54 55 56 |
# File 'lib/herokugarden/client.rb', line 54 def update(name, attributes) put("/apps/#{name}", :app => attributes) end |
#update_collaborator(app_name, email, access) ⇒ Object
Change an existing collaborator.
79 80 81 |
# File 'lib/herokugarden/client.rb', line 79 def update_collaborator(app_name, email, access) put("/apps/#{app_name}/collaborators/#{escape(email)}", { 'collaborator[access]' => access }) end |
#xml(raw) ⇒ Object
:nodoc:
145 146 147 |
# File 'lib/herokugarden/client.rb', line 145 def xml(raw) # :nodoc: REXML::Document.new(raw) end |