backdrop 0.3.0 → 0.3.1

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
- M2MzZjUzNDNkMmVhNGUxMTlhNDAyNzYwOGJlZjVlMzk1N2M5MTQ0NQ==
4
+ N2Y4MDRhNmQzZTQwOGFkNjM5YzVmNTFhOTkxNTZmYzQ3YWYxNjZmNw==
5
5
  data.tar.gz: !binary |-
6
- YmViMmI4NTBiZGYxM2U0NGJmMzkwZDBkZmM0NWFkZTgwZTU4MGQxZg==
6
+ ZjU3YmZkOWQ5MWVlY2YxOWM0OWIzM2EyYTY3ZDcxZDY4MzlhZjczNQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- OTAxMDAwODMzZDVmNDFjMmVlYmU1ZmEwNThjYjdhM2MwZWNiNWU5MGJkZjk2
10
- MWE4NGU5YmI1NDE4OWIwMThjZTgxZGI5OWZkZTI1MzM1M2EwNmFkYjNmNWJm
11
- MzRiMTYzZDhhNDIwZDhhODYwZWJiMDZkOTc4MzIzYWNlNmJlNTU=
9
+ NjY2NGNkODVlYmE0ZGE0ODAzYTliY2EyZTlhZmQ5YjI3MTEwZmI1MTg0OGVi
10
+ OWIyNzBlZmZjNzg5MTAzMTViZGEwN2RlNjRmNTFmODYwMDA1OTNjNTc4ZjA1
11
+ NmI2NTI4Y2VhYTY0ZmFkNGU2Y2MxYmM1MzE0ZWY1MmYxNWY0OGE=
12
12
  data.tar.gz: !binary |-
13
- MDAxYmMxM2MzYmYwOTIwNDY3ZWY4NmQ4YWJkZGYwYjk5N2MyYzhhNWVkODUx
14
- YjJkN2U1MzEzYTMyMzkxYmUwZjQ2YzhjYmYxM2Y5MjU4ZGRlNGQ3OGE0Yjk0
15
- MTJkYmFjZDNlOWMzYTg4NjVmYWI4M2M1ZDc1MGFhMWUxYjc1ZWE=
13
+ NzU4OTY3NGY1YzgxZTg2YzgxN2FkZThlNmEzY2E2ZmQ3Y2YyZjZkMmJlMTc5
14
+ MGZjMjVlNmFiOTkxZGU2ZDdiZjNmNzdlMDUyOGY0ZDJiZjgyOTA4YTI5MTlh
15
+ NDRmMTU3MWViMzRjNmM1MDYxNGE0NzUxOWYwZDQ4ZjQzMGEyMDY=
data/.gitignore CHANGED
@@ -8,3 +8,4 @@ lib/tasks/fixtures.rake
8
8
  /log
9
9
  /doc
10
10
  *.orig
11
+ /tmp
data/.travis.yml CHANGED
@@ -6,6 +6,9 @@ branches:
6
6
  env:
7
7
  global:
8
8
  - CODECLIMATE_REPO_TOKEN=a79c2033f8bd8de4442fbffdee847be7f8a505408ef40350ac598264d2fe2ccc
9
+ os:
10
+ - linux
11
+ - osx
9
12
  rvm:
10
13
  - 2.1.4
11
14
  - 2.0.0
data/README.md CHANGED
@@ -1,9 +1,9 @@
1
- Backdrop
1
+ Backdrop
2
2
  ========
3
3
 
4
4
  [![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) [![Coverage Status](https://img.shields.io/coveralls/tpbowden/backdrop.svg)](https://coveralls.io/r/tpbowden/backdrop?branch=master) [![Code Climate](https://codeclimate.com/github/tpbowden/backdrop/badges/gpa.svg)](https://codeclimate.com/github/tpbowden/backdrop) [![Dependency Status](https://gemnasium.com/tpbowden/Backdrop.svg)](https://gemnasium.com/tpbowden/Backdrop)
5
5
 
6
- Detects all APIs required and generates stubs based on RAD documentation
6
+ JSON fixture generation for integration testing apps which consume multiple remote APIs.
7
7
 
8
8
  Installation
9
9
  ------------
@@ -16,33 +16,45 @@ config/routes.rb
16
16
 
17
17
  mount Backdrop::App => '/backdrop'
18
18
 
19
- Ensure your APIs are generating JSON docs
20
-
21
- RspecApiDocumentation.configure do |config|
22
- config.format = :json
23
- end
24
-
25
19
  Configuration
26
20
  -------------
27
21
 
28
- config/initializers/backdrop_init.rb
22
+ Backdrop stores JSON in temporary files whilst it runs which should not be checked into version control. This can be specificied in an initializer:
29
23
 
30
24
  Backdrop.configure do |config|
31
- config.output_dir = 'output_directory'
25
+ # defaults to tmp/backdrop
26
+ config.output_dir = '/your/temp/path'
32
27
  end
33
28
 
34
- Generating Stubs
35
- ----------------
29
+ Usage
30
+ -----
31
+
32
+ require 'backdrop'
33
+
34
+ Backdrop can be used on either a single OpenStruct or an array of OpenStructs. Each of these must have a property called 'backdrop_url' which is where the generated JSON will be located. This will be removed before being used for the API.
35
+
36
+ To create an API, call `Backdrop.load` on the data. To clear all loaded APIs, use `Backdrop.clear`.
37
+
38
+ Integration with Factory Girl
39
+ -----------------------------
40
+
41
+ Backdrop is most easily used alongside [FactoryGirl](https://github.com/thoughtbot/factory_girl).
42
+
43
+ FactoryGirl.define do
44
+ factory :example, class: OpenStruct do
45
+ backdrop_url { name }
46
+ name "example"
47
+ end
48
+ end
36
49
 
37
- Create a rake task with an array of paths to your API projects
50
+ You can then use `Backdrop.load(FactoryGirl.build :example)` to create the following JSON at `/backdrop/example`:
38
51
 
39
- lib/tasks/backdrop.rake
52
+ `{"name":"example"}`
40
53
 
41
- require 'backdrop/rake_task'
54
+ Similarly, `Backdrop.load(FactoryGirl.build_list :example, 2)` would create the following JSON:
42
55
 
43
- Backdrop::RakeTask.new('output_directory', ['/paths/to/projects'])
56
+ [{"name":"example"},{"name":"example"}]
44
57
 
45
- Call `rake backdrop` to create all RAD APIs in the specified output directory
46
58
 
47
59
  Dependencies
48
60
  ------------
@@ -1,3 +1,5 @@
1
+ require 'fileutils'
2
+ require 'backdrop'
1
3
  require 'backdrop/app'
2
4
 
3
5
  module Backdrop
@@ -18,12 +20,18 @@ module Backdrop
18
20
  url = record.delete_field 'backdrop_url'
19
21
  record.to_h
20
22
  end
21
- Backdrop::App.load_api(url, JSON.generate(formatted_data))
23
+ write_api(url, JSON.generate(formatted_data))
22
24
  end
23
25
 
24
26
  def build_single(input)
25
27
  url = input.delete_field('backdrop_url')
26
- Backdrop::App.load_api(url, JSON.generate(input.to_h))
28
+ write_api(url, JSON.generate(input.to_h))
29
+ end
30
+
31
+ def write_api(url, json)
32
+ file = "#{File.join(Backdrop.configuration.output_dir, url)}.json"
33
+ FileUtils.mkdir_p(File.dirname(file))
34
+ File.write(file, json)
27
35
  end
28
36
  end
29
37
  end
data/lib/backdrop/app.rb CHANGED
@@ -2,9 +2,16 @@ require 'sinatra'
2
2
 
3
3
  module Backdrop
4
4
  class App < Sinatra::Base
5
- def self.load_api(url, data)
6
- get url do
7
- data
5
+ get '/*' do
6
+ file = File.join(
7
+ Backdrop.configuration.output_dir,
8
+ "#{params[:splat].first}.json"
9
+ )
10
+ if File.exist? file
11
+ status 200
12
+ File.read file
13
+ else
14
+ status 404
8
15
  end
9
16
  end
10
17
  end
@@ -1,4 +1,9 @@
1
1
  module Backdrop
2
2
  class Configuration
3
+ attr_accessor :output_dir
4
+
5
+ def initialize
6
+ @output_dir = 'tmp/backdrop'
7
+ end
3
8
  end
4
9
  end
@@ -1,3 +1,3 @@
1
1
  module Backdrop
2
- VERSION = '0.3.0'
2
+ VERSION = '0.3.1'
3
3
  end
@@ -4,13 +4,14 @@ require 'backdrop/api_builder'
4
4
  describe Backdrop::APIBuilder do
5
5
  describe '#build' do
6
6
  let(:url) { '/some/location' }
7
+ let(:output_file) { 'tmp/backdrop/some/location.json' }
7
8
 
8
9
  context 'when the input is a single item' do
9
10
  let(:sample_input) { build :example, backdrop_url: url }
10
11
  let(:json) { '{"some":"data"}' }
11
- it 'tells the server to load the API' do
12
- expect(Backdrop::App).to receive(:load_api).with url, json
12
+ it 'writes a file at the URL path containing the json' do
13
13
  subject.build sample_input
14
+ expect(File.read(output_file)).to eq json
14
15
  end
15
16
  end
16
17
 
@@ -18,8 +19,8 @@ describe Backdrop::APIBuilder do
18
19
  let(:sample_input) { build_list :example, 2, backdrop_url: url }
19
20
  let(:json) { '[{"some":"data"},{"some":"data"}]' }
20
21
  it 'tells the server to load the API array' do
21
- expect(Backdrop::App).to receive(:load_api).with url, json
22
22
  subject.build sample_input
23
+ expect(File.read(output_file)).to eq json
23
24
  end
24
25
  end
25
26
  end
@@ -10,7 +10,6 @@ describe Backdrop do
10
10
  end
11
11
 
12
12
  before do
13
- Backdrop.clear
14
13
  Backdrop.load sample_factory_data
15
14
  end
16
15
 
@@ -31,4 +30,12 @@ describe Backdrop do
31
30
  expect(last_response.body).to eq '[{"some":"data"},{"some":"data"}]'
32
31
  end
33
32
  end
33
+
34
+ context 'when there is no data for a route' do
35
+ let(:sample_factory_data) { build :example }
36
+ it 'gives status 404' do
37
+ get '/nothing'
38
+ expect(last_response.status).to eq 404
39
+ end
40
+ end
34
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.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom Bowden