spigot 0.2.2 → 0.3.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.
Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/README.md +5 -3
  4. data/Rakefile +1 -5
  5. data/examples/active_record.rb +18 -28
  6. data/examples/model.rb +4 -4
  7. data/examples/multi_resource.rb +61 -0
  8. data/lib/spigot.rb +11 -7
  9. data/lib/spigot/active_record.rb +28 -33
  10. data/lib/spigot/base.rb +5 -12
  11. data/lib/spigot/configuration.rb +1 -1
  12. data/lib/spigot/map/base.rb +4 -5
  13. data/lib/spigot/map/definition.rb +19 -9
  14. data/lib/spigot/map/option.rb +1 -13
  15. data/lib/spigot/map/resource.rb +11 -6
  16. data/lib/spigot/map/service.rb +5 -5
  17. data/lib/spigot/patch.rb +5 -5
  18. data/lib/spigot/proxy.rb +32 -20
  19. data/lib/spigot/record.rb +40 -13
  20. data/lib/spigot/translator.rb +27 -43
  21. data/lib/spigot/version.rb +2 -1
  22. data/script/active_record.rb +35 -0
  23. data/script/console.rb +19 -2
  24. data/spec/fixtures/data/active_user.rb +2 -4
  25. data/spec/fixtures/data/post.rb +1 -5
  26. data/spec/fixtures/data/user.rb +5 -5
  27. data/spec/fixtures/mappings/active_user_map.rb +11 -5
  28. data/spec/fixtures/mappings/post_map.rb +6 -18
  29. data/spec/fixtures/mappings/user_map.rb +1 -5
  30. data/spec/spec_helper.rb +13 -6
  31. data/spec/spigot/active_record_spec.rb +12 -5
  32. data/spec/spigot/base_spec.rb +2 -14
  33. data/spec/spigot/configuration_spec.rb +7 -7
  34. data/spec/spigot/map/base_spec.rb +12 -6
  35. data/spec/spigot/map/definition_spec.rb +51 -4
  36. data/spec/spigot/map/resource_spec.rb +4 -4
  37. data/spec/spigot/map/service_spec.rb +19 -14
  38. data/spec/spigot/patch_spec.rb +12 -0
  39. data/spec/spigot/proxy_spec.rb +17 -17
  40. data/spec/spigot/record_spec.rb +75 -4
  41. data/spec/spigot/spigot_spec.rb +9 -4
  42. data/spec/spigot/translator_spec.rb +86 -87
  43. data/spigot.gemspec +9 -10
  44. metadata +33 -47
  45. data/lib/spigot/config/spigot/github.yml +0 -7
  46. data/spec/support/active_record.rb +0 -15
@@ -1,7 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Spigot::Map::Definition do
4
- let(:resource){Spigot::Map::Resource.new(:user)}
4
+ let(:resource) { Spigot::Map::Resource.new(:user) }
5
5
 
6
6
  context '#initialize' do
7
7
  it 'assigns variables' do
@@ -12,14 +12,51 @@ describe Spigot::Map::Definition do
12
12
 
13
13
  context '#with a block' do
14
14
  it 'assigns parse' do
15
- subject = Spigot::Map::Definition.new(:foo, 'bar'){'baz'}
15
+ subject = Spigot::Map::Definition.new(:foo, 'bar') { 'baz' }
16
16
  subject.instance_variable_get(:@parse).should_not be_nil
17
17
  end
18
18
  end
19
19
  end
20
20
 
21
- context '#define' do
21
+ context '#parse' do
22
+ before do
23
+ Spigot.define do
24
+ resource :active_user do
25
+ name :name
26
+ end
27
+ end
28
+ end
22
29
 
30
+ it 'raises invalid schema if parsing data is not a hash' do
31
+ subject = Spigot::Map::Definition.define(resource, :foo, ActiveUser)
32
+ expect {
33
+ subject.parse(foo: 'b')
34
+ }.to raise_error(Spigot::InvalidSchemaError)
35
+ end
36
+
37
+ it 'does not attach the sub resource' do
38
+ subject = Spigot::Map::Definition.define(resource, :foo, ActiveUser)
39
+ subject.parse(foo: { name: 'Dean' }).should eq(active_user: { name: 'Dean' })
40
+ subject.instance_variable_get(:@parse).should be_nil
41
+ subject.instance_variable_get(:@children).length.should eq(0)
42
+ end
43
+
44
+ context 'with children' do
45
+ it 'does not attach the sub resource' do
46
+ subject = Spigot::Map::Definition.define(resource, :foo) do
47
+ bar :baz
48
+ qux ActiveUser
49
+ end
50
+ subject.parse({
51
+ foo: {
52
+ bar: 'a', qux: { name: 'Frank' }
53
+ }
54
+ }).should eq(baz: 'a', active_user: { name: 'Frank' })
55
+ end
56
+ end
57
+ end
58
+
59
+ context '#define' do
23
60
  it 'returns a definition with the given key and value' do
24
61
  subject = Spigot::Map::Definition.define(resource, :foo, 'bar')
25
62
  subject.instance_variable_get(:@name).should eq(:foo)
@@ -36,10 +73,20 @@ describe Spigot::Map::Definition do
36
73
  end
37
74
 
38
75
  it 'assigns a parse block' do
39
- subject = Spigot::Map::Definition.define(resource, :foo){|val| "formatted-#{val}" }
76
+ subject = Spigot::Map::Definition.define(resource, :foo) { |val| "formatted-#{val}" }
40
77
  subject.instance_variable_get(:@parse).should_not be_nil
41
78
  subject.instance_variable_get(:@children).length.should eq(0)
42
79
  end
43
80
  end
44
81
 
82
+ context '#to_hash' do
83
+ it 'returns a hash of values' do
84
+ subject = Spigot::Map::Definition.define(resource, :foo) do
85
+ bar :baz
86
+ qux :mjw
87
+ end
88
+ subject.to_hash.should eq(foo: { bar: :baz, qux: :mjw })
89
+ end
90
+ end
91
+
45
92
  end
@@ -1,7 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Spigot::Map::Resource do
4
- let(:subject){Spigot::Map::Resource.new(:user){ username :login }}
4
+ let(:subject) { Spigot::Map::Resource.new(:user) { username :login } }
5
5
 
6
6
  context '#initialize' do
7
7
  it 'assigns a name' do
@@ -9,14 +9,14 @@ describe Spigot::Map::Resource do
9
9
  end
10
10
 
11
11
  it 'does not require a block' do
12
- expect{
12
+ expect {
13
13
  Spigot::Map::Resource.new(:user)
14
14
  }.to_not raise_error(ArgumentError)
15
15
  end
16
16
 
17
17
  it 'builds definitions included in block' do
18
18
  Spigot::Map::Definition.should_receive(:define)
19
- subject #=> Evaluate the let statement
19
+ subject # Evaluate the let statement
20
20
  end
21
21
  end
22
22
 
@@ -36,7 +36,7 @@ describe Spigot::Map::Resource do
36
36
  end
37
37
 
38
38
  context '.options' do
39
- let(:subject){Spigot::Map::Resource.new(:user){ options{ primary_key :foo } }}
39
+ let(:subject) { Spigot::Map::Resource.new(:user) { options { primary_key :foo } } }
40
40
 
41
41
  it 'sets the options' do
42
42
  options = subject.instance_variable_get(:@options)
@@ -1,10 +1,10 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Spigot::Map::Service do
4
- let(:klass){ Spigot::Map::Service }
5
- let(:subject){ klass.new(:github) }
4
+ let(:klass) { Spigot::Map::Service }
5
+ let(:subject) { klass.new(:github) }
6
6
 
7
- before{ Spigot::Map::Base.new }
7
+ before { Spigot::Map::Base.new }
8
8
 
9
9
  context '#initialize' do
10
10
  it 'assigns a name' do
@@ -20,12 +20,12 @@ describe Spigot::Map::Service do
20
20
  it 'works with a block' do
21
21
  Spigot::Map::Service.should_receive(:new).with(:github)
22
22
 
23
- klass.service(:github){'foo'}
23
+ klass.service(:github) { 'foo' }
24
24
  end
25
25
 
26
26
  it 'works without a block' do
27
27
  Spigot::Map::Service.should_receive(:new).with(:github)
28
- expect{
28
+ expect {
29
29
  klass.service(:github)
30
30
  }.to_not raise_error(ArgumentError)
31
31
  end
@@ -36,7 +36,7 @@ describe Spigot::Map::Service do
36
36
  end
37
37
 
38
38
  context 'duplicate services' do
39
- let(:service){Spigot::Map::Service.new(:github)}
39
+ let(:service) { Spigot::Map::Service.new(:github) }
40
40
  before do
41
41
  Spigot.config.map.update(:github, service)
42
42
  end
@@ -44,7 +44,7 @@ describe Spigot::Map::Service do
44
44
  it 'uses an existing service if already defined' do
45
45
  service.should_receive(:instance_eval)
46
46
  Spigot.config.map.should_receive(:update).with(:github, service)
47
- klass.service(:github){'foo'}
47
+ klass.service(:github) { 'foo' }
48
48
  end
49
49
 
50
50
  it 'does not duplicate services' do
@@ -55,9 +55,9 @@ describe Spigot::Map::Service do
55
55
  end
56
56
 
57
57
  context '#extract' do
58
- let(:data){{a: 1}}
58
+ let(:data) { { a: 1 } }
59
59
  context 'with no map defined' do
60
- before{ Spigot.config.reset }
60
+ before { Spigot.config.reset }
61
61
  it 'returns passed params' do
62
62
  klass.extract(data).should eq(data)
63
63
  end
@@ -80,7 +80,12 @@ describe Spigot::Map::Service do
80
80
 
81
81
  it 'does not return an unmatched service' do
82
82
  Spigot::Mapping::ActiveUser.stub
83
- klass.extract(twitter: data).should eq([nil, {twitter: data}])
83
+ klass.extract(twitter: data).should eq([nil, { twitter: data }])
84
+ end
85
+
86
+ it 'assumes no service when it receives an array' do
87
+ Spigot::Mapping::ActiveUser.stub
88
+ klass.extract([data, data]).should eq([nil, [data, data]])
84
89
  end
85
90
  end
86
91
  end
@@ -88,11 +93,11 @@ describe Spigot::Map::Service do
88
93
  context '.resource' do
89
94
  it 'builds a resource' do
90
95
  Spigot::Map::Resource.should_receive(:new).with(:user)
91
- subject.resource(:user){'foo'}
96
+ subject.resource(:user) { 'foo' }
92
97
  end
93
98
 
94
99
  it 'does not require a block' do
95
- expect{
100
+ expect {
96
101
  subject.resource(:user)
97
102
  }.to_not raise_error(ArgumentError)
98
103
  end
@@ -101,9 +106,9 @@ describe Spigot::Map::Service do
101
106
  context '.reset' do
102
107
  it 'resets the resources' do
103
108
  subject.resource(:user)
104
- expect{
109
+ expect {
105
110
  subject.reset
106
- }.to change{subject.resources.length}.by(-1)
111
+ }.to change { subject.resources.length }.by(-1)
107
112
  end
108
113
  end
109
114
 
@@ -0,0 +1,12 @@
1
+ require 'spec_helper'
2
+
3
+ describe String do
4
+
5
+ context '#underscore' do
6
+ let(:subject) { 'FooBar' }
7
+ it 'converts to underscore' do
8
+ subject.underscore.should eq('foo_bar')
9
+ end
10
+ end
11
+
12
+ end
@@ -3,7 +3,7 @@ require 'spec_helper'
3
3
  describe Spigot::Proxy do
4
4
 
5
5
  context '#initialize' do
6
- let(:subject){Spigot::Proxy}
6
+ let(:subject) { Spigot::Proxy }
7
7
  it 'accepts a service and a resource' do
8
8
  proxy = subject.new(User, :github)
9
9
  proxy.resource.should eq(User)
@@ -18,61 +18,61 @@ describe Spigot::Proxy do
18
18
  end
19
19
 
20
20
  context 'Active Record aliases' do
21
- let(:subject){Spigot::Proxy.new(ActiveUser)}
21
+ let(:subject) { Spigot::Proxy.new(ActiveUser) }
22
22
  it 'aliases find' do
23
23
  ActiveUser.should_receive(:find_by_api)
24
- subject.find({a: 1})
24
+ subject.find(a: 1)
25
25
  end
26
26
 
27
27
  it 'aliases find_all' do
28
28
  ActiveUser.should_receive(:find_all_by_api)
29
- subject.find_all({a: 1})
29
+ subject.find_all(a: 1)
30
30
  end
31
31
 
32
32
  it 'aliases create' do
33
33
  ActiveUser.should_receive(:create_by_api)
34
- subject.create({a: 1})
34
+ subject.create(a: 1)
35
35
  end
36
36
 
37
37
  it 'aliases update' do
38
38
  ActiveUser.should_receive(:update_by_api)
39
- subject.update({a: 1})
39
+ subject.update(a: 1)
40
40
  end
41
41
 
42
42
  it 'aliases find_or_create' do
43
43
  ActiveUser.should_receive(:find_or_create_by_api)
44
- subject.find_or_create({a: 1})
44
+ subject.find_or_create(a: 1)
45
45
  end
46
46
 
47
47
  it 'aliases create_or_update' do
48
48
  ActiveUser.should_receive(:create_or_update_by_api)
49
- subject.create_or_update({a: 1})
49
+ subject.create_or_update(a: 1)
50
50
  end
51
51
 
52
52
  context 'with a specified service' do
53
- let(:subject){Spigot::Proxy.new(ActiveUser, :github)}
54
- before{ Spigot::Mapping::ActiveUser.stub }
53
+ let(:subject) { Spigot::Proxy.new(ActiveUser, :github) }
54
+ before { Spigot::Mapping::ActiveUser.stub }
55
55
  it 'uses the current service' do
56
- ActiveUser.should_receive(:create_by_api).with({github: {a: 1}})
57
- subject.create({a: 1})
56
+ ActiveUser.should_receive(:create_by_api).with(github: { a: 1 })
57
+ subject.create(a: 1)
58
58
  end
59
59
 
60
60
  it 'checks for conflicting services' do
61
61
  Spigot::Mapping::ActiveUser.twitter
62
- expect{
63
- subject.create(twitter: {a: 1})
62
+ expect {
63
+ subject.create(twitter: { a: 1 })
64
64
  }.to raise_error(Spigot::InvalidServiceError)
65
65
  end
66
66
 
67
67
  it 'only scopes by one service if both are defined' do
68
- ActiveUser.should_receive(:create_by_api).with({github: {a: 1}})
69
- subject.create(github: {a: 1})
68
+ ActiveUser.should_receive(:create_by_api).with(github: { a: 1 })
69
+ subject.create(github: { a: 1 })
70
70
  end
71
71
  end
72
72
  end
73
73
 
74
74
  context 'instance methods' do
75
- let(:subject){Spigot::Proxy.new(User, :github)}
75
+ let(:subject) { Spigot::Proxy.new(User, :github) }
76
76
 
77
77
  context '.translator' do
78
78
  it 'returns a translator object' do
@@ -1,14 +1,14 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Spigot::Record do
4
- let(:resource){User}
5
- let(:data){ { a: 1 } }
6
- let(:subject){ Spigot::Record.new(resource, data) }
4
+ let(:resource) { ActiveUser }
5
+ let(:data) { { username: 'dino', name: 'Dean Martin' } }
6
+ let(:subject) { Spigot::Record.new(:github, resource, data) }
7
7
 
8
8
  context '#instantiate' do
9
9
  it 'exectutes instantiate on an instance' do
10
10
  Spigot::Record.any_instance.should_receive(:instantiate)
11
- Spigot::Record.instantiate(resource, data)
11
+ Spigot::Record.instantiate(:github, resource, data)
12
12
  end
13
13
  end
14
14
 
@@ -19,4 +19,75 @@ describe Spigot::Record do
19
19
  end
20
20
  end
21
21
 
22
+ context '#create' do
23
+ it 'handles an array of values' do
24
+ Spigot::Mapping::ActiveUser.stub
25
+ expect {
26
+ Spigot::Record.create(:github, resource, [data, data, data])
27
+ }.to change { ActiveUser.count }.by(3)
28
+ end
29
+
30
+ context 'with associations' do
31
+ before do
32
+ Spigot.define do
33
+ service :github do
34
+ resource :event do
35
+ id :github_id
36
+ type :name
37
+ author ActiveUser
38
+ end
39
+ end
40
+ end
41
+ end
42
+
43
+ it 'handles an association' do
44
+ expect {
45
+ Spigot::Record.create(:github, Event, github_id: 123, name: 'Push', active_user: data)
46
+ }.to change { ActiveUser.count }.by(1)
47
+ end
48
+
49
+ it 'handles an array of associations' do
50
+ values = [
51
+ { github_id: 123, name: 'Push', active_user: data },
52
+ { github_id: 456, name: 'Comment', active_user: data },
53
+ { github_id: 789, name: 'Commit', active_user: data }]
54
+
55
+ expect {
56
+ Spigot::Record.create(:github, Event, values)
57
+ }.to change { ActiveUser.count }.by(3)
58
+ end
59
+
60
+ it 'handles nested associations' do
61
+ Spigot.config.reset
62
+ Spigot.define do
63
+ service :github do
64
+ resource :profile do
65
+ image image_url
66
+ end
67
+
68
+ resource :active_user do
69
+ full_name :name
70
+ login :username
71
+ profile Profile
72
+ end
73
+
74
+ resource :event do
75
+ id :github_id
76
+ type :name
77
+ author ActiveUser
78
+ end
79
+ end
80
+ end
81
+
82
+ user_data = { username: 'dino', name: 'Dean Martin', profile: { image_url: 'abc' } }
83
+ event_data = { github_id: 123, name: 'Push', active_user: user_data }
84
+ expect {
85
+ Spigot::Record.create(:github, Event, event_data)
86
+ }.to change { Profile.count }.by(1)
87
+
88
+ Profile.last.image_url.should eq('abc')
89
+ end
90
+ end
91
+ end
92
+
22
93
  end
@@ -1,24 +1,24 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Spigot do
4
- let(:subject){Spigot}
4
+ let(:subject) { Spigot }
5
5
  context '#resource' do
6
6
  it 'defines a resource' do
7
7
  Spigot.should_receive(:define)
8
- subject.resource(:user){'foo'}
8
+ subject.resource(:user) { 'foo' }
9
9
  end
10
10
  end
11
11
 
12
12
  context '#service' do
13
13
  it 'defines a service' do
14
14
  Spigot.should_receive(:define)
15
- subject.service(:github){'foo'}
15
+ subject.service(:github) { 'foo' }
16
16
  end
17
17
 
18
18
  it 'continues a normal definition' do
19
19
  Spigot::Map::Service.any_instance.should_receive(:resource)
20
20
  subject.service(:github) do
21
- resource(:user){ 'foo' }
21
+ resource(:user) { 'foo' }
22
22
  end
23
23
  end
24
24
  end
@@ -28,5 +28,10 @@ describe Spigot do
28
28
  Logger.should_receive(:new).with(STDOUT)
29
29
  Spigot.logger
30
30
  end
31
+
32
+ it 'sets level and formatter' do
33
+ Logger.should_receive(:new).and_return(mock(:level= => true, :formatter= => true))
34
+ Spigot.logger
35
+ end
31
36
  end
32
37
  end