backdrop 0.3.1 → 0.4.0

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
- N2Y4MDRhNmQzZTQwOGFkNjM5YzVmNTFhOTkxNTZmYzQ3YWYxNjZmNw==
4
+ YjJkMDFkMzkzNTMwNzUzNWQ3ODIxMDJjZTQ0NTMxMDkyYzQ5ZjYzNA==
5
5
  data.tar.gz: !binary |-
6
- ZjU3YmZkOWQ5MWVlY2YxOWM0OWIzM2EyYTY3ZDcxZDY4MzlhZjczNQ==
6
+ NGQ0YWU4NzcyOGU0MWY1ZTlkNGRlMDdjZDViM2JjOGRkZGRjMmZjMg==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- NjY2NGNkODVlYmE0ZGE0ODAzYTliY2EyZTlhZmQ5YjI3MTEwZmI1MTg0OGVi
10
- OWIyNzBlZmZjNzg5MTAzMTViZGEwN2RlNjRmNTFmODYwMDA1OTNjNTc4ZjA1
11
- NmI2NTI4Y2VhYTY0ZmFkNGU2Y2MxYmM1MzE0ZWY1MmYxNWY0OGE=
9
+ MWZhNDFhMjBiNDA0ZmVkMjIxZGE2NGYzYjM4OTVmMjNmZDZiZGU3ZDY5OTEz
10
+ YjllMGQ3ZjIyM2IxYjk0ODg3NWM0ZGRmYjZhODg1Yjc5MDMzZmRiN2M5N2Y5
11
+ MjNlYTI5OWFmODFlNmNhNDJiOWQxNDA2OTExOWI1ZDcwZjc2Mjk=
12
12
  data.tar.gz: !binary |-
13
- NzU4OTY3NGY1YzgxZTg2YzgxN2FkZThlNmEzY2E2ZmQ3Y2YyZjZkMmJlMTc5
14
- MGZjMjVlNmFiOTkxZGU2ZDdiZjNmNzdlMDUyOGY0ZDJiZjgyOTA4YTI5MTlh
15
- NDRmMTU3MWViMzRjNmM1MDYxNGE0NzUxOWYwZDQ4ZjQzMGEyMDY=
13
+ YzliMjRlMzA3MTIwZTNlMzJhYjU2N2NmZDNlM2I2YjhkYzE0M2RkNTIyOTFl
14
+ ZTFkNTg2ZjFjYmFkZGY3N2RhNjIxM2Y5ZTViNTAwOGU5Nzc4YTRkZDY5Yjky
15
+ MjJkZWExMzM5MGZiMTk4MzlkMTRhNjkxMDczMTM4MTUyOWQ5Mzk=
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 2.1.4
1
+ 1.9.3-p551
data/.travis.yml CHANGED
@@ -10,8 +10,9 @@ os:
10
10
  - linux
11
11
  - osx
12
12
  rvm:
13
- - 2.1.4
14
- - 2.0.0
13
+ - ruby-head
14
+ - jruby-1.7.19
15
+ - 1.9.3
15
16
  script:
16
17
  - bundle exec rake
17
18
  deploy:
@@ -22,4 +23,4 @@ deploy:
22
23
  tags: true
23
24
  all_branches: true
24
25
  repo: tpbowden/backdrop
25
- rvm: 2.0.0
26
+ rvm: 1.9.3
data/Gemfile CHANGED
@@ -3,7 +3,7 @@ source 'https://rubygems.org'
3
3
  gemspec
4
4
 
5
5
  group :test do
6
- gem 'byebug'
6
+ gem 'backports'
7
7
  gem 'codeclimate-test-reporter', require: false
8
8
  gem 'coveralls', require: false
9
9
  gem 'factory_girl'
data/README.md CHANGED
@@ -49,7 +49,7 @@ Backdrop is most easily used alongside [FactoryGirl](https://github.com/thoughtb
49
49
 
50
50
  You can then use `Backdrop.load(FactoryGirl.build :example)` to create the following JSON at `/backdrop/example`:
51
51
 
52
- `{"name":"example"}`
52
+ {"name":"example"}
53
53
 
54
54
  Similarly, `Backdrop.load(FactoryGirl.build_list :example, 2)` would create the following JSON:
55
55
 
@@ -59,4 +59,4 @@ Similarly, `Backdrop.load(FactoryGirl.build_list :example, 2)` would create the
59
59
  Dependencies
60
60
  ------------
61
61
 
62
- * Ruby >= 2.0.0
62
+ * Ruby >= 1.9.3
data/backdrop.gemspec CHANGED
@@ -14,7 +14,7 @@ Gem::Specification.new do |s|
14
14
  s.email = 'tom.b1992@gmail.com'
15
15
  s.homepage = 'http://github.com/tpbowden/backdrop'
16
16
  s.license = 'MIT'
17
- s.required_ruby_version = '>= 2.0.0'
18
- s.add_dependency 'rake', '~> 10.1'
17
+ s.required_ruby_version = '>= 1.9.3'
19
18
  s.add_dependency 'sinatra', '~> 1.4'
19
+ s.add_dependency 'backports', '~> 3.6'
20
20
  end
data/lib/backdrop.rb CHANGED
@@ -1,3 +1,4 @@
1
+ require 'backports/2.0.0/stdlib/ostruct'
1
2
  require 'backdrop/configuration'
2
3
  require 'backdrop/api_builder'
3
4
  require 'backdrop/app'
@@ -15,8 +16,8 @@ module Backdrop
15
16
  yield(configuration) if block_given?
16
17
  end
17
18
 
18
- def self.load(input)
19
- APIBuilder.new.build input
19
+ def self.load(input, url = nil)
20
+ APIBuilder.new(url).build input
20
21
  end
21
22
 
22
23
  def self.clear
@@ -4,34 +4,35 @@ require 'backdrop/app'
4
4
 
5
5
  module Backdrop
6
6
  class APIBuilder
7
+ def initialize(default_url = nil)
8
+ @config = Backdrop.configuration
9
+ @url = default_url
10
+ end
11
+
7
12
  def build(input)
13
+ hashes = Array(input).map(&:to_h)
14
+ url = extract_url(hashes)
15
+
8
16
  if input.respond_to?(:map)
9
- build_array(input)
17
+ write_api(@url || url, hashes)
10
18
  else
11
- build_single(input)
19
+ write_api(@url || url, hashes.first)
12
20
  end
13
21
  end
14
22
 
15
23
  private
16
24
 
17
- def build_array(input)
18
- url = nil
19
- formatted_data = input.map do |record|
20
- url = record.delete_field 'backdrop_url'
21
- record.to_h
25
+ def extract_url(hashes)
26
+ hashes.reduce(nil) do |_, record|
27
+ record.delete :backdrop_url
22
28
  end
23
- write_api(url, JSON.generate(formatted_data))
24
- end
25
-
26
- def build_single(input)
27
- url = input.delete_field('backdrop_url')
28
- write_api(url, JSON.generate(input.to_h))
29
29
  end
30
30
 
31
- def write_api(url, json)
32
- file = "#{File.join(Backdrop.configuration.output_dir, url)}.json"
31
+ def write_api(url, hashes)
32
+ base_dir = @config.output_dir
33
+ file = "#{File.join(base_dir, url)}.json"
33
34
  FileUtils.mkdir_p(File.dirname(file))
34
- File.write(file, json)
35
+ File.write(file, JSON.generate(hashes))
35
36
  end
36
37
  end
37
38
  end
@@ -1,3 +1,3 @@
1
1
  module Backdrop
2
- VERSION = '0.3.1'
2
+ VERSION = '0.4.0'
3
3
  end
@@ -9,7 +9,8 @@ describe Backdrop::APIBuilder do
9
9
  context 'when the input is a single item' do
10
10
  let(:sample_input) { build :example, backdrop_url: url }
11
11
  let(:json) { '{"some":"data"}' }
12
- it 'writes a file at the URL path containing the json' do
12
+
13
+ it 'writes a JSON file at the URL path' do
13
14
  subject.build sample_input
14
15
  expect(File.read(output_file)).to eq json
15
16
  end
@@ -18,7 +19,20 @@ describe Backdrop::APIBuilder do
18
19
  context 'when the input is an array of items' do
19
20
  let(:sample_input) { build_list :example, 2, backdrop_url: url }
20
21
  let(:json) { '[{"some":"data"},{"some":"data"}]' }
21
- it 'tells the server to load the API array' do
22
+
23
+ it 'writes a JSON file at the URL path' do
24
+ subject.build sample_input
25
+ expect(File.read(output_file)).to eq json
26
+ end
27
+ end
28
+
29
+ context 'when the URL is specified' do
30
+ let(:sample_input) { build :example }
31
+ let(:json) { '{"some":"data"}' }
32
+
33
+ subject { described_class.new(url) }
34
+
35
+ it 'writes a JSON file at the URL path' do
22
36
  subject.build sample_input
23
37
  expect(File.read(output_file)).to eq json
24
38
  end
@@ -2,5 +2,4 @@ require 'spec_helper'
2
2
  require 'backdrop'
3
3
 
4
4
  describe Backdrop::Configuration do
5
-
6
5
  end
@@ -21,6 +21,7 @@ describe Backdrop do
21
21
 
22
22
  describe '.reset' do
23
23
  let!(:subject) { Backdrop.configuration }
24
+
24
25
  it 'resets all configuration' do
25
26
  Backdrop.reset
26
27
  expect(Backdrop.configuration).to_not eq subject
@@ -29,11 +30,35 @@ describe Backdrop do
29
30
 
30
31
  describe '.load' do
31
32
  let(:input) { %w('some', 'data') }
33
+ let(:builder) { double :builder, build: true }
34
+
35
+ before do
36
+ allow(Backdrop::APIBuilder)
37
+ .to receive(:new).and_return builder
38
+ end
39
+
32
40
  it 'delegates to the Backdrop API Builder' do
33
- expect(Backdrop::APIBuilder)
34
- .to receive_message_chain('new.build').with input
41
+ expect(builder).to receive(:build).with input
35
42
  described_class.load input
36
43
  end
44
+
45
+ context 'when the Backdrop URL is specified' do
46
+ it 'delegates with the URL' do
47
+ expect(Backdrop::APIBuilder)
48
+ .to receive(:new).with 'a/url'
49
+
50
+ described_class.load input, 'a/url'
51
+ end
52
+ end
53
+
54
+ context 'when the Backdrop URL is implicit' do
55
+ it 'delegates with a nil URL' do
56
+ expect(Backdrop::APIBuilder)
57
+ .to receive(:new).with nil
58
+
59
+ described_class.load input
60
+ end
61
+ end
37
62
  end
38
63
 
39
64
  describe '.clear' do
@@ -9,29 +9,44 @@ describe Backdrop do
9
9
  Backdrop::App
10
10
  end
11
11
 
12
- before do
13
- Backdrop.load sample_factory_data
14
- end
12
+ context 'when the route is implicit' do
13
+ before do
14
+ Backdrop.load sample_factory_data
15
+ end
15
16
 
16
- context 'when it is a single object' do
17
- let(:sample_factory_data) { build :example }
17
+ context 'when it is a single object' do
18
+ let(:sample_factory_data) { build :example }
18
19
 
19
- it 'builds an API based on factory girl data' do
20
- get '/example/data'
21
- expect(last_response.body).to eq '{"some":"data"}'
20
+ it 'builds an API based on factory girl data' do
21
+ get '/example/data'
22
+ expect(last_response.body).to eq '{"some":"data"}'
23
+ end
24
+ end
25
+
26
+ context 'when it is an array of objects' do
27
+ let(:sample_factory_data) { build_list :example, 2 }
28
+
29
+ it 'builds an API based on the array of factory girl data' do
30
+ get '/example/data'
31
+ expect(last_response.body).to eq '[{"some":"data"},{"some":"data"}]'
32
+ end
22
33
  end
23
34
  end
24
35
 
25
- context 'when it is an array of objects' do
26
- let(:sample_factory_data) { build_list :example, 2 }
36
+ context 'when the route is explicit' do
37
+ let(:sample_factory_data) { build :example }
38
+
39
+ before do
40
+ Backdrop.load sample_factory_data, 'my/custom/url'
41
+ end
27
42
 
28
- it 'builds an API based on the array of factory girl data' do
29
- get '/example/data'
30
- expect(last_response.body).to eq '[{"some":"data"},{"some":"data"}]'
43
+ it 'builds an API based on factory girl data' do
44
+ get 'my/custom/url'
45
+ expect(last_response.body).to eq '{"some":"data"}'
31
46
  end
32
47
  end
33
48
 
34
- context 'when there is no data for a route' do
49
+ context 'when the route does not exist' do
35
50
  let(:sample_factory_data) { build :example }
36
51
  it 'gives status 404' do
37
52
  get '/nothing'
data/spec/spec_helper.rb CHANGED
@@ -26,7 +26,6 @@ require 'factory_girl'
26
26
  FactoryGirl.find_definitions
27
27
 
28
28
  RSpec.configure do |config|
29
-
30
29
  config.default_formatter = 'doc' if config.files_to_run.one?
31
30
  config.include FactoryGirl::Syntax::Methods
32
31
 
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.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom Bowden
@@ -11,33 +11,33 @@ cert_chain: []
11
11
  date: 2014-09-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: rake
14
+ name: sinatra
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - ~>
18
18
  - !ruby/object:Gem::Version
19
- version: '10.1'
19
+ version: '1.4'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ~>
25
25
  - !ruby/object:Gem::Version
26
- version: '10.1'
26
+ version: '1.4'
27
27
  - !ruby/object:Gem::Dependency
28
- name: sinatra
28
+ name: backports
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ~>
32
32
  - !ruby/object:Gem::Version
33
- version: '1.4'
33
+ version: '3.6'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - ~>
39
39
  - !ruby/object:Gem::Version
40
- version: '1.4'
40
+ version: '3.6'
41
41
  description: ! "Create and host fixture generation based on simple ruby\n objects
42
42
  for use in local integration testing against\n multiple remote
43
43
  APIs"
@@ -81,7 +81,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
81
81
  requirements:
82
82
  - - ! '>='
83
83
  - !ruby/object:Gem::Version
84
- version: 2.0.0
84
+ version: 1.9.3
85
85
  required_rubygems_version: !ruby/object:Gem::Requirement
86
86
  requirements:
87
87
  - - ! '>='
@@ -89,7 +89,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
89
89
  version: '0'
90
90
  requirements: []
91
91
  rubyforge_project:
92
- rubygems_version: 2.4.2
92
+ rubygems_version: 2.4.5
93
93
  signing_key:
94
94
  specification_version: 4
95
95
  summary: JSON Fixture generation