killbill-zendesk 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Jarfile CHANGED
@@ -1,3 +1,5 @@
1
- jar 'com.ning.billing:killbill-api', '0.1.80'
2
- jar 'com.ning.billing:killbill-util:tests', '0.1.80'
1
+ jar 'com.ning.billing:killbill-api', '0.3.0'
2
+ jar 'com.ning.billing.plugin:killbill-plugin-api-notification', '0.2.4'
3
+ jar 'com.ning.billing.plugin:killbill-plugin-api-payment', '0.2.4'
4
+ jar 'com.ning.billing:killbill-util:tests', '0.2.6-SNAPSHOT'
3
5
  jar 'javax.servlet:javax.servlet-api', '3.0.1'
data/NEWS ADDED
@@ -0,0 +1,9 @@
1
+ 0.2.0
2
+ Save local mapping if the user already exists in Zendesk
3
+ Update to killbill 1.1.2
4
+
5
+ 0.1.1
6
+ Fix activerecord connections leak
7
+
8
+ 0.1.0
9
+ Initial version
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.2.0
@@ -22,7 +22,7 @@ Gem::Specification.new do |s|
22
22
 
23
23
  s.rdoc_options << '--exclude' << '.'
24
24
 
25
- s.add_dependency 'killbill', '~> 1.0.19'
25
+ s.add_dependency 'killbill', '~> 1.1.2'
26
26
  s.add_dependency 'sinatra', '~> 1.3.4'
27
27
  s.add_dependency 'zendesk_api', '~> 0.3.10'
28
28
  s.add_dependency 'activerecord', '~> 3.2.1'
@@ -11,9 +11,10 @@ module Killbill::Zendesk
11
11
 
12
12
  user = find_by_kb_account(kb_account)
13
13
  user = create_user(kb_account) if user.nil?
14
+ save_kb_zd_mapping(kb_account, user)
14
15
 
15
16
  user.name = kb_account.name
16
- user.external_id = kb_account.external_key || kb_account.id.to_s
17
+ user.external_id = kb_account.external_key || kb_account.id
17
18
  user.locale = kb_account.locale
18
19
  user.timezone = kb_account.time_zone
19
20
  user.email = kb_account.email
@@ -31,15 +32,10 @@ module Killbill::Zendesk
31
32
 
32
33
  # Find the Kill Bill account associated with that lookup_key (account id or external key)
33
34
  def lookup_kb_account(lookup_key)
34
- if lookup_key.is_a?(Killbill::Plugin::Model::UUID)
35
- @kb_apis.get_account_by_id(lookup_key)
35
+ if lookup_key =~ /[A-Fa-f0-9]{8}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{12}/
36
+ @kb_apis.account_user_api.get_account_by_id(lookup_key, @kb_apis.create_context)
36
37
  else
37
- lookup_key_s = lookup_key.to_s
38
- if lookup_key_s =~ /[A-Fa-f0-9]{8}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{12}/
39
- lookup_kb_account(Killbill::Plugin::Model::UUID.new(lookup_key_s))
40
- else
41
- @kb_apis.get_account_by_key(lookup_key_s)
42
- end
38
+ @kb_apis.account_user_api.get_account_by_key(lookup_key, @kb_apis.create_context)
43
39
  end
44
40
  end
45
41
 
@@ -49,26 +45,25 @@ module Killbill::Zendesk
49
45
  (details.reject { |detail| detail.blank? }).join(', ')
50
46
  end
51
47
 
52
- # Create a user in Zendesk and save the id mapping locally
48
+ # Create a user in Zendesk
53
49
  def create_user(kb_account)
54
- # Create the user in Zendesk
55
- user = @client.users.create(:name => kb_account.name)
50
+ @client.users.create(:name => kb_account.name)
51
+ end
56
52
 
53
+ def save_kb_zd_mapping(kb_account, user)
57
54
  # Save the mapping locally - this is required due to the indexing lag on the Zendesk side,
58
55
  # see https://support.zendesk.com/entries/20239737:
59
56
  # When you add new data to your Zendesk, it typically takes about 2 to 3 minutes before it's indexed and can be searched.
60
57
  # This is unacceptable for us: if an account creation event is quickly followed by a account update event,
61
58
  # we wouldn't be able to retrieve the user, potentially causing duplicates and/or triggering validation errors, e.g.
62
59
  # Email 1370587241-test@tester.com is already being used by another user
63
- ZendeskUser.create! :kb_account_id => kb_account.id.to_s, :zd_user_id => user.id
64
-
65
- user
60
+ ZendeskUser.where(:kb_account_id => kb_account.id, :zd_user_id => user.id).first_or_create!
66
61
  end
67
62
 
68
63
  # Find the Zendesk user associated with that Kill Bill account
69
64
  def find_by_kb_account(kb_account)
70
65
  # Do we have already a mapping for that user?
71
- zd_account = find_by_id(kb_account.id.to_s)
66
+ zd_account = find_by_id(kb_account.id)
72
67
  return zd_account if zd_account
73
68
 
74
69
  # TODO In the search results below, should we worry about potential dups?
@@ -79,7 +74,7 @@ module Killbill::Zendesk
79
74
  return zd_account if zd_account
80
75
 
81
76
  # ...or the Kill Bill account id
82
- zd_account = find_by_external_id(kb_account.id.to_s)
77
+ zd_account = find_by_external_id(kb_account.id)
83
78
  return zd_account if zd_account
84
79
 
85
80
  # At this point, we haven't matched this user yet. To reconcile it, use the email address which is guaranteed
data/lib/zendesk.rb CHANGED
@@ -25,7 +25,7 @@ module Killbill::Zendesk
25
25
  end
26
26
 
27
27
  def on_event(event)
28
- @updater.update(event.account_id) if [:ACCOUNT_CREATION, :ACCOUNT_CHANGE].include?(event.event_type.enum)
28
+ @updater.update(event.account_id) if [:ACCOUNT_CREATION, :ACCOUNT_CHANGE].include?(event.event_type)
29
29
  end
30
30
  end
31
31
  end
data/pom.xml CHANGED
@@ -25,7 +25,7 @@
25
25
  <groupId>com.ning.killbill.ruby</groupId>
26
26
  <artifactId>zendesk-plugin</artifactId>
27
27
  <packaging>pom</packaging>
28
- <version>0.1.1</version>
28
+ <version>0.2.0</version>
29
29
  <name>zendesk-plugin</name>
30
30
  <url>http://github.com/killbill/killbill-zendesk-plugin</url>
31
31
  <description>Plugin to mirror Kill Bill data into Zendesk</description>
data/spec/spec_helper.rb CHANGED
@@ -2,6 +2,7 @@ require 'bundler'
2
2
  require 'zendesk'
3
3
 
4
4
  require 'logger'
5
+ require 'ostruct'
5
6
 
6
7
  require 'rspec'
7
8
 
@@ -18,3 +19,18 @@ ActiveRecord::Base.establish_connection(
18
19
  )
19
20
  # Create the schema
20
21
  require File.expand_path(File.dirname(__FILE__) + '../../db/schema.rb')
22
+
23
+ begin
24
+ require 'securerandom'
25
+ SecureRandom.uuid
26
+ rescue LoadError, NoMethodError
27
+ # See http://jira.codehaus.org/browse/JRUBY-6176
28
+ module SecureRandom
29
+ def self.uuid
30
+ ary = self.random_bytes(16).unpack("NnnnnN")
31
+ ary[2] = (ary[2] & 0x0fff) | 0x4000
32
+ ary[3] = (ary[3] & 0x3fff) | 0x8000
33
+ "%08x-%04x-%04x-%04x-%04x%08x" % ary
34
+ end unless respond_to?(:uuid)
35
+ end
36
+ end
@@ -1,27 +1,18 @@
1
1
  require 'spec_helper'
2
2
 
3
- class KillbillApiWithFakeGetAccountById < Killbill::Plugin::KillbillApi
4
- def initialize(*args)
5
- super(*args)
6
- @accounts = {}
7
- end
3
+ class FakeJavaUserAccountApi
4
+ attr_accessor :accounts
8
5
 
9
- def create_account(id, external_key, name, email)
10
- @accounts[id.to_s] = Killbill::Plugin::Model::Account.new(id, nil, nil, nil, external_key, name, 1, email, 1, 'USD', nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, false, true)
6
+ def initialize
7
+ @accounts = []
11
8
  end
12
9
 
13
- def get_account_by_id(id)
14
- @accounts[id.to_s]
10
+ def get_account_by_id(id, context)
11
+ @accounts.find { |account| account.id == id.to_s }
15
12
  end
16
- end
17
-
18
- class MockEvent
19
- attr_reader :event_type
20
- attr_reader :account_id
21
13
 
22
- def initialize(event_type, account_id)
23
- @event_type = event_type
24
- @account_id = account_id
14
+ def get_account_by_key(external_key, context)
15
+ @accounts.find { |account| account.external_key == external_key.to_s }
25
16
  end
26
17
  end
27
18
 
@@ -34,7 +25,9 @@ describe Killbill::Zendesk::ZendeskPlugin do
34
25
  logger.level = Logger::DEBUG
35
26
  @plugin.logger = logger
36
27
 
37
- @plugin.kb_apis = KillbillApiWithFakeGetAccountById.new(nil)
28
+ @account_api = FakeJavaUserAccountApi.new
29
+ svcs = {:account_user_api => @account_api}
30
+ @plugin.kb_apis = Killbill::Plugin::KillbillApi.new('zendesk', svcs)
38
31
 
39
32
  @plugin.start_plugin
40
33
  end
@@ -53,7 +46,7 @@ describe Killbill::Zendesk::ZendeskPlugin do
53
46
  @plugin.updater.find_by_external_id(external_key).should be_nil
54
47
 
55
48
  # Send a creation event
56
- @plugin.on_event MockEvent.new(Killbill::Plugin::Model::ExtBusEventType.new(:ACCOUNT_CREATION), kb_account_id)
49
+ @plugin.on_event OpenStruct.new(:event_type => :ACCOUNT_CREATION, :account_id => kb_account_id)
57
50
 
58
51
  # We should now verify the account exists, but we can't, due to indexing lag :/
59
52
  #@plugin.updater.find_by_external_id(external_key).email.should == email
@@ -61,7 +54,7 @@ describe Killbill::Zendesk::ZendeskPlugin do
61
54
  Killbill::Zendesk::ZendeskUser.count.should == 1
62
55
 
63
56
  # Send an update event
64
- @plugin.on_event MockEvent.new(Killbill::Plugin::Model::ExtBusEventType.new(:ACCOUNT_CHANGE), kb_account_id)
57
+ @plugin.on_event OpenStruct.new(:event_type => :ACCOUNT_CHANGE, :account_id => kb_account_id)
65
58
 
66
59
  # Verify we didn't create dups
67
60
  #@plugin.updater.find_all_by_external_id(external_key).count.should == 1
@@ -69,7 +62,7 @@ describe Killbill::Zendesk::ZendeskPlugin do
69
62
 
70
63
  # Create a new user
71
64
  external_key, kb_account_id = create_kb_account
72
- @plugin.on_event MockEvent.new(Killbill::Plugin::Model::ExtBusEventType.new(:ACCOUNT_CREATION), kb_account_id)
65
+ @plugin.on_event OpenStruct.new(:event_type => :ACCOUNT_CREATION, :account_id => kb_account_id)
73
66
 
74
67
  Killbill::Zendesk::ZendeskUser.count.should == 2
75
68
  end
@@ -78,9 +71,17 @@ describe Killbill::Zendesk::ZendeskPlugin do
78
71
 
79
72
  def create_kb_account
80
73
  external_key = Time.now.to_i.to_s + '-test'
81
- kb_account_id = Killbill::Plugin::Model::UUID.new(external_key)
74
+ kb_account_id = SecureRandom.uuid
82
75
  email = external_key + '@tester.com'
83
- @plugin.kb_apis.create_account(kb_account_id, external_key, 'Test tester', email)
76
+
77
+ account = Killbill::Plugin::Model::Account.new
78
+ account.id = kb_account_id
79
+ account.external_key = external_key
80
+ account.email = email
81
+ account.name = 'Integration spec'
82
+
83
+ @account_api.accounts << account
84
+
84
85
  return external_key, kb_account_id
85
86
  end
86
87
  end
@@ -26,9 +26,32 @@ describe Killbill::Zendesk::UserUpdater do
26
26
  updater.build_details_field(account).should == '493 Slro road, apt 33, Fola, FG, 140, Floq'
27
27
  end
28
28
 
29
+ it 'should save the mappings locally' do
30
+ updater = Killbill::Zendesk::UserUpdater.new(nil, nil, nil)
31
+ kb_account = OpenStruct.new(:id => '11-22-33-44-55')
32
+ zd_user = OpenStruct.new(:id => 9402871)
33
+
34
+ Killbill::Zendesk::ZendeskUser.all.size.should == 0
35
+
36
+ updater.save_kb_zd_mapping kb_account, zd_user
37
+ Killbill::Zendesk::ZendeskUser.all.size.should == 1
38
+ Killbill::Zendesk::ZendeskUser.find_by_kb_account_id(kb_account.id).zd_user_id.should == zd_user.id
39
+
40
+ updater.save_kb_zd_mapping kb_account, zd_user
41
+ Killbill::Zendesk::ZendeskUser.all.size.should == 1
42
+ Killbill::Zendesk::ZendeskUser.find_by_kb_account_id(kb_account.id).zd_user_id.should == zd_user.id
43
+ end
44
+
29
45
  private
30
46
 
31
47
  def build_account(address1, address2, city, state_or_province, postal_code, country)
32
- Killbill::Plugin::Model::Account.new(nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, address1, address2, nil, city, state_or_province, postal_code, country, nil, nil, nil)
48
+ account = Killbill::Plugin::Model::Account.new
49
+ account.address1 = address1
50
+ account.address2 = address2
51
+ account.city = city
52
+ account.state_or_province = state_or_province
53
+ account.postal_code = postal_code
54
+ account.country = country
55
+ account
33
56
  end
34
57
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: killbill-zendesk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-06-13 00:00:00.000000000 Z
12
+ date: 2013-06-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: killbill
@@ -17,13 +17,13 @@ dependencies:
17
17
  requirements:
18
18
  - - "~>"
19
19
  - !ruby/object:Gem::Version
20
- version: 1.0.19
20
+ version: 1.1.2
21
21
  none: false
22
22
  requirement: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 1.0.19
26
+ version: 1.1.2
27
27
  none: false
28
28
  prerelease: false
29
29
  type: :runtime
@@ -164,6 +164,7 @@ files:
164
164
  - ".gitignore"
165
165
  - Gemfile
166
166
  - Jarfile
167
+ - NEWS
167
168
  - README.md
168
169
  - Rakefile
169
170
  - VERSION