backdrop 0.1.2 → 0.2.0

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,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MWYxYjlkNWEyM2JiNzJlY2EzODA3NWViYTQ0MTE5MDhmNGVmMmZmMw==
4
+ NWJjNzVkNDk1ZDYzMDNhYmQyMmRkZmNlZjIwMDUxMmVmZDkwY2VlMQ==
5
5
  data.tar.gz: !binary |-
6
- ODRjNGZmNmEyOTEzYWFmZTliYmIyZWU3YjdiNjZhY2YzM2EzZWJmOA==
6
+ NjVkZWVkZGMyM2JkYjZiOTI1YWU0YzRmY2U2YmQ2YTJmMWJkZDNhYw==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- Njc4NmFkZGU2ZTIyMWZlYWQxMDVmYTY4ODliZjlhNjMwY2M3OGQ1NDAyYjYx
10
- NmM3Zjc0ZDNjNTEyODE4NGJiOWVjNDkxNDkwNTJmMDg4Y2Q2ODY5ZWY2M2Fj
11
- YTA1MGRhOGE0YjhiNWJkZWJhNGUxZDkyOTAxYmM4ODNlMThmYWU=
9
+ NzJjMmZhYzVmNTUyYWZlNGZmNDdkMGRkOTRmN2E3YTVlMDVjZWNiYTg0MDQ5
10
+ YzEzZTVmOTlkNmUwZDZmNzIyNGI4Mjk0NTY1OGE3ZmM0YjM2MmY1MTBlNzMx
11
+ MzI4MjdkYjExNGFiNjAzMjMzMDdjZmRhZTQ3NWZkMDcyMzlmOTU=
12
12
  data.tar.gz: !binary |-
13
- MTRhNDhkZDY5MmVjYWRkZjI4ZWIyYmQ3MDIzMmM1NjY4ZjVkMjBmZGRlOGUy
14
- NDRlOTlkOTY5ZDBmMzRhYjZhOTBmNWY0NTRlNDBkOTJlMDVjNTE1OWNiNjk1
15
- YTNjM2M2OWExMjM1NDM1YTU1MjI0MjViN2Q0OGQwZWUyOGM1NjE=
13
+ ZGY0ZWIyMDk1ZDJkMTU3ZjExMzU1ZWIxYzYwZTc5ZTJiYjY0MjRjNzNmOGRj
14
+ MWVkNzg3NzJjNDM4ZTMxM2FjZDEwNmU4YThjYWE0NTNjZDY3ZjM4ZmFiNTcz
15
+ NWJkMDc5MDlmMjc5MmFlMzBkNThmZTFhMzA5Zjg0MWRiMDM2YWE=
@@ -6,24 +6,23 @@ module Backdrop
6
6
  @output_dir = output_dir
7
7
  end
8
8
 
9
- def build_get(project, data)
9
+ def build_response(project, data, response, method)
10
10
  file = data['route']
11
- response = data['requests'].first['response_body']
12
11
  path = File.dirname(file)
13
12
  filename = File.basename(file)
14
13
  filepath = File.join(@output_dir, project, path)
15
14
  FileUtils.mkdir_p filepath
16
- File.write("#{File.join(filepath, filename)}.get", response)
15
+ File.write("#{File.join(filepath, filename)}.#{method}", response)
16
+ end
17
+
18
+ def build_get(project, data)
19
+ response = data['requests'].first['response_body']
20
+ build_response(project, data, response, 'get')
17
21
  end
18
22
 
19
23
  def build_put(project, data)
20
- file = data['route']
21
24
  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)
25
+ build_response(project, data, response, 'put')
27
26
  end
28
27
  end
29
28
  end
data/lib/backdrop/app.rb CHANGED
@@ -21,7 +21,7 @@ module Backdrop
21
21
  def self.build_route(method, route, file)
22
22
  send(method, route) do
23
23
  builder = Backdrop::Builders.const_get(method.to_s.capitalize).new
24
- builder.build(self, file)
24
+ builder.route(self, file)
25
25
  end
26
26
  end
27
27
  end
@@ -0,0 +1,13 @@
1
+ module Backdrop
2
+ class Builder
3
+ def build_response(output, project, data, response)
4
+ file = data['route']
5
+ method = data['http_method'].downcase
6
+ path = File.dirname file
7
+ filename = File.basename file
8
+ filepath = File.join(output, project, path)
9
+ FileUtils.mkdir_p filepath
10
+ File.write("#{File.join(filepath, filename)}.#{method}", response)
11
+ end
12
+ end
13
+ end
@@ -1,10 +1,15 @@
1
1
  module Backdrop
2
2
  module Builders
3
- class Get
4
- def build(app, file)
3
+ class Get < Builder
4
+ def route(app, file)
5
5
  app.content_type :json
6
6
  File.read File.expand_path(file)
7
7
  end
8
+
9
+ def build(output, project, data)
10
+ response = data['requests'].first['response_body']
11
+ build_response(output, project, data, response)
12
+ end
8
13
  end
9
14
  end
10
15
  end
@@ -1,11 +1,16 @@
1
1
  module Backdrop
2
2
  module Builders
3
- class Put
4
- def build(app, file)
3
+ class Put < Builder
4
+ def route(app, file)
5
5
  app.content_type :json
6
6
  code = File.read File.expand_path(file)
7
7
  app.status code.to_i
8
8
  end
9
+
10
+ def build(output, project, data)
11
+ response = data['requests'].first['response_status']
12
+ build_response(output, project, data, response)
13
+ end
9
14
  end
10
15
  end
11
16
  end
@@ -2,10 +2,31 @@ module Backdrop
2
2
  class Configuration
3
3
  attr_accessor :output_dir
4
4
  attr_accessor :projects
5
+ attr_reader :http
5
6
 
6
7
  def initialize
7
8
  @output_dir = './fixtures'
8
9
  @projects = []
10
+ @http = Http.new
11
+ end
12
+
13
+ class Http
14
+ attr_accessor :get
15
+ attr_accessor :put
16
+ attr_reader :http_methods
17
+
18
+ def initialize
19
+ @get = Backdrop::Builders::Get
20
+ @put = Backdrop::Builders::Put
21
+ @http_methods = [:get, :put]
22
+ end
23
+
24
+ def method_missing(method, arg)
25
+ http_method = method.to_s.gsub('=', '')
26
+ self.class.__send__(:attr_accessor, http_method)
27
+ send(method, arg)
28
+ @http_methods << (http_method.to_sym)
29
+ end
9
30
  end
10
31
  end
11
32
  end
@@ -13,11 +13,11 @@ module Backdrop
13
13
  end
14
14
 
15
15
  def build_apis(project, rad_json, output_dir)
16
- builder = Backdrop::ApiBuilder.new output_dir
17
16
  rad_json.each do |json_file|
18
17
  json = JSON.parse File.read(json_file)
19
- builder.build_get(project, json) if json['http_method'] == 'GET'
20
- builder.build_put(project, json) if json['http_method'] == 'PUT'
18
+ method = json['http_method']
19
+ builder = Backdrop.configuration.http.send(method.downcase)
20
+ builder.new.build output_dir, project, json
21
21
  end
22
22
  end
23
23
  end
@@ -1,3 +1,3 @@
1
1
  module Backdrop
2
- VERSION = '0.1.2'
2
+ VERSION = '0.2.0'
3
3
  end
data/lib/backdrop.rb CHANGED
@@ -3,9 +3,9 @@ require 'backdrop/version'
3
3
  require 'backdrop/configuration'
4
4
  require 'backdrop/runner'
5
5
  require 'backdrop/extractor'
6
+ require 'backdrop/builders/builder'
6
7
  require 'backdrop/builders/get'
7
8
  require 'backdrop/builders/put'
8
- require 'backdrop/api_builder'
9
9
  require 'backdrop/app'
10
10
 
11
11
  module Backdrop
@@ -13,6 +13,10 @@ module Backdrop
13
13
  @configuration ||= Configuration.new
14
14
  end
15
15
 
16
+ def self.reset
17
+ @configuration = Configuration.new
18
+ end
19
+
16
20
  def self.configure
17
21
  yield(configuration) if block_given?
18
22
  end
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+ require 'backdrop'
3
+
4
+ describe Backdrop::Builder do
5
+ describe '#build_response' do
6
+
7
+ before do
8
+ @output_dir = Dir.mktmpdir('output')
9
+ end
10
+
11
+ after do
12
+ FileUtils.remove_entry_secure @output_dir
13
+ end
14
+
15
+ let(:sample_data) do
16
+ { 'http_method' => 'GET',
17
+ 'route' => '/project/request',
18
+ 'requests' => [{
19
+ 'response_body' => '{"Hello":"World"}'
20
+ }] }
21
+ end
22
+ let(:project) { 'project' }
23
+ let(:response) { 'some response' }
24
+
25
+ it 'creates the response file with given input' do
26
+ subject.build_response(@output_dir, project, sample_data, response)
27
+ output = File.read(
28
+ "#{File.join(@output_dir, 'project', sample_data['route'])}.get"
29
+ )
30
+ expect(output).to eq response
31
+ end
32
+ end
33
+ end
@@ -1,6 +1,6 @@
1
1
  require 'backdrop'
2
2
  describe Backdrop::Builders::Get do
3
- describe '#build' do
3
+ describe '#route' do
4
4
  let(:app) { double :app }
5
5
  let(:file) { Tempfile.new 'file' }
6
6
 
@@ -11,12 +11,38 @@ describe Backdrop::Builders::Get do
11
11
  end
12
12
 
13
13
  it 'sets the content type to json' do
14
- subject.build app, file
14
+ subject.route app, file
15
15
  expect(app).to have_received(:content_type).with :json
16
16
  end
17
17
 
18
18
  it 'returns the contents of the file' do
19
- expect(subject.build app, file).to eq 'Hello World'
19
+ expect(subject.route app, file).to eq 'Hello World'
20
+ end
21
+ end
22
+
23
+ describe '#build' do
24
+ before do
25
+ @output_dir = Dir.mktmpdir('output')
26
+ end
27
+
28
+ after do
29
+ FileUtils.remove_entry_secure @output_dir
30
+ end
31
+
32
+ let(:data) do
33
+ { 'http_method' => 'GET',
34
+ 'route' => '/project/request',
35
+ 'requests' => [{
36
+ 'response_body' => '{"Hello":"World"}'
37
+ }] }
38
+ end
39
+
40
+ it 'creates a file for each request' do
41
+ subject.build @output_dir, 'project', data
42
+ output = File.read(
43
+ "#{File.join(@output_dir, 'project', data['route'])}.get"
44
+ )
45
+ expect(output).to eq '{"Hello":"World"}'
20
46
  end
21
47
  end
22
48
  end
@@ -2,7 +2,7 @@ require 'spec_helper'
2
2
  require 'backdrop'
3
3
 
4
4
  describe Backdrop::Builders::Put do
5
- describe '#build' do
5
+ describe '#route' do
6
6
  let(:app) { double :app }
7
7
  let(:file) { Tempfile.new 'file' }
8
8
 
@@ -13,13 +13,41 @@ describe Backdrop::Builders::Put do
13
13
  allow(app).to receive(:status).with 200
14
14
  end
15
15
  it 'sets the content type to json' do
16
- subject.build app, file
16
+ subject.route app, file
17
17
  expect(app).to have_received(:content_type).with(:json)
18
18
  end
19
19
 
20
20
  it 'sets the status code to the contents of file' do
21
- subject.build app, file
21
+ subject.route app, file
22
22
  expect(app).to have_received(:status).with 200
23
23
  end
24
24
  end
25
+
26
+ describe '#build' do
27
+
28
+ before do
29
+ @output_dir = Dir.mktmpdir('output')
30
+ end
31
+
32
+ after do
33
+ FileUtils.remove_entry_secure @output_dir
34
+ end
35
+
36
+ let(:data) do
37
+ { 'http_method' => 'PUT',
38
+ 'route' => '/project/request',
39
+ 'requests' => [{
40
+ 'response_body' => '{"Hello":"World"}',
41
+ 'response_status' => 201
42
+ }] }
43
+ end
44
+
45
+ it 'creates a file with the status for each request' do
46
+ subject.build @output_dir, 'project', data
47
+ output = File.read(
48
+ "#{File.join(@output_dir, 'project', data['route'])}.put"
49
+ )
50
+ expect(output).to eq '201'
51
+ end
52
+ end
25
53
  end
@@ -2,6 +2,7 @@ require 'spec_helper'
2
2
  require 'backdrop'
3
3
 
4
4
  describe Backdrop::Configuration do
5
+
5
6
  describe '#output_dir' do
6
7
  it 'defaults to ./fixtures' do
7
8
  expect(subject.output_dir).to eq './fixtures'
@@ -24,4 +25,39 @@ describe Backdrop::Configuration do
24
25
  expect(subject.projects).to eq [1, 2]
25
26
  end
26
27
  end
28
+
29
+ describe '#http' do
30
+ it 'allows the assignment of any method' do
31
+ expect(subject.http).to be_a Backdrop::Configuration::Http
32
+ end
33
+ end
34
+
35
+ describe Backdrop::Configuration::Http do
36
+ describe '#http_methods' do
37
+ it 'defaults the HTTP methods to the ones with defaults' do
38
+ expect(subject.http_methods).to eq [:get, :put]
39
+ end
40
+ end
41
+
42
+ describe '#METHOD' do
43
+
44
+ it 'allows anything to be set' do
45
+ subject.something = 'hello'
46
+ expect(subject.something).to eq 'hello'
47
+ end
48
+
49
+ it 'adds new methods to the http_methods array' do
50
+ subject.val = 'something'
51
+ expect(subject.http_methods).to include(:val)
52
+ end
53
+
54
+ it 'sets get to be a GET builder' do
55
+ expect(subject.get).to eq Backdrop::Builders::Get
56
+ end
57
+
58
+ it 'sets put to be the PUT builder' do
59
+ expect(subject.put).to eq Backdrop::Builders::Put
60
+ end
61
+ end
62
+ end
27
63
  end
@@ -23,40 +23,21 @@ describe Backdrop::Extractor do
23
23
  end
24
24
 
25
25
  describe '#build_apis' do
26
- let(:api_builder) { double :api_builder }
26
+ let(:builder) { double :builder }
27
27
 
28
- context 'when the request is a GET' do
29
- let(:json) { { 'http_method' => 'GET' } }
28
+ let(:json) { { 'http_method' => 'GET' } }
30
29
 
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
-
39
- it 'creates an API for each get request' do
40
- expect(api_builder).to receive(:build_get).with('project', json)
41
- subject.build_apis 'project', [@json_file.path], 'output'
42
- end
30
+ before do
31
+ allow(Backdrop::Builders::Get).to receive(:new)
32
+ .and_return builder
33
+ @json_file = Tempfile.new 'file.json'
34
+ @json_file.write(JSON.generate json)
35
+ @json_file.rewind
43
36
  end
44
37
 
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
38
+ it 'creates an API for each request' do
39
+ expect(builder).to receive(:build).with('output', 'project', json)
40
+ subject.build_apis 'project', [@json_file.path], 'output'
60
41
  end
61
42
  end
62
43
 
@@ -18,4 +18,16 @@ describe Backdrop do
18
18
  expect(Backdrop.configuration).to eq @config
19
19
  end
20
20
  end
21
+
22
+ describe '.reset' do
23
+ before do
24
+ Backdrop.configure do |config|
25
+ config.output_dir = 'somewhere'
26
+ end
27
+ end
28
+ it 'resets all configuration' do
29
+ Backdrop.reset
30
+ expect(Backdrop.configuration.output_dir).to eq './fixtures'
31
+ end
32
+ end
21
33
  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.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom Bowden
@@ -59,6 +59,7 @@ files:
59
59
  - lib/backdrop.rb
60
60
  - lib/backdrop/api_builder.rb
61
61
  - lib/backdrop/app.rb
62
+ - lib/backdrop/builders/builder.rb
62
63
  - lib/backdrop/builders/get.rb
63
64
  - lib/backdrop/builders/put.rb
64
65
  - lib/backdrop/configuration.rb
@@ -68,7 +69,7 @@ files:
68
69
  - lib/backdrop/version.rb
69
70
  - lib/tasks/default.rake
70
71
  - lib/tasks/rubocop.rake
71
- - spec/backdrop/api_builder_spec.rb
72
+ - spec/backdrop/builders/builder_spec.rb
72
73
  - spec/backdrop/builders/get_spec.rb
73
74
  - spec/backdrop/builders/put_spec.rb
74
75
  - spec/backdrop/configuration_spec.rb
@@ -1,54 +0,0 @@
1
- require 'spec_helper'
2
- require 'backdrop'
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'])}.get"
29
- )
30
- expect(output).to eq '{"Hello":"World"}'
31
- end
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
54
- end