# 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
# File lib/heroku-postgresql/client.rb, line 31 def get_database http_get @database_sha end
# File lib/heroku-postgresql/client.rb, line 35 def get_wait_status http_get "#{@database_sha}/wait_status" end
# File lib/heroku-postgresql/client.rb, line 19 def ingress http_put "#{@database_sha}/ingress" end
# File lib/heroku-postgresql/client.rb, line 23 def reset http_put "#{@database_sha}/reset" end
# File lib/heroku-postgresql/client.rb, line 27 def rotate_credentials http_put "#{@database_sha}/rotate_credentials" end
# File lib/heroku-postgresql/client.rb, line 39 def unfollow http_put "#{@database_sha}/unfollow" end
# 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
# 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
# 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
# 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
# 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
# File lib/heroku-postgresql/client.rb, line 45 def sha(url) Digest::SHA2.hexdigest url end
# 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