Skip to content

Commit

Permalink
adding the ability to save the Compose YAML representation of an app …
Browse files Browse the repository at this point in the history
…as an anonymous Gist
  • Loading branch information
dharmamike committed Jun 16, 2015
1 parent 90d6d4e commit b830d90
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 0 deletions.
8 changes: 8 additions & 0 deletions app/controllers/apps_controller.rb
Expand Up @@ -65,6 +65,14 @@ def compose_yml
end
end

def compose_gist
app = App.find(params[:id])
gist = Converters::AppConverter.new(app).to_compose_gist
html_url = gist[:html_url]
raw_url = gist[:files][Converters::AppConverter::DOCKER_COMPOSE_FILENAME][:raw_url]
render json: { links: { gist: { href: html_url, raw_url: raw_url } } }, status: 201
end

private

def app_update_params
Expand Down
11 changes: 11 additions & 0 deletions app/models/converters/app_converter.rb
@@ -1,5 +1,6 @@
module Converters
class AppConverter
DOCKER_COMPOSE_FILENAME = 'docker-compose.yml'

attr_reader :app

Expand All @@ -19,8 +20,18 @@ def to_compose_yaml
compose_hash.to_yaml
end

def to_compose_gist(filename=DOCKER_COMPOSE_FILENAME)
github_client.create_gist(description: 'created by Panamax',
public: true,
files: { filename => { content: to_compose_yaml } })
end

private

def github_client
@github_client ||= Octokit::Client.new
end

def service_to_image(service)
ServiceConverter.new(service).to_image
end
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Expand Up @@ -15,6 +15,7 @@
put :rebuild
post :template
get :compose_yml
post :compose_gist
end

resources :categories, only: [:index, :show, :create, :update, :destroy]
Expand Down
33 changes: 33 additions & 0 deletions spec/controllers/apps_controller_spec.rb
Expand Up @@ -324,4 +324,37 @@
expect(response.body).to eq('{"compose_yaml":"---\\\\n"}')
end
end

describe '#compose_gist' do
fixtures :services

let(:app) { App.first }
let(:converter_response) do
{
html_url: 'html',
files: { 'docker-compose.yml' => { raw_url: 'raw' } }
}
end
let(:converter) { double('converter', to_compose_gist: converter_response) }
let(:expected_response) { { links: { gist: { href: 'html', raw_url: 'raw' } } } }

before do
allow(Converters::AppConverter).to receive(:new).with(app).and_return(converter)
end

it 'invokes the app converter' do
expect(Converters::AppConverter).to receive(:new).with(app)
post :compose_gist, id: app.id, format: :json
end

it 'returns a created status' do
post :compose_gist, id: app.id, format: :json
expect(response.status).to eq(201)
end

it 'returns the gist URIs' do
post :compose_gist, id: app.id, format: :json
expect(response.body).to eq expected_response.to_json
end
end
end
22 changes: 22 additions & 0 deletions spec/models/converters/app_converter_spec.rb
Expand Up @@ -29,4 +29,26 @@
expect(rb.keys).to match_array apps(:app1).services.map(&:name)
end
end

context '#to_compose_gist' do
fixtures :apps, :services

let(:github_client) { double('github_client', create_gist: {}) }
let(:create_params) do
{
description: 'created by Panamax',
public: true,
files: { 'docker-compose.yml' => { content: subject.to_compose_yaml } }
}
end

before do
allow(Octokit::Client).to receive(:new).and_return(github_client)
end

it 'uses Octokit to create a new gist' do
expect(github_client).to receive(:create_gist).with(create_params)
subject.to_compose_gist
end
end
end

0 comments on commit b830d90

Please sign in to comment.