deis 0.0.1

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: 5d486dfde4bf3ddf99e7b268102d05d975e3230b
4
+ data.tar.gz: 969be0a9de1d8cab31ec775b08612dfd1a895b63
5
+ SHA512:
6
+ metadata.gz: 4e869cc7ca519b4a27f50e4c3df88f94c8ac786222deae52896feeb2a4878c879741eb5aea7ac61e341b12f8954e595dae3bb0f8f5a9bf28036141290c37bd71
7
+ data.tar.gz: 4a4d380222469535eda7faa4ffe70879286925f63a5fd1d1a977ea1e2b6403c75581f1205246e36a1e56deb354d1504af36d4a2a344ff2c911d2d80369bef577
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ group :test do
6
+ gem "awesome_print"
7
+ gem "faker"
8
+ gem "rspec", "~> 3.2"
9
+ gem 'pry-nav'
10
+ end
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Josh Lane
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.
@@ -0,0 +1,31 @@
1
+ # Deis
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'deis'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install deis
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it ( https://github.com/[my-github-username]/deis/fork )
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'deis/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "deis"
8
+ spec.version = Deis::VERSION
9
+ spec.authors = ["Josh Lane"]
10
+ spec.email = ["jlane@engineyard.com"]
11
+ spec.summary = %q{Ruby API Client for Deis Controller API}
12
+ spec.description = %q{http://docs.deis.io/en/v1.3.0/reference/api-v1.0/}
13
+ spec.homepage = "http://github.com/engineyard/deis"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
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.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+
24
+ spec.add_dependency "cistern", "~> 0.11"
25
+ spec.add_dependency "faraday", "~> 0.9"
26
+ spec.add_dependency "faraday_middleware"
27
+ spec.add_dependency "ey-logger", "~> 0.0"
28
+ end
@@ -0,0 +1,14 @@
1
+ require "deis/version"
2
+
3
+ require 'logger'
4
+
5
+ require 'cistern'
6
+ require 'faraday'
7
+ require 'faraday_middleware'
8
+ require 'ey/logger'
9
+ require 'ey/logger/faraday'
10
+
11
+ module Deis
12
+ end
13
+
14
+ require 'deis/client'
@@ -0,0 +1,22 @@
1
+ class Deis::Client < Cistern::Service
2
+ collection_path "deis/client/collections"
3
+ model_path "deis/client/models"
4
+ request_path "deis/client/requests"
5
+
6
+ collection :users
7
+ model :user
8
+ request :register_user
9
+
10
+ recognizes :url, :logger, :adapter, :username, :password, :token
11
+
12
+ requires :url
13
+
14
+ module Shared
15
+ def authenticated?
16
+ !! @auth_token
17
+ end
18
+ end
19
+ end
20
+
21
+ require 'deis/client/real'
22
+ require 'deis/client/mock'
@@ -0,0 +1,5 @@
1
+ class Deis::Client::Users < Cistern::Collection
2
+
3
+ model Deis::Client::User
4
+
5
+ end
@@ -0,0 +1,79 @@
1
+ class Deis::Client::Mock
2
+ include Deis::Client::Shared
3
+
4
+ attr_reader :username, :url, :password, :token
5
+
6
+ def self.data
7
+ @data ||= Hash.new { |h, url|
8
+ h[url] = {
9
+ :users => {},
10
+ }
11
+ }
12
+ end
13
+
14
+ def self.reset!
15
+ @data = nil
16
+ end
17
+
18
+ def self.serial_id
19
+ @current_id ||= 0
20
+ @current_id += 1
21
+ @current_id.to_s
22
+ end
23
+
24
+ def data
25
+ self.class.data[@url]
26
+ end
27
+
28
+ def serial_id
29
+ self.class.serial_id
30
+ end
31
+
32
+ def initialize(options={})
33
+ @url = options[:url]
34
+ @username, @password = options[:username], options[:password]
35
+ @auth_token = options[:token]
36
+ end
37
+
38
+ def response(options={})
39
+ method = options[:method] || :get
40
+ status = options[:status] || 200
41
+ path = options[:path]
42
+ body = options[:body]
43
+ url = options[:url] || File.join(@url, "api/v1", path)
44
+
45
+ request_headers = {
46
+ "Accept" => "application/json",
47
+ "Content-Type" => "application/json",
48
+ }
49
+
50
+ response_headers = {
51
+ "Content-Type" => "application/json",
52
+ "x_deis_api_version" => "1.1",
53
+ "x_deis_platform_version" => "1.2.2",
54
+ }
55
+
56
+ # request phase
57
+ # * :method - :get, :post, ...
58
+ # * :url - URI for the current request; also contains GET parameters
59
+ # * :body - POST parameters for :post/:put requests
60
+ # * :request_headers
61
+
62
+ # response phase
63
+ # * :status - HTTP response status code, such as 200
64
+ # * :body - the response body
65
+ # * :response_headers
66
+
67
+ env = Faraday::Env.from(
68
+ :method => method,
69
+ :url => URI.parse(url),
70
+ :body => body,
71
+ :request_headers => request_headers,
72
+ :response_headers => response_headers,
73
+ :status => status,
74
+ )
75
+
76
+ Faraday::Response::RaiseError.new.on_complete(env) ||
77
+ Faraday::Response.new(env)
78
+ end
79
+ end
@@ -0,0 +1,26 @@
1
+ class Deis::Client::User < Cistern::Model
2
+ identity :id, type: :integer
3
+
4
+ attribute :email, type: :string
5
+ attribute :first_name, type: :string
6
+ attribute :groups, type: :array
7
+ attribute :is_staff, type: :string
8
+ attribute :is_superuser, type: :string
9
+ attribute :last_name, type: :string
10
+ attribute :user_permissions, type: :array
11
+ attribute :username, type: :string
12
+
13
+ attr_accessor :password
14
+
15
+ def save
16
+ if new_record?
17
+ merge_attributes(
18
+ connection.register_user(
19
+ Cistern::Hash.stringify_keys(self.attributes.merge("password" => self.password))
20
+ ).body
21
+ )
22
+ else
23
+ raise NotImplementedError # not available ?
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,55 @@
1
+ class Deis::Client::Real
2
+
3
+ include Deis::Client::Shared
4
+
5
+ attr_accessor :username, :url, :logger
6
+
7
+ def initialize(options={})
8
+ @url = options.fetch(:url)
9
+ @logger = options[:logger] || Logger.new(nil)
10
+ adapter = options[:adapter] || Faraday.default_adapter
11
+ @username = options[:username]
12
+ password = options[:password]
13
+ token = options[:token]
14
+
15
+
16
+ @auth_token = password || token
17
+
18
+ connection_options = options[:connection_options] || {}
19
+
20
+ @connection = Faraday.new({url: @url}.merge(connection_options)) do |builder|
21
+ # response
22
+ if username && auth_token
23
+ builder.use Faraday::Request::BasicAuthentication, @username, @auth_token
24
+ end
25
+
26
+ builder.use Faraday::Response::RaiseError
27
+ builder.response :json
28
+
29
+ # request
30
+ builder.request :multipart
31
+ builder.request :json
32
+
33
+ builder.use Ey::Logger::Faraday, format: :machine, device: @logger, prefix: "deis"
34
+ builder.adapter adapter
35
+ end
36
+ end
37
+
38
+ def request(options={})
39
+ method = options[:method] || :get
40
+ url = options[:url] || File.join(@url, "v1", options.fetch(:path))
41
+ params = options[:params] || {}
42
+ body = options[:body]
43
+ headers = {
44
+ "Content-Type" => "application/json",
45
+ "Accept" => "application/json",
46
+ }.merge(options[:headers] || {})
47
+
48
+ @connection.send(method) do |req|
49
+ req.url(url)
50
+ req.headers.merge!(headers)
51
+ req.params.merge!(params)
52
+ req.body = body
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,59 @@
1
+ class Deis::Client::Real
2
+ # @see http://docs.deis.io/en/v1.3.0/reference/api-v1.0/#register-a-new-user
3
+ def register_user(params={})
4
+ request(
5
+ :path => "auth/register",
6
+ :method => :post,
7
+ :body => Cistern::Hash.slice(params, "username", "password", "email", "first_name", "last_name"),
8
+ )
9
+ end
10
+ end
11
+
12
+ class Deis::Client::Mock
13
+ def register_user(params={})
14
+ id = self.serial_id
15
+
16
+ unless params["username"]
17
+ response(
18
+ :body => {"username" => "This field is required."},
19
+ :status => 400,
20
+ )
21
+ end
22
+
23
+ unless params["password"]
24
+ response(
25
+ :body => {"password" => "This field is required."},
26
+ :status => 400,
27
+ )
28
+ end
29
+
30
+ user_params = Cistern::Hash.slice(params, "username", "password", "email", "first_name", "last_name").merge(
31
+ "id" => id,
32
+ "is_active" => true,
33
+ "is_staff" => false,
34
+ "groups" => [],
35
+ "user_permissions" => [],
36
+ "is_superuser" => false,
37
+ )
38
+
39
+ if self.data[:users].keys.empty?
40
+ user_params.merge!("is_staff" => true, "is_superuser" => true)
41
+ end
42
+
43
+ if self.data[:users].values.find { |u| u["username"] == user_params["username"] }
44
+ response(
45
+ :body => {"username" => "User with this Username already exists."},
46
+ :status => 400,
47
+ )
48
+ end
49
+
50
+ self.data[:users][id] = user_params
51
+
52
+ response(
53
+ :path => "/auth/register",
54
+ :method => :post,
55
+ :body => user_params,
56
+ :status => 201,
57
+ )
58
+ end
59
+ end
@@ -0,0 +1,3 @@
1
+ module Deis
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,23 @@
1
+ ENV['MOCK_DEIS'] ||= 'true'
2
+
3
+ Bundler.require(:test)
4
+
5
+ require File.expand_path("../../lib/deis", __FILE__)
6
+
7
+ Dir[File.expand_path("../{support,shared}/**/*.rb", __FILE__)].each {|f| require f}
8
+
9
+ if ENV["MOCK_DEIS"] == 'true'
10
+ Deis::Client.mock!
11
+ end
12
+
13
+ Cistern.formatter = Cistern::Formatter::AwesomePrint
14
+
15
+ RSpec.configure do |config|
16
+ if Deis::Client.mocking?
17
+ config.before(:each) { Deis::Client.reset! }
18
+ else
19
+ config.filter_run_excluding(mock_only: true)
20
+ end
21
+
22
+ config.order = "random"
23
+ end
@@ -0,0 +1,27 @@
1
+ require 'logger'
2
+
3
+ module ClientHelper
4
+ def create_anonymous_client(options={})
5
+ options.merge!(logger: Logger.new(STDOUT)) if ENV['VERBOSE']
6
+ options = {
7
+ :url => ENV["DEIS_URL"] || "https://deis.example.org",
8
+ }.merge(options)
9
+
10
+ Deis::Client.new(options)
11
+ end
12
+
13
+ def create_authorized_client(options={})
14
+ options.merge!(logger: Logger.new(STDOUT)) if ENV['VERBOSE']
15
+ options = {
16
+ :username => ENV["DEIS_USERNAME"],
17
+ :password => ENV["DEIS_PASSWORD"],
18
+ :url => ENV["DEIS_URL"] || "https://deis.example.org",
19
+ }.merge(options)
20
+
21
+ Deis::Client.new(options)
22
+ end
23
+ end
24
+
25
+ RSpec.configure do |config|
26
+ config.include(ClientHelper)
27
+ end
@@ -0,0 +1,53 @@
1
+ require 'spec_helper'
2
+
3
+ describe "as an anonymous client" do
4
+ let!(:client) { create_anonymous_client }
5
+
6
+ it "should create a user" do
7
+ username = SecureRandom.hex(6)
8
+ password = SecureRandom.hex(6)
9
+ email = Faker::Internet.email
10
+
11
+ client.users.create(email: email, username: username, password: password)
12
+
13
+ if Deis::Client.mocking?
14
+ user = client.data[:users].values.first
15
+
16
+ expect(user["email"]).to eq(email)
17
+ expect(user["username"]).to eq(username)
18
+ expect(user["password"]).to eq(password)
19
+ end
20
+ end
21
+
22
+ describe "#register_user" do
23
+ it "should create a new user" do
24
+ response = client.register_user(
25
+ "username" => (username = SecureRandom.hex(6)),
26
+ "password" => (password = SecureRandom.hex(6)),
27
+ "email" => Faker::Internet.email,
28
+ )
29
+
30
+ expect(response.status).to eq(201)
31
+
32
+ user = response.body
33
+
34
+ expect(user["username"]).to eq(username)
35
+ expect(user["password"]).to eq(password)
36
+
37
+ if Deis::Client.mocking?
38
+ expect(user["is_superuser"]).to eq(true)
39
+ expect(user["is_staff"]).to eq(true)
40
+ end
41
+
42
+ response = client.register_user(
43
+ "username" => SecureRandom.hex(6),
44
+ "password" => SecureRandom.hex(6),
45
+ )
46
+
47
+ another_user = response.body
48
+
49
+ expect(another_user["is_superuser"]).to eq(false)
50
+ expect(another_user["is_staff"]).to eq(false)
51
+ end
52
+ end
53
+ end
metadata ADDED
@@ -0,0 +1,149 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: deis
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Josh Lane
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: cistern
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.11'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.11'
55
+ - !ruby/object:Gem::Dependency
56
+ name: faraday
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.9'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.9'
69
+ - !ruby/object:Gem::Dependency
70
+ name: faraday_middleware
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: ey-logger
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.0'
97
+ description: http://docs.deis.io/en/v1.3.0/reference/api-v1.0/
98
+ email:
99
+ - jlane@engineyard.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - Gemfile
106
+ - LICENSE.txt
107
+ - README.md
108
+ - Rakefile
109
+ - deis.gemspec
110
+ - lib/deis.rb
111
+ - lib/deis/client.rb
112
+ - lib/deis/client/collections/users.rb
113
+ - lib/deis/client/mock.rb
114
+ - lib/deis/client/models/user.rb
115
+ - lib/deis/client/real.rb
116
+ - lib/deis/client/requests/register_user.rb
117
+ - lib/deis/version.rb
118
+ - spec/spec_helper.rb
119
+ - spec/support/client_helper.rb
120
+ - spec/users_spec.rb
121
+ homepage: http://github.com/engineyard/deis
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.2.2
142
+ signing_key:
143
+ specification_version: 4
144
+ summary: Ruby API Client for Deis Controller API
145
+ test_files:
146
+ - spec/spec_helper.rb
147
+ - spec/support/client_helper.rb
148
+ - spec/users_spec.rb
149
+ has_rdoc: