qs-heroku-client 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/CHANGELOG.md ADDED
@@ -0,0 +1,7 @@
1
+ # 0.0.2 / 2013-08-02
2
+
3
+ * Moves everything under the ``Qs`` namespace
4
+
5
+ # 0.0.1 / 2013-08-02
6
+
7
+ * The beginning
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in heroku-client.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Thorben Schröder
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,34 @@
1
+ # Qs::Heroku::Client
2
+
3
+ A library to use Heroku's [Platform API](https://devcenter.heroku.com/articles/platform-api-reference).
4
+
5
+ **Currently only retrieving and scaling dynos is supported!**
6
+
7
+ ## Setup a client
8
+
9
+ Create a client using your Heroku email address and the corresponding [API token](https://dashboard.heroku.com/account#show-api-key).
10
+
11
+ ```ruby
12
+ require 'qs/heroku/client'
13
+ client = Qs::Heroku::Client.new('your-heroku-mail@example.com', 'your-heroku-api-token')
14
+ ```
15
+
16
+ ## Retrieve the current setup of an app
17
+
18
+ You can see the number and size of all dyno types (web, worker, etc) via the [formation](https://devcenter.heroku.com/articles/platform-api-reference#formation) of an app.
19
+
20
+ ```ruby
21
+ app = client.app('your-app-name')
22
+ app.formation
23
+ # => {"web"=>{"quantity"=>2, "size"=>"1x"}, "worker"=>{"quantity"=>1, "size"=>"1x"}, "console"=>{"quantity"=>0, "size"=>"1x"}, "rake"=>{"quantity"=>0, "size"=>"1x"}}
24
+ ```
25
+
26
+ ## Scale dynos of an app
27
+
28
+ You can scale the number of dynos and change the dyno size of a certain type of process.
29
+
30
+ ```ruby
31
+ # scales the web process to 2 dynos of the size 1X
32
+ app.scale('web', 2, '1x')
33
+ # => {"web"=>{"quantity"=>2, "size"=>"1x"}, "worker"=>{"quantity"=>1, "size"=>"1x"}, "console"=>{"quantity"=>0, "size"=>"1x"}, "rake"=>{"quantity"=>0, "size"=>"1x"}}
34
+ ```
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'qs/heroku/client/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "qs-heroku-client"
8
+ spec.version = Qs::Heroku::Client::VERSION
9
+ spec.authors = ["Thorben Schröder"]
10
+ spec.email = ["stillepost@gmail.com"]
11
+ spec.description = %q{A library to use Heroku's Platform API}
12
+ spec.summary = %q{A library to use Heroku's Platform API}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+
24
+ spec.add_dependency 'faraday'
25
+ end
@@ -0,0 +1,18 @@
1
+ require "qs/heroku/client/version"
2
+ require "qs/heroku/client/error"
3
+ require "qs/heroku/client/connection"
4
+ require "qs/heroku/client/app"
5
+
6
+ module Qs
7
+ module Heroku
8
+ class Client
9
+ def initialize(email, token)
10
+ @connection = Connection.new(email, token)
11
+ end
12
+
13
+ def app(id)
14
+ App.new(id, @connection)
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,47 @@
1
+ require 'cgi'
2
+ require 'json'
3
+
4
+ module Qs
5
+ module Heroku
6
+ class Client
7
+ class App
8
+ SIZES = {
9
+ '1x' => 1,
10
+ '2x' => 2
11
+ }
12
+
13
+ def initialize(id, connection)
14
+ @id = id
15
+ @connection = connection
16
+ end
17
+
18
+ def formation
19
+ response = @connection.request(:get, "/apps/#{CGI.escape(@id)}/formation")
20
+ parse_formation(response)
21
+ end
22
+
23
+ def scale(type, quantity, size = nil)
24
+ options = {'quantity' => quantity}
25
+ if size
26
+ raise Error.new("Unknown size: #{size}!") unless SIZES[size]
27
+ options['size'] = SIZES[size]
28
+ end
29
+ response = @connection.request(:patch, "/apps/#{CGI.escape(@id)}/formation/#{CGI.escape(type)}", options)
30
+ parse_formation(response)
31
+ end
32
+
33
+ protected
34
+ def parse_formation(response)
35
+ raise Error.new("Could not retrieve formation. Status: #{response.status}") unless [200,201].include?(response.status)
36
+ data = JSON.parse(response.body)
37
+ Hash[data.map do |format|
38
+ [format['type'], {
39
+ 'quantity' => format['quantity'],
40
+ 'size' => SIZES.key(format['size'])
41
+ }]
42
+ end]
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,43 @@
1
+ require 'json'
2
+ require 'cgi'
3
+ require 'faraday'
4
+
5
+ module Qs
6
+ module Heroku
7
+ class Client
8
+ class Connection
9
+ API_BASE = 'https://api.heroku.com/'
10
+
11
+ def initialize(email, token)
12
+ @email = email
13
+ @token = token
14
+
15
+ @conn = Faraday.new(:url => API_BASE) do |faraday|
16
+ faraday.adapter Faraday.default_adapter
17
+ faraday.basic_auth(email, token)
18
+ end
19
+ end
20
+
21
+ def request(method, path, options = nil)
22
+ body = nil
23
+ if options
24
+ case method.to_s.downcase
25
+ when 'post', 'put', 'patch'
26
+ body = JSON.dump(options)
27
+ else
28
+ path += '?'
29
+ path += options.map {|k,v| "#{CGI.escape(k)}=#{CGI.escape(v)}"}.map("&")
30
+ end
31
+ end
32
+
33
+ @conn.send(method) do |req|
34
+ req.url path
35
+ req.headers['Content-Type'] = 'application/json'
36
+ req.headers['Accept'] = 'application/vnd.heroku+json; version=3'
37
+ req.body = body if body
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,8 @@
1
+ module Qs
2
+ module Heroku
3
+ class Client
4
+ class Error < StandardError
5
+ end
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,7 @@
1
+ module Qs
2
+ module Heroku
3
+ class Client
4
+ VERSION = "0.0.2"
5
+ end
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: qs-heroku-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Thorben Schröder
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-08-02 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: faraday
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: A library to use Heroku's Platform API
63
+ email:
64
+ - stillepost@gmail.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - CHANGELOG.md
71
+ - Gemfile
72
+ - LICENSE.txt
73
+ - README.md
74
+ - Rakefile
75
+ - heroku-client.gemspec
76
+ - lib/qs/heroku/client.rb
77
+ - lib/qs/heroku/client/app.rb
78
+ - lib/qs/heroku/client/connection.rb
79
+ - lib/qs/heroku/client/error.rb
80
+ - lib/qs/heroku/client/version.rb
81
+ homepage: ''
82
+ licenses:
83
+ - MIT
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ! '>='
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ requirements: []
101
+ rubyforge_project:
102
+ rubygems_version: 1.8.25
103
+ signing_key:
104
+ specification_version: 3
105
+ summary: A library to use Heroku's Platform API
106
+ test_files: []