heroku-postgres 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 108013517e3988f1fa34af6d42c0120fa5d4f42e
4
+ data.tar.gz: 56fec2f6854e863a0a46e46f88e220d72ed8a004
5
+ SHA512:
6
+ metadata.gz: de79699b8a7b51ad3c2d4b465bdba091dbdd40ba9aa5d661617c586f00a33b789356360bd4b464b29cc52d7b92054ebb44c6169f1899a9b118258f71965eb252
7
+ data.tar.gz: 39972ea78c1130c093ba6959da344443196c5abcfb4fa91d1d3133e0b84f4ff6ae72a89d723da53e306189b0e90d9d356044af855a495ce9977b3815996e154a
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1 @@
1
+ ruby-2.2.2
@@ -0,0 +1,28 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all
4
+ people who contribute through reporting issues, posting feature requests,
5
+ updating documentation, submitting pull requests or patches, and other
6
+ activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, age, or religion.
12
+
13
+ Examples of unacceptable behavior by participants include the use of sexual
14
+ language or imagery, derogatory comments or personal attacks, trolling, public
15
+ or private harassment, insults, or other unprofessional conduct.
16
+
17
+ Project maintainers have the right and responsibility to remove, edit, or
18
+ reject comments, commits, code, wiki edits, issues, and other contributions
19
+ that are not aligned to this Code of Conduct. Project maintainers who do not
20
+ follow the Code of Conduct may be removed from the project team.
21
+
22
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
23
+ reported by opening an issue or contacting one or more of the project
24
+ maintainers.
25
+
26
+ This Code of Conduct is adapted from the [Contributor
27
+ Covenant](http:contributor-covenant.org), version 1.0.0, available at
28
+ [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in heroku-postgres.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Nick Charlton
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,69 @@
1
+ # heroku-postgres
2
+
3
+ This provides a Ruby client to [Heroku][]'s [Postgres][] service. It's been
4
+ partially extracted from the [Heroku command line client][heroku-cli] as it's
5
+ otherwise not documented.
6
+
7
+ It was primarily built to automate creating backups for usage elsewhere. It's
8
+ initially limited to just being able to do this, so if there's something
9
+ missing, open an issue. It's also pretty lacking in tests for similar reasons.
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'heroku-postgres'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install heroku-postgres
26
+
27
+ ## Usage
28
+
29
+ ```ruby
30
+ require 'heroku/postgres'
31
+
32
+ # login (return a client object, but you don't need that directly)
33
+ Heroku::Postgres.login('username', 'password')
34
+
35
+ # find the database by app name and database name
36
+ db = Heroku::Postgres.find('app-name', 'db-name')
37
+
38
+ # have a look at the current backups
39
+ db.backups
40
+ # => [<Heroku::Postgres::Backup id=abc>]
41
+
42
+ # capture a backup, waiting for completion
43
+ # optionally, pass true to poll until complete
44
+ backup = db.capture_backup(true)
45
+
46
+ # or, pass a block to monitor the response whilst it's created
47
+ backup = db.capture_backup(true) do |backup|
48
+ puts backup
49
+ end
50
+
51
+ # generate a public url so you can fetch the file
52
+ backup.public_url
53
+ ```
54
+
55
+ ## Contributing
56
+
57
+ 1. Fork it ( https://github.com/nickcharlton/heroku-postgres/fork )
58
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
59
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
60
+ 4. Push to the branch (`git push origin my-new-feature`)
61
+ 5. Create a new Pull Request
62
+
63
+ ## Author
64
+
65
+ Copyright (c) 2015 Nick Charlton <nick@nickcharlton.net>.
66
+
67
+ [Heroku]: https://www.heroku.com/home
68
+ [Postgres]: https://www.heroku.com/postgres
69
+ [heroku-cli]: https://github.com/heroku/heroku
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'heroku/postgres'
5
+
6
+ require 'pry'
7
+ Pry.start
@@ -0,0 +1,5 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'heroku/postgres/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'heroku-postgres'
8
+ spec.version = Heroku::Postgres::VERSION
9
+ spec.authors = ['Nick Charlton']
10
+ spec.email = ['nick@nickcharlton.net']
11
+
12
+ spec.summary = 'Ruby library for interacting with Heroku Postgres.'
13
+ spec.description = 'Ruby library for interacting with Heroku Postgres.'
14
+ spec.homepage = 'https://github.com/nickcharlton/heroku-postgres'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_dependency 'excon', '~> 0.45'
23
+ spec.add_dependency 'json', '~> 1.8'
24
+
25
+ spec.add_development_dependency 'bundler', '~> 1.8'
26
+ spec.add_development_dependency 'rake', '~> 10.0'
27
+ spec.add_development_dependency 'rspec', '~> 3.2'
28
+ spec.add_development_dependency 'pry', '~> 0.10'
29
+ end
@@ -0,0 +1 @@
1
+ require 'heroku/postgres'
@@ -0,0 +1,45 @@
1
+ require 'excon'
2
+ require 'json'
3
+ require 'base64'
4
+
5
+ require 'heroku/postgres/version'
6
+ require 'heroku/postgres/client'
7
+ require 'heroku/postgres/resource'
8
+ require 'heroku/postgres/backup'
9
+ require 'heroku/postgres/database'
10
+
11
+ # A container for Heroku extensions.
12
+ module Heroku
13
+ # A container for Postgres.
14
+ module Postgres
15
+ attr_accessor :client, :pg_client
16
+
17
+ def self.client
18
+ @client
19
+ end
20
+
21
+ def self.pg_client
22
+ @pg_client
23
+ end
24
+
25
+ # Login to Heroku Postgres.
26
+ #
27
+ # This also configures the clients for both of the APIs we're talking to.
28
+ def self.login(username, password)
29
+ @client = Client.new(username: username, password: password)
30
+ @client.login
31
+
32
+ # setup the Heroku Postgres client
33
+ @pg_client = Client.new(url: 'https://postgres-api.heroku.com',
34
+ username: username, password: password)
35
+ @pg_client.token = @client.token
36
+
37
+ @client
38
+ end
39
+
40
+ # A shortcut for finding databases.
41
+ def self.find(app_name, db_name)
42
+ Database.find(app_name, db_name)
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,77 @@
1
+ module Heroku
2
+ module Postgres
3
+ # Representation and handling of Backup objects
4
+ class Backup < Resource
5
+ attr_accessor :app
6
+
7
+ attr_accessor :uuid, :num, :from_name, :from_type, :from_url, :to_name,
8
+ :to_type, :to_url, :options, :source_bytes,
9
+ :processed_bytes, :succeeded, :created_at, :started_at,
10
+ :canceled_at, :updated_at, :finished_at, :deleted_at,
11
+ :purged_at, :num_keep
12
+
13
+ def self.find(app_name, backup_name)
14
+ response = Postgres.pg_client.get("/apps/#{app_name}/transfers/"\
15
+ "#{backup_name}?verbose=true")
16
+
17
+ backup = new(JSON.parse(response.body))
18
+ backup.app = app_name
19
+
20
+ backup
21
+ end
22
+
23
+ def self.all_for_app(app_name)
24
+ response = Postgres.pg_client.get("/apps/#{app_name}/transfers")
25
+
26
+ # build backup objects, but only those generated with pg_dump
27
+ backups = JSON.parse(response.body).collect do |c|
28
+ next if c['from_type'] != 'pg_dump'
29
+
30
+ backup = new(c)
31
+ backup.app = app_name
32
+
33
+ backup
34
+ end.compact!
35
+
36
+ backups
37
+ end
38
+
39
+ def self.capture(app_name, db_name, poll = false, &block)
40
+ response = Postgres.pg_client.post("/databases/#{db_name}/backups")
41
+
42
+ backup = new(JSON.parse(response.body))
43
+ backup.app = app_name
44
+
45
+ if poll
46
+ finished_at = nil
47
+
48
+ until finished_at
49
+ sleep(5) # stop for 5s
50
+ backup.reload
51
+ finished_at = backup.finished_at
52
+
53
+ yield backup if block_given?
54
+ end
55
+ end
56
+
57
+ backup
58
+ end
59
+
60
+ def reload
61
+ response = Postgres.pg_client.get("/apps/#{app}/transfers/#{uuid}")
62
+ update_with_response(response.body)
63
+
64
+ self
65
+ end
66
+
67
+ # Generate a public URL for accessing this backup.
68
+ def public_url
69
+ response = Postgres.pg_client.post("/apps/#{app}/transfers/#{num}"\
70
+ '/actions/public-url')
71
+
72
+ hash = JSON.parse(response.body)
73
+ hash['url']
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,74 @@
1
+ module Heroku
2
+ module Postgres
3
+ # Client for interacting with Heroku & Heroku's Postgres API.
4
+ class Client
5
+ DEFAULT_HEADERS = { 'User-Agent' => 'Heroku-Postgres/v' \
6
+ "#{Heroku::Postgres::VERSION}" }
7
+
8
+ attr_accessor :url, :username, :password, :token
9
+
10
+ def initialize(opts = {})
11
+ @url = opts.fetch(:url, 'https://api.heroku.com')
12
+ @username = opts.fetch(:username, {})
13
+ @password = opts.fetch(:password, {})
14
+ end
15
+
16
+ # Login on behalf of the user.
17
+ #
18
+ # This is necessary to setup the class correctly so that the API key can
19
+ # be used.
20
+ #
21
+ # @return [String] the Heroku API key
22
+ def login
23
+ response = login_request(username, password)
24
+
25
+ hash = JSON.parse(response.body)
26
+ @token = hash['api_key']
27
+ end
28
+
29
+ %w(get post put delete).each do |m|
30
+ define_method m do |path, opts = {}|
31
+ request(m.to_sym, path, opts)
32
+ end
33
+ end
34
+
35
+ private
36
+
37
+ def login_request(username, password)
38
+ Excon.post('https://api.heroku.com/login',
39
+ headers: DEFAULT_HEADERS,
40
+ query: { username: username, password: password },
41
+ expects: [200])
42
+ end
43
+
44
+ def request(method, path, opts = {})
45
+ body, query, headers = parse_opts(opts)
46
+
47
+ # set the default headers
48
+ headers.merge!(DEFAULT_HEADERS)
49
+
50
+ # set the authentication header
51
+ headers['Authorization'] = "Basic #{authentication_header}"
52
+
53
+ connection = Excon.new(@url)
54
+ connection.request(method: method, path: "/client/v11#{path}",
55
+ body: body, query: query, headers: headers)
56
+ end
57
+
58
+ def parse_opts(opts)
59
+ # this might actually want to be a form
60
+ body = opts.fetch(:body, nil)
61
+ body = JSON.dump(body) if body && body.is_a?(Hash)
62
+
63
+ query = opts.fetch(:query, {})
64
+ headers = opts.fetch(:headers, {})
65
+
66
+ [body, query, headers]
67
+ end
68
+
69
+ def authentication_header
70
+ Base64.urlsafe_encode64("#{@username}:#{@token}")
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,29 @@
1
+ module Heroku
2
+ module Postgres
3
+ # Representation and handling of Database objects
4
+ class Database < Resource
5
+ attr_accessor :app, :db
6
+ attr_accessor :database_name, :database_user, :database_password,
7
+ :resource_url
8
+
9
+ def self.find(app_name, db_name)
10
+ response = Postgres.pg_client.get("/databases/#{db_name}")
11
+
12
+ db = new(JSON.parse(response.body))
13
+
14
+ db.db = db_name
15
+ db.app = app_name
16
+
17
+ db
18
+ end
19
+
20
+ def capture_backup(poll = false, &block)
21
+ Backup.capture(app, db, poll, &block)
22
+ end
23
+
24
+ def backups
25
+ Backup.all_for_app(app)
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,29 @@
1
+ module Heroku
2
+ module Postgres
3
+ # Base class for representing resources.
4
+ class Resource
5
+ def initialize(hash = {})
6
+ hash.each { |k, v| send("#{k}=", v) if respond_to?("#{k}=") }
7
+ end
8
+
9
+ def update_with_response(o, except_key = [])
10
+ hash = JSON.parse(o)
11
+
12
+ # remove keys that shouldn't be included
13
+ except_key.each { |k| hash.delete(k) }
14
+
15
+ hash.each { |k, v| send("#{k}=", v) if respond_to?("#{k}=") }
16
+ end
17
+
18
+ def to_hash
19
+ attrs = instance_variables.map { |v| v.to_s.sub(/^@/, '') }
20
+ Hash[attrs.select { |v| respond_to? v }.map { |v| [v.to_sym, send(v)] }]
21
+ end
22
+
23
+ def inspect
24
+ objects = to_hash.map { |k, v| "#{k}=#{v.inspect}" }.join(' ')
25
+ "#<#{self.class.name}:#{object_id} #{objects}>"
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,5 @@
1
+ module Heroku
2
+ module Postgres
3
+ VERSION = '1.0.0'
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,145 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: heroku-postgres
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Nick Charlton
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-07-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: excon
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.45'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.45'
27
+ - !ruby/object:Gem::Dependency
28
+ name: json
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.8'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.8'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.8'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.8'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.2'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.2'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.10'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.10'
97
+ description: Ruby library for interacting with Heroku Postgres.
98
+ email:
99
+ - nick@nickcharlton.net
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".ruby-version"
106
+ - CODE_OF_CONDUCT.md
107
+ - Gemfile
108
+ - LICENSE.txt
109
+ - README.md
110
+ - Rakefile
111
+ - bin/console
112
+ - bin/setup
113
+ - heroku-postgres.gemspec
114
+ - lib/heroku-postgres.rb
115
+ - lib/heroku/postgres.rb
116
+ - lib/heroku/postgres/backup.rb
117
+ - lib/heroku/postgres/client.rb
118
+ - lib/heroku/postgres/database.rb
119
+ - lib/heroku/postgres/resource.rb
120
+ - lib/heroku/postgres/version.rb
121
+ homepage: https://github.com/nickcharlton/heroku-postgres
122
+ licenses:
123
+ - MIT
124
+ metadata: {}
125
+ post_install_message:
126
+ rdoc_options: []
127
+ require_paths:
128
+ - lib
129
+ required_ruby_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ required_rubygems_version: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ requirements: []
140
+ rubyforge_project:
141
+ rubygems_version: 2.4.5
142
+ signing_key:
143
+ specification_version: 4
144
+ summary: Ruby library for interacting with Heroku Postgres.
145
+ test_files: []