tms_bridge 0.0.10

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NzM4YTZkNDI2MWFlM2VmMWI0NzBhNDIyNjhjNTllNWRkZDdhZDk4Zg==
5
+ data.tar.gz: !binary |-
6
+ MDIwODJkZDY5OWRlZDEzMzljNGI0ZDBiYTAyMDE0YzM1MTU2Mzc2Mg==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ YWRhYjc0YTMzMDEyMTJiOGQ2MDc1MWQ2YzJhNWMzODRmYTA3OWM4OTE4OWI3
10
+ NTNiNTYyYjAyYWFlOWYzNDI4NGE0YTViNWM2MTFhMDVjMTEyYjQ3NTBkMDUw
11
+ YzE2YjgxYjBkYzQzZDQ5MTUyNGMwNjllODZhMTBiNTk1OGZjMzU=
12
+ data.tar.gz: !binary |-
13
+ Y2I2MGYxODc5YmJiZjEzOWYzZDJhODE1ZTRmZjU5NjQ0ZTE4NTMzMTI1MzUz
14
+ ZWJhYzk0M2M0YzQ0ZWNhNmVhYWNkYmEwMzg2NTAzOTcyOGEyZjBkOTU4MjQ5
15
+ NGI5MmM1YmY2NTg2MDI1YTQ3MzUwZDAzNTJjZTBkZWZiMDNhNzQ=
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ coverage
6
+ InstalledFiles
7
+ lib/bundler/man
8
+ pkg
9
+ rdoc
10
+ spec/reports
11
+ test/tmp
12
+ test/version_tmp
13
+ tmp
14
+
15
+ # YARD artifacts
16
+ .yardoc
17
+ _yardoc
18
+ doc/
data/.rvmrc ADDED
@@ -0,0 +1,7 @@
1
+ export DYLD_LIBRARY_PATH="/usr/local/mysql/lib:$DYLD_LIBRARY_PATH"
2
+ export RUBYOPT=-Ku
3
+ export IRON_CACHE_TOKEN="6P0Hr8umXZruKzS3SbKF1vuu1E8"
4
+ export IRON_CACHE_PROJECT_ID="50a79df0a6498073e700560c"
5
+ export CC_BRIDGE_SALT="c7b5fae1-d719-4c8e-b92b-1c4c5e26ca10"
6
+
7
+ rvm --create use 1.9.3@tms.bridge > /dev/null
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in bridge.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,90 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ tms_bridge (0.0.9)
5
+ actionpack (>= 3.2.0)
6
+ activerecord (>= 3.2.0)
7
+ iron_cache
8
+ typhoeus
9
+ uuidtools
10
+
11
+ GEM
12
+ remote: https://rubygems.org/
13
+ specs:
14
+ actionpack (3.2.13)
15
+ activemodel (= 3.2.13)
16
+ activesupport (= 3.2.13)
17
+ builder (~> 3.0.0)
18
+ erubis (~> 2.7.0)
19
+ journey (~> 1.0.4)
20
+ rack (~> 1.4.5)
21
+ rack-cache (~> 1.2)
22
+ rack-test (~> 0.6.1)
23
+ sprockets (~> 2.2.1)
24
+ activemodel (3.2.13)
25
+ activesupport (= 3.2.13)
26
+ builder (~> 3.0.0)
27
+ activerecord (3.2.13)
28
+ activemodel (= 3.2.13)
29
+ activesupport (= 3.2.13)
30
+ arel (~> 3.0.2)
31
+ tzinfo (~> 0.3.29)
32
+ activesupport (3.2.13)
33
+ i18n (= 0.6.1)
34
+ multi_json (~> 1.0)
35
+ arel (3.0.2)
36
+ builder (3.0.4)
37
+ diff-lcs (1.2.4)
38
+ erubis (2.7.0)
39
+ ethon (0.5.12)
40
+ ffi (>= 1.3.0)
41
+ mime-types (~> 1.18)
42
+ ffi (1.8.1)
43
+ hike (1.2.3)
44
+ i18n (0.6.1)
45
+ iron_cache (1.4.0)
46
+ iron_core (>= 0.5.1)
47
+ iron_core (0.6.2)
48
+ rest (>= 2.2.0)
49
+ journey (1.0.4)
50
+ mime-types (1.23)
51
+ multi_json (1.7.6)
52
+ net-http-persistent (2.8)
53
+ rack (1.4.5)
54
+ rack-cache (1.2)
55
+ rack (>= 0.4)
56
+ rack-test (0.6.2)
57
+ rack (>= 1.0)
58
+ rake (10.0.4)
59
+ rest (2.6.2)
60
+ net-http-persistent
61
+ rest-client (>= 0.3.0)
62
+ rest-client (1.6.7)
63
+ mime-types (>= 1.16)
64
+ rspec (2.13.0)
65
+ rspec-core (~> 2.13.0)
66
+ rspec-expectations (~> 2.13.0)
67
+ rspec-mocks (~> 2.13.0)
68
+ rspec-core (2.13.1)
69
+ rspec-expectations (2.13.0)
70
+ diff-lcs (>= 1.1.3, < 2.0)
71
+ rspec-mocks (2.13.1)
72
+ sprockets (2.2.2)
73
+ hike (~> 1.2)
74
+ multi_json (~> 1.0)
75
+ rack (~> 1.0)
76
+ tilt (~> 1.1, != 1.3.0)
77
+ tilt (1.4.1)
78
+ typhoeus (0.6.3)
79
+ ethon (~> 0.5.11)
80
+ tzinfo (0.3.37)
81
+ uuidtools (2.1.4)
82
+
83
+ PLATFORMS
84
+ ruby
85
+
86
+ DEPENDENCIES
87
+ bundler (~> 1.3)
88
+ rake
89
+ rspec (~> 2.6)
90
+ tms_bridge!
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 erichhorde
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Bridge
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'bridge'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install bridge
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,33 @@
1
+ require 'iron_cache'
2
+ require 'uuidtools'
3
+
4
+ module IronCacher
5
+ CACHE_NAME='bridge-troll'
6
+ DEFAULT_EXPIRES_IN = 60*5
7
+ def iron_cache_client
8
+ IronCache::Client.new(project_id: ENV['IRON_CACHE_PROJECT_ID'], token: ENV['IRON_CACHE_TOKEN'])
9
+ end
10
+
11
+ def iron_cache(cache_name=CACHE_NAME)
12
+ iron_cache_client.cache(cache_name)
13
+ end
14
+
15
+ def add_to_cache(token, task_id, cache_name=CACHE_NAME)
16
+ iron_cache(cache_name).put(token, task_id, expires_in: IronCacher::DEFAULT_EXPIRES_IN)
17
+ token
18
+ end
19
+
20
+ def retrieve_from_cache(token, cache_name=CACHE_NAME)
21
+ if item=iron_cache(cache_name).get(token)
22
+ return item.value
23
+ end
24
+ end
25
+
26
+ def random_key_and_value(cache_name)
27
+ key = UUIDTools::UUID.random_create.to_s
28
+ value = UUIDTools::UUID.random_create.to_s
29
+ add_to_cache(key, value, cache_name)
30
+
31
+ return [key, value]
32
+ end
33
+ end
@@ -0,0 +1,135 @@
1
+ require 'iron_cacher'
2
+ module TmsBridge
3
+ module ControllerSupport
4
+
5
+ module Redact
6
+ def redacts_tms(as, _bridged_resource_names)
7
+ extend TmsBridge::ControllerSupport::Security unless (class << self; included_modules; end).include?(TmsBridge::ControllerSupport::Security)
8
+ self.secure_tms_bridge(as)
9
+ extend TmsBridge::ControllerSupport::Redact::ClassMethods unless (class << self; included_modules; end).include?(TmsBridge::ControllerSupport::Redact::ClassMethods)
10
+ include TmsBridge::ControllerSupport::Redact::InstanceMethods unless included_modules.include?(TmsBridge::ControllerSupport::Redact::InstanceMethods)
11
+
12
+ self.bridged_resource_names=_bridged_resource_names
13
+
14
+ class_eval <<-RUBY, __FILE__, __LINE__+1
15
+ def create
16
+ @record_class = self.bridged_resource_class
17
+ if @record_class
18
+ if @record = @record_class.find_by_tms_id(@json['tms_id'])
19
+ @record.destroy
20
+ end
21
+ render text: 'success'
22
+ else
23
+ head :ok
24
+ end
25
+ end
26
+ RUBY
27
+ end
28
+
29
+ module ClassMethods
30
+ def self.extended(base)
31
+ base.class_attribute(:bridged_resource_names)
32
+ end
33
+ end
34
+
35
+
36
+ module InstanceMethods
37
+ def bridged_resource_class
38
+ class_name = @json['record_class']
39
+ eval(class_name) if self.class.bridged_resource_names.include?(class_name)
40
+ end
41
+ end
42
+ end
43
+
44
+ module Publish
45
+ def publishes_tms(as, options={:update_only=>false})
46
+ extend TmsBridge::ControllerSupport::Security unless (class << self; included_modules; end).include?(TmsBridge::ControllerSupport::Security)
47
+ extend TmsBridge::ControllerSupport::Publish::ClassMethods unless (class << self; included_modules; end).include?(TmsBridge::ControllerSupport::Publish::ClassMethods)
48
+ include TmsBridge::ControllerSupport::Publish::InstanceMethods unless included_modules.include?(TmsBridge::ControllerSupport::Publish::InstanceMethods)
49
+
50
+ self.secure_tms_bridge(as)
51
+ class_name = self.bridged_resources.classify
52
+
53
+ self.update_only = options[:update_only]
54
+ class_eval <<-RUBY, __FILE__, __LINE__+1
55
+ def create
56
+ @#{self.bridged_resource} = #{class_name}.find_by_tms_id(@json['tms_id'])
57
+
58
+ @#{self.bridged_resource} = #{class_name}.new if @#{self.bridged_resource}.nil? && !self.update_only?
59
+ if @#{self.bridged_resource}
60
+ @#{self.bridged_resource}.attributes = @json['#{self.bridged_resource}'].slice(*#{class_name}.published_attribute_names)
61
+ @#{self.bridged_resource}.save(validate: false)
62
+ end
63
+ render text: 'success'
64
+ end
65
+ RUBY
66
+
67
+
68
+ end
69
+
70
+ module ClassMethods
71
+ def self.extended(base)
72
+ base.class_attribute :update_only
73
+ end
74
+ end
75
+
76
+ module InstanceMethods
77
+ def update_only?
78
+ self.class.update_only
79
+ end
80
+ end
81
+
82
+ end
83
+
84
+ module Security
85
+
86
+ def secure_tms_bridge(as)
87
+ include TmsBridge::ControllerSupport::Security::InstanceMethods
88
+ extend TmsBridge::ControllerSupport::Security::ClassMethods
89
+ self.as = as.to_s
90
+ self.bridged_resources = self.name.split('::').last.gsub(/Controller/, '').underscore
91
+ self.bridged_resource = self.bridged_resources.singularize
92
+ self.queue_name = self.as + '_'+self.bridged_resources
93
+ end
94
+
95
+ module InstanceMethods
96
+ include IronCacher
97
+ protected
98
+ def parse_iron_mq_json
99
+ @json=JSON.parse(request.raw_post) unless request.raw_post.blank?
100
+ unless @json && @json['tms_id']
101
+ head :ok
102
+ return false
103
+ end
104
+ end
105
+
106
+ def valid_bridge_request?
107
+ if @json && @json['cache_key']
108
+ value = Digest::SHA2.hexdigest("---#{ENV['CC_BRIDGE_SALT']}--#{@json['tms_id']}--#{self.class.queue_name}--#{IronCacher::CACHE_NAME}--")
109
+ return value == retrieve_from_cache(@json['cache_key'], IronCacher::CACHE_NAME)
110
+ end
111
+ end
112
+ end
113
+
114
+
115
+ module ClassMethods
116
+ def self.extended(base)
117
+ base.class_attribute(:queue_name)
118
+ base.class_attribute(:as)
119
+ base.class_attribute(:bridged_resource)
120
+ base.class_attribute(:bridged_resources)
121
+
122
+ base.before_filter :parse_iron_mq_json
123
+ end
124
+
125
+ end
126
+
127
+ end
128
+
129
+ end
130
+
131
+
132
+ end
133
+
134
+ ActionController::Base.send(:extend, TmsBridge::ControllerSupport::Publish)
135
+ ActionController::Base.send(:extend, TmsBridge::ControllerSupport::Redact)
@@ -0,0 +1,8 @@
1
+ module TmsBridge
2
+ module ModelSupport
3
+ def published_attribute_names
4
+ self.column_names - %w{id created_at updated_at}
5
+ end
6
+ end
7
+ end
8
+ ActiveRecord::Base.send(:extend, TmsBridge::ModelSupport)
@@ -0,0 +1,3 @@
1
+ module Bridge
2
+ VERSION = "0.0.10"
3
+ end
data/lib/tms_bridge.rb ADDED
@@ -0,0 +1,10 @@
1
+ require "tms_bridge/version"
2
+ require 'uuidtools'
3
+ require "iron_cacher"
4
+ require 'action_controller'
5
+ require 'active_record'
6
+ require "tms_bridge/controller_support"
7
+ require "tms_bridge/model_support"
8
+ module Bridge
9
+
10
+ end
@@ -0,0 +1,58 @@
1
+ require 'rspec'
2
+ require File.expand_path(File.dirname(__FILE__) + '/../lib/iron_cacher')
3
+
4
+ describe IronCacher do
5
+ class MockIronCache
6
+ extend IronCacher
7
+ end
8
+
9
+ describe "iron_cache_client" do
10
+ it "return an iron cache client instance" do
11
+ MockIronCache.iron_cache_client.should be_is_a(IronCache::Client)
12
+ end
13
+
14
+ it "should use a separate config file from the iron.json" do
15
+ MockIronCache.iron_cache_client.project_id.should == ENV['IRON_CACHE_PROJECT_ID']
16
+ end
17
+
18
+ end
19
+
20
+ describe "iron_cache" do
21
+ it "return an iron cache instance" do
22
+ MockIronCache.iron_cache.should be_is_a(IronCache::Cache)
23
+ end
24
+
25
+ it "should should return the cache with the name of the as CACHE_NAME" do
26
+ MockIronCache.iron_cache.name.should == IronCacher::CACHE_NAME
27
+ end
28
+ end
29
+
30
+ describe "add_to_cache" do
31
+ before(:each) do
32
+ @key=Time.now.to_f.to_s
33
+ end
34
+
35
+ it "should return the key" do
36
+ MockIronCache.add_to_cache(@key, 'value').should == @key
37
+ end
38
+
39
+ it "should add an expiry" do
40
+ MockIronCache.add_to_cache(@key, 'value')
41
+ expires = MockIronCache.iron_cache.get(@key)['expires']
42
+ DateTime.parse(expires).should < DateTime.parse('9999-01-01T00:00:00+00:00')
43
+ end
44
+
45
+ end
46
+
47
+ describe "random_key_and_value" do
48
+ it "should return an array" do
49
+ MockIronCache.random_key_and_value(IronCacher::CACHE_NAME).class.should == Array
50
+ end
51
+
52
+ it "should return the key as the first element and value in cache as the second" do
53
+ key,value = MockIronCache.random_key_and_value(IronCacher::CACHE_NAME)
54
+ MockIronCache.iron_cache.get(key).value.should == value
55
+ end
56
+ end
57
+
58
+ end
@@ -0,0 +1,271 @@
1
+ require 'rspec'
2
+ require 'tms_bridge'
3
+ require File.expand_path(File.dirname(__FILE__) + '/mocks')
4
+
5
+ describe TmsBridge::ControllerSupport::Security do
6
+ include IronCacher
7
+
8
+ class SecuritiesController<MockController
9
+ extend TmsBridge::ControllerSupport::Security
10
+ secure_tms_bridge :some_client
11
+ end
12
+
13
+ describe "class attributes" do
14
+ it "should define and set 'as' " do
15
+ SecuritiesController.as.should == 'some_client'
16
+ end
17
+
18
+ it "should define and set 'bridged_resource' " do
19
+ SecuritiesController.bridged_resource.should == 'security'
20
+ end
21
+
22
+ it "should define and set 'bridged_resources' " do
23
+ SecuritiesController.bridged_resources.should == 'securities'
24
+ end
25
+
26
+ it "should define and set 'queue_name' " do
27
+ SecuritiesController.queue_name.should == 'some_client_securities'
28
+ end
29
+
30
+ it "should add :parse_iron_mq_json to the before_filters" do
31
+ SecuritiesController.before_filters.should include(:parse_iron_mq_json)
32
+ end
33
+ end
34
+ describe "validate_bridge_request?" do
35
+ before(:each) do
36
+ @cache_key = 'cache_key'
37
+ @tms_id = Time.now.to_i
38
+ @digest = Digest::SHA2.hexdigest("---#{ENV['CC_BRIDGE_SALT']}--#{@tms_id}--some_client_securities--#{IronCacher::CACHE_NAME}--")
39
+ yield if block_given?
40
+ add_to_cache(@cache_key, @digest)
41
+ end
42
+ def controller
43
+ @controller ||= SecuritiesController.new
44
+ end
45
+
46
+ it "should do something " do
47
+ controller.json = {'cache_key'=>@cache_key, 'tms_id'=>@tms_id}
48
+ controller.should be_valid_bridge_request
49
+ end
50
+
51
+ it "should be false if the hash does not match" do
52
+ controller.json = {'cache_key'=>Digest::SHA2.hexdigest("---#{ENV['CC_BRIDGE_SALT']}--#{@tms_id}--does_not_match--#{IronCacher::CACHE_NAME}--"), 'tms_id'=>@tms_id}
53
+ controller.should_not be_valid_bridge_request
54
+ end
55
+
56
+ it "should be false if the cache value is not found" do
57
+ iron_cache(IronCacher::CACHE_NAME).delete(@cache_key)
58
+ controller.should_not be_valid_bridge_request
59
+ end
60
+
61
+ it "should be false if json is null" do
62
+ iron_cache(IronCacher::CACHE_NAME).delete(@cache_key)
63
+ controller.json = nil
64
+ controller.should_not be_valid_bridge_request
65
+ end
66
+
67
+ it "should be false if no cache_key was passed" do
68
+ iron_cache(IronCacher::CACHE_NAME).delete(@cache_key)
69
+ controller.json = {'tms_id'=>@tms_id}
70
+ controller.should_not be_valid_bridge_request
71
+ end
72
+
73
+ end
74
+
75
+ describe "parse_iron_mq_json" do
76
+ def controller
77
+ @controller ||= SecuritiesController.new
78
+ end
79
+
80
+ it "should set the json attribute and return nil" do
81
+ controller.request.raw_post=({tms_id: 17}.to_json)
82
+ controller.send(:parse_iron_mq_json).should be_nil
83
+ controller.json.class.should eq(Hash)
84
+ end
85
+
86
+ it "should return false and call head with :ok, if tms_id is not present" do
87
+ controller.request.raw_post= nil
88
+ controller.should_receive(:head).with(:ok)
89
+ controller.send(:parse_iron_mq_json).should == false
90
+ end
91
+
92
+ end
93
+ end
94
+
95
+
96
+ describe TmsBridge::ControllerSupport::Redact do
97
+ include IronCacher
98
+
99
+ class MockRedactionsController<MockController
100
+ attr_reader :record, :record_class
101
+ extend TmsBridge::ControllerSupport::Redact
102
+ redacts_tms :some_client, %w{MockModel}
103
+ end
104
+
105
+ def controller
106
+ @controller ||=MockRedactionsController.new
107
+ end
108
+
109
+ describe "modules" do
110
+ it "should extend security" do
111
+ (class << MockRedactionsController; included_modules; end).should include(TmsBridge::ControllerSupport::Security)
112
+ end
113
+ end
114
+
115
+ describe "class_methods" do
116
+ it "should assign bridged_resources" do
117
+ MockRedactionsController.bridged_resource_names.should == %w{MockModel}
118
+ end
119
+ end
120
+
121
+ describe "instance_methods" do
122
+ describe "bridged_resource_class" do
123
+
124
+ it "should eval the string and return it if the json attribute record_class is in MockRedactionsController.bridged_resource_names" do
125
+ controller.json = {'record_class' => 'MockModel'}
126
+ controller.bridged_resource_class.should == MockModel
127
+ end
128
+
129
+ it "should return nil if the json attribute a record_class is not in MockRedactionsController.bridged_resource_names" do
130
+ controller.json = {'record_class' => 'raise'}
131
+ controller.bridged_resource_class.should be_nil
132
+ end
133
+ end
134
+ end
135
+
136
+ describe "create" do
137
+ describe "bridged_resource_class return a class" do
138
+ before(:each) do
139
+ controller.json={'record_class'=>'MockModel', 'tms_id'=>MockModel::FOUND}
140
+ controller.stub(:bridged_resource_class).and_return(MockModel)
141
+ end
142
+
143
+ it "should call controller#bridged_resource_class" do
144
+ controller.should_receive(:bridged_resource_class)
145
+ controller.create
146
+ end
147
+
148
+ it "should assign to record class" do
149
+ controller.create
150
+ controller.record_class.should == MockModel
151
+ end
152
+
153
+ it "should call MockModel.find_by_tms_id" do
154
+ MockModel.should_receive(:find_by_tms_id).with(MockModel::FOUND)
155
+ controller.create
156
+ end
157
+
158
+ it "should assign to record if found" do
159
+ controller.create
160
+ controller.record.should be_is_a(FoundMockPublishing)
161
+ end
162
+
163
+ it "should call destroy on the found record" do
164
+ controller.create
165
+ controller.record.called_destroy.should == true
166
+ end
167
+
168
+ it "should call render" do
169
+ controller.should_receive(:render).with(text: 'success')
170
+ controller.create
171
+ end
172
+ it "should not throw an error if the record is not found" do
173
+ controller.json={'record_class'=>'MockModel', 'tms_id'=>MockModel::NOT_FOUND}
174
+ controller.record.should be_nil
175
+ controller.should_receive(:render).with(text: 'success')
176
+ controller.create
177
+ end
178
+ end
179
+ end
180
+ describe "if an invalid record class is passed" do
181
+
182
+ before(:each) do
183
+ controller.stub(:bridged_resource_class).and_return(nil)
184
+ end
185
+ it "should call head :ok and" do
186
+ controller.should_receive(:head).with(:ok)
187
+ controller.create
188
+ controller.record.should be_nil
189
+ end
190
+ end
191
+ end
192
+
193
+ describe TmsBridge::ControllerSupport::Publish do
194
+
195
+ class MockPublishingsController<MockController
196
+ attr_reader :mock_publishing
197
+
198
+ extend TmsBridge::ControllerSupport::Publish
199
+ publishes_tms :some_client
200
+ end
201
+
202
+ def controller
203
+ @controller ||= MockPublishingsController.new
204
+ end
205
+
206
+ describe "modules" do
207
+ it "should extend security" do
208
+ (class << MockPublishingsController; included_modules; end).should include(TmsBridge::ControllerSupport::Security)
209
+ end
210
+
211
+ it "should support update_only?" do
212
+ controller.update_only?.should == controller.class.update_only
213
+ end
214
+ end
215
+
216
+ describe "create" do
217
+ before(:each) do
218
+ @attributes = {'some_key'=>'some value'}
219
+ @tms_id = MockPublishing::NOT_FOUND
220
+ controller.json = {'mock_publishing'=>@attributes, 'tms_id'=>@tms_id}
221
+ end
222
+
223
+ it "should assign to the mock_publishing attribute" do
224
+ controller.create
225
+ controller.mock_publishing.should_not be_nil
226
+ end
227
+
228
+ it "should assign to attributes" do
229
+ controller.create
230
+ controller.mock_publishing.attributes.should == @attributes
231
+ end
232
+
233
+ it "it should call MockPublishing.find_by_tms_id" do
234
+ MockPublishing.should_receive(:find_by_tms_id).with(@tms_id)
235
+ controller.create
236
+ end
237
+
238
+ it "it should call MockPublishing.new if MockPublishing.find_by_tms_id returns nil" do
239
+ MockPublishing.should_receive(:new)
240
+ controller.create
241
+ end
242
+
243
+ it "should not pass attributes to the mock model taht are not supported" do
244
+ puts MockPublishing.published_attribute_names
245
+ MockPublishing.should_receive(:published_attribute_names){['some_key']}
246
+ controller.create
247
+ end
248
+
249
+ it "it should call MockPublishing.new if MockPublishing.find_by_tms_id returns nothing" do
250
+ controller.json = {'mock_publishing'=>@attributes, 'tms_id'=>MockPublishing::NOT_FOUND}
251
+ MockPublishing.should_receive(:new)
252
+ controller.create
253
+ end
254
+
255
+ it "it should call MockPublishing.new if MockPublishing.find_by_tms_id returns nothing and update_only? == true" do
256
+ controller.json = {'mock_publishing'=>@attributes, 'tms_id'=>MockPublishing::NOT_FOUND}
257
+ MockPublishing.should_not_receive(:new)
258
+ controller.stub(:update_only?){true}
259
+ controller.create
260
+ end
261
+
262
+ it "should call save on the mock_publishing model" do
263
+ controller.create
264
+ controller.mock_publishing.called_save.should == true
265
+ end
266
+ it "should call render " do
267
+ controller.should_receive(:render).with(text: 'success')
268
+ controller.create
269
+ end
270
+ end
271
+ end
@@ -0,0 +1,51 @@
1
+ class MockModel
2
+ attr_accessor :attributes, :called_save, :called_destroy
3
+
4
+ extend TmsBridge::ModelSupport
5
+ NOT_FOUND=false
6
+ FOUND=true
7
+
8
+ def self.column_names
9
+ ['updated_at', 'created_at', 'id', 'some_key']
10
+ end
11
+
12
+ def save(*args)
13
+ self.called_save = true
14
+ end
15
+
16
+ def destroy
17
+ self.called_destroy = true
18
+ end
19
+ def self.find_by_tms_id(tms_id)
20
+ return tms_id == FOUND ? FoundMockPublishing.new : nil
21
+ end
22
+ end
23
+
24
+ class MockPublishing<MockModel
25
+ end
26
+
27
+ class FoundMockPublishing<MockModel
28
+ end
29
+
30
+ class MockController
31
+ cattr_accessor :before_filters
32
+ attr_accessor :json
33
+ attr_reader :request
34
+
35
+ def self.before_filter(filter)
36
+ @@before_filters ||= []
37
+ @@before_filters << filter
38
+ end
39
+
40
+ def initialize
41
+ @request = MockRequest.new
42
+ end
43
+
44
+ def head(*args);end
45
+ def render(*args);end
46
+
47
+ end
48
+
49
+ class MockRequest
50
+ attr_accessor :raw_post
51
+ end
@@ -0,0 +1,23 @@
1
+ require 'rspec'
2
+ require 'tms_bridge'
3
+ require File.expand_path(File.dirname(__FILE__) + '/mocks')
4
+
5
+ describe TmsBridge::ModelSupport do
6
+ describe "published_attributes" do
7
+ it "should not include 'id'" do
8
+ MockModel.published_attribute_names.should_not include('id')
9
+ end
10
+
11
+ it "should not include 'created_at'" do
12
+ MockModel.published_attribute_names.should_not include('created_at')
13
+ end
14
+
15
+ it "should not include 'updated_at" do
16
+ MockModel.published_attribute_names.should_not include('updated_at')
17
+ end
18
+
19
+ it "should include items declared in column_names" do
20
+ MockModel.published_attribute_names.should include('some_key')
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'tms_bridge/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "tms_bridge"
8
+ spec.version = Bridge::VERSION
9
+ spec.authors = ["Erich Timkar"]
10
+ spec.email = ["erich@teamdriveaway.com"]
11
+ spec.description = %q{Provides parsing and authtentication for publishing into TMS client apps. }
12
+ spec.summary = ''
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec", "~> 2.6"
24
+ spec.add_dependency "actionpack", ">= 3.2.0"
25
+ spec.add_dependency "activerecord", ">= 3.2.0"
26
+ spec.add_dependency "uuidtools"
27
+ spec.add_dependency "iron_cache"
28
+ spec.add_dependency "typhoeus"
29
+ end
metadata ADDED
@@ -0,0 +1,178 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tms_bridge
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.10
5
+ platform: ruby
6
+ authors:
7
+ - Erich Timkar
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-11-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '2.6'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '2.6'
55
+ - !ruby/object:Gem::Dependency
56
+ name: actionpack
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: 3.2.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: 3.2.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: activerecord
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: 3.2.0
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: 3.2.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: uuidtools
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ! '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ! '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: iron_cache
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ! '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ! '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: typhoeus
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ! '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: ! 'Provides parsing and authtentication for publishing into TMS client
126
+ apps. '
127
+ email:
128
+ - erich@teamdriveaway.com
129
+ executables: []
130
+ extensions: []
131
+ extra_rdoc_files: []
132
+ files:
133
+ - .gitignore
134
+ - .rvmrc
135
+ - Gemfile
136
+ - Gemfile.lock
137
+ - LICENSE.txt
138
+ - README.md
139
+ - Rakefile
140
+ - lib/iron_cacher.rb
141
+ - lib/tms_bridge.rb
142
+ - lib/tms_bridge/controller_support.rb
143
+ - lib/tms_bridge/model_support.rb
144
+ - lib/tms_bridge/version.rb
145
+ - spec/iron_cacher_spec.rb
146
+ - spec/tms_bridge/controller_support_spec.rb
147
+ - spec/tms_bridge/mocks.rb
148
+ - spec/tms_bridge/model_support_spec.rb
149
+ - tms_bridge.gemspec
150
+ homepage: ''
151
+ licenses:
152
+ - MIT
153
+ metadata: {}
154
+ post_install_message:
155
+ rdoc_options: []
156
+ require_paths:
157
+ - lib
158
+ required_ruby_version: !ruby/object:Gem::Requirement
159
+ requirements:
160
+ - - ! '>='
161
+ - !ruby/object:Gem::Version
162
+ version: '0'
163
+ required_rubygems_version: !ruby/object:Gem::Requirement
164
+ requirements:
165
+ - - ! '>='
166
+ - !ruby/object:Gem::Version
167
+ version: '0'
168
+ requirements: []
169
+ rubyforge_project:
170
+ rubygems_version: 2.1.9
171
+ signing_key:
172
+ specification_version: 4
173
+ summary: ''
174
+ test_files:
175
+ - spec/iron_cacher_spec.rb
176
+ - spec/tms_bridge/controller_support_spec.rb
177
+ - spec/tms_bridge/mocks.rb
178
+ - spec/tms_bridge/model_support_spec.rb