backdrop 0.0.8 → 0.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 03e8c9bdd322c60e34a4f1d3fe5f1173d47b3ae2
4
- data.tar.gz: 0dd41d8928c2280cf52c318d15b4050f7b01f5da
3
+ metadata.gz: a475e27a7d3fc87705af139f517a6e00ed727e9f
4
+ data.tar.gz: 6ec0698f911273e27b9075256a54e2b572f16ce2
5
5
  SHA512:
6
- metadata.gz: 89a5dc5b3ed5b7842cc432c2b5ccb0315f4f1f69f2cfd62f8cde0905984246757b5d970a852750563484aabcd1d684cda8c3e7345371b8f9f6a45f8736adca03
7
- data.tar.gz: 29fd2c92f71ecb53bd0ec838ef8e02a809bbfa083e1cb98fe8a7865e3a11efc4756d21a1d286040adb002cc75e800081712268fd72de6da6134e320ba4858402
6
+ metadata.gz: b2aad5ee00d310efbd1f987aa1f9a8dbd5057248e6ae11c43c138eb894e1e56b8b1e68d20d297951803e440f89c3cba330253a212298406ff53d7aaf462ced7d
7
+ data.tar.gz: 046999fe5ae52cdc7a5ba1bf300e326c5a2f3e1576dcde5f05cdad7f6a8aac1245a355555e8f6d1769ae93bdefe8c6619e583cb998d3de5e249a3cd5c847e4f3
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ fixtures
2
+ *.swp
3
+ *.gem
4
+ Gemfile.lock
5
+ .rvmrc
6
+ lib/tasks/fixtures.rake
7
+ /tmp
8
+ /log
9
+ /doc
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,5 @@
1
+ Documentation:
2
+ Enabled: false
3
+ AllCops:
4
+ Exclude:
5
+ - 'vendor/**/*'
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use --create ruby-2.1.2@backdrop
data/.travis.yml ADDED
@@ -0,0 +1,9 @@
1
+ language: ruby
2
+ branches:
3
+ only:
4
+ - master
5
+ rvm:
6
+ - 2.1.2
7
+ - 2.0.0
8
+ script:
9
+ - bundle exec rake
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ group :test do
6
+ gem 'rake_rack'
7
+ gem 'rspec'
8
+ gem 'rubocop'
9
+ gem 'simplecov', require: false
10
+ end
data/README.md ADDED
@@ -0,0 +1,40 @@
1
+ Backdrop [![Code Climate](https://codeclimate.com/github/tpbowden/Backdrop/badges/gpa.svg)](https://codeclimate.com/github/tpbowden/Backdrop) [![Gem Version](https://badge.fury.io/rb/backdrop.svg)](http://badge.fury.io/rb/backdrop) [![Build Status](https://travis-ci.org/tpbowden/Backdrop.svg?branch=master)](https://travis-ci.org/tpbowden/Backdrop) [![Dependency Status](https://gemnasium.com/tpbowden/Backdrop.svg)](https://gemnasium.com/tpbowden/Backdrop)
2
+ ========
3
+
4
+ Detects all APIs required and generates stubs based on RAD documentation
5
+
6
+ Installation
7
+ ------------
8
+
9
+ Gemfile
10
+
11
+ gem 'backdrop'
12
+
13
+ config/routes.rb
14
+
15
+ mount Backdrop::App => '/backdrop'
16
+
17
+ Ensure your APIs are generating JSON docs
18
+
19
+ RspecApiDocumentation.configure do |config|
20
+ config.format = :json
21
+ end
22
+
23
+
24
+ Generating Stubs
25
+ ----------------
26
+
27
+ Create a rake task with an array of paths to your API projects
28
+
29
+ lib/tasks/backdrop.rake
30
+
31
+ require 'backdrop/rake_task'
32
+
33
+ Backdrop::RakeTask.new('output_directory', ['/paths/to/projects'])
34
+
35
+ Call `rake backdrop` to create all RAD APIs in the specified output directory
36
+
37
+ Dependencies
38
+ ============
39
+
40
+ * Ruby >= 2.0.0
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ Dir.glob('lib/tasks/*.rake').each { |task| import task }
data/backdrop.gemspec ADDED
@@ -0,0 +1,16 @@
1
+ $LOAD_PATH.push File.expand_path('../lib', __FILE__)
2
+ require 'backdrop/version'
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = 'backdrop'
6
+ s.version = Backdrop::VERSION
7
+ s.date = '2014-09-16'
8
+ s.summary = 'Fixture generation using RAD'
9
+ s.description = 'Fixture generation using RAD'
10
+ s.authors = ['Tom Bowden', 'Richard Vickerstaff', 'Adam Whittingham']
11
+ s.files = `git ls-files`.split($/)
12
+ s.homepage = 'http://github.com/tpbowden/backdrop'
13
+ s.license = 'MIT'
14
+ s.add_dependency 'sinatra', '~> 1.4'
15
+ s.add_dependency 'rake', '~> 10.1'
16
+ end
data/config.ru ADDED
@@ -0,0 +1,2 @@
1
+ require './sonar'
2
+ run Sonar
@@ -0,0 +1,19 @@
1
+ require 'json'
2
+
3
+ module Backdrop
4
+ class ApiBuilder
5
+ def initialize(output_dir)
6
+ @output_dir = output_dir
7
+ end
8
+
9
+ def build_get(project, data)
10
+ file = data['route']
11
+ response = data['requests'].first['response_body']
12
+ path = File.dirname(file)
13
+ filename = File.basename(file)
14
+ filepath = File.join(@output_dir, project, path)
15
+ FileUtils.mkdir_p filepath
16
+ File.write("#{File.join(filepath, filename)}.json", response)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,17 @@
1
+ require 'sinatra/base'
2
+
3
+ module Backdrop
4
+ class App < Sinatra::Base
5
+ files = Dir['fixtures/**/*']
6
+
7
+ files.each do |file|
8
+ route = file.gsub 'fixtures', ''
9
+ route = route.gsub '.json', ''
10
+
11
+ get "#{route}" do
12
+ content_type :json
13
+ File.read File.expand_path(file)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,24 @@
1
+ require 'json'
2
+ require 'backdrop/api_builder'
3
+
4
+ module Backdrop
5
+ class Extractor
6
+ def project_name(path)
7
+ path.gsub('/workspace', '').match(/\/(\w+)$/).captures.first
8
+ end
9
+
10
+ def load_docs(project_path)
11
+ docs_dir = File.join(File.expand_path(project_path), 'doc/api')
12
+ Dir["#{docs_dir}/**/*.json"]
13
+ .reject { |filename| filename =~ /index\.json$/ }
14
+ end
15
+
16
+ def build_apis(project, rad_json, output_dir)
17
+ builder = Backdrop::ApiBuilder.new output_dir
18
+ rad_json.each do |json_file|
19
+ json = JSON.parse File.read(json_file)
20
+ builder.build_get(project, json) if json['http_method'] == 'GET'
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,14 @@
1
+ require 'rake'
2
+ require 'rake/tasklib'
3
+ require 'backdrop/runner'
4
+
5
+ module Backdrop
6
+ class RakeTask < Rake::TaskLib
7
+ def initialize(output_dir, projects)
8
+ desc 'Run backdrop'
9
+ task :backdrop do
10
+ Backdrop::Runner.new(output_dir, projects).run
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,20 @@
1
+ require 'backdrop/extractor'
2
+
3
+ module Backdrop
4
+ class Runner
5
+ def initialize(output_dir, projects)
6
+ @projects = projects
7
+ @output_dir = output_dir
8
+ end
9
+
10
+ def run
11
+ FileUtils.rm_rf @output_dir
12
+ extractor = Backdrop::Extractor.new
13
+ @projects.each do |project_path|
14
+ project_name = extractor.project_name(project_path)
15
+ json_to_emulate = extractor.load_docs(project_path)
16
+ extractor.build_apis project_name, json_to_emulate, @output_dir
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,3 @@
1
+ module Backdrop
2
+ VERSION = '0.0.9'
3
+ end
data/lib/backdrop.rb CHANGED
@@ -1,4 +1 @@
1
1
  require 'backdrop/app'
2
-
3
- dir = File.expand_path('./tasks', File.dirname(__FILE__))
4
- Dir.glob("#{dir}/backdrop.rake").each { |r| import r }
@@ -0,0 +1,8 @@
1
+ require 'rake_rack'
2
+
3
+ task default: [
4
+ :rubocop,
5
+ :"rake_rack:rspec",
6
+ :"rake_rack:coverage:check_specs",
7
+ :"rake_rack:ok_rainbow"
8
+ ]
@@ -0,0 +1,3 @@
1
+ require 'rubocop/rake_task'
2
+
3
+ RuboCop::RakeTask.new
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+ require 'backdrop/api_builder'
3
+ require 'tmpdir'
4
+
5
+ describe Backdrop::ApiBuilder do
6
+ before do
7
+ @output_dir = Dir.mktmpdir('output')
8
+ end
9
+
10
+ after do
11
+ FileUtils.remove_entry_secure @output_dir
12
+ end
13
+
14
+ let(:subject) { Backdrop::ApiBuilder.new(@output_dir) }
15
+ describe '#build_get' do
16
+
17
+ let(:sample_api) do
18
+ { 'http_method' => 'GET',
19
+ 'route' => '/project/request',
20
+ 'requests' => [{
21
+ 'response_body' => '{"Hello":"World"}'
22
+ }] }
23
+ end
24
+
25
+ it 'creates a json file for each request/response pair' do
26
+ subject.build_get 'project', sample_api
27
+ output = File.read(
28
+ "#{File.join(@output_dir, 'project', sample_api['route'])}.json"
29
+ )
30
+ expect(output).to eq '{"Hello":"World"}'
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,62 @@
1
+ require 'spec_helper'
2
+ require 'backdrop/extractor'
3
+ require 'backdrop/api_builder'
4
+ require 'tmpdir'
5
+ require 'tempfile'
6
+
7
+ describe Backdrop::Extractor do
8
+
9
+ describe '#project_name' do
10
+ context 'when running a normal project' do
11
+ let(:file_path) { '/files/projects/name' }
12
+
13
+ it 'works out the project name based on its path' do
14
+ expect(subject.project_name(file_path)).to eq 'name'
15
+ end
16
+ end
17
+
18
+ context 'when running a project on Jenkins' do
19
+ let(:file_path) { '/files/projects/jenkins_project/workspace' }
20
+ it 'strips workspace/ from the path (for Jenkins builds)' do
21
+ expect(subject.project_name(file_path)).to eq 'jenkins_project'
22
+ end
23
+ end
24
+ end
25
+
26
+ describe '#build_apis' do
27
+ let(:api_builder) { double :api_builder }
28
+ let(:json) { { 'http_method' => 'GET' } }
29
+ before do
30
+ allow(Backdrop::ApiBuilder).to receive(:new).with('output')
31
+ .and_return api_builder
32
+ @json_file = Tempfile.new 'file.json'
33
+ @json_file.write(JSON.generate json)
34
+ @json_file.rewind
35
+ end
36
+ context 'when the request is a GET' do
37
+ it 'creates an API for each get request' do
38
+ expect(api_builder).to receive(:build_get).with('project', json)
39
+ subject.build_apis 'project', [@json_file.path], 'output'
40
+ end
41
+ end
42
+ end
43
+
44
+ describe '#load_docs' do
45
+ before do
46
+ @dir = Dir.mktmpdir
47
+ FileUtils.mkdir_p("#{@dir}/doc/api")
48
+ File.write("#{@dir}/doc/api/index.json", '{"hello":"world"}')
49
+ File.write("#{@dir}/doc/api/api.json", '{"json":"api"}')
50
+ end
51
+
52
+ after do
53
+ FileUtils.remove_entry_secure @dir
54
+ end
55
+
56
+ it 'loads the files but excludes the index.json' do
57
+ expect(subject.load_docs(@dir)).to match_array([
58
+ "#{@dir}/doc/api/api.json"
59
+ ])
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,32 @@
1
+ require 'spec_helper'
2
+ require 'backdrop/extractor'
3
+ require 'backdrop/runner'
4
+
5
+ describe Backdrop::Runner do
6
+
7
+ let(:output_dir) { '/fixtures' }
8
+ let(:project_paths) { ['/project/path'] }
9
+ let(:project_name) { 'path' }
10
+ let(:rad_docs) { ['file.json'] }
11
+ let(:subject) { Backdrop::Runner.new(output_dir, project_paths) }
12
+
13
+ describe '#run' do
14
+ let(:extractor) do
15
+ double :extractor, project_name: project_name, load_docs: rad_docs
16
+ end
17
+ let(:builder) { double :builder }
18
+
19
+ before do
20
+ allow(Backdrop::Extractor).to receive(:new).and_return extractor
21
+ end
22
+
23
+ it 'works out the project name' do
24
+ expect(extractor).to receive(:build_apis).with(
25
+ project_name,
26
+ rad_docs,
27
+ output_dir
28
+ )
29
+ subject.run
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,39 @@
1
+ require 'spec_helper'
2
+ require 'backdrop/runner'
3
+ require 'tmpdir'
4
+
5
+ describe 'Backdrop' do
6
+
7
+ describe 'running backdrop with an out directory and API project' do
8
+ let!(:test_dir) { Dir.mktmpdir }
9
+ let(:subject) { Backdrop::Runner.new(output_path, [project_path]) }
10
+ let(:output_path) { File.join(test_dir, 'output') }
11
+ let(:project_path) { File.join(test_dir, 'project') }
12
+ let(:rad_output) do
13
+ { 'http_method' => 'GET',
14
+ 'route' => '/resource/request',
15
+ 'requests' => [{
16
+ 'response_body' => '{"Hello":"World"}'
17
+ }] }
18
+ end
19
+
20
+ before do
21
+ FileUtils.mkdir_p(File.join(project_path, 'doc/api/resource'))
22
+ File.write(
23
+ File.join(project_path, '/doc/api/request.json'),
24
+ JSON.generate(rad_output)
25
+ )
26
+ end
27
+
28
+ after do
29
+ FileUtils.remove_entry_secure test_dir
30
+ end
31
+
32
+ it 'creates the APIs in the target directory' do
33
+ subject.run
34
+ out = (File.read(File.join(output_path, 'project/resource/request.json')))
35
+ expect(out).to eq '{"Hello":"World"}'
36
+ end
37
+ end
38
+
39
+ end
@@ -0,0 +1,7 @@
1
+ require 'simplecov'
2
+
3
+ SimpleCov.coverage_dir 'log/coverage/rspec'
4
+ SimpleCov.start do
5
+ add_filter 'vendor'
6
+ add_filter 'spec'
7
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: backdrop
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom Bowden
@@ -46,8 +46,30 @@ executables: []
46
46
  extensions: []
47
47
  extra_rdoc_files: []
48
48
  files:
49
+ - ".gitignore"
50
+ - ".rspec"
51
+ - ".rubocop.yml"
52
+ - ".rvmrc"
53
+ - ".travis.yml"
54
+ - Gemfile
55
+ - README.md
56
+ - Rakefile
57
+ - backdrop.gemspec
58
+ - config.ru
49
59
  - lib/backdrop.rb
50
- - lib/tasks/backdrop.rake
60
+ - lib/backdrop/api_builder.rb
61
+ - lib/backdrop/app.rb
62
+ - lib/backdrop/extractor.rb
63
+ - lib/backdrop/rake_task.rb
64
+ - lib/backdrop/runner.rb
65
+ - lib/backdrop/version.rb
66
+ - lib/tasks/default.rake
67
+ - lib/tasks/rubocop.rake
68
+ - spec/backdrop/api_builder_spec.rb
69
+ - spec/backdrop/extractor_spec.rb
70
+ - spec/backdrop/runner_spec.rb
71
+ - spec/integration/backdrop_spec.rb
72
+ - spec/spec_helper.rb
51
73
  homepage: http://github.com/tpbowden/backdrop
52
74
  licenses:
53
75
  - MIT
@@ -1,9 +0,0 @@
1
- require 'json'
2
- require 'backdrop/runner'
3
-
4
- namespace :backdrop do
5
- desc 'Loads all paths to RAD docs and creates the stub APIs'
6
- task :update_fixtures do
7
- Backdrop::Runner.new(@backdrop_output_dir, @backdrop_api_projects).run
8
- end
9
- end