knp 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0c079e51495b1cd0b1427d622d5473ce85b0c00b
4
- data.tar.gz: 3475b42afea5d212cf632f7163cf5f46c1b3ada3
3
+ metadata.gz: b677aa9e2cf218bc79121fbfda2ae8a9c9421847
4
+ data.tar.gz: e62b3fd5a487642f35a6628afa53195b87ed92a0
5
5
  SHA512:
6
- metadata.gz: c1059bed7fc0cdd0a55578336a66dc34949bb627ed82a199c1b4d364437f728a98783b7ec88bbd7cd080d7b80a219fb77ff32d2f9762820f037ca5edae0e54be
7
- data.tar.gz: 5e49acb6f45f13a558466475871aa52b3f8eb001d2b83f8ed311db9d63b90027a55f1e939aee590b53c390d01e46a0d8c520f7bd6a82af2dd6aaef2a89ecd062
6
+ metadata.gz: d428958832a4fae788865db46d537b40d6a4f538c1e9ba853734ea5420350e7cf97313b2a1973435678b453122d88332d8a020c17d604c2801c72c43316add59
7
+ data.tar.gz: 39044bba32f2ff5b64e8823508fe224e0ae9de27a4c25b25ed05334ecc2c2c91f6cccfecf6d799c2ec1020b27d49ff2da52200fedbfc6c539c131798626c55c1
@@ -1,8 +1,8 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- knp (0.0.1)
5
- killbill-client (~> 0.8.0)
4
+ knp (0.0.2)
5
+ killbill-client (~> 0.10.2)
6
6
  rails (~> 3.2.0)
7
7
 
8
8
  GEM
@@ -45,7 +45,7 @@ GEM
45
45
  railties (>= 3.0, < 5.0)
46
46
  thor (>= 0.14, < 2.0)
47
47
  json (1.8.1)
48
- killbill-client (0.8.1)
48
+ killbill-client (0.10.2)
49
49
  json (>= 1.2.0)
50
50
  mail (2.5.4)
51
51
  mime-types (~> 1.16)
data/README.md CHANGED
@@ -3,6 +3,17 @@ Knp
3
3
 
4
4
  The Kill Bill Notifications Proxy is a Rails mountable engine that can be exposed on a public IP to process notifications from gateways.
5
5
 
6
+ Assuming your Knp public address is killbill-public.acme.com, configure the notification URL in the gateway to be killbill-public.acme.com/notifications/_plugin-name_:
7
+
8
+ * https://killbill-public.acme.com/notifications/killbill-adyen
9
+ * https://killbill-public.acme.com/notifications/killbill-bitpay
10
+
11
+ If you have multiple Kill Bill clusters, you can specify an optional cluster parameter in the path (see also the configuration section):
12
+
13
+ * https://killbill-public.acme.com/notifications/killbill-adyen/europe
14
+ * https://killbill-public.acme.com/notifications/killbill-adyen/us
15
+ * https://killbill-public.acme.com/notifications/killbill-bitpay/europe
16
+ * https://killbill-public.acme.com/notifications/killbill-bitpay/us
6
17
 
7
18
  Mounting Knp into your own Rails app
8
19
  ------------------------------------
@@ -14,6 +25,43 @@ Mounting Knp into your own Rails app
14
25
  Configuration
15
26
  -------------
16
27
 
28
+ ### Using a configuration file
29
+
30
+ Create a file ```config/knp.yml```:
31
+
32
+ ```
33
+ development:
34
+ killbill:
35
+ base_uri: http://127.0.0.1:8080/
36
+ api_key: bob
37
+ api_secret: lazar
38
+ username: admin
39
+ password: password
40
+ ```
41
+
42
+ Check out the [symmetric-encryption](https://github.com/reidmorrison/symmetric-encryption) gem for encrypting the password.
43
+
44
+ If you have multiple Kill Bill clusters:
45
+
46
+ ```
47
+ development:
48
+ killbill:
49
+ us:
50
+ base_uri: http://killbill-us.acme.com:8080/
51
+ api_key: bob_us
52
+ api_secret: lazar_us
53
+ username: admin
54
+ password: password
55
+ europe:
56
+ base_uri: http://killbill-europe.acme:8080/
57
+ api_key: bob_europe
58
+ api_secret: lazar_europe
59
+ username: admin
60
+ password: password
61
+ ```
62
+
63
+ ### Using code
64
+
17
65
  Specify your Kill Bill server configuration in ```config/initializers/killbill_client.rb```:
18
66
 
19
67
  ```
@@ -2,12 +2,8 @@ module Knp
2
2
  class NotificationsController < ApplicationController
3
3
 
4
4
  # Catch-all
5
- #
6
- # Assuming your KNP public address is killbill-public.acme.com, configure the notification
7
- # URL in the gateway to be: killbill-public.acme.com/notifications/<plugin-name>.
8
- # Examples: killbill-public.acme.com/notifications/killbill-adyen, killbill-public.acme.com/notifications/killbill-bitpay
9
5
  def notify
10
- notification = build_notification(params[:plugin_name])
6
+ notification = build_notification(params[:plugin_name], params[:cluster])
11
7
  kb_response = notification.notify!
12
8
  build_response(kb_response)
13
9
  rescue => e
@@ -17,8 +13,8 @@ module Knp
17
13
 
18
14
  private
19
15
 
20
- def build_notification(plugin_name)
21
- notification = Notification.new
16
+ def build_notification(plugin_name, cluster)
17
+ notification = Notification.new(cluster)
22
18
  notification.plugin_name = plugin_name
23
19
  notification.body = request.body.read
24
20
  notification.params = request.query_parameters
@@ -8,19 +8,21 @@ module Knp
8
8
  attr_accessor :user
9
9
  attr_accessor :reason
10
10
  attr_accessor :comment
11
+ attr_accessor :base_uri
11
12
  attr_accessor :api_key
12
13
  attr_accessor :api_secret
13
14
  attr_accessor :username
14
15
  attr_accessor :password
15
16
 
16
- def initialize
17
- self.user = Knp.user || 'Knp'
18
- self.reason = Knp.reason
19
- self.comment = Knp.comment
20
- self.api_key = Knp.api_key
21
- self.api_secret = Knp.api_secret
22
- self.username = Knp.username
23
- self.password = Knp.password
17
+ def initialize(cluster=nil)
18
+ self.user = KNP_CONFIG.username(cluster)
19
+ self.reason = KNP_CONFIG.reason(cluster)
20
+ self.comment = KNP_CONFIG.comment(cluster)
21
+ self.base_uri = URI.parse(KNP_CONFIG.base_uri(cluster))
22
+ self.api_key = KNP_CONFIG.api_key(cluster)
23
+ self.api_secret = KNP_CONFIG.api_secret(cluster)
24
+ self.username = KNP_CONFIG.username(cluster)
25
+ self.password = KNP_CONFIG.password(cluster)
24
26
  end
25
27
 
26
28
  def notify!
@@ -51,6 +53,7 @@ module Knp
51
53
  :user => user,
52
54
  :reason => reason,
53
55
  :comment => comment,
56
+ :base_uri => base_uri,
54
57
  :api_key => api_key,
55
58
  :api_secret => api_secret,
56
59
  :username => username,
@@ -1,4 +1,4 @@
1
1
  Knp::Engine.routes.draw do
2
2
 
3
- match '/notifications/:plugin_name', :to => 'notifications#notify', :via => [:get, :post, :put]
3
+ match '/notifications/:plugin_name(/:cluster)', :to => 'notifications#notify', :via => [:get, :post, :put]
4
4
  end
@@ -17,7 +17,7 @@ Gem::Specification.new do |s|
17
17
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
18
 
19
19
  s.add_dependency "rails", "~> 3.2.0"
20
- s.add_dependency "killbill-client", "~> 0.8.0"
20
+ s.add_dependency "killbill-client", "~> 0.10.2"
21
21
 
22
22
  s.add_development_dependency "sqlite3"
23
23
  end
data/lib/knp.rb CHANGED
@@ -1,12 +1,95 @@
1
- require "knp/engine"
1
+ require 'knp/engine'
2
2
 
3
3
  module Knp
4
4
 
5
- mattr_accessor :user
6
- mattr_accessor :reason
7
- mattr_accessor :comment
8
- mattr_accessor :api_key
9
- mattr_accessor :api_secret
10
- mattr_accessor :username
11
- mattr_accessor :password
5
+ class Configuration
6
+
7
+ def initialize(user_configuration_file=nil)
8
+ @user_configuration_file = user_configuration_file
9
+ end
10
+
11
+ %w(base_uri api_key api_secret username password user reason comment).each do |config_key|
12
+ define_method(config_key) do |cluster=nil|
13
+ config_value = nil
14
+ config_value = user_configuration_from_key('killbill', cluster, config_key) unless cluster.nil?
15
+ config_value ||= user_configuration_from_key('killbill', config_key)
16
+ config_value ||= send("default_#{config_key}")
17
+ config_value
18
+ end
19
+ end
20
+
21
+ protected
22
+
23
+ def default_base_uri
24
+ default = 'http://127.0.0.1:8080/'
25
+ KillBillClient.url || default
26
+ rescue KillBillClient::ConfigurationError
27
+ default
28
+ end
29
+
30
+ def default_api_key
31
+ KillBillClient.api_key || 'bob'
32
+ end
33
+
34
+ def default_api_secret
35
+ KillBillClient.api_secret || 'lazar'
36
+ end
37
+
38
+ def default_username
39
+ KillBillClient.username || 'admin'
40
+ end
41
+
42
+ def default_password
43
+ KillBillClient.password || 'password'
44
+ end
45
+
46
+ def default_user
47
+ 'Knp'
48
+ end
49
+
50
+ def default_reason
51
+ nil
52
+ end
53
+
54
+ def default_comment
55
+ nil
56
+ end
57
+
58
+ private
59
+
60
+ #
61
+ # Return a specific key from the user configuration in config/knp.yml
62
+ #
63
+ # ==== Returns
64
+ #
65
+ # Mixed:: requested_key or nil
66
+ #
67
+ def user_configuration_from_key(*keys)
68
+ keys.inject(user_configuration) do |hash, key|
69
+ hash[key] if hash
70
+ end
71
+ end
72
+
73
+ #
74
+ # Memoized hash of configuration options for the current Rails environment
75
+ # as specified in config/knp.yml
76
+ #
77
+ # ==== Returns
78
+ #
79
+ # Hash:: configuration options for current environment
80
+ #
81
+ def user_configuration
82
+ @user_configuration ||=
83
+ begin
84
+ if !@user_configuration_file.nil? && File.exist?(@user_configuration_file)
85
+ File.open(@user_configuration_file) do |file|
86
+ processed = ERB.new(file.read).result
87
+ YAML.load(processed)[Rails.env]
88
+ end
89
+ else
90
+ {}
91
+ end
92
+ end
93
+ end
94
+ end
12
95
  end
@@ -9,5 +9,10 @@ require 'killbill_client'
9
9
  module Knp
10
10
  class Engine < ::Rails::Engine
11
11
  isolate_namespace Knp
12
+
13
+ initializer 'knp.load_app_root' do |app|
14
+ config_file = File.join(app.root, 'config', 'knp.yml') unless app.root.nil?
15
+ ::Knp::KNP_CONFIG = Configuration.new(config_file)
16
+ end
12
17
  end
13
18
  end
@@ -1,3 +1,3 @@
1
1
  module Knp
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -0,0 +1,84 @@
1
+ require 'test_helper'
2
+
3
+ class ConfigurationTest < ActiveSupport::TestCase
4
+
5
+ test 'configuration with defaults' do
6
+ verify_config('http://127.0.0.1:8080/', 'bob', 'lazar', 'admin', 'password', 'Knp', nil, nil)
7
+ end
8
+
9
+ test 'configuration with Kill Bill client overrides' do
10
+ KillBillClient.url = 'http://127.0.0.1:9090/'
11
+ KillBillClient.api_key = 'bob-override'
12
+ KillBillClient.api_secret = 'lazar-override'
13
+ KillBillClient.username = 'admin-override'
14
+ KillBillClient.password = 'password-override'
15
+
16
+ verify_config('http://127.0.0.1:9090/', 'bob-override', 'lazar-override', 'admin-override', 'password-override', 'Knp', nil, nil)
17
+
18
+ KillBillClient.url = nil
19
+ KillBillClient.api_key = nil
20
+ KillBillClient.api_secret = nil
21
+ KillBillClient.username = nil
22
+ KillBillClient.password = nil
23
+ end
24
+
25
+ test 'configuration with file override' do
26
+ Dir.mktmpdir do |dir|
27
+ file_path = File.join(dir, 'knp.yml')
28
+ file = File.new(file_path, 'w+')
29
+ file.write(<<-eos)
30
+ test:
31
+ killbill:
32
+ base_uri: http://127.0.0.1:7070/
33
+ api_key: bob-file-override
34
+ api_secret: lazar-file-override
35
+ username: admin-file-override
36
+ password: password-file-override
37
+ eos
38
+ file.close
39
+
40
+ verify_config('http://127.0.0.1:7070/', 'bob-file-override', 'lazar-file-override', 'admin-file-override', 'password-file-override', 'Knp', nil, nil, Knp::Configuration.new(file_path))
41
+ end
42
+ end
43
+
44
+ test 'configuration with file and clusters override' do
45
+ Dir.mktmpdir do |dir|
46
+ file_path = File.join(dir, 'knp.yml')
47
+ file = File.new(file_path, 'w+')
48
+ file.write(<<-eos)
49
+ test:
50
+ killbill:
51
+ us:
52
+ base_uri: http://killbill-us.acme.com:8080/
53
+ api_key: bob_us
54
+ api_secret: lazar_us
55
+ username: admin_us
56
+ password: password_us
57
+ europe:
58
+ base_uri: http://killbill-europe.acme.com:8080/
59
+ api_key: bob_europe
60
+ api_secret: lazar_europe
61
+ username: admin_europe
62
+ password: password_europe
63
+ eos
64
+ file.close
65
+
66
+ config = Knp::Configuration.new(file_path)
67
+ verify_config('http://killbill-europe.acme.com:8080/', 'bob_europe', 'lazar_europe', 'admin_europe', 'password_europe', 'Knp', nil, nil, config, 'europe')
68
+ verify_config('http://killbill-us.acme.com:8080/', 'bob_us', 'lazar_us', 'admin_us', 'password_us', 'Knp', nil, nil, Knp::Configuration.new(file_path), 'us')
69
+ end
70
+ end
71
+
72
+ private
73
+
74
+ def verify_config(base_uri, api_key, api_secret, username, password, user, reason, comment, config=Knp::Configuration.new, cluster=nil)
75
+ assert_equal base_uri, config.base_uri(cluster)
76
+ assert_equal api_key, config.api_key(cluster)
77
+ assert_equal api_secret, config.api_secret(cluster)
78
+ assert_equal username, config.username(cluster)
79
+ assert_equal password, config.password(cluster)
80
+ assert_equal user, config.user(cluster)
81
+ assert_equal reason, config.reason(cluster)
82
+ assert_equal comment, config.comment(cluster)
83
+ end
84
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: knp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Killbill core team
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-06 00:00:00.000000000 Z
11
+ date: 2015-01-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: 0.8.0
33
+ version: 0.10.2
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: 0.8.0
40
+ version: 0.10.2
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: sqlite3
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -117,6 +117,7 @@ files:
117
117
  - test/functional/knp/mock_notifications_controller_test.rb
118
118
  - test/knp_test.rb
119
119
  - test/test_helper.rb
120
+ - test/unit/knp/configuration_test.rb
120
121
  homepage: http://killbill.io
121
122
  licenses: []
122
123
  metadata: {}
@@ -178,3 +179,4 @@ test_files:
178
179
  - test/functional/knp/mock_notifications_controller_test.rb
179
180
  - test/knp_test.rb
180
181
  - test/test_helper.rb
182
+ - test/unit/knp/configuration_test.rb