remote_factory_girl 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/remote_factory_girl/config_applier.rb +7 -7
- data/lib/remote_factory_girl/factory_girl_json_parser.rb +29 -0
- data/lib/remote_factory_girl/version.rb +1 -1
- data/spec/models/remote_factory_girl/config_applier_spec.rb +30 -13
- data/spec/models/remote_factory_girl/factory_girl_json_parser_spec.rb +22 -0
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d8b42f8eed81c36f43aae8e0965e146f7a6882ee
|
4
|
+
data.tar.gz: 4aed3ffaff3534cd3fe42d1556636e9b3632431b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5101e0ebec7e4b744df4971682aebb58d642ddade2445af34d9fc52d5029f8523163d746c734e93bf9840e1f6e5d95f936970418ddff42c6b9851a82b54c6acd
|
7
|
+
data.tar.gz: 5f7c90c31af76477fff52c6d57cb633ad1523451dfc5e98f972f180725fd5c4f33fea261e0a129024090eb2f3e1227283f02599a77624b3a6389f8c00e9a4273
|
@@ -1,7 +1,6 @@
|
|
1
1
|
require 'remote_factory_girl/hash_to_dot'
|
2
2
|
require 'remote_factory_girl/json_to_active_resource'
|
3
|
-
require '
|
4
|
-
require 'json'
|
3
|
+
require 'remote_factory_girl/factory_girl_json_parser'
|
5
4
|
|
6
5
|
module RemoteFactoryGirl
|
7
6
|
class ConfigApplier
|
@@ -21,13 +20,14 @@ module RemoteFactoryGirl
|
|
21
20
|
apply_config_options
|
22
21
|
end
|
23
22
|
|
23
|
+
private
|
24
|
+
|
24
25
|
def default_config
|
25
26
|
{ :hash_to_dot_klass => HashToDot,
|
26
|
-
:json_to_active_resource_klass => JsonToActiveResource
|
27
|
+
:json_to_active_resource_klass => JsonToActiveResource,
|
28
|
+
:response_parser => FactoryGirlJsonParser }
|
27
29
|
end
|
28
30
|
|
29
|
-
private
|
30
|
-
|
31
31
|
def apply_config_options
|
32
32
|
if config[:return_as_active_resource]
|
33
33
|
configured_json = config[:json_to_active_resource_klass].convert(json)
|
@@ -42,8 +42,8 @@ module RemoteFactoryGirl
|
|
42
42
|
config[:return_response_as] == :dot_notation ? config[:hash_to_dot_klass].convert(parsed_json) : parsed_json
|
43
43
|
end
|
44
44
|
|
45
|
-
def return_with_root(
|
46
|
-
config[:return_with_root] == false ?
|
45
|
+
def return_with_root(response_hash)
|
46
|
+
config[:return_with_root] == false ? config[:response_parser].without_root(response_hash) : response_hash
|
47
47
|
end
|
48
48
|
end
|
49
49
|
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module RemoteFactoryGirl
|
2
|
+
class FactoryGirlJsonParser
|
3
|
+
|
4
|
+
def self.without_root(response_hash)
|
5
|
+
new(response_hash).without_root
|
6
|
+
end
|
7
|
+
|
8
|
+
attr_reader :response_hash
|
9
|
+
|
10
|
+
def initialize(response_hash)
|
11
|
+
@response_hash = response_hash
|
12
|
+
end
|
13
|
+
|
14
|
+
def without_root
|
15
|
+
has_root_key? ? response_array.last : response_hash
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def has_root_key?
|
21
|
+
response_array.length == 2 && response_array.last.is_a?(Hash)
|
22
|
+
end
|
23
|
+
|
24
|
+
def response_array
|
25
|
+
@response_array ||= Array(response_hash).flatten
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
@@ -1,29 +1,44 @@
|
|
1
1
|
require 'remote_factory_girl/config_applier'
|
2
2
|
require 'remote_factory_girl/json_to_active_resource'
|
3
|
+
require 'ostruct'
|
3
4
|
|
4
5
|
describe RemoteFactoryGirl::ConfigApplier do
|
5
6
|
|
6
7
|
describe '.post' do
|
7
|
-
|
8
|
-
let(:unparsed_json) {
|
9
|
-
'{ "user": {"first_name": "Sam", "last_name": "Iam"}}'
|
10
|
-
}
|
11
8
|
let(:json) {
|
12
9
|
{ :user => { :first_name => "Sam", :last_name => "Iam"}}
|
13
10
|
}
|
14
11
|
let(:hash_to_dot_klass) { double('RemoteFactoryGirl::HashToDot') }
|
15
12
|
let(:dish_json_with_user) { OpenStruct.new(:user => OpenStruct.new(:first_name => 'Sam', :last_name => 'Iam')) }
|
16
|
-
let(:
|
13
|
+
let(:dot_notation_without_root) { OpenStruct.new(:first_name => 'Sam', :last_name => 'Iam') }
|
17
14
|
|
18
15
|
describe '.apply_config' do
|
19
|
-
|
20
|
-
|
21
|
-
|
16
|
+
describe 'when configured to return root key' do
|
17
|
+
it 'should return a hash with a root key when initialized with a hash that has a root key' do
|
18
|
+
response = RemoteFactoryGirl::ConfigApplier.apply_config(json, :with_root => true)
|
19
|
+
|
20
|
+
expect(response).to have_key(:user)
|
21
|
+
end
|
22
22
|
end
|
23
23
|
|
24
|
-
|
25
|
-
|
26
|
-
|
24
|
+
describe 'when configured to not return root key' do
|
25
|
+
|
26
|
+
let(:factory_girl_json_parser) { double('RemoteFactoryGirl::FactoryGirlJsonParser', without_root: { :first_name => 'Sam', :last_name => 'Iam'}) }
|
27
|
+
|
28
|
+
it 'should not return a hash with a root key when initialized with a hash that has a root key' do
|
29
|
+
response = RemoteFactoryGirl::ConfigApplier.apply_config(json, :return_with_root => false,
|
30
|
+
:factory_girl_json_parser => factory_girl_json_parser)
|
31
|
+
|
32
|
+
expect(response).to_not have_key(:user)
|
33
|
+
expect(response[:first_name]).to eq('Sam')
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'should not return a hash with a root key when initialized with a hash that does not have a root key' do
|
37
|
+
hash_without_root_key = { :first_name => 'Sam', :last_name => 'Iam'}
|
38
|
+
response = RemoteFactoryGirl::ConfigApplier.apply_config(hash_without_root_key, :return_with_root => false,
|
39
|
+
:factory_girl_json_parser => factory_girl_json_parser)
|
40
|
+
expect(response[:first_name]).to eq('Sam')
|
41
|
+
end
|
27
42
|
end
|
28
43
|
|
29
44
|
it 'should return an object that responds to dot notation' do
|
@@ -34,10 +49,12 @@ describe RemoteFactoryGirl::ConfigApplier do
|
|
34
49
|
end
|
35
50
|
|
36
51
|
it 'should not return root hash key and should return an object that responds to dot notation' do
|
37
|
-
|
52
|
+
factory_girl_json_parser = double('RemoteFactoryGirl::FactoryGirlJsonParser', without_root: { :first_name => 'Sam', :last_name => 'Iam'})
|
53
|
+
hash_to_dot_klass.stub(:convert).and_return(dot_notation_without_root)
|
38
54
|
response = RemoteFactoryGirl::ConfigApplier.apply_config(json, :return_response_as => :dot_notation,
|
39
55
|
:return_with_root => false,
|
40
|
-
:hash_to_dot_klass => hash_to_dot_klass
|
56
|
+
:hash_to_dot_klass => hash_to_dot_klass,
|
57
|
+
:factory_girl_json_parser => factory_girl_json_parser)
|
41
58
|
expect(response.first_name).to eq('Sam')
|
42
59
|
end
|
43
60
|
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'remote_factory_girl/factory_girl_json_parser'
|
2
|
+
|
3
|
+
describe RemoteFactoryGirl::FactoryGirlJsonParser do
|
4
|
+
|
5
|
+
let(:hash_with_root) { { :user => { :first_name => 'Sam', :last_name => 'Iam' }} }
|
6
|
+
let(:hash_without_root) { { :first_name => 'Sam', :last_name => 'Iam' } }
|
7
|
+
|
8
|
+
describe '.without_root' do
|
9
|
+
it 'should return a hash without a root key when given a hash with a root key' do
|
10
|
+
without_root = RemoteFactoryGirl::FactoryGirlJsonParser.without_root(hash_with_root)
|
11
|
+
|
12
|
+
expect(without_root).to_not have_key(:user)
|
13
|
+
expect(without_root[:first_name]).to eq('Sam')
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'should return a hash without a root key when given a hash without a root key' do
|
17
|
+
without_root = RemoteFactoryGirl::FactoryGirlJsonParser.without_root(hash_with_root)
|
18
|
+
|
19
|
+
expect(without_root).to_not have_key('user')
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: remote_factory_girl
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- tdouce
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-06-
|
11
|
+
date: 2014-06-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -83,6 +83,7 @@ files:
|
|
83
83
|
- lib/remote_factory_girl/config.rb
|
84
84
|
- lib/remote_factory_girl/config_applier.rb
|
85
85
|
- lib/remote_factory_girl/exceptions.rb
|
86
|
+
- lib/remote_factory_girl/factory_girl_json_parser.rb
|
86
87
|
- lib/remote_factory_girl/hash_to_dot.rb
|
87
88
|
- lib/remote_factory_girl/http.rb
|
88
89
|
- lib/remote_factory_girl/json_to_active_resource.rb
|
@@ -91,6 +92,7 @@ files:
|
|
91
92
|
- spec/integration/remote_factory_girl_spec.rb
|
92
93
|
- spec/models/remote_factory_girl/config_applier_spec.rb
|
93
94
|
- spec/models/remote_factory_girl/config_spec.rb
|
95
|
+
- spec/models/remote_factory_girl/factory_girl_json_parser_spec.rb
|
94
96
|
- spec/models/remote_factory_girl/http_spec.rb
|
95
97
|
- spec/models/remote_factory_girl/json_to_active_resource_spec.rb
|
96
98
|
- spec/models/remote_factory_girl_spec.rb
|
@@ -124,8 +126,8 @@ test_files:
|
|
124
126
|
- spec/integration/remote_factory_girl_spec.rb
|
125
127
|
- spec/models/remote_factory_girl/config_applier_spec.rb
|
126
128
|
- spec/models/remote_factory_girl/config_spec.rb
|
129
|
+
- spec/models/remote_factory_girl/factory_girl_json_parser_spec.rb
|
127
130
|
- spec/models/remote_factory_girl/http_spec.rb
|
128
131
|
- spec/models/remote_factory_girl/json_to_active_resource_spec.rb
|
129
132
|
- spec/models/remote_factory_girl_spec.rb
|
130
133
|
- spec/spec_helper.rb
|
131
|
-
has_rdoc:
|