spigot 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,7 @@
1
1
  ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- Mzk3YmFlOGM4MmQ1M2JkNDBmNjBjYzZmZTNmYWFkOGJjMDE5MGMyMg==
5
- data.tar.gz: !binary |-
6
- ZTE4ZGJjNzQyYzk2YzZhZTcwNmQxY2I2NzdmNzgyMjZkNDJmZjllYw==
2
+ SHA1:
3
+ metadata.gz: 43fb252116ef08da32e804dd2cd1a5bf1272f386
4
+ data.tar.gz: eb143439496123d5318df58b43bb353855ed9f32
7
5
  SHA512:
8
- metadata.gz: !binary |-
9
- ZTUzMTM5MWQ3ZGEzYTk4ZDBmMGQ2Mzc1M2MzNGI0NjM4ODE4N2RiZjc2NGQz
10
- M2FjZGQzM2JjMWQ0ZjM3Y2FkMzc0MGU4NzlhNDE2YzA4MDhiNmQwNmY5Y2U1
11
- OTU4MDk2NTY0ZGEwY2IxYWI0NWVhODFlODdmNDQxNTEwZWE2ZmI=
12
- data.tar.gz: !binary |-
13
- OTk3ZDdkY2UwZjgxOGMxZGM1MzIwN2FjOGExZTQ4NTRkZDVmYmQ2MTM3NzBm
14
- ZGRjMDI0NTg1MmU3MWNkNDgwMTM0N2VlMzU1NTE3ZTg1MzZlNTE4M2JjZmRh
15
- MjdkMTY1MTZlNzhkNzZhNDgzMDZhNGZiZjgyMWY1YjFhMjk5YjA=
6
+ metadata.gz: 2b8c421ded53429d8569655cf014921af15656ba77479dd07e05bafde28df913022b5f591236552235980359598127b48ff234ebccc9d1ac7468c1164d8110b3
7
+ data.tar.gz: b08b8e03fb47aa5d5a474aca6eaa08d166fbba299e77afbd73af6e8430bbe1e9ce35c41278d18b6536f911804572acc74b9062f87ce992eae6cdfd0a253d154b
@@ -23,17 +23,13 @@ class User < ActiveRecord::Base
23
23
  include Spigot::Base
24
24
  end
25
25
 
26
- Spigot.define do
27
- service :github do
28
- resource :user do
29
- id :github_id
30
- login :username
31
- avatar_url :image_url
32
- url :profile_url
33
- options do
34
- primary_key :username
35
- end
36
- end
26
+ Spigot.resource :user do
27
+ id :github_id
28
+ login :username
29
+ avatar_url :image_url
30
+ url :profile_url
31
+ options do
32
+ primary_key :username
37
33
  end
38
34
  end
39
35
 
@@ -47,14 +43,14 @@ puts "\nIt Returned a whole bunch of data: "
47
43
  puts "#{data.inspect[0..100]}... etc, etc, etc (#{data.keys.length} more keys received)"
48
44
 
49
45
  puts "\nWe don't want to use all of it. We can define a map on Spigot:"
50
- puts User.spigot(:github).map.to_hash.inspect
46
+ puts User.spigot.map.to_hash.inspect
51
47
  puts "Each key is an attribute received from the API, and the corresponding value is our column name."
52
48
 
53
49
  puts "\nWe define our primary key in the spigot `User` options, so we know how to check if the record already exists:"
54
- puts User.spigot(:github).options.inspect
50
+ puts User.spigot.options.inspect
55
51
 
56
52
 
57
- puts "\nWe can create a new user with one nice and easy line: `User.find_or_create_by_api(:github, data)`"
58
- puts User.find_or_create_by_api(:github, data).inspect
53
+ puts "\nWe can create a new user with one nice and easy line: `User.find_or_create_by_api(data)`"
54
+ puts User.find_or_create_by_api(data).inspect
59
55
 
60
56
  puts "\nEnjoy!"
@@ -21,6 +21,18 @@ module Spigot
21
21
  (config.map || Spigot::Map::Base.new).define(&block)
22
22
  end
23
23
 
24
+ def self.service(name, &block)
25
+ define do
26
+ service(name, &block)
27
+ end
28
+ end
29
+
30
+ def self.resource(name, &block)
31
+ define do
32
+ resource(name, &block)
33
+ end
34
+ end
35
+
24
36
  def self.configure
25
37
  yield config
26
38
  end
@@ -36,9 +48,10 @@ module Spigot
36
48
 
37
49
  def self.logger
38
50
  @log ||= Spigot.config.logger || begin
39
- buffer = Logger.new(STDOUT)
40
- buffer.level = $0 == 'irb' ? Logger::DEBUG : Logger::INFO
41
- buffer.formatter = proc{|severity, datetime, progname, msg| "#{msg}\n"}
51
+ if buffer = Logger.new(STDOUT)
52
+ buffer.level = ($0 == 'irb' ? Logger::DEBUG : Logger::INFO)
53
+ buffer.formatter = proc{|severity, datetime, progname, msg| "#{msg}\n"}
54
+ end
42
55
  buffer
43
56
  end
44
57
  end
@@ -2,70 +2,69 @@ module Spigot
2
2
  module ActiveRecord
3
3
  module ClassMethods
4
4
 
5
- ## #find_by_api(service, api_data)
5
+ ## #find_by_api(params)
6
6
  # Build a query based on the defined map for this resource
7
7
  # to find a single matching record in the database
8
8
  #
9
- # @param service [Symbol] Service from which the data was received
10
- # @param api_data [Hash] The data as received from the remote api, unformatted.
11
- def find_by_api(service, api_data)
12
- find_all_by_api(service, api_data).first
9
+ # @param params [Hash] Data as received from the api with optional service key
10
+ def find_by_api(params={})
11
+ find_all_by_api(params).first
13
12
  end
14
13
 
15
- ## #find_all_by_api(service, api_data)
14
+ ## #find_all_by_api(params)
16
15
  # Build a query based on the defined map for this resource
17
16
  # to find all matching records in the database
18
17
  #
19
- # @param service [Symbol] Service from which the data was received
20
- # @param api_data [Hash] The data as received from the remote api, unformatted.
21
- def find_all_by_api(service, api_data)
22
- find_by_translator Translator.new(self, service, api_data)
18
+ # @param params [Hash] Data as received from the api with optional service key
19
+ def find_all_by_api(params={})
20
+ service, data = Spigot::Map::Service.extract(params)
21
+ find_by_translator Translator.new(self, service, data)
23
22
  end
24
23
 
25
- ## #create_by_api(service, api_data)
24
+ ## #create_by_api(params)
26
25
  # Insert mapped data into the calling model's table. Does not
27
26
  # do any checks on existing content already present in the database
28
27
  #
29
- # @param service [Symbol] Service from which the data was received
30
- # @param api_data [Hash] The data as received from the remote api, unformatted.
31
- def create_by_api(service, api_data)
32
- create_by_translator Translator.new(self, service, api_data)
28
+ # @param params [Hash] Data as received from the api with optional service key
29
+ def create_by_api(params={})
30
+ service, data = Spigot::Map::Service.extract(params)
31
+ create_by_translator Translator.new(self, service, data)
33
32
  end
34
33
 
35
- ## #update_by_api(service, api_data)
34
+ ## #update_by_api(params)
36
35
  # Queries the database to find an existing record, based on the options
37
36
  # provided to spigot. If a record is found, it updates that record
38
37
  # with any new data received by the API
39
38
  #
40
- # @param service [Symbol] Service from which the data was received
41
- # @param api_data [Hash] The data as received from the remote api, unformatted.
42
- def update_by_api(service, api_data)
43
- babel = Translator.new(self, service, api_data)
39
+ # @param params [Hash] Data as received from the api with optional service key
40
+ def update_by_api(params={})
41
+ service, data = Spigot::Map::Service.extract(params)
42
+ babel = Translator.new(self, service, data)
44
43
  record = find_by_translator(babel).first
45
44
  update_by_translator(babel, record) if record.present?
46
45
  end
47
46
 
48
- ## #find_or_create_by_api(service, api_data)
47
+ ## #find_or_create_by_api(params)
49
48
  # Queries the database to find an existing record. If that record is found
50
49
  # simply return it, otherwise create a new record and return it. This does
51
50
  # not update any existing record. If you want that, use `create_or_update_by_api`
52
51
  #
53
- # @param service [Symbol] Service from which the data was received
54
- # @param api_data [Hash] The data as received from the remote api, unformatted.
55
- def find_or_create_by_api(service, api_data)
56
- babel = Translator.new(self, service, api_data)
52
+ # @param params [Hash] Data as received from the api with optional service key
53
+ def find_or_create_by_api(params={})
54
+ service, data = Spigot::Map::Service.extract(params)
55
+ babel = Translator.new(self, service, data)
57
56
  find_by_translator(babel).first || create_by_translator(babel)
58
57
  end
59
58
 
60
- ## #create_or_update_by_api(service, api_data)
59
+ ## #create_or_update_by_api(params)
61
60
  # Queries the database to find an existing record. If that record is found
62
61
  # it updates it with passed api_data and returns the record. Otherwise it
63
62
  # creates a new record and returns the newly created record.
64
63
  #
65
- # @param service [Symbol] Service from which the data was received
66
- # @param api_data [Hash] The data as received from the remote api, unformatted.
67
- def create_or_update_by_api(service, api_data)
68
- babel = Translator.new(self, service, api_data)
64
+ # @param params [Hash] Data as received from the api with optional service key
65
+ def create_or_update_by_api(params={})
66
+ service, data = Spigot::Map::Service.extract(params)
67
+ babel = Translator.new(self, service, data)
69
68
  record = find_by_translator(babel).first
70
69
  record.present? ? update_by_translator(babel, record) : create_by_translator(babel)
71
70
  end
@@ -7,30 +7,29 @@ module Spigot
7
7
  end
8
8
 
9
9
  module ClassMethods
10
- # #self.new_by_api(service, api_data)
10
+ # #self.new_by_api(params)
11
11
  # Instantiate a new object mapping the api data to the calling object's attributes
12
12
  #
13
- # @param service [Symbol] Service which will be doing the translating. Must have a corresponding yaml file
14
- # @param api_data [Hash] The data as received from the remote api, unformatted.
15
- def new_by_api(service, api_data)
16
- Record.instantiate(self, formatted_api_data(service, api_data))
13
+ # @param params [Hash] Data as received from the api with optional service key
14
+ def new_by_api(params)
15
+ Record.instantiate(self, formatted_api_data(params))
17
16
  end
18
17
 
19
- # #self.formatted_api_data(service, api_data)
18
+ # #self.formatted_api_data(params)
20
19
  # Create a Spigot::Translator for the given service and return the formatted data.
21
20
  #
22
- # @param service [Symbol] Service which will be doing the translating. Must have a corresponding yaml file
23
- # @param api_data [Hash] The data as received from the remote api, unformatted.
24
- def formatted_api_data(service, api_data)
25
- Translator.new(self, service, api_data).format
21
+ # @param params [Hash] Data as received from the api with optional service key
22
+ def formatted_api_data(params={})
23
+ service, data = Spigot::Map::Service.extract(params)
24
+ Translator.new(self, service, data).format
26
25
  end
27
26
 
28
27
  # #self.spigot
29
28
  # Return a Spigot::Proxy that provides accessor methods to the spigot library
30
29
  #
31
30
  # @param service [Symbol] Service which pertains to the data being processed on the implementation
32
- def spigot(service)
33
- Spigot::Proxy.new(service, self)
31
+ def spigot(service=nil)
32
+ Spigot::Proxy.new(self, service)
34
33
  end
35
34
  end
36
35
 
@@ -36,6 +36,18 @@
36
36
  resources.detect{|r| r.instance_variable_get(:@name).to_sym == name.to_sym}
37
37
  end
38
38
 
39
+ def self.extract(params)
40
+ return params if current_map.nil?
41
+ name = params.keys.first
42
+ service = current_map.service(name)
43
+
44
+ if service.nil?
45
+ return [nil, params]
46
+ else
47
+ return [service.name.to_sym, params[name]]
48
+ end
49
+ end
50
+
39
51
  private
40
52
 
41
53
  def self.current_map
@@ -13,16 +13,51 @@ module Spigot
13
13
  #
14
14
  # @param service [String] This is the service that dictates the proxy.
15
15
  # @param resource [Object] This is the class implementing the proxy.
16
- def initialize(service, resource)
17
- raise MissingServiceError, "No service definition found for #{service}" if Spigot.config.map.service(service).nil?
16
+ def initialize(resource, service=nil)
18
17
  @service = service
19
18
  @resource = resource
20
19
  end
21
20
 
21
+ ## #find
22
+ # Alias for find_by_api
23
+ def find(params={})
24
+ resource.find_by_api(service_scoped(params))
25
+ end
26
+
27
+ ## #find_all
28
+ # Alias for find_all_by_api
29
+ def find_all(params={})
30
+ resource.find_all_by_api(service_scoped(params))
31
+ end
32
+
33
+ ## #create
34
+ # Alias for create_by_api
35
+ def create(params={})
36
+ resource.create_by_api(service_scoped(params))
37
+ end
38
+
39
+ ## #update
40
+ # Alias for update_by_api
41
+ def update(params={})
42
+ resource.update_by_api(service_scoped(params))
43
+ end
44
+
45
+ ## #find_or_create
46
+ # Alias for find_or_create_by_api
47
+ def find_or_create(params={})
48
+ resource.find_or_create_by_api(service_scoped(params))
49
+ end
50
+
51
+ ## #create_or_update
52
+ # Alias for create_or_update_by_api
53
+ def create_or_update(params={})
54
+ resource.create_or_update_by_api(service_scoped(params))
55
+ end
56
+
22
57
  ## #translator
23
58
  # Instantiate a Spigot::Translator object with the contextual service and resource
24
59
  def translator
25
- Translator.new(service, resource)
60
+ Translator.new(resource, service)
26
61
  end
27
62
 
28
63
  ## #map
@@ -37,5 +72,19 @@ module Spigot
37
72
  translator.options || {}
38
73
  end
39
74
 
75
+ private
76
+
77
+ def service_scoped(params={})
78
+ return params if @service.nil?
79
+ return {@service => params} if Spigot.config.map.nil?
80
+
81
+ key, data = Spigot::Map::Service.extract(params)
82
+ return {@service => params} if key.nil?
83
+
84
+ raise Spigot::InvalidServiceError, "You cannot specify two services" if key.to_sym != @service.to_sym
85
+
86
+ {key => data}
87
+ end
88
+
40
89
  end
41
90
  end
@@ -86,7 +86,7 @@ module Spigot
86
86
  return @resource_map if defined?(@resource_map)
87
87
  resource_key = resource.to_s.underscore
88
88
  @resource_map = service_map[resource_key]
89
- raise MissingResourceError, "There is no #{resource_key} resource_map for #{service}" if @resource_map.nil?
89
+ raise MissingResourceError, "There is no #{resource_key} resource_map" if @resource_map.nil?
90
90
  @resource_map
91
91
  end
92
92
 
@@ -107,7 +107,7 @@ module Spigot
107
107
  @service_map = Spigot.config.map.service(service || :any)
108
108
  if @service_map.nil?
109
109
  if service.nil?
110
- raise MissingResourceError, "There is no #{resource_key} resource_map"
110
+ raise MissingResourceError, "There is no #{resource.to_s.underscore} resource_map"
111
111
  else
112
112
  raise InvalidServiceError, "No definition found for #{service}"
113
113
  end
@@ -1,3 +1,3 @@
1
1
  module Spigot
2
- VERSION = "0.2.1"
2
+ VERSION = "0.2.2"
3
3
  end
@@ -1,32 +1,29 @@
1
1
  require 'active_record'
2
2
  require 'spigot'
3
3
 
4
- Spigot.define do
5
- service :github do
6
- resource :active_user do
7
- id :github_id
8
- full_name :name
9
- login :username
10
- contact do
11
- address :address
12
- telephone do
13
- work :work_phone
14
- home :home_phone
15
- end
16
- url :homepage do |value|
17
- "https://github.com/#{value}"
18
- end
19
- end
4
+ Spigot.resource(:active_user) do
5
+ id :github_id
6
+ full_name :name
7
+ login :username
8
+ contact do
9
+ address :address
10
+ telephone do
11
+ work :work_phone
12
+ home :home_phone
20
13
  end
21
-
22
- resource :pull_request do
23
- id :id
24
- title :title
25
- body :body
14
+ url :homepage do |value|
15
+ "https://github.com/#{value}"
26
16
  end
27
17
  end
28
18
  end
29
19
 
20
+ Spigot.service(:twitter) do
21
+ resource :active_user do
22
+ name :name
23
+ username :username
24
+ end
25
+ end
26
+
30
27
  ActiveRecord::Base.logger = Spigot.logger
31
28
  require File.join(Spigot.root, 'spec', 'support', 'active_record')
32
29
 
@@ -9,6 +9,15 @@ module Spigot
9
9
  end
10
10
  end
11
11
 
12
+ def self.twitter
13
+ Spigot.service(:twitter) do
14
+ resource :active_user do
15
+ login :username
16
+ full_name :name
17
+ end
18
+ end
19
+ end
20
+
12
21
  def self.with_options
13
22
  template do
14
23
  login :username
@@ -44,35 +53,6 @@ module Spigot
44
53
  end
45
54
  end
46
55
 
47
- # def self.basic
48
- # {'active_user' => base}
49
- # end
50
-
51
- # def self.non_unique_key
52
- # {'active_user' => base.merge('auth_token' => 'token', 'spigot' => non_unique)}
53
- # end
54
-
55
- # def self.with_invalid_options
56
- # {'active_user' => base.merge('spigot' => invalid_options)}
57
- # end
58
-
59
- # private
60
-
61
- # def self.base
62
- # {'full_name' => 'name', 'login' => 'username'}
63
- # end
64
-
65
- # def self.options
66
- # {'primary_key' => 'username', 'foreign_key' => 'login'}
67
- # end
68
-
69
- # def self.non_unique
70
- # {'primary_key' => 'token', 'foreign_key' => 'auth_token'}
71
- # end
72
-
73
- # def self.invalid_options
74
- # {'primary_key' => 'nosuchcolumn', 'foreign_key' => 'nosuchkey'}
75
- # end
76
56
  end
77
57
  end
78
58
  end
@@ -9,6 +9,13 @@ module Spigot
9
9
  end
10
10
  end
11
11
 
12
+ def self.abridged
13
+ Spigot.resource :user do
14
+ full_name :name
15
+ login :username
16
+ end
17
+ end
18
+
12
19
  def self.interpolated
13
20
  template do
14
21
  full_name :name
@@ -8,7 +8,7 @@ describe Spigot::ActiveRecord do
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(:github, {full_name: 'Dean Martin'})
11
+ subject.find_by_api({full_name: 'Dean Martin'})
12
12
  }.to raise_error(Spigot::MissingResourceError)
13
13
  end
14
14
  end
@@ -21,7 +21,7 @@ describe Spigot::ActiveRecord do
21
21
  end
22
22
 
23
23
  it 'queries by the specified primary_key' do
24
- subject.find_by_api(:github, data).should eq(user)
24
+ subject.find_by_api(github: data).should eq(user)
25
25
  end
26
26
  end
27
27
 
@@ -33,14 +33,14 @@ describe Spigot::ActiveRecord do
33
33
  end
34
34
 
35
35
  it 'returns all records matching primary key' do
36
- subject.find_all_by_api(:github, data).length.should eq(2)
36
+ subject.find_all_by_api(github: data).length.should eq(2)
37
37
  end
38
38
  end
39
39
 
40
40
  context '#create_by_api' do
41
41
  before{ Spigot::Mapping::ActiveUser.with_options }
42
42
  it 'creates a record' do
43
- record = subject.create_by_api(:github, data)
43
+ record = subject.create_by_api(github: data)
44
44
  record.id.should_not be_nil
45
45
  record.name.should eq('Dean Martin')
46
46
  record.username.should eq('classyasfuck')
@@ -55,7 +55,7 @@ describe Spigot::ActiveRecord do
55
55
  end
56
56
 
57
57
  it 'updates a record' do
58
- record = subject.update_by_api(:github, data.merge(full_name: 'Dino Baby'))
58
+ record = subject.update_by_api(github: data.merge(full_name: 'Dino Baby'))
59
59
  record.name.should eq('Dino Baby')
60
60
  end
61
61
  end
@@ -66,12 +66,12 @@ describe Spigot::ActiveRecord do
66
66
  Spigot::Mapping::ActiveUser.with_options
67
67
  end
68
68
  it 'returns an existing record' do
69
- record = subject.find_or_create_by_api(:github, data)
69
+ record = subject.find_or_create_by_api(github: data)
70
70
  record.id.should eq(user.id)
71
71
  end
72
72
 
73
73
  it 'creates a record when none exists' do
74
- record = subject.find_or_create_by_api(:github, Spigot::Data::ActiveUser.alt)
74
+ record = subject.find_or_create_by_api(github: Spigot::Data::ActiveUser.alt)
75
75
  record.id.should_not eq(user.id)
76
76
  record.name.should eq('Frank Sinatra')
77
77
  record.username.should eq('livetilidie')
@@ -85,14 +85,14 @@ describe Spigot::ActiveRecord do
85
85
  end
86
86
 
87
87
  it 'updates an existing record' do
88
- record = subject.create_or_update_by_api(:github, data.merge(full_name: 'Dino Baby'))
88
+ record = subject.create_or_update_by_api(github: data.merge(full_name: 'Dino Baby'))
89
89
  record.id.should eq(user.id)
90
90
  record.name.should eq('Dino Baby')
91
91
  record.username.should eq('classyasfuck')
92
92
  end
93
93
 
94
94
  it 'creates a record when none exists' do
95
- record = subject.create_or_update_by_api(:github, Spigot::Data::ActiveUser.alt)
95
+ record = subject.create_or_update_by_api(github: Spigot::Data::ActiveUser.alt)
96
96
  record.id.should_not eq(user.id)
97
97
  record.name.should eq('Frank Sinatra')
98
98
  record.username.should eq('livetilidie')
@@ -7,19 +7,19 @@ describe Spigot::Base do
7
7
  context '#new_by_api' do
8
8
  it 'instantiates a record' do
9
9
  Spigot::Record.should_receive(:instantiate)
10
- ActiveUser.new_by_api(:github, data)
10
+ ActiveUser.new_by_api(github: data)
11
11
  end
12
12
  end
13
13
 
14
14
  context '#formatted_api_data' do
15
15
  it 'calls format on the translator' do
16
16
  Spigot::Translator.any_instance.should_receive(:format)
17
- ActiveUser.formatted_api_data(:github, data)
17
+ ActiveUser.formatted_api_data(github: data)
18
18
  end
19
19
 
20
20
  it 'returns formatted data' do
21
21
  Spigot::Translator.any_instance.should_receive(:format)
22
- formatted = ActiveUser.formatted_api_data(:github, data)
22
+ formatted = ActiveUser.formatted_api_data(github: data)
23
23
  end
24
24
  end
25
25
 
@@ -27,12 +27,6 @@ describe Spigot::Base do
27
27
  it 'returns a spigot proxy' do
28
28
  ActiveUser.spigot(:github).should be_a_kind_of(Spigot::Proxy)
29
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
30
  end
37
31
 
38
32
  end
@@ -54,6 +54,37 @@ describe Spigot::Map::Service do
54
54
  end
55
55
  end
56
56
 
57
+ context '#extract' do
58
+ let(:data){{a: 1}}
59
+ context 'with no map defined' do
60
+ before{ Spigot.config.reset }
61
+ it 'returns passed params' do
62
+ klass.extract(data).should eq(data)
63
+ end
64
+ end
65
+
66
+ context 'with a map defined' do
67
+ it 'returns a nil service when none is defined' do
68
+ klass.extract(data).should eq([nil, data])
69
+ end
70
+
71
+ it 'searches for existing service definition' do
72
+ Spigot.config.map.should_receive(:service).with(:a)
73
+ klass.extract(data)
74
+ end
75
+
76
+ it 'returns the correct service if present' do
77
+ Spigot::Mapping::ActiveUser.stub
78
+ klass.extract(github: data).should eq([:github, data])
79
+ end
80
+
81
+ it 'does not return an unmatched service' do
82
+ Spigot::Mapping::ActiveUser.stub
83
+ klass.extract(twitter: data).should eq([nil, {twitter: data}])
84
+ end
85
+ end
86
+ end
87
+
57
88
  context '.resource' do
58
89
  it 'builds a resource' do
59
90
  Spigot::Map::Resource.should_receive(:new).with(:user)
@@ -0,0 +1,99 @@
1
+ require 'spec_helper'
2
+
3
+ describe Spigot::Proxy do
4
+
5
+ context '#initialize' do
6
+ let(:subject){Spigot::Proxy}
7
+ it 'accepts a service and a resource' do
8
+ proxy = subject.new(User, :github)
9
+ proxy.resource.should eq(User)
10
+ proxy.service.should eq(:github)
11
+ end
12
+
13
+ it 'does not require a service' do
14
+ expect {
15
+ subject.new(User)
16
+ }.to_not raise_error
17
+ end
18
+ end
19
+
20
+ context 'Active Record aliases' do
21
+ let(:subject){Spigot::Proxy.new(ActiveUser)}
22
+ it 'aliases find' do
23
+ ActiveUser.should_receive(:find_by_api)
24
+ subject.find({a: 1})
25
+ end
26
+
27
+ it 'aliases find_all' do
28
+ ActiveUser.should_receive(:find_all_by_api)
29
+ subject.find_all({a: 1})
30
+ end
31
+
32
+ it 'aliases create' do
33
+ ActiveUser.should_receive(:create_by_api)
34
+ subject.create({a: 1})
35
+ end
36
+
37
+ it 'aliases update' do
38
+ ActiveUser.should_receive(:update_by_api)
39
+ subject.update({a: 1})
40
+ end
41
+
42
+ it 'aliases find_or_create' do
43
+ ActiveUser.should_receive(:find_or_create_by_api)
44
+ subject.find_or_create({a: 1})
45
+ end
46
+
47
+ it 'aliases create_or_update' do
48
+ ActiveUser.should_receive(:create_or_update_by_api)
49
+ subject.create_or_update({a: 1})
50
+ end
51
+
52
+ context 'with a specified service' do
53
+ let(:subject){Spigot::Proxy.new(ActiveUser, :github)}
54
+ before{ Spigot::Mapping::ActiveUser.stub }
55
+ it 'uses the current service' do
56
+ ActiveUser.should_receive(:create_by_api).with({github: {a: 1}})
57
+ subject.create({a: 1})
58
+ end
59
+
60
+ it 'checks for conflicting services' do
61
+ Spigot::Mapping::ActiveUser.twitter
62
+ expect{
63
+ subject.create(twitter: {a: 1})
64
+ }.to raise_error(Spigot::InvalidServiceError)
65
+ end
66
+
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})
70
+ end
71
+ end
72
+ end
73
+
74
+ context 'instance methods' do
75
+ let(:subject){Spigot::Proxy.new(User, :github)}
76
+
77
+ context '.translator' do
78
+ it 'returns a translator object' do
79
+ Spigot::Translator.should_receive(:new).with(User, :github)
80
+ subject.translator
81
+ end
82
+ end
83
+
84
+ context '.map' do
85
+ it 'returns the currently defined map' do
86
+ Spigot::Translator.any_instance.should_receive(:resource_map)
87
+ subject.map
88
+ end
89
+ end
90
+
91
+ context '.options' do
92
+ it 'returns the current options' do
93
+ Spigot::Translator.any_instance.should_receive(:options)
94
+ subject.options
95
+ end
96
+ end
97
+ end
98
+
99
+ end
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ describe Spigot::Record do
4
+ let(:resource){User}
5
+ let(:data){ { a: 1 } }
6
+ let(:subject){ Spigot::Record.new(resource, data) }
7
+
8
+ context '#instantiate' do
9
+ it 'exectutes instantiate on an instance' do
10
+ Spigot::Record.any_instance.should_receive(:instantiate)
11
+ Spigot::Record.instantiate(resource, data)
12
+ end
13
+ end
14
+
15
+ context '.instantiate' do
16
+ it 'sends a new statement to the resource' do
17
+ resource.should_receive(:new).with(data)
18
+ subject.instantiate
19
+ end
20
+ end
21
+
22
+ end
@@ -0,0 +1,32 @@
1
+ require 'spec_helper'
2
+
3
+ describe Spigot do
4
+ let(:subject){Spigot}
5
+ context '#resource' do
6
+ it 'defines a resource' do
7
+ Spigot.should_receive(:define)
8
+ subject.resource(:user){'foo'}
9
+ end
10
+ end
11
+
12
+ context '#service' do
13
+ it 'defines a service' do
14
+ Spigot.should_receive(:define)
15
+ subject.service(:github){'foo'}
16
+ end
17
+
18
+ it 'continues a normal definition' do
19
+ Spigot::Map::Service.any_instance.should_receive(:resource)
20
+ subject.service(:github) do
21
+ resource(:user){ 'foo' }
22
+ end
23
+ end
24
+ end
25
+
26
+ context '#logger' do
27
+ it 'produces a logger' do
28
+ Logger.should_receive(:new).with(STDOUT)
29
+ Spigot.logger
30
+ end
31
+ end
32
+ end
@@ -180,6 +180,15 @@ describe Spigot::Translator do
180
180
  expect(no_service.format).to eq({name: 'Dean Martin', username: 'classyasfuck'})
181
181
  end
182
182
  end
183
+
184
+ context 'with an abridged definition' do
185
+ let(:data){ Spigot::Data::User.basic }
186
+ let(:subject){ Spigot::Translator.new(User.new, nil, data) }
187
+ before{ Spigot::Mapping::User.abridged }
188
+ it 'reads one layer' do
189
+ expect(subject.format).to eq({name: 'Dean Martin', username: 'classyasfuck'})
190
+ end
191
+ end
183
192
  end
184
193
  end
185
194
 
metadata CHANGED
@@ -1,27 +1,27 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spigot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthew Werner
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-10 00:00:00.000000000 Z
11
+ date: 2013-12-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ! '>='
17
+ - - '>='
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0'
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
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
@@ -42,84 +42,84 @@ dependencies:
42
42
  name: rake
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ! '>='
45
+ - - '>='
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ! '>='
52
+ - - '>='
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: hashie
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - ! '>='
59
+ - - '>='
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - ! '>='
66
+ - - '>='
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: simplecov
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - ! '>='
73
+ - - '>='
74
74
  - !ruby/object:Gem::Version
75
75
  version: '0'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - ! '>='
80
+ - - '>='
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: rspec
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - ! '>='
87
+ - - '>='
88
88
  - !ruby/object:Gem::Version
89
89
  version: '0'
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
- - - ! '>='
94
+ - - '>='
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
97
  - !ruby/object:Gem::Dependency
98
98
  name: mocha
99
99
  requirement: !ruby/object:Gem::Requirement
100
100
  requirements:
101
- - - ! '>='
101
+ - - '>='
102
102
  - !ruby/object:Gem::Version
103
103
  version: '0'
104
104
  type: :development
105
105
  prerelease: false
106
106
  version_requirements: !ruby/object:Gem::Requirement
107
107
  requirements:
108
- - - ! '>='
108
+ - - '>='
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0'
111
111
  - !ruby/object:Gem::Dependency
112
112
  name: sqlite3
113
113
  requirement: !ruby/object:Gem::Requirement
114
114
  requirements:
115
- - - ! '>='
115
+ - - '>='
116
116
  - !ruby/object:Gem::Version
117
117
  version: '0'
118
118
  type: :development
119
119
  prerelease: false
120
120
  version_requirements: !ruby/object:Gem::Requirement
121
121
  requirements:
122
- - - ! '>='
122
+ - - '>='
123
123
  - !ruby/object:Gem::Version
124
124
  version: '0'
125
125
  description: Spigot provides a clean interface translating API data into context relevant
@@ -158,7 +158,6 @@ files:
158
158
  - spec/fixtures/data/post.rb
159
159
  - spec/fixtures/data/user.rb
160
160
  - spec/fixtures/mappings/active_user_map.rb
161
- - spec/fixtures/mappings/mappings.rb
162
161
  - spec/fixtures/mappings/post_map.rb
163
162
  - spec/fixtures/mappings/user_map.rb
164
163
  - spec/spec_helper.rb
@@ -169,6 +168,9 @@ files:
169
168
  - spec/spigot/map/definition_spec.rb
170
169
  - spec/spigot/map/resource_spec.rb
171
170
  - spec/spigot/map/service_spec.rb
171
+ - spec/spigot/proxy_spec.rb
172
+ - spec/spigot/record_spec.rb
173
+ - spec/spigot/spigot_spec.rb
172
174
  - spec/spigot/translator_spec.rb
173
175
  - spec/support/active_record.rb
174
176
  - spigot.gemspec
@@ -182,17 +184,17 @@ require_paths:
182
184
  - lib
183
185
  required_ruby_version: !ruby/object:Gem::Requirement
184
186
  requirements:
185
- - - ! '>='
187
+ - - '>='
186
188
  - !ruby/object:Gem::Version
187
189
  version: '0'
188
190
  required_rubygems_version: !ruby/object:Gem::Requirement
189
191
  requirements:
190
- - - ! '>='
192
+ - - '>='
191
193
  - !ruby/object:Gem::Version
192
194
  version: '0'
193
195
  requirements: []
194
196
  rubyforge_project:
195
- rubygems_version: 2.1.5
197
+ rubygems_version: 2.0.6
196
198
  signing_key:
197
199
  specification_version: 4
198
200
  summary: Spigot provides a clean interface translating API data into context relevant
@@ -202,7 +204,6 @@ test_files:
202
204
  - spec/fixtures/data/post.rb
203
205
  - spec/fixtures/data/user.rb
204
206
  - spec/fixtures/mappings/active_user_map.rb
205
- - spec/fixtures/mappings/mappings.rb
206
207
  - spec/fixtures/mappings/post_map.rb
207
208
  - spec/fixtures/mappings/user_map.rb
208
209
  - spec/spec_helper.rb
@@ -213,5 +214,8 @@ test_files:
213
214
  - spec/spigot/map/definition_spec.rb
214
215
  - spec/spigot/map/resource_spec.rb
215
216
  - spec/spigot/map/service_spec.rb
217
+ - spec/spigot/proxy_spec.rb
218
+ - spec/spigot/record_spec.rb
219
+ - spec/spigot/spigot_spec.rb
216
220
  - spec/spigot/translator_spec.rb
217
221
  - spec/support/active_record.rb
@@ -1,7 +0,0 @@
1
- module Spigot
2
- module Mapping
3
- def self.multiple_resources
4
- User.basic.merge(Post.basic)
5
- end
6
- end
7
- end