salesflip-jobboersen_integration 0.0.4 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,35 @@
1
+ module AccountExtras
2
+ def self.included( base )
3
+ base.class_eval do
4
+ field :tjb_id
5
+
6
+ after_create :create_in_tjb
7
+ after_update :update_in_tjb, :unless => :do_not_update
8
+
9
+ attr_accessor :do_not_update
10
+ end
11
+ base.send(:include, InstanceMethods)
12
+ end
13
+
14
+ module InstanceMethods
15
+ def create_in_tjb
16
+ response = connection.post(
17
+ "/administration/accounts.xml?auth_token=#{self.user.tjb_auth_token}",
18
+ self.to_xml(:only => [:name]))
19
+ doc = REXML::Document.new(response.body)
20
+ self.update_attributes :tjb_id => REXML::XPath.first(doc, '//_id').text,
21
+ :do_not_update => true
22
+ rescue
23
+ nil
24
+ end
25
+
26
+ def update_in_tjb
27
+ connection.put("/administration/accounts/#{self.id}.xml?auth_token=#{self.user.tjb_auth_token}",
28
+ self.to_xml(:only => [:name]))
29
+ end
30
+
31
+ def connection
32
+ ActiveResource::Connection.new(JobboersenIntegration.jobboersen_uri)
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,46 @@
1
+ module ContactExtras
2
+ def self.included( base )
3
+ base.class_eval do
4
+ field :tjb_id
5
+
6
+ after_create :create_in_tjb
7
+ after_update :update_in_tjb, :unless => :do_not_put
8
+
9
+ attr_accessor :do_not_update
10
+ end
11
+
12
+ base.send(:include, InstanceMethods)
13
+ end
14
+
15
+ module InstanceMethods
16
+ def create_in_tjb
17
+ response = connection.post(
18
+ "/administration/contacts.xml?auth_token=#{self.user.tjb_auth_token}", tjb_xml)
19
+ doc = REXML::Document.new(response.body)
20
+ self.update_attributes :tjb_id => REXML::XPath.first(doc, '//_id').text, :do_not_update => true
21
+ rescue
22
+ nil
23
+ end
24
+
25
+ def update_in_tjb
26
+ response = connection.put(
27
+ "/administration/contacts/#{self.id}.xml?auth_token=#{self.user.tjb_auth_token}", tjb_xml)
28
+ rescue
29
+ nil
30
+ end
31
+
32
+ def connection
33
+ ActiveResource::Connection.new(JobboersenIntegration.jobboersen_uri)
34
+ end
35
+
36
+ def tjb_xml
37
+ password = generate_password
38
+ { :first_name => self.first_name, :last_name => self.last_name, :email => self.email,
39
+ :password => password, :password_confirmation => password }.to_xml(:root => 'contact')
40
+ end
41
+
42
+ def generate_password
43
+ Array.new(10/2) { rand(256) }.pack('C*').unpack('H*').first
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,43 @@
1
+ require 'rexml/document'
2
+
3
+ module UserExtras
4
+ def self.included( base )
5
+ base.class_eval do
6
+ field :tjb_id
7
+ field :tjb_auth_token
8
+
9
+ after_create :create_in_tjb
10
+ after_update :update_in_tjb, :unless => :do_not_update
11
+
12
+ attr_accessor :do_not_update
13
+ end
14
+
15
+ base.send(:include, InstanceMethods)
16
+ end
17
+
18
+ module InstanceMethods
19
+ def create_in_tjb
20
+ response = connection.post('/administration/administrators.xml',
21
+ self.to_xml(:only => :email))
22
+ if response.code == '200'
23
+ doc = REXML::Document.new(response.body)
24
+ self.update_attributes :tjb_id => REXML::XPath.first(doc, '//_id').text,
25
+ :tjb_auth_token => REXML::XPath.first(doc, '//authentication_token').text,
26
+ :do_not_update => true
27
+ end
28
+ rescue
29
+ nil
30
+ end
31
+
32
+ def update_in_tjb
33
+ connection.put("/administration/administrators/#{self.id}.xml",
34
+ self.to_xml(:only => :email))
35
+ rescue
36
+ nil
37
+ end
38
+
39
+ def connection
40
+ connection = ActiveResource::Connection.new(JobboersenIntegration.jobboersen_uri)
41
+ end
42
+ end
43
+ end
@@ -1,3 +1,13 @@
1
1
  module JobboersenIntegration
2
+ require 'active_resource'
2
3
  require 'jobboersen_integration/engine' if defined?(Rails) && Rails::VERSION::MAJOR == 3
4
+ require 'app/lib/user_extras'
5
+ require 'app/lib/account_extras'
6
+ require 'app/lib/contact_extras'
7
+
8
+ class << self
9
+ def jobboersen_uri
10
+ 'http://1000jobboersen.de'
11
+ end
12
+ end
3
13
  end
@@ -12,7 +12,23 @@ module JobboersenIntegration
12
12
  paths.lib = 'lib/app/lib'
13
13
  paths.lib.tasks = 'lib/app/lib/tasks/tasks.rake'
14
14
 
15
+ # http://www.cowboycoded.com/2010/08/02/hooking-in-your-rails-3-engine-or-railtie-initializer-in-the-right-place/
15
16
  initializer 'jobboersen_integration.init_controller_hooks', :after => :set_load_path do |app|
17
+ Rails.logger.info 'Loading jobboersen_integration plugin'
18
+
19
+ Salesflip::Plugin.register(:jobboersen_integration) do
20
+ name 'JobBoersen Integration'
21
+ author 'Matt Beedle'
22
+ version '0.4'
23
+ description 'Everything to seemlessly integrate 1000jobboersen.de'
24
+ end
25
+
26
+ require 'app/lib/account_extras'
27
+ Account.send(:include, AccountExtras)
28
+
29
+ require 'app/lib/user_extras'
30
+ User.send(:include, UserExtras)
31
+
16
32
  require 'app/lib/controller_hooks'
17
33
  end
18
34
  end
@@ -0,0 +1,10 @@
1
+ class Account
2
+ include Mongoid::Document
3
+
4
+ field :name
5
+
6
+ referenced_in :user
7
+ references_many :contacts
8
+
9
+ validates_presence_of :user
10
+ end
@@ -0,0 +1,11 @@
1
+ class Contact
2
+ include Mongoid::Document
3
+
4
+ field :first_name
5
+ field :last_name
6
+ field :email
7
+
8
+ referenced_in :user
9
+
10
+ validates_presence_of :last_name, :user
11
+ end
@@ -0,0 +1,5 @@
1
+ class User
2
+ include Mongoid::Document
3
+
4
+ field :email
5
+ end
@@ -0,0 +1,27 @@
1
+ require 'rubygems'
2
+ require 'mongoid'
3
+ require 'shoulda'
4
+ require 'fakeweb'
5
+
6
+ Mongoid.configure do |config|
7
+ name = "jobboersen_integration_test"
8
+ host = "localhost"
9
+ config.master = Mongo::Connection.new.db(name)
10
+ config.logger = nil
11
+ end
12
+
13
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
14
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
15
+
16
+ MODELS = File.join(File.dirname(__FILE__), "models")
17
+ $LOAD_PATH.unshift(MODELS)
18
+
19
+ require 'jobboersen_integration'
20
+
21
+ Dir[ File.join(MODELS, "*.rb") ].sort.each { |file| require File.basename(file) }
22
+
23
+ class ActiveSupport::TestCase
24
+ setup do
25
+ FakeWeb.allow_net_connect = false
26
+ end
27
+ end
@@ -0,0 +1,34 @@
1
+ require 'test_helper'
2
+
3
+ class AccountTest < ActiveSupport::TestCase
4
+ setup do
5
+ Account.send(:include, AccountExtras)
6
+ @user = User.create :email => 'test@test.com', :tjb_auth_token => 'La9Hbm8SnPJN-F-zUhlp'
7
+ end
8
+
9
+ should 'have a tjb_id field' do
10
+ assert Account.fields.map(&:first).include?('tjb_id')
11
+ end
12
+
13
+ should 'have do_not_update accessor' do
14
+ assert Account.instance_methods.include?('do_not_update')
15
+ assert Account.instance_methods.include?('do_not_update=')
16
+ end
17
+
18
+ should 'should update the tjb_id when the account has been created in 1000jobboersen' do
19
+ FakeWeb.register_uri(:post,
20
+ 'http://1000jobboersen.de/administration/accounts.xml?auth_token=La9Hbm8SnPJN-F-zUhlp',
21
+ :body => File.read('test/support/account_create_success.xml'))
22
+ account = Account.create! :name => 'test account', :user => @user
23
+ assert_equal '4bcdbfea24d8f22230000011', account.tjb_id
24
+ end
25
+
26
+ should 'not blow up if a 500 response is returned' do
27
+ FakeWeb.register_uri(:post,
28
+ 'http://1000jobboersen.de/administration/accounts.xml?auth_token=La9Hbm8SnPJN-F-zUhlp',
29
+ :status => '500')
30
+ account = Account.create :name => 'test account', :user => @user
31
+ assert account.id
32
+ assert !account.new_record?
33
+ end
34
+ end
@@ -0,0 +1,47 @@
1
+ require 'test_helper'
2
+
3
+ class ContactTest < ActiveSupport::TestCase
4
+ setup do
5
+ Contact.send(:include, ContactExtras)
6
+ @user = User.create :email => 'test@test.com', :tjb_auth_token => 'La9Hbm8SnPJN-F-zUhlp'
7
+ end
8
+
9
+ should 'have tjb_id field' do
10
+ assert Contact.fields.map(&:first).include?('tjb_id')
11
+ end
12
+
13
+ should 'have do_not_update accessor' do
14
+ assert Contact.instance_methods.include?('do_not_update')
15
+ assert Contact.instance_methods.include?('do_not_update=')
16
+ end
17
+
18
+ should 'only send first_name, last_name, email, password and password_confirmation in xml' do
19
+ contact = Contact.new :first_name => 'Matt', :last_name => 'Beedle',
20
+ :email => 'mattbeedle@googlemail.com', :password => 'berlin5000',
21
+ :password_confirmation => 'berlin5000', :another_field => 'asfewaf'
22
+ assert_match(/first\-name/, contact.tjb_xml)
23
+ assert_match(/last\-name/, contact.tjb_xml)
24
+ assert_match(/email/, contact.tjb_xml)
25
+ assert_match(/password/, contact.tjb_xml)
26
+ assert_match(/password\-confirmation/, contact.tjb_xml)
27
+ assert_no_match(/another\-field/, contact.tjb_xml)
28
+ end
29
+
30
+ should 'set the tjb_id when the contact has been created in 1000jobboersen' do
31
+ FakeWeb.register_uri(:post,
32
+ "http://1000jobboersen.de/administration/contacts.xml?auth_token=La9Hbm8SnPJN-F-zUhlp",
33
+ :body => File.read('test/support/contact_create_success.xml'))
34
+ contact = Contact.create! :first_name => 'Matt', :last_name => 'Beedle',
35
+ :email => 'mattbeedle@googlemail.com', :user => @user
36
+ assert_equal '4b8ead6a24d8f247be001073', contact.tjb_id
37
+ end
38
+
39
+ should 'not blow up when 1000jobboersen returns an error response' do
40
+ FakeWeb.register_uri(:post,
41
+ "http://1000jobboersen.de/administration/contacts.xml?auth_token=La9Hbm8SnPJN-F-zUhlp",
42
+ :status => '500')
43
+ contact = Contact.create! :first_name => 'Matt', :last_name => 'Beedle',
44
+ :email => 'mattbeedle@googlemail.com', :user => @user
45
+ assert !contact.new_record?
46
+ end
47
+ end
@@ -0,0 +1,33 @@
1
+ require 'test_helper'
2
+
3
+ class UserTest < ActiveSupport::TestCase
4
+ setup do
5
+ User.send(:include, UserExtras)
6
+ end
7
+
8
+ should 'have tjb_id field' do
9
+ assert User.fields.map(&:first).include?('tjb_id')
10
+ end
11
+
12
+ should 'have do_not_update accessor' do
13
+ assert User.instance_methods.include?('do_not_update')
14
+ assert User.instance_methods.include?('do_not_update=')
15
+ end
16
+
17
+ should 'have tjb_auth_token field' do
18
+ assert User.fields.map(&:first).include?('tjb_auth_token')
19
+ end
20
+
21
+ context 'when the user does not exist in 1000jobboersen' do
22
+ setup do
23
+ FakeWeb.register_uri(:post, 'http://1000jobboersen.de/administration/administrators.xml',
24
+ :body => File.read('test/support/administrator_create_success.xml'))
25
+ end
26
+
27
+ should 'update the tjb_id when the administrator account has been created in 1000jobboersen' do
28
+ user = User.create! :email => 'test@test.com'
29
+ assert_equal '4bcdbfea24d8f22230000011', user.tjb_id
30
+ assert_equal 'La9Hbm8SnPJN-F-zUhlp', user.tjb_auth_token
31
+ end
32
+ end
33
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: salesflip-jobboersen_integration
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 27
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
+ - 1
8
9
  - 0
9
- - 4
10
- version: 0.0.4
10
+ version: 0.1.0
11
11
  platform: ruby
12
12
  authors: []
13
13
 
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-10-12 00:00:00 +02:00
18
+ date: 2010-10-30 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -29,11 +29,20 @@ extra_rdoc_files: []
29
29
 
30
30
  files:
31
31
  - lib/app/controller_hooks.rb
32
+ - lib/app/lib/account_extras.rb
33
+ - lib/app/lib/contact_extras.rb
32
34
  - lib/app/lib/controller_hooks.rb
33
35
  - lib/app/lib/tasks/tasks.rake
34
- - lib/app/models/account.rb
36
+ - lib/app/lib/user_extras.rb
35
37
  - lib/jobboersen_integration.rb
36
38
  - lib/jobboersen_integration/engine.rb
39
+ - test/models/account.rb
40
+ - test/models/contact.rb
41
+ - test/models/user.rb
42
+ - test/test_helper.rb
43
+ - test/unit/account_test.rb
44
+ - test/unit/contact_test.rb
45
+ - test/unit/user_test.rb
37
46
  has_rdoc: true
38
47
  homepage:
39
48
  licenses: []
@@ -68,5 +77,11 @@ rubygems_version: 1.3.7
68
77
  signing_key:
69
78
  specification_version: 3
70
79
  summary: Salesflip 1000jobboersen integration
71
- test_files: []
72
-
80
+ test_files:
81
+ - test/models/account.rb
82
+ - test/models/contact.rb
83
+ - test/models/user.rb
84
+ - test/test_helper.rb
85
+ - test/unit/account_test.rb
86
+ - test/unit/contact_test.rb
87
+ - test/unit/user_test.rb
@@ -1,8 +0,0 @@
1
- class Account
2
- after_create :send_to_tjb
3
-
4
- protected
5
- def send_to_tjb
6
- throw 'test'
7
- end
8
- end