Class: Wizarddev::Heroku::PlatformClient

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/wizarddev/heroku/platform_client.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(oauth_key, app) ⇒ PlatformClient

Returns a new instance of PlatformClient.



20
21
22
23
24
25
26
# File 'lib/wizarddev/heroku/platform_client.rb', line 20

def initialize(oauth_key, app)
  @app = app
  self.class.headers(
    'Accept' => 'application/vnd.heroku+json; version=3',
    'Authorization' => "Bearer #{oauth_key}"
  )
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



9
10
11
# File 'lib/wizarddev/heroku/platform_client.rb', line 9

def app
  @app
end

Class Method Details

.load_authObject



16
17
18
# File 'lib/wizarddev/heroku/platform_client.rb', line 16

def self.load_auth
  auth = Netrc.read['api.heroku.com']
end

.local_auth(app) ⇒ Object



12
13
14
# File 'lib/wizarddev/heroku/platform_client.rb', line 12

def self.local_auth(app)
  new(load_auth.password, app)
end

Instance Method Details

#latest_releaseObject



28
29
30
31
32
33
34
35
36
37
# File 'lib/wizarddev/heroku/platform_client.rb', line 28

def latest_release
  resp = self.class.get(
    "/apps/#{app}/releases",
    headers: {
      'Range' => 'version ..; order=desc, max=1;'
    }
  )
  raise "Error fetching latest release: #{response.body}" unless resp.success?
  resp.first
end

#restart_allObject



39
40
41
# File 'lib/wizarddev/heroku/platform_client.rb', line 39

def restart_all
  self.class.delete("/apps/#{app}/dynos")
end

#run(cmd) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/wizarddev/heroku/platform_client.rb', line 43

def run(cmd)
  cmdstr = { attach: 'true', command: cmd }.to_json
  opt = {
    body: cmdstr,
    headers: { 'Content-Type' => 'application/json' }
  }
  response = self.class.post("/apps/#{app}/dynos", opt)
  session = Rendezvous.new(
    input: StringIO.new,
    output: StringIO.new,
    url: response['attach_url']
  )
  session.start
  session.output.rewind
  session.output.read
end

#run_with_code(cmd) ⇒ Object



60
61
62
63
64
# File 'lib/wizarddev/heroku/platform_client.rb', line 60

def run_with_code(cmd)
  cmd = "#{cmd}; echo $?"
  *output, code = run(cmd).split("\n")
  [output.join("\n"), code.to_i]
end