class HerokuPostgresql::Client

Constants

Version

Public Class Methods

new(url) click to toggle source
# File lib/heroku-postgresql/client.rb, line 10
def initialize(url)
  @heroku_postgresql_host = ENV["HEROKU_POSTGRESQL_HOST"] || "https://shogun.heroku.com"
  @database_sha = sha(url)
  @heroku_postgresql_resource = RestClient::Resource.new(
    "#{@heroku_postgresql_host}/client/v10/databases",
    :headers =>  { :x_heroku_gem_version  => Heroku::Client.version }
    )
end

Public Instance Methods

get_database() click to toggle source
# File lib/heroku-postgresql/client.rb, line 31
def get_database
  http_get @database_sha
end
get_wait_status() click to toggle source
# File lib/heroku-postgresql/client.rb, line 35
def get_wait_status
  http_get "#{@database_sha}/wait_status"
end
ingress() click to toggle source
# File lib/heroku-postgresql/client.rb, line 19
def ingress
  http_put "#{@database_sha}/ingress"
end
reset() click to toggle source
# File lib/heroku-postgresql/client.rb, line 23
def reset
  http_put "#{@database_sha}/reset"
end
rotate_credentials() click to toggle source
# File lib/heroku-postgresql/client.rb, line 27
def rotate_credentials
  http_put "#{@database_sha}/rotate_credentials"
end
unfollow() click to toggle source
# File lib/heroku-postgresql/client.rb, line 39
def unfollow
  http_put "#{@database_sha}/unfollow"
end

Protected Instance Methods

checking_client_version() { || ... } click to toggle source
# File lib/heroku-postgresql/client.rb, line 59
def checking_client_version
  begin
    yield
  rescue RestClient::BadRequest => e
    if message = json_decode(e.response.to_s)["upgrade_message"]
      abort(message)
    else
      raise e
    end
  end
end
display_heroku_warning(response) click to toggle source
# File lib/heroku-postgresql/client.rb, line 71
def display_heroku_warning(response)
  warning = response.headers[:x_heroku_warning]
  display warning if warning
  response
end
http_get(path) click to toggle source
# File lib/heroku-postgresql/client.rb, line 77
def http_get(path)
  checking_client_version do
    retry_on_exception(RestClient::Exception) do
      response = @heroku_postgresql_resource[path].get
      display_heroku_warning response
      sym_keys(json_decode(response.to_s))
    end
  end
end
http_post(path, payload = {}) click to toggle source
# File lib/heroku-postgresql/client.rb, line 87
def http_post(path, payload = {})
  checking_client_version do
    response = @heroku_postgresql_resource[path].post(json_encode(payload))
    display_heroku_warning response
    sym_keys(json_decode(response.to_s))
  end
end
http_put(path, payload = {}) click to toggle source
# File lib/heroku-postgresql/client.rb, line 95
def http_put(path, payload = {})
  checking_client_version do
    response = @heroku_postgresql_resource[path].put(json_encode(payload))
    display_heroku_warning response
    sym_keys(json_decode(response.to_s))
  end
end
sha(url) click to toggle source
# File lib/heroku-postgresql/client.rb, line 45
def sha(url)
  Digest::SHA2.hexdigest url
end
sym_keys(c) click to toggle source
# File lib/heroku-postgresql/client.rb, line 49
def sym_keys(c)
  if c.is_a?(Array)
    c.map { |e| sym_keys(e) }
  else
    c.inject({}) do |h, (k, v)|
      h[k.to_sym] = v; h
    end
  end
end