spigot 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
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,3 +1,4 @@
1
+ # Spigot::VERSION
1
2
  module Spigot
2
- VERSION = "0.2.2"
3
+ VERSION = '0.3.0'
3
4
  end
@@ -0,0 +1,35 @@
1
+ ActiveRecord::Base.establish_connection({
2
+ adapter: 'sqlite3',
3
+ database: ':memory:'
4
+ })
5
+
6
+ ActiveRecord::Schema.define do
7
+ self.verbose = false
8
+
9
+ create_table :active_users, force: true do |t|
10
+ t.integer :github_id
11
+ t.integer :profile_id
12
+ t.string :name
13
+ t.string :username
14
+ t.string :token
15
+ end
16
+
17
+ create_table :posts, force: true do |t|
18
+ t.integer :active_user_id
19
+ t.string :title
20
+ t.text :body
21
+ t.timestamps
22
+ end
23
+
24
+ create_table :events, force: true do |t|
25
+ t.string :github_id
26
+ t.integer :active_user_id
27
+ t.string :name
28
+ t.timestamps
29
+ end
30
+
31
+ create_table :profiles, force: true do |t|
32
+ t.string :image_url
33
+ t.timestamps
34
+ end
35
+ end
@@ -15,6 +15,17 @@ Spigot.resource(:active_user) do
15
15
  "https://github.com/#{value}"
16
16
  end
17
17
  end
18
+ options do
19
+ primary_key :username
20
+ end
21
+ end
22
+
23
+ Spigot.resource(:post) do
24
+ headline :title
25
+ content :body
26
+ options do
27
+ primary_key :username
28
+ end
18
29
  end
19
30
 
20
31
  Spigot.service(:twitter) do
@@ -25,10 +36,16 @@ Spigot.service(:twitter) do
25
36
  end
26
37
 
27
38
  ActiveRecord::Base.logger = Spigot.logger
28
- require File.join(Spigot.root, 'spec', 'support', 'active_record')
39
+ require File.join(Spigot.root, 'script', 'active_record')
29
40
 
30
41
  class ActiveUser < ActiveRecord::Base
31
42
  include Spigot::Base
43
+ has_many :posts
44
+ end
45
+
46
+ class Post < ActiveRecord::Base
47
+ include Spigot::Base
48
+ belongs_to :user
32
49
  end
33
50
 
34
- user = ActiveUser.create(name: 'Matt', username: 'mttwrnr', token: 'abc123')
51
+ ActiveUser.create(name: 'Matt', username: 'mwerner', token: 'abc123')
@@ -2,15 +2,13 @@ module Spigot
2
2
  module Data
3
3
  class ActiveUser
4
4
  class << self
5
-
6
5
  def basic
7
- {full_name: 'Dean Martin', login: 'classyasfuck', auth_token: '123abc'}
6
+ { full_name: 'Dean Martin', login: 'classyasfuck', auth_token: '123abc' }
8
7
  end
9
8
 
10
9
  def alt
11
- {full_name: 'Frank Sinatra', login: 'livetilidie', auth_token: '987zyx'}
10
+ { full_name: 'Frank Sinatra', login: 'livetilidie', auth_token: '987zyx' }
12
11
  end
13
-
14
12
  end
15
13
  end
16
14
  end
@@ -3,11 +3,7 @@ module Spigot
3
3
  class Post
4
4
  class << self
5
5
  def basic
6
- {'title' => 'Brief Article', 'body' => 'lorem ipsum'}
7
- end
8
-
9
- def alt
10
- {'title' => 'Regular Article', 'body' => 'dolor sit amet', 'author' => 'Dean Martin'}
6
+ { 'title' => 'Brief Article', 'body' => 'lorem ipsum' }
11
7
  end
12
8
  end
13
9
  end
@@ -3,11 +3,11 @@ module Spigot
3
3
  class User
4
4
  class << self
5
5
  def basic
6
- {'id' => '123', 'full_name' => 'Dean Martin', 'login' => 'classyasfuck'}
6
+ { 'id' => '123', 'full_name' => 'Dean Martin', 'login' => 'classyasfuck' }
7
7
  end
8
8
 
9
9
  def alt
10
- {'full_name' => 'Frank Sinatra', 'login' => 'livetilidie', 'auth_token' => '456bcd'}
10
+ { 'full_name' => 'Frank Sinatra', 'login' => 'livetilidie', 'auth_token' => '456bcd' }
11
11
  end
12
12
 
13
13
  def full
@@ -19,7 +19,7 @@ module Spigot
19
19
  end
20
20
 
21
21
  def nested_array
22
- {'account' => 'Rockafella', 'users' => array, 'count' => 2}
22
+ { 'account' => 'Rockafella', 'users' => array, 'count' => 2 }
23
23
  end
24
24
 
25
25
  def nested
@@ -27,13 +27,13 @@ module Spigot
27
27
  end
28
28
 
29
29
  def double_nested
30
- full.merge('login' => {'contact' => login_info, 'last_seen_ip' => '127.0.0.1'})
30
+ full.merge('login' => { 'contact' => login_info, 'last_seen_ip' => '127.0.0.1' })
31
31
  end
32
32
 
33
33
  private
34
34
 
35
35
  def login_info
36
- {'email' => 'dino@amore.io', 'user_name' => 'classyasfuck'}
36
+ { 'email' => 'dino@amore.io', 'user_name' => 'classyasfuck' }
37
37
  end
38
38
  end
39
39
  end
@@ -1,7 +1,6 @@
1
1
  module Spigot
2
2
  module Mapping
3
3
  class ActiveUser
4
-
5
4
  def self.stub
6
5
  template do
7
6
  login :username
@@ -24,7 +23,16 @@ module Spigot
24
23
  full_name :name
25
24
  options do
26
25
  primary_key :username
27
- foreign_key :login
26
+ end
27
+ end
28
+ end
29
+
30
+ def self.invalid_primary_key
31
+ template do
32
+ login :username
33
+ full_name :name
34
+ options do
35
+ primary_key :doesnotexist
28
36
  end
29
37
  end
30
38
  end
@@ -36,7 +44,6 @@ module Spigot
36
44
  auth_token :token
37
45
  options do
38
46
  primary_key :token
39
- foreign_key :auth_token
40
47
  end
41
48
  end
42
49
  end
@@ -47,12 +54,11 @@ module Spigot
47
54
  Spigot.define do
48
55
  service :github do
49
56
  resource :active_user do
50
- self.instance_eval(&block)
57
+ instance_eval(&block)
51
58
  end
52
59
  end
53
60
  end
54
61
  end
55
-
56
62
  end
57
63
  end
58
64
  end
@@ -1,22 +1,10 @@
1
- module Spigot
2
- module Mapping
3
- class Post
4
-
5
-
6
- def self.basic
7
-
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
-
1
+ module Spigot::Mapping::Post
2
+ def self.basic
3
+ Spigot.service(:github) do
4
+ resource 'wrapper/post' do
5
+ title :title
6
+ body :description
17
7
  end
18
-
19
-
20
8
  end
21
9
  end
22
10
  end
@@ -1,7 +1,6 @@
1
1
  module Spigot
2
2
  module Mapping
3
3
  class User
4
-
5
4
  def self.basic
6
5
  template do
7
6
  full_name :name
@@ -116,7 +115,6 @@ module Spigot
116
115
  login :username
117
116
  options do
118
117
  primary_key :username
119
- foreign_key :login
120
118
  end
121
119
  end
122
120
  end
@@ -127,7 +125,6 @@ module Spigot
127
125
  login :username
128
126
  options do
129
127
  primary_key :username
130
- foreign_key :login
131
128
  end
132
129
  end
133
130
  end
@@ -138,12 +135,11 @@ module Spigot
138
135
  Spigot.define do
139
136
  service :github do
140
137
  resource :user do
141
- self.instance_eval(&block)
138
+ instance_eval(&block)
142
139
  end
143
140
  end
144
141
  end
145
142
  end
146
-
147
143
  end
148
144
  end
149
145
  end
@@ -5,21 +5,28 @@ require 'rspec'
5
5
  require 'spigot'
6
6
  require 'hashie'
7
7
  require 'active_record'
8
- require "test/unit"
9
- require "mocha/setup"
10
8
 
11
- %w(fixtures support).each do |dir|
12
- Dir[File.join(Spigot.root, "spec/#{dir}/**/*.rb")].each {|f| require f}
13
- end
9
+ require File.join(Spigot.root, 'script/active_record.rb')
10
+ Dir[File.join(Spigot.root, 'spec/fixtures/**/*.rb')].each { |f| require f }
14
11
 
15
12
  # Mocked Classes
16
- User = Class.new(Hashie::Mash)
13
+ User = Class.new(Hashie::Mash) do
14
+ include Spigot::Base
15
+ end
17
16
  Post = Class.new(Hashie::Mash)
18
17
 
19
18
  class ActiveUser < ActiveRecord::Base
20
19
  include Spigot::Base
21
20
  end
22
21
 
22
+ class Event < ActiveRecord::Base
23
+ include Spigot::Base
24
+ end
25
+
26
+ class Profile < ActiveRecord::Base
27
+ include Spigot::Base
28
+ end
29
+
23
30
  module Wrapper
24
31
  Post = Class.new(Hashie::Mash)
25
32
  end
@@ -1,16 +1,23 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Spigot::ActiveRecord do
4
- let(:subject){ActiveUser}
5
- let(:data){ Spigot::Data::ActiveUser.basic.merge(id: '987') }
6
- let(:user){ subject.create(name: 'Dean Martin', username: 'classyasfuck') }
4
+ let(:subject) { ActiveUser }
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
9
  it 'requires the primary key to be accurate' do
10
10
  expect {
11
- subject.find_by_api({full_name: 'Dean Martin'})
11
+ subject.find_by_api(full_name: 'Dean Martin')
12
12
  }.to raise_error(Spigot::MissingResourceError)
13
13
  end
14
+
15
+ it 'requires valid primary_keys' do
16
+ Spigot::Mapping::ActiveUser.invalid_primary_key
17
+ expect {
18
+ subject.find_by_api(github: { full_name: 'Dean Martin' })
19
+ }.to raise_error(Spigot::InvalidSchemaError)
20
+ end
14
21
  end
15
22
 
16
23
  context 'with valid mapping' do
@@ -38,7 +45,7 @@ describe Spigot::ActiveRecord do
38
45
  end
39
46
 
40
47
  context '#create_by_api' do
41
- before{ Spigot::Mapping::ActiveUser.with_options }
48
+ before { Spigot::Mapping::ActiveUser.with_options }
42
49
  it 'creates a record' do
43
50
  record = subject.create_by_api(github: data)
44
51
  record.id.should_not be_nil
@@ -1,8 +1,8 @@
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
+ let(:data) { Spigot::Data::User.basic }
5
+ before { Spigot::Mapping::ActiveUser.stub }
6
6
 
7
7
  context '#new_by_api' do
8
8
  it 'instantiates a record' do
@@ -11,18 +11,6 @@ describe Spigot::Base do
11
11
  end
12
12
  end
13
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
14
  context '#spigot' do
27
15
  it 'returns a spigot proxy' do
28
16
  ActiveUser.spigot(:github).should be_a_kind_of(Spigot::Proxy)
@@ -8,24 +8,24 @@ describe Spigot::Configuration do
8
8
  end
9
9
 
10
10
  context 'access' do
11
- it "is callable from .configure" do
11
+ it 'is callable from .configure' do
12
12
  Spigot.configure do |c|
13
13
  expect(c).to be_kind_of(Spigot::Configuration)
14
14
  end
15
15
  end
16
16
 
17
17
  context 'options' do
18
- let(:map){{'user' => {a: 1}}}
19
- let(:options_key){'my_special_key'}
18
+ let(:map) { { 'user' => { a: 1 } } }
19
+ let(:options_key) { 'my_special_key' }
20
20
 
21
- it "is able to set the options_key" do
22
- Spigot.configure{|config| config.options_key = options_key }
21
+ it 'is able to set the options_key' do
22
+ Spigot.configure { |config| config.options_key = options_key }
23
23
  expect(Spigot.config.options_key).to eq(options_key)
24
24
  end
25
25
 
26
- it "is able to set the logger" do
26
+ it 'is able to set the logger' do
27
27
  logger = Logger.new(STDOUT)
28
- Spigot.configure{|config| config.logger = logger }
28
+ Spigot.configure { |config| config.logger = logger }
29
29
  expect(Spigot.config.logger).to eq(logger)
30
30
  end
31
31
  end
@@ -1,8 +1,8 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Spigot::Map::Base do
4
- let(:subject){Spigot::Map::Base.new}
5
- let(:service){Spigot::Map::Service.new(:github)}
4
+ let(:subject) { Spigot::Map::Base.new }
5
+ let(:service) { Spigot::Map::Service.new(:github) }
6
6
 
7
7
  context '#initialize' do
8
8
  it 'initializes a services array' do
@@ -10,10 +10,16 @@ describe Spigot::Map::Base do
10
10
  end
11
11
  end
12
12
 
13
+ context '#inspect' do
14
+ it 'returns a string' do
15
+ subject.inspect.should match(/Spigot::Map::Base/)
16
+ end
17
+ end
18
+
13
19
  context '.define' do
14
20
  it 'accepts a block' do
15
21
  Spigot::Map::Service.should_receive(:class_eval)
16
- subject.define{'foo'}
22
+ subject.define { 'foo' }
17
23
  end
18
24
 
19
25
  it 'does not require a block' do
@@ -48,8 +54,8 @@ describe Spigot::Map::Base do
48
54
  end
49
55
 
50
56
  it 'allows multiple updates' do
51
- subject.define{ service(:github) }
52
- subject.define{ service(:twitter) }
57
+ subject.define { service(:github) }
58
+ subject.define { service(:twitter) }
53
59
 
54
60
  subject.services.length.should eq(2)
55
61
  end
@@ -72,7 +78,7 @@ describe Spigot::Map::Base do
72
78
  context '.to_hash' do
73
79
  it 'returns a hash of current services' do
74
80
  subject.update(:github, service)
75
- subject.to_hash.should eq({github: {}})
81
+ subject.to_hash.should eq(github: {})
76
82
  end
77
83
  end
78
84