backdrop 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MDhkOWEwZWU4YWZiMDMwM2Y3OWZkYWM2NzkyMTY3ODMwNGE2M2FlMg==
4
+ MWYxYjlkNWEyM2JiNzJlY2EzODA3NWViYTQ0MTE5MDhmNGVmMmZmMw==
5
5
  data.tar.gz: !binary |-
6
- ZGViM2Y3OWI0MmM0MTA0YmE5YzM2MDRkZjJkNmExMTY3YjY0MDBjNw==
6
+ ODRjNGZmNmEyOTEzYWFmZTliYmIyZWU3YjdiNjZhY2YzM2EzZWJmOA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- NTc0ZGJhY2U5NjE2MzhiMzEwYmFhYTE1ZGUwZTEzYjZiZTU4ZGVhNWUxOTFi
10
- OGFhMDFiMDMzZDc5YzRhN2Q0ZGU0YjZmMDU4NjFmNzIzMWJkZWQ3N2EzNjY2
11
- ZTA2ZjU0ZTUwOTExNTY4NjZiOTgwNDdjZDE0OTQ4MzdjM2YwNzE=
9
+ Njc4NmFkZGU2ZTIyMWZlYWQxMDVmYTY4ODliZjlhNjMwY2M3OGQ1NDAyYjYx
10
+ NmM3Zjc0ZDNjNTEyODE4NGJiOWVjNDkxNDkwNTJmMDg4Y2Q2ODY5ZWY2M2Fj
11
+ YTA1MGRhOGE0YjhiNWJkZWJhNGUxZDkyOTAxYmM4ODNlMThmYWU=
12
12
  data.tar.gz: !binary |-
13
- OTA2NzE0NWE0MWNhY2M2NzQwMmI2MmJkMmFlZjRlNGIxMmRjN2E3MjAxNmJi
14
- MGYyZGYzZjk0NzI4MTE1OWFlOTJlZjAyNDQ0ODE3ZGJjMjQxMTYzNjI3ZjMy
15
- OWIzYzAyZDQxY2E1MmI0YmUzYjA4ZjU2ZjMyZmVmMzkyMzlmMDU=
13
+ MTRhNDhkZDY5MmVjYWRkZjI4ZWIyYmQ3MDIzMmM1NjY4ZjVkMjBmZGRlOGUy
14
+ NDRlOTlkOTY5ZDBmMzRhYjZhOTBmNWY0NTRlNDBkOTJlMDVjNTE1OWNiNjk1
15
+ YTNjM2M2OWExMjM1NDM1YTU1MjI0MjViN2Q0OGQwZWUyOGM1NjE=
data/Gemfile CHANGED
@@ -3,6 +3,7 @@ source 'https://rubygems.org'
3
3
  gemspec
4
4
 
5
5
  group :test do
6
+ gem 'byebug'
6
7
  gem 'codeclimate-test-reporter', require: false
7
8
  gem 'coveralls', require: false
8
9
  gem 'rack-test'
data/config.ru CHANGED
@@ -1,3 +1,3 @@
1
1
  require 'backdrop'
2
2
 
3
- Backdrop::App.run
3
+ Backdrop::App.run!
@@ -13,7 +13,17 @@ module Backdrop
13
13
  filename = File.basename(file)
14
14
  filepath = File.join(@output_dir, project, path)
15
15
  FileUtils.mkdir_p filepath
16
- File.write("#{File.join(filepath, filename)}.json", response)
16
+ File.write("#{File.join(filepath, filename)}.get", response)
17
+ end
18
+
19
+ def build_put(project, data)
20
+ file = data['route']
21
+ response = data['requests'].first['response_status']
22
+ path = File.dirname(file)
23
+ filename = File.basename(file)
24
+ filepath = File.join(@output_dir, project, path)
25
+ FileUtils.mkdir_p filepath
26
+ File.write("#{File.join(filepath, filename)}.put", response)
17
27
  end
18
28
  end
19
29
  end
data/lib/backdrop/app.rb CHANGED
@@ -1,22 +1,28 @@
1
1
  module Backdrop
2
2
  class App < Sinatra::Base
3
+ SUPPORTED_HTTP_METHODS = [:get, :put]
4
+
3
5
  def initialize(app = nil)
4
6
  super app
5
7
  self.class.build_routes
6
8
  end
7
9
 
8
10
  def self.build_routes
9
- files = Dir["#{Backdrop.configuration.output_dir}/**/*"]
10
-
11
- files.each do |file|
12
- route = file.gsub Backdrop.configuration.output_dir, ''
13
- route = route.gsub '.json', ''
14
-
15
- get route do
16
- content_type :json
17
- File.read File.expand_path(file)
11
+ SUPPORTED_HTTP_METHODS.each do |method|
12
+ files = Dir["#{Backdrop.configuration.output_dir}/**/*.#{method}"]
13
+ files.each do |file|
14
+ route = file.gsub Backdrop.configuration.output_dir, ''
15
+ route = route.gsub ".#{method}", ''
16
+ build_route(method, route, file)
18
17
  end
19
18
  end
20
19
  end
20
+
21
+ def self.build_route(method, route, file)
22
+ send(method, route) do
23
+ builder = Backdrop::Builders.const_get(method.to_s.capitalize).new
24
+ builder.build(self, file)
25
+ end
26
+ end
21
27
  end
22
28
  end
@@ -0,0 +1,10 @@
1
+ module Backdrop
2
+ module Builders
3
+ class Get
4
+ def build(app, file)
5
+ app.content_type :json
6
+ File.read File.expand_path(file)
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,11 @@
1
+ module Backdrop
2
+ module Builders
3
+ class Put
4
+ def build(app, file)
5
+ app.content_type :json
6
+ code = File.read File.expand_path(file)
7
+ app.status code.to_i
8
+ end
9
+ end
10
+ end
11
+ end
@@ -17,6 +17,7 @@ module Backdrop
17
17
  rad_json.each do |json_file|
18
18
  json = JSON.parse File.read(json_file)
19
19
  builder.build_get(project, json) if json['http_method'] == 'GET'
20
+ builder.build_put(project, json) if json['http_method'] == 'PUT'
20
21
  end
21
22
  end
22
23
  end
@@ -1,3 +1,3 @@
1
1
  module Backdrop
2
- VERSION = '0.1.1'
2
+ VERSION = '0.1.2'
3
3
  end
data/lib/backdrop.rb CHANGED
@@ -3,6 +3,8 @@ require 'backdrop/version'
3
3
  require 'backdrop/configuration'
4
4
  require 'backdrop/runner'
5
5
  require 'backdrop/extractor'
6
+ require 'backdrop/builders/get'
7
+ require 'backdrop/builders/put'
6
8
  require 'backdrop/api_builder'
7
9
  require 'backdrop/app'
8
10
 
@@ -25,9 +25,30 @@ describe Backdrop::ApiBuilder do
25
25
  it 'creates a json file for each request/response pair' do
26
26
  subject.build_get 'project', sample_api
27
27
  output = File.read(
28
- "#{File.join(@output_dir, 'project', sample_api['route'])}.json"
28
+ "#{File.join(@output_dir, 'project', sample_api['route'])}.get"
29
29
  )
30
30
  expect(output).to eq '{"Hello":"World"}'
31
31
  end
32
32
  end
33
+
34
+ describe '#build_put' do
35
+
36
+ let(:sample_api) do
37
+ { 'http_method' => 'PUT',
38
+ 'route' => '/project/request',
39
+ 'requests' => [{
40
+ 'response_body' => nil,
41
+ 'response_status' => 201
42
+ }] }
43
+ end
44
+
45
+ it 'creates a json file for each request/response pair' do
46
+ subject.build_put 'project', sample_api
47
+ output = File.read(
48
+ "#{File.join(@output_dir, 'project', sample_api['route'])}.put"
49
+ )
50
+ expect(output).to eq '201'
51
+ end
52
+
53
+ end
33
54
  end
@@ -0,0 +1,22 @@
1
+ require 'backdrop'
2
+ describe Backdrop::Builders::Get do
3
+ describe '#build' do
4
+ let(:app) { double :app }
5
+ let(:file) { Tempfile.new 'file' }
6
+
7
+ before do
8
+ file.write('Hello World')
9
+ file.rewind
10
+ allow(app).to receive(:content_type).with(:json)
11
+ end
12
+
13
+ it 'sets the content type to json' do
14
+ subject.build app, file
15
+ expect(app).to have_received(:content_type).with :json
16
+ end
17
+
18
+ it 'returns the contents of the file' do
19
+ expect(subject.build app, file).to eq 'Hello World'
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+ require 'backdrop'
3
+
4
+ describe Backdrop::Builders::Put do
5
+ describe '#build' do
6
+ let(:app) { double :app }
7
+ let(:file) { Tempfile.new 'file' }
8
+
9
+ before do
10
+ file.write(200)
11
+ file.rewind
12
+ allow(app).to receive(:content_type).with(:json)
13
+ allow(app).to receive(:status).with 200
14
+ end
15
+ it 'sets the content type to json' do
16
+ subject.build app, file
17
+ expect(app).to have_received(:content_type).with(:json)
18
+ end
19
+
20
+ it 'sets the status code to the contents of file' do
21
+ subject.build app, file
22
+ expect(app).to have_received(:status).with 200
23
+ end
24
+ end
25
+ end
@@ -24,20 +24,40 @@ describe Backdrop::Extractor do
24
24
 
25
25
  describe '#build_apis' do
26
26
  let(:api_builder) { double :api_builder }
27
- let(:json) { { 'http_method' => 'GET' } }
28
- before do
29
- allow(Backdrop::ApiBuilder).to receive(:new).with('output')
30
- .and_return api_builder
31
- @json_file = Tempfile.new 'file.json'
32
- @json_file.write(JSON.generate json)
33
- @json_file.rewind
34
- end
27
+
35
28
  context 'when the request is a GET' do
29
+ let(:json) { { 'http_method' => 'GET' } }
30
+
31
+ before do
32
+ allow(Backdrop::ApiBuilder).to receive(:new).with('output')
33
+ .and_return api_builder
34
+ @json_file = Tempfile.new 'file.json'
35
+ @json_file.write(JSON.generate json)
36
+ @json_file.rewind
37
+ end
38
+
36
39
  it 'creates an API for each get request' do
37
40
  expect(api_builder).to receive(:build_get).with('project', json)
38
41
  subject.build_apis 'project', [@json_file.path], 'output'
39
42
  end
40
43
  end
44
+
45
+ context 'when the request is a PUT' do
46
+ let(:json) { { 'http_method' => 'PUT' } }
47
+
48
+ before do
49
+ allow(Backdrop::ApiBuilder).to receive(:new).with('output')
50
+ .and_return api_builder
51
+ @json_file = Tempfile.new 'file.json'
52
+ @json_file.write(JSON.generate json)
53
+ @json_file.rewind
54
+ end
55
+
56
+ it 'creates an API for each post request' do
57
+ expect(api_builder).to receive(:build_put).with('project', json)
58
+ subject.build_apis 'project', [@json_file.path], 'output'
59
+ end
60
+ end
41
61
  end
42
62
 
43
63
  describe '#load_docs' do
@@ -3,46 +3,69 @@ require 'backdrop'
3
3
  require 'tmpdir'
4
4
  require 'rack/test'
5
5
 
6
- describe 'Backdrop' do
6
+ describe 'Backdrop POST' do
7
7
  include Rack::Test::Methods
8
- let!(:test_dir) { Dir.mktmpdir }
8
+
9
+ before(:all) do
10
+ @test_dir = Dir.mktmpdir
11
+ end
12
+
13
+ after(:all) do
14
+ FileUtils.remove_entry_secure @test_dir
15
+ end
9
16
  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') }
17
+ let(:output_path) { File.join(@test_dir, 'output') }
18
+ let(:project_path) { File.join(@test_dir, 'project') }
12
19
 
13
20
  def app
14
21
  Backdrop.configure do |config|
15
- config.output_dir = File.join(test_dir, 'output')
22
+ config.output_dir = output_path
16
23
  end
17
24
  Backdrop::App
18
25
  end
19
26
 
20
- describe 'running backdrop with an out directory and API project' do
21
- let(:rad_output) do
27
+ describe 'running with an out directory and API project' do
28
+ let(:put_output) do
29
+ { 'http_method' => 'PUT',
30
+ 'route' => 'resource/put_request',
31
+ 'requests' => [{
32
+ 'response_body' => nil,
33
+ 'response_status' => 201
34
+ }] }
35
+ end
36
+
37
+ let(:get_output) do
22
38
  { 'http_method' => 'GET',
23
- 'route' => '/resource/request',
39
+ 'route' => '/resource/get_request',
24
40
  'requests' => [{
25
41
  'response_body' => '{"Hello":"World"}'
26
42
  }] }
27
43
  end
28
-
29
44
  before do
30
45
  FileUtils.mkdir_p(File.join(project_path, 'doc/api/resource'))
31
46
  File.write(
32
- File.join(project_path, '/doc/api/request.json'),
33
- JSON.generate(rad_output)
47
+ File.join(project_path, '/doc/api/put_request.json'),
48
+ JSON.generate(put_output)
49
+ )
50
+ File.write(
51
+ File.join(project_path, '/doc/api/get_request.json'),
52
+ JSON.generate(get_output)
34
53
  )
35
54
  subject.run
36
55
  end
37
56
 
38
- after do
39
- FileUtils.remove_entry_secure test_dir
57
+ context 'when the method is PUT' do
58
+ it 'responds with the correct status' do
59
+ put '/project/resource/put_request'
60
+ expect(last_response.status).to eq 201
61
+ end
40
62
  end
41
63
 
42
- it 'creates the APIs in the target directory' do
43
- get '/project/resource/request'
44
- expect(last_response.body).to eq '{"Hello":"World"}'
64
+ context 'when the method is GET' do
65
+ it 'responds with the response body' do
66
+ get '/project/resource/get_request'
67
+ expect(last_response.body).to eq '{"Hello":"World"}'
68
+ end
45
69
  end
46
70
  end
47
-
48
71
  end
data/spec/spec_helper.rb CHANGED
@@ -21,3 +21,21 @@ SimpleCov.start do
21
21
  add_filter 'vendor'
22
22
  add_filter 'spec'
23
23
  end
24
+
25
+ RSpec.configure do |config|
26
+
27
+ config.default_formatter = 'doc' if config.files_to_run.one?
28
+
29
+ config.order = :random
30
+
31
+ Kernel.srand config.seed
32
+
33
+ config.expect_with :rspec do |expectations|
34
+ expectations.syntax = :expect
35
+ end
36
+
37
+ config.mock_with :rspec do |mocks|
38
+ mocks.syntax = :expect
39
+ mocks.verify_partial_doubles = true
40
+ end
41
+ 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.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom Bowden
@@ -59,6 +59,8 @@ files:
59
59
  - lib/backdrop.rb
60
60
  - lib/backdrop/api_builder.rb
61
61
  - lib/backdrop/app.rb
62
+ - lib/backdrop/builders/get.rb
63
+ - lib/backdrop/builders/put.rb
62
64
  - lib/backdrop/configuration.rb
63
65
  - lib/backdrop/extractor.rb
64
66
  - lib/backdrop/rake_task.rb
@@ -67,6 +69,8 @@ files:
67
69
  - lib/tasks/default.rake
68
70
  - lib/tasks/rubocop.rake
69
71
  - spec/backdrop/api_builder_spec.rb
72
+ - spec/backdrop/builders/get_spec.rb
73
+ - spec/backdrop/builders/put_spec.rb
70
74
  - spec/backdrop/configuration_spec.rb
71
75
  - spec/backdrop/extractor_spec.rb
72
76
  - spec/backdrop/runner_spec.rb