spigot 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/README.md +25 -32
  4. data/Rakefile +6 -0
  5. data/examples/active_record.rb +18 -15
  6. data/examples/model.rb +38 -7
  7. data/lib/spigot.rb +18 -6
  8. data/lib/spigot/configuration.rb +4 -5
  9. data/lib/spigot/map/base.rb +44 -0
  10. data/lib/spigot/map/definition.rb +69 -0
  11. data/lib/spigot/map/option.rb +27 -0
  12. data/lib/spigot/map/resource.rb +37 -0
  13. data/lib/spigot/map/service.rb +43 -0
  14. data/lib/spigot/patch.rb +1 -1
  15. data/lib/spigot/proxy.rb +3 -2
  16. data/lib/spigot/translator.rb +22 -71
  17. data/lib/spigot/version.rb +1 -1
  18. data/script/console.rb +26 -20
  19. data/spec/fixtures/data/active_user.rb +17 -0
  20. data/spec/fixtures/data/post.rb +15 -0
  21. data/spec/fixtures/data/user.rb +41 -0
  22. data/spec/fixtures/mappings/active_user_map.rb +58 -22
  23. data/spec/fixtures/mappings/post_map.rb +10 -10
  24. data/spec/fixtures/mappings/user_map.rb +73 -29
  25. data/spec/spec_helper.rb +3 -12
  26. data/spec/spigot/active_record_spec.rb +34 -26
  27. data/spec/spigot/base_spec.rb +32 -1
  28. data/spec/spigot/configuration_spec.rb +0 -27
  29. data/spec/spigot/map/base_spec.rb +70 -0
  30. data/spec/spigot/map/definition_spec.rb +45 -0
  31. data/spec/spigot/map/resource_spec.rb +57 -0
  32. data/spec/spigot/map/service_spec.rb +88 -0
  33. data/spec/spigot/translator_spec.rb +110 -113
  34. data/spigot.gemspec +3 -2
  35. metadata +43 -20
  36. data/examples/.DS_Store +0 -0
  37. data/lib/.DS_Store +0 -0
  38. data/lib/spigot/config/.DS_Store +0 -0
  39. data/spec/.DS_Store +0 -0
  40. data/spec/fixtures/.DS_Store +0 -0
  41. data/spec/fixtures/api_data.rb +0 -46
  42. data/spec/spigot/factory_spec.rb +0 -5
@@ -1,22 +1,22 @@
1
1
  module Spigot
2
2
  module Mapping
3
-
4
3
  class Post
5
4
 
5
+
6
6
  def self.basic
7
- {'post' => base}
8
- end
9
7
 
10
- def self.namespaced
11
- {'wrapper/post' => base}
8
+ Spigot.define do
9
+ service :github do
10
+ resource 'wrapper/post' do
11
+ title :title
12
+ body :description
13
+ end
14
+ end
15
+ end
16
+
12
17
  end
13
18
 
14
- private
15
19
 
16
- def self.base
17
- {'title' => 'title', 'body' => 'description'}
18
- end
19
20
  end
20
-
21
21
  end
22
22
  end
@@ -1,59 +1,103 @@
1
1
  module Spigot
2
2
  module Mapping
3
-
4
3
  class User
4
+
5
5
  def self.basic
6
- {'user' => base}
6
+ template do
7
+ full_name :name
8
+ login :username
9
+ end
7
10
  end
8
11
 
9
- def self.symbolized
10
- {user: {full_name: 'name', login: 'username'}}
12
+ def self.interpolated
13
+ template do
14
+ full_name :name
15
+ login :username do |value|
16
+ "@#{value}"
17
+ end
18
+ end
11
19
  end
12
20
 
13
- def self.nested
14
- {'user' => base.merge('login' => {'email' => 'contact', 'user_name' => 'username'})}
21
+ def self.nested_interpolation
22
+ template do
23
+ full_name :name
24
+ login do
25
+ email :contact
26
+ user_name :username do |value|
27
+ "@#{value}"
28
+ end
29
+ end
30
+ end
15
31
  end
16
32
 
17
- def self.nested_twice
18
- {'user' => base.merge('login' => {
19
- 'contact' => {'work_email' => 'email', 'user_name' => 'username'}
20
- })}
33
+ def self.nested
34
+ template do
35
+ full_name :name
36
+ login do
37
+ email :contact
38
+ user_name :username
39
+ end
40
+ end
21
41
  end
22
42
 
23
- def self.array
24
- {'user' => base}
43
+ def self.nested_twice
44
+ template do
45
+ full_name :name
46
+ login do
47
+ last_seen_ip :ip
48
+ contact do
49
+ email :contact
50
+ user_name :username
51
+ end
52
+ end
53
+ end
25
54
  end
26
55
 
27
56
  def self.nested_array
28
- {'user' => {'account' => 'name', 'count' => 'user_count', 'users' => base}}
29
- end
30
-
31
- def self.nested_account_members
32
- {'activeuser' => {'account_name' => 'name', 'url' => 'url', 'members' => {'login' => 'email', 'full_name' => 'name'}}}
57
+ template do
58
+ account :name
59
+ count :user_count
60
+ users do
61
+ full_name :name
62
+ login :username
63
+ end
64
+ end
33
65
  end
34
66
 
35
67
  def self.with_options
36
- {'user' => base.merge('spigot' => options)}
68
+ template do
69
+ full_name :name
70
+ login :username
71
+ options do
72
+ primary_key :username
73
+ foreign_key :login
74
+ end
75
+ end
37
76
  end
38
77
 
39
78
  def self.with_conditions
40
- {'user' => base.merge('spigot' => options.merge(conditions))}
79
+ template do
80
+ full_name :name
81
+ login :username
82
+ options do
83
+ primary_key :username
84
+ foreign_key :login
85
+ end
86
+ end
41
87
  end
42
88
 
43
89
  private
44
90
 
45
- def self.base
46
- {'full_name' => 'name', 'login' => 'username'}
47
- end
48
-
49
- def self.options
50
- {'primary_key' => 'username', 'foreign_key' => 'login'}
91
+ def self.template(&block)
92
+ Spigot.define do
93
+ service :github do
94
+ resource :user do
95
+ self.instance_eval(&block)
96
+ end
97
+ end
98
+ end
51
99
  end
52
100
 
53
- def self.conditions
54
- {'conditions' => 'username, name'}
55
- end
56
101
  end
57
-
58
102
  end
59
103
  end
@@ -1,3 +1,6 @@
1
+ require 'simplecov'
2
+ SimpleCov.start
3
+
1
4
  require 'rspec'
2
5
  require 'spigot'
3
6
  require 'hashie'
@@ -20,18 +23,6 @@ module Wrapper
20
23
  Post = Class.new(Hashie::Mash)
21
24
  end
22
25
 
23
- def with_mapping(name, map)
24
- before do
25
- instance_variable_set("@prior_#{name}".to_sym, Spigot.config.translations)
26
- Spigot.configure{|c| c.translations = map }
27
- end
28
-
29
- after do
30
- map_cache = instance_variable_get("@prior_#{name}".to_sym)
31
- Spigot.configure{|c| c.translations = map_cache }
32
- end
33
- end
34
-
35
26
  RSpec.configure do |config|
36
27
  config.after(:each) do
37
28
  ActiveUser.delete_all
@@ -2,48 +2,46 @@ require 'spec_helper'
2
2
 
3
3
  describe Spigot::ActiveRecord do
4
4
  let(:subject){ActiveUser}
5
- let(:service){:github}
6
- let(:data){ Spigot::ApiData.user.merge(id: '987') }
5
+ let(:data){ Spigot::Data::ActiveUser.basic.merge(id: '987') }
6
+ let(:user){ subject.create(name: 'Dean Martin', username: 'classyasfuck') }
7
7
 
8
8
  context 'with invalid mapping' do
9
- with_mapping(:basic_active_user, Spigot::Mapping::ActiveUser.with_invalid_options)
10
-
11
- context '#find_by_api' do
12
- it 'requires the primary key to be accurate' do
13
- expect {
14
- subject.find_by_api(service, {full_name: 'Dean Martin'}).should_not be_nil
15
- }.to raise_error(Spigot::InvalidSchemaError)
16
- end
9
+ it 'requires the primary key to be accurate' do
10
+ expect {
11
+ subject.find_by_api(:github, {full_name: 'Dean Martin'})
12
+ }.to raise_error(Spigot::MissingResourceError)
17
13
  end
18
14
  end
19
15
 
20
16
  context 'with valid mapping' do
21
- with_mapping(:active_user_with_options, Spigot::Mapping::ActiveUser.with_options)
22
- let(:user){ subject.create(name: 'Dean Martin', username: 'classyasfuck') }
23
-
24
17
  context '#find_by_api' do
25
- before{ user }
18
+ before do
19
+ user
20
+ Spigot::Mapping::ActiveUser.with_options
21
+ end
26
22
 
27
23
  it 'queries by the specified primary_key' do
28
- subject.find_by_api(service, data).should eq(user)
24
+ subject.find_by_api(:github, data).should eq(user)
29
25
  end
30
26
  end
31
27
 
32
28
  context '#find_all_by_api' do
33
- with_mapping(:non_unique_key, Spigot::Mapping::ActiveUser.non_unique_key)
34
29
  before do
30
+ Spigot::Mapping::ActiveUser.non_unique_keys
35
31
  user.update_attribute(:token, '123abc')
36
32
  subject.create(name: 'Frank Sinatra', username: 'livetilidie', token: '123abc')
37
33
  end
38
34
 
39
35
  it 'returns all records matching primary key' do
40
- subject.find_all_by_api(service, data).length.should eq(2)
36
+ subject.find_all_by_api(:github, data).length.should eq(2)
41
37
  end
42
38
  end
43
39
 
44
40
  context '#create_by_api' do
41
+ before{ Spigot::Mapping::ActiveUser.with_options }
45
42
  it 'creates a record' do
46
- record = subject.create_by_api(service, data)
43
+ record = subject.create_by_api(:github, data)
44
+ record.id.should_not be_nil
47
45
  record.name.should eq('Dean Martin')
48
46
  record.username.should eq('classyasfuck')
49
47
  record.token.should be_nil
@@ -51,22 +49,29 @@ describe Spigot::ActiveRecord do
51
49
  end
52
50
 
53
51
  context '#update_by_api' do
54
- before{ user }
52
+ before do
53
+ user
54
+ Spigot::Mapping::ActiveUser.with_options
55
+ end
56
+
55
57
  it 'updates a record' do
56
- record = subject.update_by_api(service, data.merge(full_name: 'Dino Baby'))
58
+ record = subject.update_by_api(:github, data.merge(full_name: 'Dino Baby'))
57
59
  record.name.should eq('Dino Baby')
58
60
  end
59
61
  end
60
62
 
61
63
  context '#find_or_create_by_api' do
62
- before{ user }
64
+ before do
65
+ user
66
+ Spigot::Mapping::ActiveUser.with_options
67
+ end
63
68
  it 'returns an existing record' do
64
- record = subject.find_or_create_by_api(service, data)
69
+ record = subject.find_or_create_by_api(:github, data)
65
70
  record.id.should eq(user.id)
66
71
  end
67
72
 
68
73
  it 'creates a record when none exists' do
69
- record = subject.find_or_create_by_api(service, Spigot::ApiData.updated_user)
74
+ record = subject.find_or_create_by_api(:github, Spigot::Data::ActiveUser.alt)
70
75
  record.id.should_not eq(user.id)
71
76
  record.name.should eq('Frank Sinatra')
72
77
  record.username.should eq('livetilidie')
@@ -74,17 +79,20 @@ describe Spigot::ActiveRecord do
74
79
  end
75
80
 
76
81
  context '#create_or_update_by_api' do
77
- before{ user }
82
+ before do
83
+ user
84
+ Spigot::Mapping::ActiveUser.with_options
85
+ end
78
86
 
79
87
  it 'updates an existing record' do
80
- record = subject.create_or_update_by_api(service, data.merge(full_name: 'Dino Baby'))
88
+ record = subject.create_or_update_by_api(:github, data.merge(full_name: 'Dino Baby'))
81
89
  record.id.should eq(user.id)
82
90
  record.name.should eq('Dino Baby')
83
91
  record.username.should eq('classyasfuck')
84
92
  end
85
93
 
86
94
  it 'creates a record when none exists' do
87
- record = subject.create_or_update_by_api(service, Spigot::ApiData.updated_user)
95
+ record = subject.create_or_update_by_api(:github, Spigot::Data::ActiveUser.alt)
88
96
  record.id.should_not eq(user.id)
89
97
  record.name.should eq('Frank Sinatra')
90
98
  record.username.should eq('livetilidie')
@@ -1,7 +1,38 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Spigot::Base do
4
+ let(:data){Spigot::Data::User.basic}
5
+ before{ Spigot::Mapping::ActiveUser.stub }
4
6
 
5
-
7
+ context '#new_by_api' do
8
+ it 'instantiates a record' do
9
+ Spigot::Record.should_receive(:instantiate)
10
+ ActiveUser.new_by_api(:github, data)
11
+ end
12
+ end
13
+
14
+ context '#formatted_api_data' do
15
+ it 'calls format on the translator' do
16
+ Spigot::Translator.any_instance.should_receive(:format)
17
+ ActiveUser.formatted_api_data(:github, data)
18
+ end
19
+
20
+ it 'returns formatted data' do
21
+ Spigot::Translator.any_instance.should_receive(:format)
22
+ formatted = ActiveUser.formatted_api_data(:github, data)
23
+ end
24
+ end
25
+
26
+ context '#spigot' do
27
+ it 'returns a spigot proxy' do
28
+ ActiveUser.spigot(:github).should be_a_kind_of(Spigot::Proxy)
29
+ end
30
+
31
+ it 'requires a valid service' do
32
+ expect{
33
+ ActiveUser.spigot(:invalid)
34
+ }.to raise_error(Spigot::MissingServiceError)
35
+ end
36
+ end
6
37
 
7
38
  end
@@ -1,14 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Spigot::Configuration do
4
- before do
5
- @prior_translations = Spigot.config.translations
6
- end
7
-
8
- after do
9
- Spigot.configure{|c| c.translations = @prior_translations }
10
- end
11
-
12
4
  context 'defaults' do
13
5
  it 'is a hash of default configuration' do
14
6
  expect(Spigot::Configuration.defaults).to be_kind_of(Hash)
@@ -16,14 +8,6 @@ describe Spigot::Configuration do
16
8
  end
17
9
 
18
10
  context 'access' do
19
- before do
20
- @previous_path = Spigot.config.path
21
- end
22
-
23
- after do
24
- Spigot.configure{|config| config.path = @previous_path }
25
- end
26
-
27
11
  it "is callable from .configure" do
28
12
  Spigot.configure do |c|
29
13
  expect(c).to be_kind_of(Spigot::Configuration)
@@ -31,20 +15,9 @@ describe Spigot::Configuration do
31
15
  end
32
16
 
33
17
  context 'options' do
34
- let(:path){'/baller'}
35
18
  let(:map){{'user' => {a: 1}}}
36
19
  let(:options_key){'my_special_key'}
37
20
 
38
- it "is able to set the path" do
39
- Spigot.configure{|config| config.path = path }
40
- expect(Spigot.config.path).to eq(path)
41
- end
42
-
43
- it "is able to set translations" do
44
- Spigot.configure{|config| config.translations = map }
45
- expect(Spigot.config.translations).to eq(map)
46
- end
47
-
48
21
  it "is able to set the options_key" do
49
22
  Spigot.configure{|config| config.options_key = options_key }
50
23
  expect(Spigot.config.options_key).to eq(options_key)
@@ -0,0 +1,70 @@
1
+ require 'spec_helper'
2
+
3
+ describe Spigot::Map::Base do
4
+ let(:subject){Spigot::Map::Base.new}
5
+ let(:service){Spigot::Map::Service.new(:github)}
6
+
7
+ context '#initialize' do
8
+ it 'initializes a services array' do
9
+ subject.services.should eq([])
10
+ end
11
+ end
12
+
13
+ context '.define' do
14
+ it 'accepts a block' do
15
+ Spigot::Map::Service.should_receive(:class_eval)
16
+ subject.define{'foo'}
17
+ end
18
+
19
+ it 'does not require a block' do
20
+ Spigot::Map::Service.should_not_receive(:class_eval)
21
+ subject.define
22
+ end
23
+
24
+ it 'works with one service' do
25
+ subject.define do
26
+ service :github
27
+ end
28
+
29
+ subject.services.length.should eq(1)
30
+ end
31
+
32
+ it 'allows multiple services' do
33
+ subject.define do
34
+ service :github
35
+ service :twitter
36
+ end
37
+
38
+ subject.services.length.should eq(2)
39
+ end
40
+
41
+ it 'allows multiple updates' do
42
+ subject.define{ service(:github) }
43
+ subject.define{ service(:twitter) }
44
+
45
+ subject.services.length.should eq(2)
46
+ end
47
+ end
48
+
49
+ context '.reset' do
50
+ it 'resets services' do
51
+ subject.reset
52
+ subject.services.should eq([])
53
+ end
54
+ end
55
+
56
+ context '.service' do
57
+ it 'finds an existing service' do
58
+ subject.update(:github, service)
59
+ subject.service(:github).should eq(service)
60
+ end
61
+ end
62
+
63
+ context '.to_hash' do
64
+ it 'returns a hash of current services' do
65
+ subject.update(:github, service)
66
+ subject.to_hash.should eq({github: {}})
67
+ end
68
+ end
69
+
70
+ end